Search in sources :

Example 36 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class UserServiceTest method saveRole_shouldSaveGivenRoleToTheDatabase.

/**
 * @see UserService#saveRole(Role)
 */
@Test
public void saveRole_shouldSaveGivenRoleToTheDatabase() {
    Role role = new Role("new role", "new desc");
    userService.saveRole(role);
    Assert.assertNotNull(userService.getRole("new role"));
}
Also used : Role(org.openmrs.Role) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 37 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class UserServiceTest method createUser_shouldShouldCreateUserWhoIsPatientAlready.

@Test
@SkipBaseSetup
public void createUser_shouldShouldCreateUserWhoIsPatientAlready() throws SQLException {
    // create the basic user and give it full rights
    initializeInMemoryDatabase();
    // authenticate to the temp database
    authenticate();
    assertTrue("The context needs to be correctly authenticated to by a user", Context.isAuthenticated());
    // add in some basic data
    executeDataSet(XML_FILENAME);
    // the user should not exist yet
    User preliminaryFetchedUser = userService.getUser(2);
    assertNull(preliminaryFetchedUser);
    // get the person object we'll make into a user
    Person personToMakeUser = Context.getPersonService().getPerson(2);
    // this avoids a lazy init exception, since we're going to clear the session
    ((Patient) personToMakeUser).getIdentifiers().size();
    Context.clearSession();
    // this is the user object we'll be saving
    User user = new User(personToMakeUser);
    user.setUsername("bwolfe");
    user.setSystemId("asdf");
    // included in xml file
    user.addRole(new Role("Some Role", "This is a test role"));
    // make sure everything was added to the user correctly
    assertTrue(user.getUsername().equals("bwolfe"));
    assertTrue(user.hasRole("Some Role"));
    // do the actual creating of the user object
    userService.createUser(user, "Openmr5xy");
    Assert.assertNotNull("User was not created", userService.getUser(user.getUserId()));
    Integer shouldCreateUserWhoIsPatientAlreadyTestUserIdCreated = user.getUserId();
    Context.flushSession();
    // get the same user we just created and make sure the user portion exists
    User fetchedUser = userService.getUser(shouldCreateUserWhoIsPatientAlreadyTestUserIdCreated);
    User fetchedUser3 = userService.getUser(3);
    if (fetchedUser3 != null) {
        throw new RuntimeException("There is a user with id #3");
    }
    assertNotNull("Uh oh, the user object was not created", fetchedUser);
    assertNotNull("Uh oh, the username was not saved", fetchedUser.getUsername());
    assertTrue("Uh oh, the username was not saved", fetchedUser.getUsername().equals("bwolfe"));
    assertTrue("Uh oh, the role was not assigned", fetchedUser.hasRole("Some Role"));
    Context.clearSession();
    List<User> allUsers = userService.getAllUsers();
    assertEquals(11, allUsers.size());
    // there should still only be the one patient we created in the xml file
    List<Patient> allPatientsSet = Context.getPatientService().getAllPatients();
    assertEquals(1, allPatientsSet.size());
}
Also used : Role(org.openmrs.Role) User(org.openmrs.User) Patient(org.openmrs.Patient) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) SkipBaseSetup(org.openmrs.test.SkipBaseSetup)

Example 38 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class UserServiceTest method getUsers_shouldNotFailIfRolesAreSearchedButNameIsEmpty.

/**
 * This is a regression test for TRUNK-2108 <br>
 *
 * @see UserService#getUsers(String,List,boolean)
 */
@Test
public void getUsers_shouldNotFailIfRolesAreSearchedButNameIsEmpty() {
    Role role = new Role("Provider");
    List<Role> roles = new ArrayList<>();
    roles.add(role);
    Assert.assertEquals(2, userService.getUsers("", roles, true).size());
}
Also used : Role(org.openmrs.Role) ArrayList(java.util.ArrayList) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 39 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class UserServiceTest method getUsers_shouldFetchUsersWithAtLeastOneOfTheGivenRoleObjects.

/**
 * @see UserService#getUsers(String,List,boolean)
 */
@Test
public void getUsers_shouldFetchUsersWithAtLeastOneOfTheGivenRoleObjects() {
    executeDataSet(XML_FILENAME);
    List<Role> roles = Collections.singletonList(new Role("Some Role"));
    Assert.assertEquals(1, userService.getUsers("Susy Kingman", roles, false).size());
}
Also used : Role(org.openmrs.Role) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 40 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class UserServiceTest method purgeRole_shouldDeleteGivenRoleFromDatabase.

/**
 * @see UserService#purgeRole(Role)
 */
@Test
public void purgeRole_shouldDeleteGivenRoleFromDatabase() {
    executeDataSet(XML_FILENAME);
    Role role = userService.getRole("Some Role To Delete");
    userService.purgeRole(role);
    Assert.assertNull(userService.getRole("Some Role To Delete"));
}
Also used : Role(org.openmrs.Role) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Role (org.openmrs.Role)42 Test (org.junit.Test)27 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)26 User (org.openmrs.User)15 BindException (org.springframework.validation.BindException)8 Errors (org.springframework.validation.Errors)8 Privilege (org.openmrs.Privilege)7 EncounterRole (org.openmrs.EncounterRole)6 HashSet (java.util.HashSet)5 Person (org.openmrs.Person)5 PersonName (org.openmrs.PersonName)4 ArrayList (java.util.ArrayList)3 Before (org.junit.Before)3 Encounter (org.openmrs.Encounter)3 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 EncounterType (org.openmrs.EncounterType)2 RelationshipType (org.openmrs.RelationshipType)2