Improve coverage for Intents REST API test
Also, added @Provider annotation to all exception mappers so the Jersey unit test framework will apply them during testing. Change-Id: Ifd9237de2e159cd97b64dcb9bde4a66f14bd0dfc
Showing
9 changed files
with
77 additions
and
0 deletions
... | @@ -16,11 +16,14 @@ | ... | @@ -16,11 +16,14 @@ |
16 | package org.onosproject.rest.exceptions; | 16 | package org.onosproject.rest.exceptions; |
17 | 17 | ||
18 | import javax.ws.rs.core.Response; | 18 | import javax.ws.rs.core.Response; |
19 | +import javax.ws.rs.ext.Provider; | ||
20 | + | ||
19 | import java.io.IOException; | 21 | import java.io.IOException; |
20 | 22 | ||
21 | /** | 23 | /** |
22 | * Mapper for IO exceptions to the BAD_REQUEST response code. | 24 | * Mapper for IO exceptions to the BAD_REQUEST response code. |
23 | */ | 25 | */ |
26 | +@Provider | ||
24 | public class BadRequestMapper extends AbstractMapper<IOException> { | 27 | public class BadRequestMapper extends AbstractMapper<IOException> { |
25 | @Override | 28 | @Override |
26 | protected Response.Status responseStatus() { | 29 | protected Response.Status responseStatus() { | ... | ... |
... | @@ -18,10 +18,12 @@ package org.onosproject.rest.exceptions; | ... | @@ -18,10 +18,12 @@ package org.onosproject.rest.exceptions; |
18 | import org.onlab.util.ItemNotFoundException; | 18 | import org.onlab.util.ItemNotFoundException; |
19 | 19 | ||
20 | import javax.ws.rs.core.Response; | 20 | import javax.ws.rs.core.Response; |
21 | +import javax.ws.rs.ext.Provider; | ||
21 | 22 | ||
22 | /** | 23 | /** |
23 | * Mapper for service not found exceptions to the NOT_FOUND response code. | 24 | * Mapper for service not found exceptions to the NOT_FOUND response code. |
24 | */ | 25 | */ |
26 | +@Provider | ||
25 | public class EntityNotFoundMapper extends AbstractMapper<ItemNotFoundException> { | 27 | public class EntityNotFoundMapper extends AbstractMapper<ItemNotFoundException> { |
26 | @Override | 28 | @Override |
27 | protected Response.Status responseStatus() { | 29 | protected Response.Status responseStatus() { | ... | ... |
... | @@ -16,10 +16,12 @@ | ... | @@ -16,10 +16,12 @@ |
16 | package org.onosproject.rest.exceptions; | 16 | package org.onosproject.rest.exceptions; |
17 | 17 | ||
18 | import javax.ws.rs.core.Response; | 18 | import javax.ws.rs.core.Response; |
19 | +import javax.ws.rs.ext.Provider; | ||
19 | 20 | ||
20 | /** | 21 | /** |
21 | * Mapper for illegal argument exceptions to the BAD_REQUEST response code. | 22 | * Mapper for illegal argument exceptions to the BAD_REQUEST response code. |
22 | */ | 23 | */ |
24 | +@Provider | ||
23 | public class IllegalArgumentExceptionMapper extends AbstractMapper<IllegalArgumentException> { | 25 | public class IllegalArgumentExceptionMapper extends AbstractMapper<IllegalArgumentException> { |
24 | @Override | 26 | @Override |
25 | protected Response.Status responseStatus() { | 27 | protected Response.Status responseStatus() { | ... | ... |
... | @@ -16,10 +16,12 @@ | ... | @@ -16,10 +16,12 @@ |
16 | package org.onosproject.rest.exceptions; | 16 | package org.onosproject.rest.exceptions; |
17 | 17 | ||
18 | import javax.ws.rs.core.Response; | 18 | import javax.ws.rs.core.Response; |
19 | +import javax.ws.rs.ext.Provider; | ||
19 | 20 | ||
20 | /** | 21 | /** |
21 | * Mapper for illegal state exceptions to the BAD_REQUEST response code. | 22 | * Mapper for illegal state exceptions to the BAD_REQUEST response code. |
22 | */ | 23 | */ |
24 | +@Provider | ||
23 | public class IllegalStateExceptionMapper extends AbstractMapper<IllegalStateException> { | 25 | public class IllegalStateExceptionMapper extends AbstractMapper<IllegalStateException> { |
24 | @Override | 26 | @Override |
25 | protected Response.Status responseStatus() { | 27 | protected Response.Status responseStatus() { | ... | ... |
... | @@ -16,12 +16,14 @@ | ... | @@ -16,12 +16,14 @@ |
16 | package org.onosproject.rest.exceptions; | 16 | package org.onosproject.rest.exceptions; |
17 | 17 | ||
18 | import javax.ws.rs.core.Response; | 18 | import javax.ws.rs.core.Response; |
19 | +import javax.ws.rs.ext.Provider; | ||
19 | 20 | ||
20 | import com.sun.jersey.api.NotFoundException; | 21 | import com.sun.jersey.api.NotFoundException; |
21 | 22 | ||
22 | /** | 23 | /** |
23 | * Mapper for api not found exceptions to the NOT_FOUND response code. | 24 | * Mapper for api not found exceptions to the NOT_FOUND response code. |
24 | */ | 25 | */ |
26 | +@Provider | ||
25 | public class NotFoundMapper extends AbstractMapper<NotFoundException> { | 27 | public class NotFoundMapper extends AbstractMapper<NotFoundException> { |
26 | 28 | ||
27 | @Override | 29 | @Override | ... | ... |
... | @@ -16,10 +16,12 @@ | ... | @@ -16,10 +16,12 @@ |
16 | package org.onosproject.rest.exceptions; | 16 | package org.onosproject.rest.exceptions; |
17 | 17 | ||
18 | import javax.ws.rs.core.Response; | 18 | import javax.ws.rs.core.Response; |
19 | +import javax.ws.rs.ext.Provider; | ||
19 | 20 | ||
20 | /** | 21 | /** |
21 | * Mapper for service not found exceptions to the INTERNAL_SERVER_ERROR response code. | 22 | * Mapper for service not found exceptions to the INTERNAL_SERVER_ERROR response code. |
22 | */ | 23 | */ |
24 | +@Provider | ||
23 | public class ServerErrorMapper extends AbstractMapper<RuntimeException> { | 25 | public class ServerErrorMapper extends AbstractMapper<RuntimeException> { |
24 | @Override | 26 | @Override |
25 | protected Response.Status responseStatus() { | 27 | protected Response.Status responseStatus() { | ... | ... |
... | @@ -18,10 +18,12 @@ package org.onosproject.rest.exceptions; | ... | @@ -18,10 +18,12 @@ package org.onosproject.rest.exceptions; |
18 | import org.onlab.osgi.ServiceNotFoundException; | 18 | import org.onlab.osgi.ServiceNotFoundException; |
19 | 19 | ||
20 | import javax.ws.rs.core.Response; | 20 | import javax.ws.rs.core.Response; |
21 | +import javax.ws.rs.ext.Provider; | ||
21 | 22 | ||
22 | /** | 23 | /** |
23 | * Mapper for service not found exceptions to the SERVICE_UNAVAILABLE response code. | 24 | * Mapper for service not found exceptions to the SERVICE_UNAVAILABLE response code. |
24 | */ | 25 | */ |
26 | +@Provider | ||
25 | public class ServiceNotFoundMapper extends AbstractMapper<ServiceNotFoundException> { | 27 | public class ServiceNotFoundMapper extends AbstractMapper<ServiceNotFoundException> { |
26 | @Override | 28 | @Override |
27 | protected Response.Status responseStatus() { | 29 | protected Response.Status responseStatus() { | ... | ... |
... | @@ -17,10 +17,12 @@ package org.onosproject.rest.exceptions; | ... | @@ -17,10 +17,12 @@ package org.onosproject.rest.exceptions; |
17 | 17 | ||
18 | import javax.ws.rs.WebApplicationException; | 18 | import javax.ws.rs.WebApplicationException; |
19 | import javax.ws.rs.core.Response; | 19 | import javax.ws.rs.core.Response; |
20 | +import javax.ws.rs.ext.Provider; | ||
20 | 21 | ||
21 | /** | 22 | /** |
22 | * Exception mapper for WebApplicationExceptions. | 23 | * Exception mapper for WebApplicationExceptions. |
23 | */ | 24 | */ |
25 | +@Provider | ||
24 | public class WebApplicationExceptionMapper extends AbstractMapper<WebApplicationException> { | 26 | public class WebApplicationExceptionMapper extends AbstractMapper<WebApplicationException> { |
25 | 27 | ||
26 | /** | 28 | /** | ... | ... |
... | @@ -331,15 +331,29 @@ public class IntentsResourceTest extends ResourceTest { | ... | @@ -331,15 +331,29 @@ public class IntentsResourceTest extends ResourceTest { |
331 | expect(mockIntentService.getIntent(Key.of("0", APP_ID))) | 331 | expect(mockIntentService.getIntent(Key.of("0", APP_ID))) |
332 | .andReturn(intent) | 332 | .andReturn(intent) |
333 | .anyTimes(); | 333 | .anyTimes(); |
334 | + expect(mockIntentService.getIntent(Key.of(0, APP_ID))) | ||
335 | + .andReturn(intent) | ||
336 | + .anyTimes(); | ||
337 | + expect(mockIntentService.getIntent(Key.of("0x0", APP_ID))) | ||
338 | + .andReturn(null) | ||
339 | + .anyTimes(); | ||
334 | replay(mockIntentService); | 340 | replay(mockIntentService); |
335 | expect(mockCoreService.getAppId(APP_ID.name())) | 341 | expect(mockCoreService.getAppId(APP_ID.name())) |
336 | .andReturn(APP_ID).anyTimes(); | 342 | .andReturn(APP_ID).anyTimes(); |
337 | replay(mockCoreService); | 343 | replay(mockCoreService); |
338 | final WebResource rs = resource(); | 344 | final WebResource rs = resource(); |
345 | + | ||
346 | + // Test get using key string | ||
339 | final String response = rs.path("intents/" + APP_ID.name() | 347 | final String response = rs.path("intents/" + APP_ID.name() |
340 | + "/0").get(String.class); | 348 | + "/0").get(String.class); |
341 | final JsonObject result = JsonObject.readFrom(response); | 349 | final JsonObject result = JsonObject.readFrom(response); |
342 | assertThat(result, matchesIntent(intent)); | 350 | assertThat(result, matchesIntent(intent)); |
351 | + | ||
352 | + // Test get using numeric value | ||
353 | + final String responseNumeric = rs.path("intents/" + APP_ID.name() | ||
354 | + + "/0x0").get(String.class); | ||
355 | + final JsonObject resultNumeric = JsonObject.readFrom(responseNumeric); | ||
356 | + assertThat(resultNumeric, matchesIntent(intent)); | ||
343 | } | 357 | } |
344 | 358 | ||
345 | /** | 359 | /** |
... | @@ -390,6 +404,23 @@ public class IntentsResourceTest extends ResourceTest { | ... | @@ -390,6 +404,23 @@ public class IntentsResourceTest extends ResourceTest { |
390 | } | 404 | } |
391 | 405 | ||
392 | /** | 406 | /** |
407 | + * Tests creating an intent with POST and illegal JSON. | ||
408 | + */ | ||
409 | + @Test | ||
410 | + public void testBadPost() { | ||
411 | + replay(mockCoreService); | ||
412 | + replay(mockIntentService); | ||
413 | + | ||
414 | + String json = "this is invalid!"; | ||
415 | + WebResource rs = resource(); | ||
416 | + | ||
417 | + ClientResponse response = rs.path("intents") | ||
418 | + .type(MediaType.APPLICATION_JSON_TYPE) | ||
419 | + .post(ClientResponse.class, json); | ||
420 | + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_BAD_REQUEST)); | ||
421 | + } | ||
422 | + | ||
423 | + /** | ||
393 | * Tests removing an intent with DELETE. | 424 | * Tests removing an intent with DELETE. |
394 | */ | 425 | */ |
395 | @Test | 426 | @Test |
... | @@ -429,4 +460,33 @@ public class IntentsResourceTest extends ResourceTest { | ... | @@ -429,4 +460,33 @@ public class IntentsResourceTest extends ResourceTest { |
429 | .delete(ClientResponse.class); | 460 | .delete(ClientResponse.class); |
430 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); | 461 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); |
431 | } | 462 | } |
463 | + | ||
464 | + /** | ||
465 | + * Tests removal of a non existent intent with DELETE. | ||
466 | + */ | ||
467 | + @Test | ||
468 | + public void testBadRemove() { | ||
469 | + final ApplicationId appId = new DefaultApplicationId(2, "app"); | ||
470 | + | ||
471 | + expect(mockCoreService.getAppId("app")) | ||
472 | + .andReturn(appId).once(); | ||
473 | + replay(mockCoreService); | ||
474 | + | ||
475 | + expect(mockIntentService.getIntent(Key.of(2, appId))) | ||
476 | + .andReturn(null) | ||
477 | + .once(); | ||
478 | + expect(mockIntentService.getIntent(Key.of("0x2", appId))) | ||
479 | + .andReturn(null) | ||
480 | + .once(); | ||
481 | + | ||
482 | + replay(mockIntentService); | ||
483 | + | ||
484 | + WebResource rs = resource(); | ||
485 | + | ||
486 | + ClientResponse response = rs.path("intents/app/0x2") | ||
487 | + .type(MediaType.APPLICATION_JSON_TYPE) | ||
488 | + .delete(ClientResponse.class); | ||
489 | + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); | ||
490 | + } | ||
491 | + | ||
432 | } | 492 | } | ... | ... |
-
Please register or login to post a comment