blob: b6cc7a6d3afa4d74c8d4523c4a9b0c2d6c95b74d [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;
Jian Lia424a052016-05-30 21:02:33 +090024import org.onosproject.core.ApplicationId;
Jian Li8bcef8b2016-01-06 11:35:53 -080025import org.onosproject.core.CoreService;
Jian Lia424a052016-05-30 21:02:33 +090026import org.onosproject.core.DefaultApplicationId;
Jian Li8bcef8b2016-01-06 11:35:53 -080027import org.onosproject.net.flow.DefaultTrafficSelector;
28import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flow.criteria.Criteria;
30import org.onosproject.net.flow.criteria.Criterion;
31import org.onosproject.net.flowobjective.DefaultForwardingObjective;
32import org.onosproject.net.flowobjective.ForwardingObjective;
33
34import java.io.IOException;
35import java.io.InputStream;
36
Jian Lia424a052016-05-30 21:02:33 +090037import static org.easymock.EasyMock.*;
Jian Li8bcef8b2016-01-06 11:35:53 -080038import 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);
Jian Lia424a052016-05-30 21:02:33 +090052 static final String SAMPLE_APP_ID = "org.onosproject.sample";
Jian Li8bcef8b2016-01-06 11:35:53 -080053
54 /**
55 * Sets up for each test.
56 * Creates a context and fetches the ForwardingObjective codec.
57 */
58 @Before
59 public void setUp() {
60 context = new MockCodecContext();
61 forwardingObjectiveCodec = context.codec(ForwardingObjective.class);
62 assertThat(forwardingObjectiveCodec, notNullValue());
63
Jian Li8bcef8b2016-01-06 11:35:53 -080064 context.registerService(CoreService.class, mockCoreService);
65 }
66
67 /**
68 * Tests encoding of a ForwardingObjective object.
69 */
70 @Test
71 public void testForwardingObjectiveEncode() {
72
73 Criterion criterion1 = Criteria.matchVlanId(VlanId.ANY);
74 Criterion criterion2 = Criteria.matchEthType((short) 0x8844);
75 TrafficSelector selector = DefaultTrafficSelector.builder()
76 .add(criterion1)
77 .add(criterion2)
78 .build();
79
80 ForwardingObjective forwardingObj = DefaultForwardingObjective.builder()
81 .makePermanent()
82 .fromApp(APP_ID)
83 .withPriority(60)
84 .withFlag(ForwardingObjective.Flag.SPECIFIC)
85 .nextStep(1)
86 .withSelector(selector)
87 .add();
88
89 ObjectNode forwardingObjJson = forwardingObjectiveCodec.encode(forwardingObj, context);
90 assertThat(forwardingObjJson, matchesForwardingObjective(forwardingObj));
91 }
92
93 /**
94 * Test decoding of a ForwardingObjective object.
95 */
96 @Test
97 public void testForwardingObjectiveDecode() throws IOException {
Jian Lia424a052016-05-30 21:02:33 +090098
99 ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID);
100
101 expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes();
102 replay(mockCoreService);
103
Jian Li8bcef8b2016-01-06 11:35:53 -0800104 ForwardingObjective forwardingObjective = getForwardingObjective("ForwardingObjective.json");
105
106 assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC));
107 assertThat(forwardingObjective.priority(), is(60));
108 assertThat(forwardingObjective.timeout(), is(1));
109 assertThat(forwardingObjective.op(), is(ForwardingObjective.Operation.ADD));
110 assertThat(forwardingObjective.permanent(), is(false));
Jian Lia424a052016-05-30 21:02:33 +0900111 assertThat(forwardingObjective.appId().name(), is(SAMPLE_APP_ID));
Jian Li8bcef8b2016-01-06 11:35:53 -0800112 }
113
114 /**
115 * Reads in a forwardingObjectiveJsonCodec from the given resource and decodes it.
116 *
117 * @param resourceName resource to use to read the JSON for the rule
118 * @return decoded forwardingObjectiveJsonCodec
119 * @throws IOException if processing the resource fails
120 */
121 private ForwardingObjective getForwardingObjective(String resourceName) throws IOException {
122 InputStream jsonStream = ForwardingObjectiveCodecTest.class.getResourceAsStream(resourceName);
123 JsonNode json = context.mapper().readTree(jsonStream);
124 assertThat(json, notNullValue());
125 ForwardingObjective forwardingObjective = forwardingObjectiveCodec.decode((ObjectNode) json, context);
126 assertThat(forwardingObjective, notNullValue());
127 return forwardingObjective;
128 }
129}