Merge pull request #11 from myrle-krantz/develop

roles sorted by identifier.
diff --git a/component-test/src/main/java/AbstractComponentTest.java b/component-test/src/main/java/AbstractComponentTest.java
index 7f18da3..ce69ea8 100644
--- a/component-test/src/main/java/AbstractComponentTest.java
+++ b/component-test/src/main/java/AbstractComponentTest.java
@@ -123,7 +123,7 @@
     return identityManager;
   }
 
-  AutoUserContext enableAndLoginAdmin() throws InterruptedException {
+  AutoUserContext loginAdmin() throws InterruptedException {
     final Authentication adminAuthentication =
             getTestSubject().login(ADMIN_IDENTIFIER, TestEnvironment.encodePassword(ADMIN_PASSWORD));
     Assert.assertNotNull(adminAuthentication);
@@ -143,7 +143,7 @@
    */
   String createUserWithNonexpiredPassword(final String password, final String role) throws InterruptedException {
     final String username = testEnvironment.generateUniqueIdentifer("Ahmes");
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       getTestSubject().createUser(new UserWithPassword(username, role, TestEnvironment.encodePassword(password)));
 
       {
diff --git a/component-test/src/main/java/TestApplications.java b/component-test/src/main/java/TestApplications.java
index 4dbf6f8..49d0cc1 100644
--- a/component-test/src/main/java/TestApplications.java
+++ b/component-test/src/main/java/TestApplications.java
@@ -141,7 +141,7 @@
     final String user1id;
     final String user2Password;
     final String user2id;
-    try (final AutoUserContext ignored = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignored = loginAdmin()) {
       final String selfManagementRoleId = createSelfManagementRole();
       final String roleManagementRoleId = createRoleManagementRole();
 
@@ -272,7 +272,7 @@
 
     final String userid;
     final String userPassword;
-    try (final AutoUserContext ignored = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignored = loginAdmin()) {
       final String selfManagementRoleId = createRole(rolePermission, userPermission);
 
       userPassword = RandomStringUtils.randomAlphanumeric(5);
diff --git a/component-test/src/main/java/TestAuthentication.java b/component-test/src/main/java/TestAuthentication.java
index 0b58edd..fdab6e5 100644
--- a/component-test/src/main/java/TestAuthentication.java
+++ b/component-test/src/main/java/TestAuthentication.java
@@ -40,13 +40,13 @@
   //@Repeat(25)
   public void testAdminLogin() throws InterruptedException {
     //noinspection EmptyTryBlock
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
     }
   }
 
   @Test
   public void testAdminLogout() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       getTestSubject().logout();
 
       try {
@@ -79,7 +79,7 @@
 
   @Test()
   public void testPermissionsCorrectInAdminToken() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final Authentication adminAuthentication =
               getTestSubject().login(ADMIN_IDENTIFIER, TestEnvironment.encodePassword(ADMIN_PASSWORD));
       Assert.assertNotNull(adminAuthentication);
@@ -101,7 +101,7 @@
 
   @Test()
   public void testPermissionsCorrectInTokenWhenMultiplePermittableGroupsInRole() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final PermittableEndpoint horusEndpoint = buildPermittableEndpoint("horus");
       final PermittableGroup horusGroup = buildPermittableGroup("horus_Group", horusEndpoint);
       getTestSubject().createPermittableGroup(horusGroup);
diff --git a/component-test/src/main/java/TestKeyRotation.java b/component-test/src/main/java/TestKeyRotation.java
index deda6d7..cf58551 100644
--- a/component-test/src/main/java/TestKeyRotation.java
+++ b/component-test/src/main/java/TestKeyRotation.java
@@ -37,7 +37,7 @@
     final Anubis anubis = tenantApplicationSecurityEnvironment.getAnubis();
 
     //noinspection EmptyTryBlock
-    try (final AutoUserContext ignored = enableAndLoginAdmin())
+    try (final AutoUserContext ignored = loginAdmin())
     {
       //Don't do anything yet.
     }
diff --git a/component-test/src/main/java/TestPasswords.java b/component-test/src/main/java/TestPasswords.java
index 7e0e677..1a9045b 100644
--- a/component-test/src/main/java/TestPasswords.java
+++ b/component-test/src/main/java/TestPasswords.java
@@ -37,7 +37,7 @@
   public void testAdminChangeUserPassword() throws InterruptedException {
     final String username = createUserWithNonexpiredPassword(AHMES_PASSWORD, ADMIN_ROLE);
 
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final String newPassword = TestEnvironment.encodePassword(
               AHMES_PASSWORD + "make_it_a_little_longer");
 
@@ -79,7 +79,7 @@
 
   @Test
   public void testAdminChangeAdminPassword() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final String newPassword = TestEnvironment.encodePassword(
               ADMIN_PASSWORD + "make_it_a_little_longer");
 
@@ -149,7 +149,7 @@
       }
 
       //noinspection EmptyTryBlock
-      try (final AutoUserContext ignored2 = enableAndLoginAdmin()) { //logging into admin with the old password should *not* fail.
+      try (final AutoUserContext ignored2 = loginAdmin()) { //logging into admin with the old password should *not* fail.
       }
     }
   }
@@ -157,7 +157,7 @@
   @Test(expected = IllegalArgumentException.class)
   public void loginWithUnencodedPasswordShouldThrowIllegalArgumentException() throws InterruptedException {
 
-    try (final AutoUserContext ignored = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignored = loginAdmin()) {
       final String selfManagementRoleId = createSelfManagementRole();
 
       final String userPassword = RandomStringUtils.randomAlphanumeric(5);
@@ -171,7 +171,7 @@
   @Test
   public void activatedAntonyPasswordDoesntExpire() throws InterruptedException {
 
-    try (final AutoUserContext ignored = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignored = loginAdmin()) {
       final Authentication adminAuthentication =
               getTestSubject().login(ADMIN_IDENTIFIER, TestEnvironment.encodePassword(ADMIN_PASSWORD));
       Assert.assertEquals(null, adminAuthentication.getPasswordExpiration());
@@ -180,7 +180,7 @@
 
   @Test
   public void onlyAntonyCanSetAntonyPassword() throws InterruptedException {
-    try (final AutoUserContext ignored = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignored = loginAdmin()) {
 
       final String roleIdentifier = createRole(buildUserPermission(), buildSelfPermission(), buildRolePermission());
       final String username = createUserWithNonexpiredPassword(AHMES_PASSWORD, roleIdentifier);
diff --git a/component-test/src/main/java/TestPermittableGroups.java b/component-test/src/main/java/TestPermittableGroups.java
index d078849..78e5d46 100644
--- a/component-test/src/main/java/TestPermittableGroups.java
+++ b/component-test/src/main/java/TestPermittableGroups.java
@@ -30,7 +30,7 @@
 public class TestPermittableGroups extends AbstractComponentTest {
   @Test
   public void getPermittableGroups() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final PermittableGroup identityManagementPermittableGroup
               = getTestSubject().getPermittableGroup(PermittableGroupIds.IDENTITY_MANAGEMENT);
       Assert.assertNotNull(identityManagementPermittableGroup);
@@ -50,7 +50,7 @@
 
   @Test(expected = IllegalArgumentException.class)
   public void createWithIllegalMethodThrows() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final String identifier = testEnvironment.generateUniqueIdentifer("group");
 
       final PermittableEndpoint permittableEndpoint = buildPermittableEndpoint();
@@ -64,7 +64,7 @@
 
   @Test
   public void create() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final String identifier = testEnvironment.generateUniqueIdentifer("group");
 
       final PermittableEndpoint permittableEndpoint = buildPermittableEndpoint();
diff --git a/component-test/src/main/java/TestRefreshToken.java b/component-test/src/main/java/TestRefreshToken.java
index f27eb95..1ff7a3f 100644
--- a/component-test/src/main/java/TestRefreshToken.java
+++ b/component-test/src/main/java/TestRefreshToken.java
@@ -44,7 +44,7 @@
 
   @Test(expected = InvalidTokenException.class)
   public void adminLoginAccessTokenShouldTimeOut() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       Thread.sleep(TimeUnit.SECONDS.toMillis(ACCESS_TOKEN_TIME_TO_LIVE + 1));
       getTestSubject().getUser(ADMIN_IDENTIFIER);
     }
diff --git a/component-test/src/main/java/TestRoles.java b/component-test/src/main/java/TestRoles.java
index 16db3b8..ccc899a 100644
--- a/component-test/src/main/java/TestRoles.java
+++ b/component-test/src/main/java/TestRoles.java
@@ -22,8 +22,8 @@
 import org.junit.Assert;
 import org.junit.Test;
 
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
 
 import static io.mifos.identity.internal.util.IdentityConstants.SU_ROLE;
 
@@ -31,10 +31,49 @@
  * @author Myrle Krantz
  */
 public class TestRoles extends AbstractComponentTest {
+  @Test
+  public void testRolesSortedAlphabetically() throws InterruptedException {
+    try (final AutoUserContext ignore = loginAdmin()) {
+      final Permission rolePermission = buildRolePermission();
+
+      final Role role1 = buildRole(testEnvironment.generateUniqueIdentifer("abba"), rolePermission);
+      final Role role2 = buildRole(testEnvironment.generateUniqueIdentifer("bubba"), rolePermission);
+      final Role role3 = buildRole(testEnvironment.generateUniqueIdentifer("c1"), rolePermission);
+      final Role role4 = buildRole(testEnvironment.generateUniqueIdentifer("calla"), rolePermission);
+      final Role role5 = buildRole(testEnvironment.generateUniqueIdentifer("uelf"), rolePermission);
+      final Role role6 = buildRole(testEnvironment.generateUniqueIdentifer("ulf"), rolePermission);
+
+      getTestSubject().createRole(role2);
+      getTestSubject().createRole(role1);
+      getTestSubject().createRole(role6);
+      getTestSubject().createRole(role4);
+      getTestSubject().createRole(role3);
+      getTestSubject().createRole(role5);
+
+      Assert.assertTrue(eventRecorder.wait(EventConstants.OPERATION_POST_ROLE, role1.getIdentifier()));
+      Assert.assertTrue(eventRecorder.wait(EventConstants.OPERATION_POST_ROLE, role2.getIdentifier()));
+      Assert.assertTrue(eventRecorder.wait(EventConstants.OPERATION_POST_ROLE, role3.getIdentifier()));
+      Assert.assertTrue(eventRecorder.wait(EventConstants.OPERATION_POST_ROLE, role4.getIdentifier()));
+      Assert.assertTrue(eventRecorder.wait(EventConstants.OPERATION_POST_ROLE, role5.getIdentifier()));
+      Assert.assertTrue(eventRecorder.wait(EventConstants.OPERATION_POST_ROLE, role6.getIdentifier()));
+
+      final List<Role> roles = getTestSubject().getRoles();
+      final List<String> idList = roles.stream().map(Role::getIdentifier).collect(Collectors.toList());
+      final List<String> sortedList = Arrays.asList(
+              role1.getIdentifier(),
+              role2.getIdentifier(),
+              role3.getIdentifier(),
+              role4.getIdentifier(),
+              role5.getIdentifier(),
+              role6.getIdentifier());
+      final List<String> filterOutIdsFromOtherTests = idList.stream().filter(sortedList::contains).collect(Collectors.toList());
+      Assert.assertEquals(sortedList, filterOutIdsFromOtherTests);
+    }
+  }
 
   @Test
   public void testCreateRole() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final String roleIdentifier = generateRoleIdentifier();
 
       final Permission rolePermission = buildRolePermission();
@@ -59,7 +98,7 @@
 
   @Test(expected = IllegalArgumentException.class)
   public void shouldNotBeAbleToCreateRoleNamedDeactivated() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final Permission rolePermission = buildRolePermission();
       final Role deactivated = buildRole("deactivated", rolePermission);
 
@@ -69,7 +108,7 @@
 
   @Test
   public void deleteRole() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final String roleIdentifier = createRoleManagementRole();
 
       final Role role = getTestSubject().getRole(roleIdentifier);
@@ -89,7 +128,7 @@
 
   @Test(expected= NotFoundException.class)
   public void deleteRoleThatDoesntExist() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final String randomIdentifier = generateRoleIdentifier();
 
       getTestSubject().deleteRole(randomIdentifier);
@@ -98,7 +137,7 @@
 
   @Test()
   public void changeRole() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final String roleIdentifier = createRoleManagementRole();
 
       final Role role = getTestSubject().getRole(roleIdentifier);
@@ -118,7 +157,7 @@
 
   @Test
   public void testChangePharaohRoleFails() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final Role referenceRole = getTestSubject().getRole(SU_ROLE);
       final Role roleChangeRequest = buildRole(SU_ROLE, buildSelfPermission());
 
@@ -138,7 +177,7 @@
   @Test
   public void testDeletePharaohRoleFails() throws InterruptedException {
 
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final Role adminRole = getTestSubject().getRole(ADMIN_ROLE);
       try {
         getTestSubject().deleteRole(ADMIN_ROLE);
diff --git a/component-test/src/main/java/TestUsers.java b/component-test/src/main/java/TestUsers.java
index 90b7663..c3f55d3 100644
--- a/component-test/src/main/java/TestUsers.java
+++ b/component-test/src/main/java/TestUsers.java
@@ -41,7 +41,7 @@
 
     final String username = createUserWithNonexpiredPassword(AHMES_PASSWORD, ADMIN_ROLE);
 
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final User user = getTestSubject().getUser(username);
       Assert.assertNotNull(user);
       Assert.assertEquals("Correct user identifier?", username, user.getIdentifier());
@@ -61,7 +61,7 @@
       Assert.assertTrue(found);
     }
 
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final List<User> users = getTestSubject().getUsers();
       Assert.assertTrue(Helpers.instancePresent(users, User::getIdentifier, username));
       Assert.assertTrue(Helpers.instancePresent(users, User::getIdentifier, "Ahmes_friend"));
@@ -118,7 +118,7 @@
 
   @Test
   public void testAdminProvisioning() throws InterruptedException {
-    try (final AutoUserContext ignore = enableAndLoginAdmin()) {
+    try (final AutoUserContext ignore = loginAdmin()) {
       final List<Role> roleIdentifiers = getTestSubject().getRoles();
       Assert.assertTrue(Helpers.instancePresent(roleIdentifiers, Role::getIdentifier, ADMIN_ROLE));
 
diff --git a/service/src/main/java/io/mifos/identity/internal/service/RoleService.java b/service/src/main/java/io/mifos/identity/internal/service/RoleService.java
index e488ddf..f7609b3 100644
--- a/service/src/main/java/io/mifos/identity/internal/service/RoleService.java
+++ b/service/src/main/java/io/mifos/identity/internal/service/RoleService.java
@@ -41,6 +41,7 @@
   public List<Role> findAll() {
     return repository.getAll().stream()
         .map(this::mapEntity)
+        .sorted((x, y) -> (x.getIdentifier().compareTo(y.getIdentifier())))
         .collect(Collectors.toList());
   }