Jian Li
Committed by Gerrit Code Review

[ONOS-4409] Merge getApp REST methods into one, revise unit test

Change-Id: I47e5dd1d795404d3ba2c73487858a3d389790ac9
......@@ -166,34 +166,25 @@ public class ApplicationsWebResource extends AbstractWebResource {
}
/**
* Gets application Id entry by short id.
*
* @param shortId numerical id of application
* @return 200 OK; 404; 401
* @onos.rsModel ApplicationId
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("ids/short")
public Response getAppIdByShortId(@QueryParam("id") int shortId) {
CoreService service = get(CoreService.class);
ApplicationId appId = service.getAppId((short) shortId);
return response(appId);
}
/**
* Gets application Id entry by name.
* Gets applicationId entry by either id or name.
*
* @param id id of application
* @param name name of application
* @return 200 OK; 404; 401
* @onos.rsModel ApplicationId
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("ids/name")
public Response getAppIdByName(@QueryParam("name") String name) {
@Path("ids/entry")
public Response getAppIdByName(@QueryParam("id") Short id,
@QueryParam("name") String name) {
CoreService service = get(CoreService.class);
ApplicationId appId = service.getAppId(name);
ApplicationId appId = null;
if (id != null) {
appId = service.getAppId(id);
} else if (name != null) {
appId = service.getAppId(name);
}
return response(appId);
}
......
......@@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.onlab.osgi.ServiceDirectory;
......@@ -42,6 +43,7 @@ import org.onosproject.core.DefaultApplication;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.core.Version;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
......@@ -487,8 +489,8 @@ public class ApplicationsResourceTest extends ResourceTest {
replay(coreService);
WebTarget wt = target();
String response = wt.path("applications/ids/short")
.queryParam("id", 1).request().get(String.class);
String response = wt.path("applications/ids/entry")
.queryParam("id", 1).request().get(String.class);
JsonObject result = Json.parse(response).asObject();
assertThat(result, notNullValue());
......@@ -507,7 +509,7 @@ public class ApplicationsResourceTest extends ResourceTest {
replay(coreService);
WebTarget wt = target();
String response = wt.path("applications/ids/name")
String response = wt.path("applications/ids/entry")
.queryParam("name", "app2").request().get(String.class);
JsonObject result = Json.parse(response).asObject();
......@@ -517,4 +519,19 @@ public class ApplicationsResourceTest extends ResourceTest {
verify(coreService);
}
/**
* Tests a GET of an applicationId without specifying any parameters.
*/
@Test
public void getAppWithNoParam() {
WebTarget wt = target();
try {
wt.path("applications/ids/entry").request().get();
} catch (NotFoundException ex) {
Assert.assertThat(ex.getMessage(),
containsString("HTTP 404 Not Found"));
}
}
}
......