/** * @see org.openmrs.api.handler.SaveHandler#handle(org.openmrs.OpenmrsObject, org.openmrs.User, * java.util.Date, java.lang.String) */ @Override public void handle(User user, User creator, Date dateCreated, String other) { // if the user doesn't have a system id, generate one if (StringUtils.isEmpty(user.getSystemId())) { user.setSystemId(Context.getUserService().generateSystemId()); } // the framework only automatically recurses on properties that are Collection<OpenmrsObject> // so we need to do this manually if (user.getPerson() != null) { loadLazyHibernateCollections(user); RequiredDataAdvice.recursivelyHandle(SaveHandler.class, user.getPerson(), creator, dateCreated, other, new ArrayList<>()); } }
/** * @see UserService#hasDuplicateUsername(User) */ @Test public void hasDuplicateUsername_shouldVerifyThatUsernameAndSystemIdIsUnique() { executeDataSet(XML_FILENAME); User user = new User(); user.setSystemId("8-3"); user.setUsername("a unique username"); Assert.assertTrue(userService.hasDuplicateUsername(user)); user = new User(); user.setSystemId("a unique system id"); user.setUsername("userWithSha512Hash"); Assert.assertTrue(userService.hasDuplicateUsername(user)); }
@Test public void createUser_shouldNotAllowSystemIdEqualsExistingUsername() { User someUser = userService.getUserByUsername(SOME_USERNAME); User newUser = userWithValidPerson(); newUser.setSystemId(someUser.getUsername()); expectedException.expect(DAOException.class); expectedException.expectMessage( String.format("Username %s or system id %s is already in use.", newUser.getUsername(), newUser.getSystemId())); userService.createUser(newUser, SOME_VALID_PASSWORD); }
@Test public void createUser_shouldNotAllowDuplicatedSystemId() { User someUser = userService.getUserByUsername(SOME_USERNAME); User newUser = userWithValidPerson(); newUser.setSystemId(someUser.getSystemId()); expectedException.expect(DAOException.class); expectedException.expectMessage( String.format("Username %s or system id %s is already in use.", newUser.getUsername(), newUser.getSystemId())); userService.createUser(newUser, SOME_VALID_PASSWORD); }
/** * Run this before each unit test in this class. The "@Before" method in * {@link BaseContextSensitiveTest} is run right before this method. * * @throws Exception */ @Before public void runBeforeEachTest() { PersonName name = new PersonName("Joe", "J", "Doe"); name.setDateCreated(new Date()); Person person = new Person(); person.setDateCreated(new Date()); person.setPersonDateCreated(person.getDateCreated()); person.setGender("M"); userJoe = new User(); userJoe.setSystemId("100-30"); userJoe.setPerson(person); userJoe.addName(name); userJoe.setUsername("juser"); userJoe.setDateCreated(new Date()); if (dao == null) { // fetch the dao from the spring application context // this bean name matches the name in /metadata/spring/applicationContext-service.xml dao = (UserDAO) applicationContext.getBean("userDAO"); } dao.saveUser(userJoe, null); }
@Test public void createUser_shouldNotAllowSystemIdEqualsUsernameWithLuhnCheckDigit() { User someUser = userService.getUserByUsername(SOME_USERNAME); User newUser = userWithValidPerson(); newUser.setUsername(someUser.getUsername()); newUser.setSystemId(decorateWithLuhnIdentifier(someUser.getUsername())); expectedException.expect(DAOException.class); expectedException.expectMessage( String.format("Username %s or system id %s is already in use.", newUser.getUsername(), newUser.getSystemId())); userService.createUser(newUser, SOME_VALID_PASSWORD); }
/** * @see AuditableInterceptor#onSave(Object,Serializable,Object[],String[],Type[]) */ @Test public void onSave_shouldBeCalledWhenSavingOpenmrsObject() { User u = new User(); u.setSystemId("user"); u.setPerson(Context.getPersonService().getPerson(1)); Context.getUserService().createUser(u, "Admin123"); Assert.assertSame(Context.getAuthenticatedUser(), u.getCreator()); Assert.assertNotNull(u.getDateCreated()); }
/** * @see UserValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { User user = new User(); user.setUsername("test"); user.setSystemId("systemId"); user.setRetireReason("retireReason"); user.setPerson(new Person(999)); user.getPerson().addName(new PersonName("Users", "Need", "People")); user.getPerson().setGender("F"); Errors errors = new BindException(user, "user"); validator.validate(user, errors); Assert.assertFalse(errors.hasErrors()); }
user.setSystemId("asdf"); user.addRole(new Role("Some Role", "This is a test role")); //included in xml file
/** * @see UserValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { User user = new User(); user.setUsername(STRING_WITH_LENGTH_GREATER_THAN_50); user.setSystemId(STRING_WITH_LENGTH_GREATER_THAN_50); user.setRetireReason(STRING_WITH_LENGTH_GREATER_THAN_50); user.setPerson(new Person(999)); user.getPerson().addName(new PersonName(STRING_WITH_LENGTH_GREATER_THAN_50, STRING_WITH_LENGTH_GREATER_THAN_50, STRING_WITH_LENGTH_GREATER_THAN_50)); user.getPerson().setGender(STRING_WITH_LENGTH_GREATER_THAN_50); Errors errors = new BindException(user, "user"); validator.validate(user, errors); Assert.assertTrue(errors.hasFieldErrors("username")); Assert.assertTrue(errors.hasFieldErrors("systemId")); Assert.assertTrue(errors.hasFieldErrors("retireReason")); Assert.assertTrue(errors.hasFieldErrors("person.names[0].givenName")); Assert.assertTrue(errors.hasFieldErrors("person.names[0].middleName")); Assert.assertTrue(errors.hasFieldErrors("person.names[0].familyName")); Assert.assertTrue(errors.hasFieldErrors("person.gender")); }
User mrsUserToOpenMRSUser(MRSUser mrsUser) { User user = getOrCreateUser(mrsUser.getUserId()); Person person = user.getPerson(); clearAttributes(user); MRSPerson mrsPerson = mrsUser.getPerson(); PersonName personName = new PersonName(mrsPerson.getFirstName(), mrsPerson.getMiddleName(), mrsPerson.getLastName()); person.addName(personName); person.setGender(PERSON_UNKNOWN_GENDER); for (MRSAttribute attribute : mrsPerson.getAttributes()) { PersonAttributeType attributeType = personService.getPersonAttributeTypeByName(attribute.getName()); person.addAttribute(new PersonAttribute(attributeType, attribute.getValue())); } Role role = userService.getRole(mrsUser.getSecurityRole()); user.addRole(role); addProviderRole(user, role); user.setSystemId(mrsUser.getSystemId()); user.setUsername(mrsUser.getUserName()); return user; }