Ray Milkey

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
......@@ -16,11 +16,14 @@
package org.onosproject.rest.exceptions;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
/**
* Mapper for IO exceptions to the BAD_REQUEST response code.
*/
@Provider
public class BadRequestMapper extends AbstractMapper<IOException> {
@Override
protected Response.Status responseStatus() {
......
......@@ -18,10 +18,12 @@ package org.onosproject.rest.exceptions;
import org.onlab.util.ItemNotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
/**
* Mapper for service not found exceptions to the NOT_FOUND response code.
*/
@Provider
public class EntityNotFoundMapper extends AbstractMapper<ItemNotFoundException> {
@Override
protected Response.Status responseStatus() {
......
......@@ -16,10 +16,12 @@
package org.onosproject.rest.exceptions;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
/**
* Mapper for illegal argument exceptions to the BAD_REQUEST response code.
*/
@Provider
public class IllegalArgumentExceptionMapper extends AbstractMapper<IllegalArgumentException> {
@Override
protected Response.Status responseStatus() {
......
......@@ -16,10 +16,12 @@
package org.onosproject.rest.exceptions;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
/**
* Mapper for illegal state exceptions to the BAD_REQUEST response code.
*/
@Provider
public class IllegalStateExceptionMapper extends AbstractMapper<IllegalStateException> {
@Override
protected Response.Status responseStatus() {
......
......@@ -16,12 +16,14 @@
package org.onosproject.rest.exceptions;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import com.sun.jersey.api.NotFoundException;
/**
* Mapper for api not found exceptions to the NOT_FOUND response code.
*/
@Provider
public class NotFoundMapper extends AbstractMapper<NotFoundException> {
@Override
......
......@@ -16,10 +16,12 @@
package org.onosproject.rest.exceptions;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
/**
* Mapper for service not found exceptions to the INTERNAL_SERVER_ERROR response code.
*/
@Provider
public class ServerErrorMapper extends AbstractMapper<RuntimeException> {
@Override
protected Response.Status responseStatus() {
......
......@@ -18,10 +18,12 @@ package org.onosproject.rest.exceptions;
import org.onlab.osgi.ServiceNotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
/**
* Mapper for service not found exceptions to the SERVICE_UNAVAILABLE response code.
*/
@Provider
public class ServiceNotFoundMapper extends AbstractMapper<ServiceNotFoundException> {
@Override
protected Response.Status responseStatus() {
......
......@@ -17,10 +17,12 @@ package org.onosproject.rest.exceptions;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
/**
* Exception mapper for WebApplicationExceptions.
*/
@Provider
public class WebApplicationExceptionMapper extends AbstractMapper<WebApplicationException> {
/**
......
......@@ -331,15 +331,29 @@ public class IntentsResourceTest extends ResourceTest {
expect(mockIntentService.getIntent(Key.of("0", APP_ID)))
.andReturn(intent)
.anyTimes();
expect(mockIntentService.getIntent(Key.of(0, APP_ID)))
.andReturn(intent)
.anyTimes();
expect(mockIntentService.getIntent(Key.of("0x0", APP_ID)))
.andReturn(null)
.anyTimes();
replay(mockIntentService);
expect(mockCoreService.getAppId(APP_ID.name()))
.andReturn(APP_ID).anyTimes();
replay(mockCoreService);
final WebResource rs = resource();
// Test get using key string
final String response = rs.path("intents/" + APP_ID.name()
+ "/0").get(String.class);
final JsonObject result = JsonObject.readFrom(response);
assertThat(result, matchesIntent(intent));
// Test get using numeric value
final String responseNumeric = rs.path("intents/" + APP_ID.name()
+ "/0x0").get(String.class);
final JsonObject resultNumeric = JsonObject.readFrom(responseNumeric);
assertThat(resultNumeric, matchesIntent(intent));
}
/**
......@@ -390,6 +404,23 @@ public class IntentsResourceTest extends ResourceTest {
}
/**
* Tests creating an intent with POST and illegal JSON.
*/
@Test
public void testBadPost() {
replay(mockCoreService);
replay(mockIntentService);
String json = "this is invalid!";
WebResource rs = resource();
ClientResponse response = rs.path("intents")
.type(MediaType.APPLICATION_JSON_TYPE)
.post(ClientResponse.class, json);
assertThat(response.getStatus(), is(HttpURLConnection.HTTP_BAD_REQUEST));
}
/**
* Tests removing an intent with DELETE.
*/
@Test
......@@ -429,4 +460,33 @@ public class IntentsResourceTest extends ResourceTest {
.delete(ClientResponse.class);
assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));
}
/**
* Tests removal of a non existent intent with DELETE.
*/
@Test
public void testBadRemove() {
final ApplicationId appId = new DefaultApplicationId(2, "app");
expect(mockCoreService.getAppId("app"))
.andReturn(appId).once();
replay(mockCoreService);
expect(mockIntentService.getIntent(Key.of(2, appId)))
.andReturn(null)
.once();
expect(mockIntentService.getIntent(Key.of("0x2", appId)))
.andReturn(null)
.once();
replay(mockIntentService);
WebResource rs = resource();
ClientResponse response = rs.path("intents/app/0x2")
.type(MediaType.APPLICATION_JSON_TYPE)
.delete(ClientResponse.class);
assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));
}
}
......