CreateUserCmd extends BaseCmd { @Override public void execute() { validateParams(); CallContext.current().setEventDetails("UserName: " + getUserName() + ", FirstName :" + getFirstName() + ", LastName: " + getLastName()); User user = _accountService.createUser(getUserName(), getPassword(), getFirstName(), getLastName(), getEmail(), getTimezone(), getAccountName(), getDomainId(), getUserUUID()); if (user != null) { UserResponse response = _responseGenerator.createUserResponse(user); response.setResponseName(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a user"); } }  String getAccountName(); Long getDomainId(); String getEmail(); String getFirstName(); String getLastName(); String getPassword(); String getTimezone(); String getUserName(); String getUserUUID(); @Override String getCommandName(); @Override long getEntityOwnerId(); @Override void execute(); static final Logger s_logger; }
@Test public void testExecuteWithNotBlankPassword() { ReflectionTestUtils.setField(createUserCmd, "password", "Test"); try { createUserCmd.execute(); } catch (ServerApiException e) { Assert.assertTrue("Received exception as the mock accountService createUser returns null user", true); } Mockito.verify(accountService, Mockito.times(1)).createUser(null, "Test", null, null, null, null, null, null, null); }
@Test public void testExecuteWithNullPassword() { ReflectionTestUtils.setField(createUserCmd, "password", null); try { createUserCmd.execute(); Assert.fail("should throw exception for a null password"); } catch (ServerApiException e) { Assert.assertEquals(ApiErrorCode.PARAM_ERROR,e.getErrorCode()); Assert.assertEquals("Empty passwords are not allowed", e.getMessage()); } Mockito.verify(accountService, Mockito.never()).createUser(null, null, null, null, null, null, null, null, null); }
@Test public void testExecuteWithEmptyPassword() { ReflectionTestUtils.setField(createUserCmd, "password", ""); try { createUserCmd.execute(); Assert.fail("should throw exception for a empty password"); } catch (ServerApiException e) { Assert.assertEquals(ApiErrorCode.PARAM_ERROR,e.getErrorCode()); Assert.assertEquals("Empty passwords are not allowed", e.getMessage()); } Mockito.verify(accountService, Mockito.never()).createUser(null, null, null, null, null, null, null, null, null); }