blob: 9b4e2b12b3d16cfd883586e11da00eca22610343 [file] [log] [blame]
Jian Li8bcef8b2016-01-06 11:35:53 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li8bcef8b2016-01-06 11:35:53 -08003 *
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.onlab.packet.VlanId;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.flow.DefaultTrafficSelector;
26import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flow.criteria.Criteria;
28import org.onosproject.net.flow.criteria.Criterion;
29import org.onosproject.net.flowobjective.DefaultForwardingObjective;
30import org.onosproject.net.flowobjective.ForwardingObjective;
31
32import java.io.IOException;
33import java.io.InputStream;
34
35import static org.easymock.EasyMock.createMock;
36import static org.easymock.EasyMock.expect;
37import static org.easymock.EasyMock.replay;
38import static org.hamcrest.MatcherAssert.assertThat;
39import static org.hamcrest.Matchers.is;
40import static org.hamcrest.Matchers.notNullValue;
41import static org.onosproject.codec.impl.ForwardingObjectiveJsonMatcher.matchesForwardingObjective;
42import static org.onosproject.net.NetTestTools.APP_ID;
43
44/**
45 * Unit tests for ForwardingObjective Codec.
46 */
47public class ForwardingObjectiveCodecTest {
48
49 MockCodecContext context;
50 JsonCodec<ForwardingObjective> forwardingObjectiveCodec;
51 final CoreService mockCoreService = createMock(CoreService.class);
52
53 /**
54 * Sets up for each test.
55 * Creates a context and fetches the ForwardingObjective codec.
56 */
57 @Before
58 public void setUp() {
59 context = new MockCodecContext();
60 forwardingObjectiveCodec = context.codec(ForwardingObjective.class);
61 assertThat(forwardingObjectiveCodec, notNullValue());
62
63 expect(mockCoreService.registerApplication(ForwardingObjectiveCodec.REST_APP_ID))
64 .andReturn(APP_ID).anyTimes();
65 replay(mockCoreService);
66 context.registerService(CoreService.class, mockCoreService);
67 }
68
69 /**
70 * Tests encoding of a ForwardingObjective object.
71 */
72 @Test
73 public void testForwardingObjectiveEncode() {
74
75 Criterion criterion1 = Criteria.matchVlanId(VlanId.ANY);
76 Criterion criterion2 = Criteria.matchEthType((short) 0x8844);
77 TrafficSelector selector = DefaultTrafficSelector.builder()
78 .add(criterion1)
79 .add(criterion2)
80 .build();
81
82 ForwardingObjective forwardingObj = DefaultForwardingObjective.builder()
83 .makePermanent()
84 .fromApp(APP_ID)
85 .withPriority(60)
86 .withFlag(ForwardingObjective.Flag.SPECIFIC)
87 .nextStep(1)
88 .withSelector(selector)
89 .add();
90
91 ObjectNode forwardingObjJson = forwardingObjectiveCodec.encode(forwardingObj, context);
92 assertThat(forwardingObjJson, matchesForwardingObjective(forwardingObj));
93 }
94
95 /**
96 * Test decoding of a ForwardingObjective object.
97 */
98 @Test
99 public void testForwardingObjectiveDecode() throws IOException {
100 ForwardingObjective forwardingObjective = getForwardingObjective("ForwardingObjective.json");
101
102 assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC));
103 assertThat(forwardingObjective.priority(), is(60));
104 assertThat(forwardingObjective.timeout(), is(1));
105 assertThat(forwardingObjective.op(), is(ForwardingObjective.Operation.ADD));
106 assertThat(forwardingObjective.permanent(), is(false));
107 }
108
109 /**
110 * Reads in a forwardingObjectiveJsonCodec from the given resource and decodes it.
111 *
112 * @param resourceName resource to use to read the JSON for the rule
113 * @return decoded forwardingObjectiveJsonCodec
114 * @throws IOException if processing the resource fails
115 */
116 private ForwardingObjective getForwardingObjective(String resourceName) throws IOException {
117 InputStream jsonStream = ForwardingObjectiveCodecTest.class.getResourceAsStream(resourceName);
118 JsonNode json = context.mapper().readTree(jsonStream);
119 assertThat(json, notNullValue());
120 ForwardingObjective forwardingObjective = forwardingObjectiveCodec.decode((ObjectNode) json, context);
121 assertThat(forwardingObjective, notNullValue());
122 return forwardingObjective;
123 }
124}