Add tests for change password, remove user. All admin operations covered.

This commit is contained in:
Paulo Gustavo Veiga 2012-03-12 13:50:43 -03:00
parent 1aebcf48e3
commit 3f2deda442
2 changed files with 86 additions and 13 deletions

View File

@ -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) {

View File

@ -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<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
final ResponseEntity<RestUser> 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<RestUser> result = findUser(requestHeaders, templateRest, location);
// Change password ...
requestHeaders.setContentType(MediaType.TEXT_PLAIN);
HttpEntity<String> createUserEntity = new HttpEntity<String>("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<RestUser> 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<RestUser> createUserEntity = new HttpEntity<RestUser>(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<RestUser> 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<RestUser> findUser(HttpHeaders requestHeaders, RestTemplate templateRest, URI location) {
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
final String url = "http://localhost:8080" + location;
final ResponseEntity<RestUser> 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<RestUser> findUserByEmail(HttpHeaders requestHeaders, RestTemplate templateRest, final String email) {
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(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<RestUser> createUserEntity = new HttpEntity<RestUser>(restUser, requestHeaders);
return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity);
}
private HttpHeaders createHeaders(MediaType mediaType) {