Committed by
Gerrit Code Review
Add unit test for MulticastRoute REST API
Change-Id: I33fb93256e701cbff8af3e4247917c90fea8da59
Showing
2 changed files
with
253 additions
and
0 deletions
1 | +/* | ||
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 | + */ | ||
16 | +package org.onosproject.rest; | ||
17 | + | ||
18 | +import com.eclipsesource.json.Json; | ||
19 | +import com.eclipsesource.json.JsonArray; | ||
20 | +import com.eclipsesource.json.JsonObject; | ||
21 | +import com.google.common.collect.ImmutableSet; | ||
22 | +import org.glassfish.jersey.client.ClientProperties; | ||
23 | +import org.hamcrest.Description; | ||
24 | +import org.hamcrest.TypeSafeMatcher; | ||
25 | +import org.junit.Before; | ||
26 | +import org.junit.Test; | ||
27 | +import org.onlab.osgi.ServiceDirectory; | ||
28 | +import org.onlab.osgi.TestServiceDirectory; | ||
29 | +import org.onlab.packet.IpAddress; | ||
30 | +import org.onlab.rest.BaseResource; | ||
31 | +import org.onosproject.codec.CodecService; | ||
32 | +import org.onosproject.codec.impl.CodecManager; | ||
33 | +import org.onosproject.net.mcast.McastRoute; | ||
34 | +import org.onosproject.net.mcast.MulticastRouteService; | ||
35 | + | ||
36 | +import javax.ws.rs.client.Entity; | ||
37 | +import javax.ws.rs.client.WebTarget; | ||
38 | +import javax.ws.rs.core.MediaType; | ||
39 | +import javax.ws.rs.core.Response; | ||
40 | +import java.io.InputStream; | ||
41 | +import java.net.HttpURLConnection; | ||
42 | +import java.util.Set; | ||
43 | + | ||
44 | +import static org.easymock.EasyMock.anyObject; | ||
45 | +import static org.easymock.EasyMock.createMock; | ||
46 | +import static org.easymock.EasyMock.expect; | ||
47 | +import static org.easymock.EasyMock.expectLastCall; | ||
48 | +import static org.easymock.EasyMock.replay; | ||
49 | +import static org.easymock.EasyMock.verify; | ||
50 | +import static org.hamcrest.Matchers.hasSize; | ||
51 | +import static org.hamcrest.Matchers.is; | ||
52 | +import static org.hamcrest.Matchers.notNullValue; | ||
53 | +import static org.junit.Assert.assertThat; | ||
54 | + | ||
55 | +/** | ||
56 | + * Unit tests for multicast route REST APIs. | ||
57 | + */ | ||
58 | +public class MulticastRouteResourceTest extends ResourceTest { | ||
59 | + | ||
60 | + final MulticastRouteService mockMulticastRouteService = | ||
61 | + createMock(MulticastRouteService.class); | ||
62 | + | ||
63 | + private McastRoute route1; | ||
64 | + private McastRoute route2; | ||
65 | + private McastRoute route3; | ||
66 | + | ||
67 | + private void initMcastRouteMocks() { | ||
68 | + IpAddress source1 = IpAddress.valueOf("1.1.1.1"); | ||
69 | + IpAddress source2 = IpAddress.valueOf("2.2.2.2"); | ||
70 | + IpAddress source3 = IpAddress.valueOf("3.3.3.3"); | ||
71 | + | ||
72 | + IpAddress group = IpAddress.valueOf("224.0.0.1"); | ||
73 | + | ||
74 | + route1 = new McastRoute(source1, group, McastRoute.Type.PIM); | ||
75 | + route2 = new McastRoute(source2, group, McastRoute.Type.IGMP); | ||
76 | + route3 = new McastRoute(source3, group, McastRoute.Type.STATIC); | ||
77 | + } | ||
78 | + | ||
79 | + @Before | ||
80 | + public void setupTest() { | ||
81 | + final CodecManager codecService = new CodecManager(); | ||
82 | + codecService.activate(); | ||
83 | + | ||
84 | + ServiceDirectory testDirectory = | ||
85 | + new TestServiceDirectory() | ||
86 | + .add(MulticastRouteService.class, mockMulticastRouteService) | ||
87 | + .add(CodecService.class, codecService); | ||
88 | + BaseResource.setServiceDirectory(testDirectory); | ||
89 | + } | ||
90 | + | ||
91 | + /** | ||
92 | + * Hamcrest matcher to check that a mcast route representation in JSON matches | ||
93 | + * the actual mcast route. | ||
94 | + */ | ||
95 | + public static class McastRouteJsonMatcher extends TypeSafeMatcher<JsonObject> { | ||
96 | + private final McastRoute route; | ||
97 | + private String reason = ""; | ||
98 | + | ||
99 | + public McastRouteJsonMatcher(McastRoute mcastRoute) { | ||
100 | + this.route = mcastRoute; | ||
101 | + } | ||
102 | + | ||
103 | + @Override | ||
104 | + protected boolean matchesSafely(JsonObject jsonMcastRoute) { | ||
105 | + | ||
106 | + // check source | ||
107 | + String jsonSource = jsonMcastRoute.get("source").asString(); | ||
108 | + String source = route.source().toString(); | ||
109 | + if (!jsonSource.equals(source)) { | ||
110 | + reason = "Mcast route source was " + jsonSource; | ||
111 | + return false; | ||
112 | + } | ||
113 | + | ||
114 | + // check group | ||
115 | + String jsonGroup = jsonMcastRoute.get("group").asString(); | ||
116 | + String group = route.group().toString(); | ||
117 | + if (!jsonGroup.equals(group)) { | ||
118 | + reason = "Mcast route group was " + jsonSource; | ||
119 | + return false; | ||
120 | + } | ||
121 | + | ||
122 | + // check type | ||
123 | + String jsonType = jsonMcastRoute.get("type").asString(); | ||
124 | + String type = route.type().toString(); | ||
125 | + if (!jsonType.equals(type)) { | ||
126 | + reason = "Mcast route type was " + jsonSource; | ||
127 | + return false; | ||
128 | + } | ||
129 | + | ||
130 | + return true; | ||
131 | + } | ||
132 | + | ||
133 | + @Override | ||
134 | + public void describeTo(Description description) { | ||
135 | + description.appendText(reason); | ||
136 | + } | ||
137 | + } | ||
138 | + | ||
139 | + private static McastRouteJsonMatcher matchesMcastRoute(McastRoute route) { | ||
140 | + return new McastRouteJsonMatcher(route); | ||
141 | + } | ||
142 | + | ||
143 | + /** | ||
144 | + * Hamcrest matcher to check that a Mcast route is represented properly in | ||
145 | + * a JSON array of Mcastroutes. | ||
146 | + */ | ||
147 | + public static class McastRouteJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { | ||
148 | + private final McastRoute route; | ||
149 | + private String reason = ""; | ||
150 | + | ||
151 | + public McastRouteJsonArrayMatcher(McastRoute mcastRoute) { | ||
152 | + this.route = mcastRoute; | ||
153 | + } | ||
154 | + | ||
155 | + @Override | ||
156 | + protected boolean matchesSafely(JsonArray json) { | ||
157 | + boolean found = false; | ||
158 | + for (int index = 0; index < json.size(); index++) { | ||
159 | + final JsonObject jsonMcastRoute = json.get(index).asObject(); | ||
160 | + | ||
161 | + final String source = route.source().toString(); | ||
162 | + final String group = route.group().toString(); | ||
163 | + final String type = route.type().toString(); | ||
164 | + final String jsonSource = jsonMcastRoute.get("source").asString(); | ||
165 | + final String jsonGroup = jsonMcastRoute.get("group").asString(); | ||
166 | + final String jsonType = jsonMcastRoute.get("type").asString(); | ||
167 | + | ||
168 | + if (jsonSource.equals(source) && jsonGroup.equals(group) && | ||
169 | + jsonType.equals(type)) { | ||
170 | + found = true; | ||
171 | + assertThat(jsonMcastRoute, matchesMcastRoute(route)); | ||
172 | + } | ||
173 | + } | ||
174 | + | ||
175 | + return found; | ||
176 | + } | ||
177 | + | ||
178 | + @Override | ||
179 | + public void describeTo(Description description) { | ||
180 | + description.appendText(reason); | ||
181 | + } | ||
182 | + } | ||
183 | + | ||
184 | + private static McastRouteJsonArrayMatcher hasMcastRoute(McastRoute route) { | ||
185 | + return new McastRouteJsonArrayMatcher(route); | ||
186 | + } | ||
187 | + | ||
188 | + /** | ||
189 | + * Tests the results of the REST API GET when there are active mcastroutes. | ||
190 | + */ | ||
191 | + @Test | ||
192 | + public void testMcastRoutePopulatedArray() { | ||
193 | + initMcastRouteMocks(); | ||
194 | + final Set<McastRoute> mcastRoutes = ImmutableSet.of(route1, route2, route3); | ||
195 | + expect(mockMulticastRouteService.getRoutes()).andReturn(mcastRoutes).anyTimes(); | ||
196 | + replay(mockMulticastRouteService); | ||
197 | + | ||
198 | + final WebTarget wt = target(); | ||
199 | + final String response = wt.path("mcast").request().get(String.class); | ||
200 | + final JsonObject result = Json.parse(response).asObject(); | ||
201 | + assertThat(result, notNullValue()); | ||
202 | + | ||
203 | + assertThat(result.names(), hasSize(1)); | ||
204 | + assertThat(result.names().get(0), is("routes")); | ||
205 | + final JsonArray jsonMcastRoutes = result.get("routes").asArray(); | ||
206 | + assertThat(jsonMcastRoutes, notNullValue()); | ||
207 | + assertThat(jsonMcastRoutes, hasMcastRoute(route1)); | ||
208 | + assertThat(jsonMcastRoutes, hasMcastRoute(route2)); | ||
209 | + assertThat(jsonMcastRoutes, hasMcastRoute(route3)); | ||
210 | + } | ||
211 | + | ||
212 | + /** | ||
213 | + * Tests creating a Mcast route with POST. | ||
214 | + */ | ||
215 | + @Test | ||
216 | + public void testMcastRoutePost() { | ||
217 | + mockMulticastRouteService.add(anyObject()); | ||
218 | + expectLastCall(); | ||
219 | + replay(mockMulticastRouteService); | ||
220 | + | ||
221 | + WebTarget wt = target(); | ||
222 | + InputStream jsonStream = MulticastRouteResourceTest.class | ||
223 | + .getResourceAsStream("mcastroute.json"); | ||
224 | + | ||
225 | + Response response = wt.path("mcast/") | ||
226 | + .request(MediaType.APPLICATION_JSON_TYPE) | ||
227 | + .post(Entity.json(jsonStream)); | ||
228 | + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); | ||
229 | + | ||
230 | + verify(mockMulticastRouteService); | ||
231 | + } | ||
232 | + | ||
233 | + /** | ||
234 | + * Tests deletion a Mcast route with DELETE. | ||
235 | + */ | ||
236 | + @Test | ||
237 | + public void testMcastRouteDelete() { | ||
238 | + mockMulticastRouteService.remove(anyObject()); | ||
239 | + expectLastCall(); | ||
240 | + replay(mockMulticastRouteService); | ||
241 | + | ||
242 | + WebTarget wt = target().property( | ||
243 | + ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true); | ||
244 | + InputStream jsonStream = MulticastRouteResourceTest.class | ||
245 | + .getResourceAsStream("mcastroute.json"); | ||
246 | + wt.request().method("DELETE", Entity.json(jsonStream)); | ||
247 | + } | ||
248 | +} |
-
Please register or login to post a comment