blob: fb4f1e8a91bbfa8a780b4f2eebd0472d40483857 [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;
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070029import org.onosproject.net.flowobjective.DefaultNextTreatment;
Jian Li8bcef8b2016-01-06 11:35:53 -080030import org.onosproject.net.flowobjective.NextObjective;
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070031import org.onosproject.net.flowobjective.NextTreatment;
Jian Li8bcef8b2016-01-06 11:35:53 -080032
33import java.io.IOException;
34import java.io.InputStream;
35
36import static org.easymock.EasyMock.createMock;
37import static org.easymock.EasyMock.expect;
38import static org.easymock.EasyMock.replay;
39import static org.hamcrest.MatcherAssert.assertThat;
40import static org.hamcrest.Matchers.is;
41import static org.hamcrest.Matchers.notNullValue;
42import static org.onosproject.codec.impl.NextObjectiveJsonMatcher.matchesNextObjective;
43import static org.onosproject.net.NetTestTools.APP_ID;
44
45/**
46 * Unit tests for NextObjective Codec.
47 */
48public class NextObjectiveCodecTest {
49
50 MockCodecContext context;
51 JsonCodec<NextObjective> nextObjectiveCodec;
52 final CoreService mockCoreService = createMock(CoreService.class);
Jian Lia424a052016-05-30 21:02:33 +090053 static final String SAMPLE_APP_ID = "org.onosproject.sample";
Jian Li8bcef8b2016-01-06 11:35:53 -080054
55 /**
56 * Sets up for each test.
57 * Creates a context and fetches the NextObjective codec.
58 */
59 @Before
60 public void setUp() {
61 context = new MockCodecContext();
62 nextObjectiveCodec = context.codec(NextObjective.class);
63 assertThat(nextObjectiveCodec, notNullValue());
64
Jian Li8bcef8b2016-01-06 11:35:53 -080065 context.registerService(CoreService.class, mockCoreService);
66 }
67
68 /**
69 * Tests encoding of a NextObjective object.
70 */
71 @Test
72 public void testNextObjectiveEncode() {
73
74 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070075 NextTreatment nextTreatment = DefaultNextTreatment.of(treatment, 5);
Jian Li8bcef8b2016-01-06 11:35:53 -080076
77 NextObjective nextObj = DefaultNextObjective.builder()
78 .makePermanent()
79 .withType(NextObjective.Type.HASHED)
80 .fromApp(APP_ID)
81 .withPriority(60)
82 .withId(5)
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070083 .addTreatment(nextTreatment)
Jian Li8bcef8b2016-01-06 11:35:53 -080084 .add();
85
86 ObjectNode nextObjJson = nextObjectiveCodec.encode(nextObj, context);
87 assertThat(nextObjJson, matchesNextObjective(nextObj));
88 }
89
90 /**
91 * Test decoding of a NextObjective object.
92 */
93 @Test
94 public void testNextObjectiveDecode() throws IOException {
Jian Lia424a052016-05-30 21:02:33 +090095
96 ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID);
97
98 expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes();
99 replay(mockCoreService);
100
Jian Li8bcef8b2016-01-06 11:35:53 -0800101 NextObjective nextObjective = getNextObjective("NextObjective.json");
102
103 assertThat(nextObjective.type(), is(NextObjective.Type.FAILOVER));
104 assertThat(nextObjective.op(), is(NextObjective.Operation.ADD));
105 }
106
107 /**
108 * Reads in a nextObjective from the given resource and decodes it.
109 *
110 * @param resourceName resource to use to read the JSON for the rule
111 * @return decoded nextObjective
112 * @throws IOException if processing the resource fails
113 */
114 private NextObjective getNextObjective(String resourceName) throws IOException {
115 InputStream jsonStream = NextObjectiveCodecTest.class.getResourceAsStream(resourceName);
116 JsonNode json = context.mapper().readTree(jsonStream);
117 assertThat(json, notNullValue());
118 NextObjective nextObjective = nextObjectiveCodec.decode((ObjectNode) json, context);
119 assertThat(nextObjective, notNullValue());
120 return nextObjective;
121 }
122}