blob: 7718c811fe86a050e1e0757191ae4141f48d7655 [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.criteria.Criteria;
28import org.onosproject.net.flow.criteria.Criterion;
29import org.onosproject.net.flowobjective.DefaultFilteringObjective;
30import org.onosproject.net.flowobjective.FilteringObjective;
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.FilteringObjectiveJsonMatcher.matchesFilteringObjective;
42import static org.onosproject.net.NetTestTools.APP_ID;
43
44/**
45 * Unit tests for FilteringObjective Codec.
46 */
47public class FilteringObjectiveCodecTest {
48
49 MockCodecContext context;
50 JsonCodec<FilteringObjective> filteringObjectiveCodec;
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 FilteringObjective codec.
57 */
58 @Before
59 public void setUp() {
60 context = new MockCodecContext();
61 filteringObjectiveCodec = context.codec(FilteringObjective.class);
62 assertThat(filteringObjectiveCodec, notNullValue());
63
Jian Li8bcef8b2016-01-06 11:35:53 -080064 context.registerService(CoreService.class, mockCoreService);
65 }
66
67 /**
68 * Tests encoding of a FilteringObjective object.
69 */
70 @Test
71 public void testFilteringObjectiveEncode() {
72
73 Criterion condition1 = Criteria.matchVlanId(VlanId.ANY);
74 Criterion condition2 = Criteria.matchEthType((short) 0x8844);
75
76 FilteringObjective filteringObj = DefaultFilteringObjective.builder()
77 .makePermanent()
78 .permit()
79 .fromApp(APP_ID)
80 .withPriority(60)
81 .addCondition(condition1)
82 .addCondition(condition2)
83 .add();
84
85 // TODO: need to add test case for TrafficTreatment (META in filteringObj)
86
87 ObjectNode filteringObjJson = filteringObjectiveCodec.encode(filteringObj, context);
88 assertThat(filteringObjJson, matchesFilteringObjective(filteringObj));
89 }
90
91 /**
92 * Test decoding of a FilteringObjective object.
93 */
94 @Test
95 public void testFilteringObjectiveDecode() throws IOException {
Jian Lia424a052016-05-30 21:02:33 +090096
97 ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID);
98
99 expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes();
100 replay(mockCoreService);
101
Jian Li8bcef8b2016-01-06 11:35:53 -0800102 FilteringObjective filteringObjective = getFilteringObjective("FilteringObjective.json");
103
104 assertThat(filteringObjective.type(), is(FilteringObjective.Type.PERMIT));
105 assertThat(filteringObjective.priority(), is(60));
106 assertThat(filteringObjective.timeout(), is(1));
107 assertThat(filteringObjective.op(), is(FilteringObjective.Operation.ADD));
108 assertThat(filteringObjective.permanent(), is(false));
109 }
110
111 /**
112 * Reads in a filteringObjective from the given resource and decodes it.
113 *
114 * @param resourceName resource to use to read the JSON for the rule
115 * @return decoded filteringObjective
116 * @throws IOException if processing the resource fails
117 */
118 private FilteringObjective getFilteringObjective(String resourceName) throws IOException {
119 InputStream jsonStream = FilteringObjectiveCodecTest.class.getResourceAsStream(resourceName);
120 JsonNode json = context.mapper().readTree(jsonStream);
121 assertThat(json, notNullValue());
122 FilteringObjective filteringObjective = filteringObjectiveCodec.decode((ObjectNode) json, context);
123 assertThat(filteringObjective, notNullValue());
124 return filteringObjective;
125 }
126}