blob: 48c481791d3c55c8355088e9b274efe279e2c89b [file] [log] [blame]
Ray Milkeyb82c42b2015-06-30 09:42:20 -07001/*
2 * Copyright 2015 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 java.io.IOException;
19import java.io.InputStream;
20
21import org.junit.Assert;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.Link;
27import org.onosproject.net.intent.Constraint;
28import org.onosproject.net.intent.constraint.AnnotationConstraint;
29import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
30import org.onosproject.net.intent.constraint.BandwidthConstraint;
31import org.onosproject.net.intent.constraint.LambdaConstraint;
32import org.onosproject.net.intent.constraint.LatencyConstraint;
33import org.onosproject.net.intent.constraint.LinkTypeConstraint;
34import org.onosproject.net.intent.constraint.ObstacleConstraint;
35import org.onosproject.net.intent.constraint.WaypointConstraint;
36
37import com.fasterxml.jackson.databind.JsonNode;
38import com.fasterxml.jackson.databind.node.ObjectNode;
39
40import static com.google.common.base.Preconditions.checkNotNull;
41import static org.easymock.EasyMock.createMock;
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.replay;
44import static org.hamcrest.MatcherAssert.assertThat;
45import static org.hamcrest.Matchers.hasItem;
46import static org.hamcrest.Matchers.hasSize;
47import static org.hamcrest.Matchers.instanceOf;
48import static org.hamcrest.Matchers.is;
49import static org.hamcrest.Matchers.notNullValue;
50import static org.onosproject.net.NetTestTools.APP_ID;
51import static org.onosproject.net.NetTestTools.did;
52
53/**
54 * Unit tests for Constraint codec.
55 */
56public class ConstraintCodecTest {
57
58 MockCodecContext context;
59 JsonCodec<Constraint> constraintCodec;
60 final CoreService mockCoreService = createMock(CoreService.class);
61
62 /**
63 * Sets up for each test. Creates a context and fetches the flow rule
64 * codec.
65 */
66 @Before
67 public void setUp() {
68 context = new MockCodecContext();
69 constraintCodec = context.codec(Constraint.class);
70 assertThat(constraintCodec, notNullValue());
71
72 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
73 .andReturn(APP_ID).anyTimes();
74 replay(mockCoreService);
75 context.registerService(CoreService.class, mockCoreService);
76 }
77
78 /**
79 * Reads in a constraint from the given resource and decodes it.
80 *
81 * @param resourceName resource to use to read the JSON for the constraint
82 * @return decoded constraint
83 */
84 private Constraint getConstraint(String resourceName) {
85 InputStream jsonStream = ConstraintCodecTest.class
86 .getResourceAsStream(resourceName);
87 try {
88 JsonNode json = context.mapper().readTree(jsonStream);
89 assertThat(json, notNullValue());
90 Constraint constraint = constraintCodec.decode((ObjectNode) json, context);
91 assertThat(constraint, notNullValue());
92 return checkNotNull(constraint);
93 } catch (IOException ioe) {
94 Assert.fail(ioe.getMessage());
95 throw new IllegalStateException("cannot happen");
96 }
97 }
98
99
100 /**
101 * Tests link type constraint.
102 */
103 @Test
104 public void linkTypeConstraint() {
105 Constraint constraint = getConstraint("LinkTypeConstraint.json");
106 assertThat(constraint, instanceOf(LinkTypeConstraint.class));
107
108 LinkTypeConstraint linkTypeConstraint = (LinkTypeConstraint) constraint;
109 assertThat(linkTypeConstraint.isInclusive(), is(false));
110 assertThat(linkTypeConstraint.types(), hasSize(2));
111 assertThat(linkTypeConstraint.types(), hasItem(Link.Type.OPTICAL));
112 assertThat(linkTypeConstraint.types(), hasItem(Link.Type.DIRECT));
113 }
114
115 /**
116 * Tests annotation constraint.
117 */
118 @Test
119 public void annotationConstraint() {
120 Constraint constraint = getConstraint("AnnotationConstraint.json");
121 assertThat(constraint, instanceOf(AnnotationConstraint.class));
122
123 AnnotationConstraint annotationConstraint = (AnnotationConstraint) constraint;
124 assertThat(annotationConstraint.key(), is("key"));
125 assertThat(annotationConstraint.threshold(), is(123.0D));
126 }
127
128 /**
129 * Tests bandwidth constraint.
130 */
131 @Test
132 public void bandwidthConstraint() {
133 Constraint constraint = getConstraint("BandwidthConstraint.json");
134 assertThat(constraint, instanceOf(BandwidthConstraint.class));
135
136 BandwidthConstraint bandwidthConstraint = (BandwidthConstraint) constraint;
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800137 assertThat(bandwidthConstraint.bandwidth().bps(), is(345.678D));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700138 }
139
140 /**
141 * Tests lambda constraint.
142 */
143 @Test
144 public void lambdaConstraint() {
145 Constraint constraint = getConstraint("LambdaConstraint.json");
146 assertThat(constraint, instanceOf(LambdaConstraint.class));
147
148 LambdaConstraint lambdaConstraint = (LambdaConstraint) constraint;
149 assertThat(lambdaConstraint.lambda().toInt(), is(444));
150 }
151
152 /**
153 * Tests latency constraint.
154 */
155 @Test
156 public void latencyConstraint() {
157 Constraint constraint = getConstraint("LatencyConstraint.json");
158 assertThat(constraint, instanceOf(LatencyConstraint.class));
159
160 LatencyConstraint latencyConstraint = (LatencyConstraint) constraint;
161 assertThat(latencyConstraint.latency().toMillis(), is(111L));
162 }
163
164 /**
165 * Tests obstacle constraint.
166 */
167 @Test
168 public void obstacleConstraint() {
169 Constraint constraint = getConstraint("ObstacleConstraint.json");
170 assertThat(constraint, instanceOf(ObstacleConstraint.class));
171
172 ObstacleConstraint obstacleConstraint = (ObstacleConstraint) constraint;
173
174 assertThat(obstacleConstraint.obstacles(), hasItem(did("dev1")));
175 assertThat(obstacleConstraint.obstacles(), hasItem(did("dev2")));
176 assertThat(obstacleConstraint.obstacles(), hasItem(did("dev3")));
177 }
178
179 /**
180 * Tests waypoint constaint.
181 */
182 @Test
183 public void waypointConstraint() {
184 Constraint constraint = getConstraint("WaypointConstraint.json");
185 assertThat(constraint, instanceOf(WaypointConstraint.class));
186
187 WaypointConstraint waypointConstraint = (WaypointConstraint) constraint;
188
189 assertThat(waypointConstraint.waypoints(), hasItem(did("devA")));
190 assertThat(waypointConstraint.waypoints(), hasItem(did("devB")));
191 assertThat(waypointConstraint.waypoints(), hasItem(did("devC")));
192 }
193
194 /**
195 * Tests asymmetric path constraint.
196 */
197 @Test
198 public void asymmetricPathConstraint() {
199 Constraint constraint = getConstraint("AsymmetricPathConstraint.json");
200 assertThat(constraint, instanceOf(AsymmetricPathConstraint.class));
201 }
202}