@Test
public void updateValidation()
throws IOException
{
UserResource resource = new UserResource();
resource.setFirstName("updateValidation");
resource.setUserId("updateValidation");
resource.setStatus("active");
resource.setEmail("nexus@user.com");
resource.addRole("role1");
Response response = this.messageUtil.sendMessage(Method.POST, resource);
if (!response.getStatus().isSuccess()) {
Assert.fail("Could not create user: " + response.getStatus());
}
// get the Resource object
UserResource responseResource = this.messageUtil.getResourceFromResponse(response);
Assert.assertEquals(responseResource.getFirstName(), resource.getFirstName());
Assert.assertEquals(responseResource.getUserId(), resource.getUserId());
Assert.assertEquals(responseResource.getStatus(), resource.getStatus());
Assert.assertEquals(responseResource.getEmail(), resource.getEmail());
Assert.assertEquals(resource.getRoles(), responseResource.getRoles());
getSecurityConfigUtil().verifyUser(resource);
// update the user
resource.setFirstName("updateValidation");
resource.setUserId("updateValidation");
resource.setStatus("active");
resource.setEmail("");
resource.addRole("role1");
response = this.messageUtil.sendMessage(Method.PUT, resource);
if (response.getStatus().isSuccess()) {
Assert.fail("User should not have been created: " + response.getStatus());
}
Assert.assertTrue(response.getEntity().getText().startsWith("{\"errors\":"));
/**
* NO STATUS
*/
resource.setFirstName("updateValidation");
resource.setUserId("updateValidation");
resource.setStatus("");
resource.setEmail("nexus@user.com");
resource.addRole("role1");
response = this.messageUtil.sendMessage(Method.PUT, resource);
if (response.getStatus().isSuccess()) {
Assert.fail("User should not have been created: " + response.getStatus());
}
String errorText = response.getEntity().getText();
Assert.assertTrue("expected error, but was: " + errorText, errorText.startsWith("{\"errors\":"));
// FIXME: should we keep supporting this?
// /**
// * NO ROLES
// */
// resource.setFirstName( "updateValidation" );
// resource.setUserId( "updateValidation" );
// resource.setStatus( "active" );
// resource.setEmail( "nexus@user.com" );
// resource.getRoles().clear();
//
// response = this.messageUtil.sendMessage( Method.PUT, resource );
//
//
// if ( response.getStatus().isSuccess() )
// {
// Assert.fail( "User should not have been created: " + response.getStatus() );
// }
// Assert.assertTrue( response.getEntity().getText().startsWith( "{\"errors\":" ) );
/**
* INVALID ROLE
*/
resource.setFirstName("updateValidation");
resource.setUserId("updateValidation");
resource.setStatus("active");
resource.setEmail("nexus@user.com");
resource.addRole("INVALID_ROLE");
response = this.messageUtil.sendMessage(Method.PUT, resource);
if (response.getStatus().isSuccess()) {
Assert.fail("User should not have been created: " + response.getStatus());
}
Assert.assertTrue(response.getEntity().getText().startsWith("{\"errors\":"));
/**
* NO NAME
*/
resource.setFirstName("");
resource.setUserId("updateValidation");
resource.setStatus("active");
resource.setEmail("nexus@user.com");
resource.addRole("role1");
response = this.messageUtil.sendMessage(Method.PUT, resource);
if (response.getStatus().isSuccess()) {
Assert.fail("User should not have been created: " + response.getStatus());
}
Assert.assertTrue(response.getEntity().getText().startsWith("{\"errors\":"));
/**
* NO USER ID
*/
resource.setFirstName("updateValidation");
resource.setUserId(null);
resource.setStatus("active");
resource.setEmail("nexus@user.com");
resource.addRole("role1");
response = this.messageUtil.sendMessage(Method.PUT, resource);
if (response.getStatus().isSuccess()) {
Assert.fail("User should not have been created: " + response.getStatus());
}
// This is actually not a validation error, but a 'not found' error, so result will NOT contain the validation
// errors
// Assert.assertTrue( response.getEntity().getText().startsWith( "{\"errors\":" ) );
/**
* DUPLICATE EMAIL
*/
UserResource duplicateResource = new UserResource();
duplicateResource.setEmail("dup@email.com");
duplicateResource.setFirstName("dupname");
duplicateResource.setStatus("active");
duplicateResource.setUserId("dup-user2");
duplicateResource.addRole("role1");
response = this.messageUtil.sendMessage(Method.POST, duplicateResource);
if (!response.getStatus().isSuccess()) {
Assert.fail("User should have been created: " + response.getStatus());