blob: 06c6c9c832d61b7cf45d5af12b57d6d2f5707c9b [file] [log] [blame]
Jian Li8bcef8b2016-01-06 11:35:53 -08001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.flow.DefaultTrafficTreatment;
25import org.onosproject.net.flow.TrafficTreatment;
26import org.onosproject.net.flowobjective.DefaultNextObjective;
27import org.onosproject.net.flowobjective.NextObjective;
28
29import java.io.IOException;
30import java.io.InputStream;
31
32import static org.easymock.EasyMock.createMock;
33import static org.easymock.EasyMock.expect;
34import static org.easymock.EasyMock.replay;
35import static org.hamcrest.MatcherAssert.assertThat;
36import static org.hamcrest.Matchers.is;
37import static org.hamcrest.Matchers.notNullValue;
38import static org.onosproject.codec.impl.NextObjectiveJsonMatcher.matchesNextObjective;
39import static org.onosproject.net.NetTestTools.APP_ID;
40
41/**
42 * Unit tests for NextObjective Codec.
43 */
44public class NextObjectiveCodecTest {
45
46 MockCodecContext context;
47 JsonCodec<NextObjective> nextObjectiveCodec;
48 final CoreService mockCoreService = createMock(CoreService.class);
49
50 /**
51 * Sets up for each test.
52 * Creates a context and fetches the NextObjective codec.
53 */
54 @Before
55 public void setUp() {
56 context = new MockCodecContext();
57 nextObjectiveCodec = context.codec(NextObjective.class);
58 assertThat(nextObjectiveCodec, notNullValue());
59
60 expect(mockCoreService.registerApplication(NextObjectiveCodec.REST_APP_ID))
61 .andReturn(APP_ID).anyTimes();
62 replay(mockCoreService);
63 context.registerService(CoreService.class, mockCoreService);
64 }
65
66 /**
67 * Tests encoding of a NextObjective object.
68 */
69 @Test
70 public void testNextObjectiveEncode() {
71
72 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
73
74 NextObjective nextObj = DefaultNextObjective.builder()
75 .makePermanent()
76 .withType(NextObjective.Type.HASHED)
77 .fromApp(APP_ID)
78 .withPriority(60)
79 .withId(5)
80 .addTreatment(treatment)
81 .add();
82
83 ObjectNode nextObjJson = nextObjectiveCodec.encode(nextObj, context);
84 assertThat(nextObjJson, matchesNextObjective(nextObj));
85 }
86
87 /**
88 * Test decoding of a NextObjective object.
89 */
90 @Test
91 public void testNextObjectiveDecode() throws IOException {
92 NextObjective nextObjective = getNextObjective("NextObjective.json");
93
94 assertThat(nextObjective.type(), is(NextObjective.Type.FAILOVER));
95 assertThat(nextObjective.op(), is(NextObjective.Operation.ADD));
96 }
97
98 /**
99 * Reads in a nextObjective from the given resource and decodes it.
100 *
101 * @param resourceName resource to use to read the JSON for the rule
102 * @return decoded nextObjective
103 * @throws IOException if processing the resource fails
104 */
105 private NextObjective getNextObjective(String resourceName) throws IOException {
106 InputStream jsonStream = NextObjectiveCodecTest.class.getResourceAsStream(resourceName);
107 JsonNode json = context.mapper().readTree(jsonStream);
108 assertThat(json, notNullValue());
109 NextObjective nextObjective = nextObjectiveCodec.decode((ObjectNode) json, context);
110 assertThat(nextObjective, notNullValue());
111 return nextObjective;
112 }
113}