diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java b/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java index bf060856..3b9c14fc 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java @@ -72,7 +72,7 @@ public class AdminController extends BaseController { response.setHeader("Location","/service/admin/users/" + user.getId()); } - @RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"}) + @RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"}) @ResponseStatus(value = HttpStatus.NO_CONTENT) public void changePassword(@RequestBody String password, @PathVariable long id) throws IOException, WiseMappingException { if (password == null) { diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java index ff178a52..30f4dbee 100644 --- a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java @@ -12,6 +12,8 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; + import java.io.IOException; import java.net.HttpURLConnection; @@ -28,12 +30,63 @@ public class RestAdminITCase { private static final String BASE_REST_URL = HOST_PORT + "service"; @Test(dataProvider = "ContentType-Provider-Function") - public void findUser(final @NotNull MediaType mediaType) { // Configure media types ... + public void changePassword(final @NotNull MediaType mediaType) { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); final RestTemplate templateRest = createTemplate(); - HttpEntity findUserEntity = new HttpEntity(requestHeaders); - final ResponseEntity result = templateRest.exchange(BASE_REST_URL + "/admin/users/2", HttpMethod.GET, findUserEntity, RestUser.class); + // Fill user data ... + final RestUser restUser = new RestUser(); + final String email = "foo-to-change" + System.nanoTime() + "@example.org"; + restUser.setEmail(email); + restUser.setUsername("foo"); + restUser.setFirstname("foo first name"); + restUser.setLastname("foo last name"); + restUser.setPassword("foo password"); + + // User has been created ... + final URI location = createUser(requestHeaders, templateRest, restUser); + + // Check that the user has been created ... + ResponseEntity result = findUser(requestHeaders, templateRest, location); + + // Change password ... + requestHeaders.setContentType(MediaType.TEXT_PLAIN); + HttpEntity createUserEntity = new HttpEntity("some-new-password", requestHeaders); + System.out.println("Changed password to:" + email); + templateRest.put(BASE_REST_URL + "/admin/users/{id}/password", createUserEntity, result.getBody().getId()); + } + + + @Test(dataProvider = "ContentType-Provider-Function") + public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ... + final HttpHeaders requestHeaders = createHeaders(mediaType); + final RestTemplate templateRest = createTemplate(); + + // Fill user data ... + final RestUser restUser = new RestUser(); + final String email = "foo-to-delete" + System.nanoTime() + "@example.org"; + restUser.setEmail(email); + restUser.setUsername("foo"); + restUser.setFirstname("foo first name"); + restUser.setLastname("foo last name"); + restUser.setPassword("foo password"); + + // User has been created ... + final URI location = createUser(requestHeaders, templateRest, restUser); + + // Check that the user has been created ... + ResponseEntity result = findUser(requestHeaders, templateRest, location); + + // Delete user ... + templateRest.delete(BASE_REST_URL + "/admin/users/{id}", result.getBody().getId()); + + // Is the user there ? + // Check that the user has been created ... + try { + findUser(requestHeaders, templateRest, location); + fail("User could not be deleted !"); + } catch (Exception e) { + } } @Test(dataProvider = "ContentType-Provider-Function") @@ -41,29 +94,49 @@ public class RestAdminITCase { // Configure media types ... final HttpHeaders requestHeaders = createHeaders(mediaType); - - // Configure media ... final RestTemplate templateRest = createTemplate(); // Fill user data ... final RestUser restUser = new RestUser(); - restUser.setEmail("foo" + System.nanoTime() + "@example.org"); + final String email = "foo" + System.nanoTime() + "@example.org"; + restUser.setEmail(email); restUser.setUsername("foo"); restUser.setFirstname("foo first name"); restUser.setLastname("foo last name"); restUser.setPassword("foo password"); - // Post request ... - HttpEntity createUserEntity = new HttpEntity(restUser, requestHeaders); - URI location = templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity); - System.out.println("location:" + location); + // Create a new user ... + final URI location = createUser(requestHeaders, templateRest, restUser); // Check that the user has been created ... + ResponseEntity result = findUser(requestHeaders, templateRest, location); + assertEquals(result.getBody(), restUser, "Returned object object seems not be the same."); + + + // Find by email and check ... + result = findUserByEmail(requestHeaders, templateRest, email); + assertEquals(result.getBody(), restUser, "Returned object object seems not be the same."); + + } + + private ResponseEntity findUser(HttpHeaders requestHeaders, RestTemplate templateRest, URI location) { HttpEntity findUserEntity = new HttpEntity(requestHeaders); final String url = "http://localhost:8080" + location; - final ResponseEntity result = templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class); - assertEquals(result.getBody(), restUser, "Returned object object seems not be the same."); + return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class); + } + + private ResponseEntity findUserByEmail(HttpHeaders requestHeaders, RestTemplate templateRest, final String email) { + HttpEntity findUserEntity = new HttpEntity(requestHeaders); + + // Add extension only to avoid the fact that the last part is extracted ... + final String url = "http://localhost:8080/service/admin/users/email/{email}.json"; + return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class, email); + } + + private URI createUser(HttpHeaders requestHeaders, RestTemplate templateRest, RestUser restUser) { + HttpEntity createUserEntity = new HttpEntity(restUser, requestHeaders); + return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity); } private HttpHeaders createHeaders(MediaType mediaType) {