Sonar: Reduce cyclomatic complexity 79/88779/1
authorDominik Mizyn <d.mizyn@samsung.com>
Wed, 29 May 2019 10:22:27 +0000 (12:22 +0200)
committerDominik Mizyn <d.mizyn@samsung.com>
Wed, 29 May 2019 14:37:08 +0000 (16:37 +0200)
Reduce the number of conditional operators for equals(). Improve
testEquals() to better cover this method.

This patch also:

* immediately returns expression instead of assigning it to the
  temporary variable "str",
* adds the "@Override" annotation above equals() method signature.

Issue-ID: PORTAL-595
Change-Id: I15f600acce873eb3f22cc405d06a50890c7e87c3
Signed-off-by: Dominik Mizyn <d.mizyn@samsung.com>
ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/domain/EPUserApp.java
ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/domain/EPUserAppTest.java

index 3470a9e..c52bc30 100644 (file)
@@ -61,13 +61,12 @@ public class EPUserApp extends DomainVo implements java.io.Serializable, Compara
        }
        
        public Long getAppRoleId() {
-               return (role.getAppRoleId() == null) ? null : role.getAppRoleId();
+               return this.role.getAppRoleId();
        }
                
        @Override 
        public String toString() {
-               String str = "[u: "+getUserId()+"; a: "+getAppId()+", r: "+getRoleId()+"; appRoleId: "+getAppRoleId()+"]";
-               return str;
+               return "[u: "+getUserId()+"; a: "+getAppId()+", r: "+getRoleId()+"; appRoleId: "+getAppRoleId()+"]";
        }
        
        public Long getUserId() {
@@ -102,6 +101,7 @@ public class EPUserApp extends DomainVo implements java.io.Serializable, Compara
                this.priority = priority;
        }
 
+       @Override
        public boolean equals(Object other) {
                if ((this == other))
                        return true;
@@ -111,10 +111,10 @@ public class EPUserApp extends DomainVo implements java.io.Serializable, Compara
                        return false;
                EPUserApp castOther = (EPUserApp) other;
 
-               return (this.getUserId().equals(castOther.getUserId()))
-                               && (this.getApp().getId().equals(castOther.getApp().getId()))
-                               && (this.getRole().getId().equals(castOther.getRole().getId()))
-                               && ((this.priority==null && castOther.getPriority()==null) || this.getPriority().equals(castOther.getPriority()));
+               return (otherUserIdIsSameAsThisUserId(castOther))
+                               && (otherAppIdIsSameAsThis(castOther))
+                               && (otherRoleIsSameAsThis(castOther))
+                               && (otherPriorityIsSameAsThis(castOther));
        }
 
        public int hashCode() {
@@ -135,4 +135,19 @@ public class EPUserApp extends DomainVo implements java.io.Serializable, Compara
 
            return c1.compareTo(c2);
        }
+       private boolean otherPriorityIsSameAsThis(EPUserApp other){
+               return (this.priority==null && other.getPriority()==null) || this.getPriority().equals(other.getPriority());
+       }
+
+       private boolean otherRoleIsSameAsThis(EPUserApp other){
+               return this.getRole().getId().equals(other.getRole().getId());
+       }
+
+       private boolean otherAppIdIsSameAsThis(EPUserApp other){
+               return this.getApp().getId().equals(other.getApp().getId());
+       }
+
+       private boolean otherUserIdIsSameAsThisUserId(EPUserApp other){
+               return this.getUserId().equals(other.getUserId());
+       }
 }
index 2cc03a6..0923d03 100644 (file)
@@ -121,10 +121,9 @@ public class EPUserAppTest {
         
         
         }
-    
+
     @Test
     public void testEquals(){
-        
         EPRole epRole = new EPRole();
         epRole.setId((long) 12345);
         epRole.setName("test");
@@ -132,19 +131,22 @@ public class EPUserAppTest {
         epRole.setPriority(1);
         epRole.setAppId((long)1);
         epRole.setAppRoleId((long)1);
-        
+
         EPUserApp user1 = mockEPUserApp();
         user1.setApp(mockEPApp());
         user1.setRole(epRole);
-        
+
         EPUserApp user2 = mockEPUserApp();
         user2.setApp(mockEPApp());
         user2.setRole(epRole);
-        
+
+        EPUserApp nullUser = null;
+
+        assertTrue(user1.equals(user1));
+        assertFalse(user1.equals(nullUser));
+        assertFalse(user1.equals(Long.valueOf(1)));
         assertTrue(user1.equals(user2));
-        
         }
-    
     private EPApp mockEPApp() {
         EPApp epApp = new EPApp();
         epApp.setId((long) 12345);