blob: 355209fdccb9c3cab61a081cca5107de14dfa65c [file] [log] [blame]
Jian Li8bcef8b2016-01-06 11:35:53 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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.onosproject.codec.JsonCodec;
Jian Lia424a052016-05-30 21:02:33 +090023import org.onosproject.core.ApplicationId;
Jian Li8bcef8b2016-01-06 11:35:53 -080024import org.onosproject.core.CoreService;
Jian Lia424a052016-05-30 21:02:33 +090025import org.onosproject.core.DefaultApplicationId;
Jian Li8bcef8b2016-01-06 11:35:53 -080026import org.onosproject.net.flow.DefaultTrafficTreatment;
27import org.onosproject.net.flow.TrafficTreatment;
28import org.onosproject.net.flowobjective.DefaultNextObjective;
29import org.onosproject.net.flowobjective.NextObjective;
30
31import java.io.IOException;
32import java.io.InputStream;
33
34import static org.easymock.EasyMock.createMock;
35import static org.easymock.EasyMock.expect;
36import static org.easymock.EasyMock.replay;
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.Matchers.is;
39import static org.hamcrest.Matchers.notNullValue;
40import static org.onosproject.codec.impl.NextObjectiveJsonMatcher.matchesNextObjective;
41import static org.onosproject.net.NetTestTools.APP_ID;
42
43/**
44 * Unit tests for NextObjective Codec.
45 */
46public class NextObjectiveCodecTest {
47
48 MockCodecContext context;
49 JsonCodec<NextObjective> nextObjectiveCodec;
50 final CoreService mockCoreService = createMock(CoreService.class);
Jian Lia424a052016-05-30 21:02:33 +090051 static final String SAMPLE_APP_ID = "org.onosproject.sample";
Jian Li8bcef8b2016-01-06 11:35:53 -080052
53 /**
54 * Sets up for each test.
55 * Creates a context and fetches the NextObjective codec.
56 */
57 @Before
58 public void setUp() {
59 context = new MockCodecContext();
60 nextObjectiveCodec = context.codec(NextObjective.class);
61 assertThat(nextObjectiveCodec, notNullValue());
62
Jian Li8bcef8b2016-01-06 11:35:53 -080063 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 {
Jian Lia424a052016-05-30 21:02:33 +090092
93 ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID);
94
95 expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes();
96 replay(mockCoreService);
97
Jian Li8bcef8b2016-01-06 11:35:53 -080098 NextObjective nextObjective = getNextObjective("NextObjective.json");
99
100 assertThat(nextObjective.type(), is(NextObjective.Type.FAILOVER));
101 assertThat(nextObjective.op(), is(NextObjective.Operation.ADD));
102 }
103
104 /**
105 * Reads in a nextObjective from the given resource and decodes it.
106 *
107 * @param resourceName resource to use to read the JSON for the rule
108 * @return decoded nextObjective
109 * @throws IOException if processing the resource fails
110 */
111 private NextObjective getNextObjective(String resourceName) throws IOException {
112 InputStream jsonStream = NextObjectiveCodecTest.class.getResourceAsStream(resourceName);
113 JsonNode json = context.mapper().readTree(jsonStream);
114 assertThat(json, notNullValue());
115 NextObjective nextObjective = nextObjectiveCodec.decode((ObjectNode) json, context);
116 assertThat(nextObjective, notNullValue());
117 return nextObjective;
118 }
119}