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