changeset 205:03b19e051f0c

Add equals/hashcode to Role class Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/024225.html
author Jie Kang <jkang@redhat.com>
date Thu, 20 Jul 2017 13:38:48 -0400
parents 6e9a0f606b38
children 8276de658fa9
files common/core/src/main/java/com/redhat/thermostat/gateway/common/core/auth/keycloak/Role.java common/core/src/test/java/com/redhat/thermostat/gateway/common/core/auth/keycloak/RoleTest.java
diffstat 2 files changed, 51 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/gateway/common/core/auth/keycloak/Role.java	Thu Jul 20 10:06:51 2017 -0400
+++ b/common/core/src/main/java/com/redhat/thermostat/gateway/common/core/auth/keycloak/Role.java	Thu Jul 20 13:38:48 2017 -0400
@@ -36,8 +36,9 @@
 
 package com.redhat.thermostat.gateway.common.core.auth.keycloak;
 
+import java.util.Objects;
+
 public class Role {
-
     public static final String ROLE_DELIMITER = "-";
     public static final String[] RESTRICTED_CHARACTERS = new String[]{","};
 
@@ -45,6 +46,8 @@
     private final String realm;
 
     public Role(String actions, String realm) {
+        Objects.requireNonNull(actions);
+        Objects.requireNonNull(realm);
         this.actions = actions;
         this.realm = realm;
     }
@@ -56,4 +59,27 @@
     public String getRealm() {
         return this.realm;
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        Role role = (Role) o;
+
+        if (!actions.equals(role.actions)) {
+            return false;
+        }
+
+        return realm.equals(role.realm);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(actions, realm);
+    }
 }
--- a/common/core/src/test/java/com/redhat/thermostat/gateway/common/core/auth/keycloak/RoleTest.java	Thu Jul 20 10:06:51 2017 -0400
+++ b/common/core/src/test/java/com/redhat/thermostat/gateway/common/core/auth/keycloak/RoleTest.java	Thu Jul 20 13:38:48 2017 -0400
@@ -37,6 +37,8 @@
 package com.redhat.thermostat.gateway.common.core.auth.keycloak;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
 
@@ -56,6 +58,28 @@
         verifyRole(r, "rw", "realm-1.2-3");
     }
 
+    /*
+    Roles are added to set data structures which rely on the equals()
+    implementation to prevent duplicates
+     */
+    @Test
+    public void testEquals() {
+        Role one = new Role("a", "b");
+        Role two = new Role("a", "b");
+
+        assertEquals(one, two);
+    }
+
+    @Test
+    public void testNotEquals() {
+        Role one = new Role("a", "b");
+        Role two = new Role("a", "c");
+        Role three = new Role("c", "b");
+
+        assertNotEquals(one, two);
+        assertNotEquals(one, three);
+    }
+
     private void verifyRole(Role role, String expectedActions, String expectedRole) {
         assertEquals(expectedActions, role.getActions());
         assertEquals(expectedRole, role.getRealm());