blob: c826d375bc175d55cdc157e7d09e2d1f35b4660b [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.onlab.packet.VlanId;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.flow.criteria.Criteria;
26import org.onosproject.net.flow.criteria.Criterion;
27import org.onosproject.net.flowobjective.DefaultFilteringObjective;
28import org.onosproject.net.flowobjective.FilteringObjective;
29
30import java.io.IOException;
31import java.io.InputStream;
32
33import static org.easymock.EasyMock.createMock;
34import static org.easymock.EasyMock.expect;
35import static org.easymock.EasyMock.replay;
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.is;
38import static org.hamcrest.Matchers.notNullValue;
39import static org.onosproject.codec.impl.FilteringObjectiveJsonMatcher.matchesFilteringObjective;
40import static org.onosproject.net.NetTestTools.APP_ID;
41
42/**
43 * Unit tests for FilteringObjective Codec.
44 */
45public class FilteringObjectiveCodecTest {
46
47 MockCodecContext context;
48 JsonCodec<FilteringObjective> filteringObjectiveCodec;
49 final CoreService mockCoreService = createMock(CoreService.class);
50
51 /**
52 * Sets up for each test.
53 * Creates a context and fetches the FilteringObjective codec.
54 */
55 @Before
56 public void setUp() {
57 context = new MockCodecContext();
58 filteringObjectiveCodec = context.codec(FilteringObjective.class);
59 assertThat(filteringObjectiveCodec, notNullValue());
60
61 expect(mockCoreService.registerApplication(FilteringObjectiveCodec.REST_APP_ID))
62 .andReturn(APP_ID).anyTimes();
63 replay(mockCoreService);
64 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 {
96 FilteringObjective filteringObjective = getFilteringObjective("FilteringObjective.json");
97
98 assertThat(filteringObjective.type(), is(FilteringObjective.Type.PERMIT));
99 assertThat(filteringObjective.priority(), is(60));
100 assertThat(filteringObjective.timeout(), is(1));
101 assertThat(filteringObjective.op(), is(FilteringObjective.Operation.ADD));
102 assertThat(filteringObjective.permanent(), is(false));
103 }
104
105 /**
106 * Reads in a filteringObjective from the given resource and decodes it.
107 *
108 * @param resourceName resource to use to read the JSON for the rule
109 * @return decoded filteringObjective
110 * @throws IOException if processing the resource fails
111 */
112 private FilteringObjective getFilteringObjective(String resourceName) throws IOException {
113 InputStream jsonStream = FilteringObjectiveCodecTest.class.getResourceAsStream(resourceName);
114 JsonNode json = context.mapper().readTree(jsonStream);
115 assertThat(json, notNullValue());
116 FilteringObjective filteringObjective = filteringObjectiveCodec.decode((ObjectNode) json, context);
117 assertThat(filteringObjective, notNullValue());
118 return filteringObjective;
119 }
120}