Updated default setting controllers & added tests
Change-Id: I4efa3177fd914d8b5960175f142c7acd6028f960
Change-Id: I4efa3177fd914d8b5960175f142c7acd6028f960
diff --git a/full/Changes b/full/Changes
index 02d7491..8fd48a9 100644
--- a/full/Changes
+++ b/full/Changes
@@ -21,6 +21,8 @@
- Removed codes related to user registration & password management (margaretha)
22/01/2019
- Added create, edit, retrieve user default setting controllers (margaretha)
+22/01/2019
+ - Updated default setting controllers & added tests (margaretha)
# version 0.61.4
14/11/2018
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/DefaultSettingService.java b/full/src/main/java/de/ids_mannheim/korap/service/DefaultSettingService.java
index 39ee4ce..181d934 100644
--- a/full/src/main/java/de/ids_mannheim/korap/service/DefaultSettingService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/service/DefaultSettingService.java
@@ -2,6 +2,7 @@
import java.util.Map;
+import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -18,29 +19,37 @@
@Autowired
private DefaultSettingDao settingDao;
- public void handlePutRequest (String username,
- Map<String, Object> form, String authenticatedUser)
+ private void verifiyUsername (String username, String authenticatedUser)
throws KustvaktException {
if (!username.equals(authenticatedUser)) {
throw new KustvaktException(StatusCodes.INVALID_ARGUMENT,
"Username verification failed. Path parameter username "
- + "must be the same as the authenticated username.");
+ + "must be the same as the authenticated username.",
+ username);
}
- else if (form == null || form.isEmpty()) {
+ }
+
+ public int handlePutRequest (String username, Map<String, Object> map,
+ String authenticatedUser) throws KustvaktException {
+ verifiyUsername(username, authenticatedUser);
+
+ if (map == null || map.isEmpty()) {
throw new KustvaktException(StatusCodes.INVALID_ARGUMENT,
"Entity body is empty. No settings are given.");
}
Userdata userdata = new UserSettings(username);
- userdata.readQuietly(form, false);
+ userdata.readQuietly(map, false);
DefaultSetting defaultSetting =
settingDao.retrieveDefautlSetting(username);
if (defaultSetting == null) {
createDefaultSetting(username, userdata);
+ return HttpStatus.SC_CREATED;
}
else {
updateDefaultSetting(defaultSetting, userdata);
+ return HttpStatus.SC_OK;
}
}
@@ -61,8 +70,11 @@
settingDao.updateDefaultSetting(setting);
}
- public String retrieveDefaultSettings (String username)
- throws KustvaktException {
+ public String retrieveDefaultSettings (String username,
+ String authenticatedUser) throws KustvaktException {
+
+ verifiyUsername(username, authenticatedUser);
+
DefaultSetting defaultSetting =
settingDao.retrieveDefautlSetting(username);
return defaultSetting.getSettings();
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/UserController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/UserController.java
index 904fab4..638cd8a 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/UserController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/UserController.java
@@ -34,7 +34,7 @@
*
*/
@Controller
-@Path("{version}/user")
+@Path("{version}/{username: ~[a-zA-Z0-9_]+}")
@ResourceFilters({ AuthenticationFilter.class, APIVersionFilter.class,
PiwikFilter.class })
public class UserController {
@@ -47,40 +47,41 @@
private OAuth2ScopeService scopeService;
@PUT
- @Path("settings/{username}")
+ @Path("setting")
@Consumes(MediaType.APPLICATION_JSON)
@ResourceFilters({ AuthenticationFilter.class, PiwikFilter.class,
BlockingFilter.class })
public Response createDefaultSetting (@Context SecurityContext context,
- @PathParam("username") String username,
- Map<String, Object> form) {
+ @PathParam("username") String username, Map<String, Object> map) {
TokenContext tokenContext = (TokenContext) context.getUserPrincipal();
try {
scopeService.verifyScope(tokenContext,
OAuth2Scope.CREATE_DEFAULT_SETTING);
- settingService.handlePutRequest(username, form,
+ int statusCode = settingService.handlePutRequest(username, map,
tokenContext.getUsername());
+ return Response.status(statusCode).build();
}
catch (KustvaktException e) {
throw kustvaktResponseHandler.throwit(e);
}
- return Response.ok().build();
+
}
@GET
- @Path("settings")
+ @Path("setting")
@ResourceFilters({ AuthenticationFilter.class, PiwikFilter.class,
BlockingFilter.class })
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
- public Response createDefaultSetting (@Context SecurityContext context) {
+ public Response retrieveDefaultSetting (@Context SecurityContext context,
+ @PathParam("username") String username) {
TokenContext tokenContext = (TokenContext) context.getUserPrincipal();
try {
scopeService.verifyScope(tokenContext,
OAuth2Scope.CREATE_DEFAULT_SETTING);
- String settings = settingService
- .retrieveDefaultSettings(tokenContext.getUsername());
+ String settings = settingService.retrieveDefaultSettings(username,
+ tokenContext.getUsername());
return Response.ok(settings).build();
}
catch (KustvaktException e) {
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/UserControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/UserControllerTest.java
index da336a0..d074d02 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/UserControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/UserControllerTest.java
@@ -18,6 +18,7 @@
import de.ids_mannheim.korap.config.Attributes;
import de.ids_mannheim.korap.config.SpringJerseyTest;
import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
import de.ids_mannheim.korap.utils.JsonUtils;
/**
@@ -26,15 +27,16 @@
*/
public class UserControllerTest extends SpringJerseyTest {
- private String username = "UserControllerTest";
+ private String username = "~UserControllerTest";
+ private String username2 = "~UserControllerTest2";
- private ClientResponse sendPutRequest (Map<String, Object> form)
- throws KustvaktException {
- ClientResponse response = resource().path(API_VERSION).path("user")
- .path("settings").path(username)
+ private ClientResponse sendPutRequest (String username,
+ Map<String, Object> map) throws KustvaktException {
+ ClientResponse response = resource().path(API_VERSION)
+ .path(username).path("setting")
.header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
.createBasicAuthorizationHeaderValue(username, "pass"))
- .type(MediaType.APPLICATION_JSON).entity(form)
+ .type(MediaType.APPLICATION_JSON).entity(map)
.put(ClientResponse.class);
return response;
@@ -45,52 +47,85 @@
String json = "{\"foundry\":\"opennlp\",\"metadata\":\"author title "
+ "textSigle availability\",\"resultPerPage\":25}";
- ClientResponse response = resource().path(API_VERSION).path("user")
- .path("settings").path(username)
+ ClientResponse response = resource().path(API_VERSION)
+ .path(username).path("setting")
.header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
.createBasicAuthorizationHeaderValue(username, "pass"))
.type(MediaType.APPLICATION_JSON).entity(json)
.put(ClientResponse.class);
- assertEquals(Status.OK.getStatusCode(), response.getStatus());
+ assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
- testRetrieveSettings("opennlp", 25,
+ testRetrieveSettings(username, "opennlp", 25,
"author title textSigle availability");
}
@Test
- public void testCreateSettingWithForm () throws KustvaktException {
- Map<String, Object> form = new HashMap<>();
- form.put("foundry", "opennlp");
- form.put("resultPerPage", 25);
- form.put("metadata", "author title textSigle availability");
+ public void testCreateSettingWithMap () throws KustvaktException {
+ Map<String, Object> map = new HashMap<>();
+ map.put("foundry", "opennlp");
+ map.put("resultPerPage", 25);
+ map.put("metadata", "author title textSigle availability");
- ClientResponse response = sendPutRequest(form);
- assertEquals(Status.OK.getStatusCode(), response.getStatus());
+ ClientResponse response = sendPutRequest(username2, map);
+ assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
- testRetrieveSettings("opennlp", 25,
+ testRetrieveSettings(username2, "opennlp", 25,
"author title textSigle availability");
- testUpdateSetting();
+ testUpdateSetting(username2);
}
- private void testUpdateSetting () throws KustvaktException {
- Map<String, Object> form = new HashMap<>();
- form.put("foundry", "malt");
- form.put("resultPerPage", 15);
- form.put("metadata", "author title");
+ @Test
+ public void testPutDifferentUsername () throws KustvaktException {
+ String json = "{\"foundry\":\"opennlp\",\"metadata\":\"author title "
+ + "textSigle availability\",\"resultPerPage\":25}";
- ClientResponse response = sendPutRequest(form);
+ ClientResponse response = resource().path(API_VERSION)
+ .path(username).path("setting")
+ .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+ .createBasicAuthorizationHeaderValue(username2, "pass"))
+ .type(MediaType.APPLICATION_JSON).entity(json)
+ .put(ClientResponse.class);
+
+ assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
+ String entity = response.getEntity(String.class);
+ JsonNode node = JsonUtils.readTree(entity);
+ assertEquals(StatusCodes.INVALID_ARGUMENT,
+ node.at("/errors/0/0").asInt());
+ }
+
+ @Test
+ public void testGetDifferentUsername () throws KustvaktException {
+ ClientResponse response = resource().path(API_VERSION)
+ .path(username).path("setting")
+ .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+ .createBasicAuthorizationHeaderValue(username2, "pass"))
+ .get(ClientResponse.class);
+
+ assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
+ String entity = response.getEntity(String.class);
+ JsonNode node = JsonUtils.readTree(entity);
+ assertEquals(StatusCodes.INVALID_ARGUMENT,
+ node.at("/errors/0/0").asInt());
+ }
+
+ private void testUpdateSetting (String username) throws KustvaktException {
+ Map<String, Object> map = new HashMap<>();
+ map.put("foundry", "malt");
+ map.put("resultPerPage", 15);
+ map.put("metadata", "author title");
+
+ ClientResponse response = sendPutRequest(username, map);
assertEquals(Status.OK.getStatusCode(), response.getStatus());
- testRetrieveSettings("malt", 15, "author title");
-
+ testRetrieveSettings(username, "malt", 15, "author title");
}
- private void testRetrieveSettings (String foundry, int numOfResult,
+ private void testRetrieveSettings (String username, String foundry, int numOfResult,
String metadata) throws KustvaktException {
- ClientResponse response = resource().path(API_VERSION).path("user")
- .path("settings")
+ ClientResponse response = resource().path(API_VERSION)
+ .path(username).path("setting")
.header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
.createBasicAuthorizationHeaderValue(username, "pass"))
.header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")