Fix Sonar Issues on policy-models-tosca 76/119076/4
authorlapentafd <francesco.lapenta@est.tech>
Tue, 9 Mar 2021 15:13:05 +0000 (15:13 +0000)
committerlapentafd <francesco.lapenta@est.tech>
Mon, 15 Mar 2021 11:01:09 +0000 (11:01 +0000)
Tests refactoring to reduce number of assertions

Removed lines 167-169 in PfKeyImplTest

Issue-ID: POLICY-3094
Change-Id: I94ef11068811c81108b0fbf3e5b878d02ab25306
Signed-off-by: lapentafd <francesco.lapenta@est.tech>
models-base/src/test/java/org/onap/policy/models/base/PfKeyImplTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypesTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilterTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplateTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTriggerTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java

index 54edc87..2afc94b 100644 (file)
@@ -26,7 +26,6 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -164,10 +163,6 @@ public class PfKeyImplTest {
         assertEquals(0, someKey0.compareTo(someKey0));
         assertEquals(-36, someKey0.compareTo(new DummyPfKey()));
 
-        assertNotEquals(someKey0, null);
-        assertEquals(someKey0, (Object) someKey0);
-        assertNotEquals(someKey0, (Object) new DummyPfKey());
-
         MyKey someKey8 = new MyKey();
         someKey8.setVersion(VERSION001);
         assertFalse(someKey8.isNullKey());
index 088e8e8..a322e06 100644 (file)
@@ -220,22 +220,19 @@ public class AuthorativeToscaProviderPolicyTypeTest {
                 ToscaEntityFilter.<ToscaPolicyType>builder().build());
 
         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
-        assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
-        assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+        checkEqualsNameDescription(beforePolicyType, gotPolicyType);
 
         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
 
         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
-        assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
-        assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+        checkEqualsNameDescription(beforePolicyType, gotPolicyType);
 
         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
                 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
 
         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
-        assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
-        assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+        checkEqualsNameDescription(beforePolicyType, gotPolicyType);
 
         List<ToscaPolicyType> gotPolicyTypeList =
                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
@@ -263,6 +260,11 @@ public class AuthorativeToscaProviderPolicyTypeTest {
         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
     }
 
+    private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
+        assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
+        assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+    }
+
     @Test
     public void testPolicyTypesCreate() throws Exception {
         assertThatThrownBy(() -> {
index 44bc534..7399fdc 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -61,7 +61,10 @@ public class JpaToscaDataTypeTest {
 
         assertThatThrownBy(() -> new JpaToscaDataType((JpaToscaDataType) null))
                 .isInstanceOf(NullPointerException.class);
+    }
 
+    @Test
+    public void testDataTypeProperties() {
         PfConceptKey dtKey = new PfConceptKey("tdt", VERSION_001);
         JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
 
@@ -109,6 +112,14 @@ public class JpaToscaDataTypeTest {
         assertEquals(tdtClone0, tdt);
 
         assertFalse(new JpaToscaDataType().validate("").isValid());
+        validateJpaToscaDataTypeOperations(tdt);
+
+        assertThatThrownBy(() -> {
+            tdt.validate(null);
+        }).hasMessageMatching("fieldName is marked .*on.*ull but is null");
+    }
+
+    private void validateJpaToscaDataTypeOperations(JpaToscaDataType tdt) {
         assertTrue(tdt.validate("").isValid());
 
         tdt.getConstraints().add(null);
@@ -120,11 +131,10 @@ public class JpaToscaDataTypeTest {
         assertFalse(tdt.validate("").isValid());
         tdt.getProperties().remove(null);
         assertTrue(tdt.validate("").isValid());
+    }
 
-        assertThatThrownBy(() -> {
-            tdt.validate(null);
-        }).hasMessageMatching("fieldName is marked .*on.*ull but is null");
-
+    @Test
+    public void testDataTypeConstraints() {
         ToscaDataType dat = new ToscaDataType();
         dat.setName("name");
         dat.setVersion("1.2.3");
index 83d0403..8f339ef 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -61,7 +61,10 @@ public class JpaToscaDataTypesTest {
 
         assertThatThrownBy(() -> new JpaToscaDataTypes(null, new TreeMap<PfConceptKey, JpaToscaDataType>()))
                 .hasMessageMatching(KEY_IS_NULL);
+    }
 
+    @Test
+    public void testDerivedDataTypes() {
         List<Map<String, ToscaDataType>> dtMapList = new ArrayList<>();
         dtMapList.add(new LinkedHashMap<>());
 
index ecffec8..280f1b8 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -62,7 +62,10 @@ public class JpaToscaEventFilterTest {
 
         assertThatThrownBy(() -> new JpaToscaEventFilter((JpaToscaEventFilter) null))
                 .isInstanceOf(NullPointerException.class);
+    }
 
+    @Test
+    public void testEventFilter() {
         PfConceptKey efParentKey = new PfConceptKey("tParentKey", VERSION_001);
         PfReferenceKey efKey = new PfReferenceKey(efParentKey, "trigger0");
         PfConceptKey nodeKey = new PfConceptKey("tParentKey", VERSION_001);
@@ -75,12 +78,10 @@ public class JpaToscaEventFilterTest {
         assertEquals(A_CAPABILITY, tef.getCapability());
 
         JpaToscaEventFilter tdtClone0 = new JpaToscaEventFilter(tef);
-        assertEquals(tef, tdtClone0);
-        assertEquals(0, tef.compareTo(tdtClone0));
+        checkEqualsEventFilter(tef, tdtClone0);
 
         JpaToscaEventFilter tdtClone1 = new JpaToscaEventFilter(tef);
-        assertEquals(tef, tdtClone1);
-        assertEquals(0, tef.compareTo(tdtClone1));
+        checkEqualsEventFilter(tef, tdtClone1);
 
         assertEquals(-1, tef.compareTo(null));
         assertEquals(0, tef.compareTo(tef));
@@ -101,6 +102,21 @@ public class JpaToscaEventFilterTest {
 
         assertEquals(2, tef.getKeys().size());
         assertEquals(2, new JpaToscaEventFilter().getKeys().size());
+    }
+
+    private void checkEqualsEventFilter(JpaToscaEventFilter tef1, JpaToscaEventFilter tef2) {
+        assertEquals(tef1, tef2);
+        assertEquals(0, tef1.compareTo(tef2));
+    }
+
+    @Test
+    public void testValidationEventFilter() {
+        PfConceptKey efParentKey = new PfConceptKey("tParentKey", VERSION_001);
+        PfReferenceKey efKey = new PfReferenceKey(efParentKey, "trigger0");
+        PfConceptKey nodeKey = new PfConceptKey("tParentKey", VERSION_001);
+        JpaToscaEventFilter tef = new JpaToscaEventFilter(efKey, nodeKey);
+
+        JpaToscaEventFilter tdtClone0 = new JpaToscaEventFilter(tef);
 
         new JpaToscaEventFilter().clean();
         tef.clean();
index 714b672..3790329 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -58,6 +58,12 @@ public class JpaToscaTopologyTemplateTest {
         assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null))
                 .isInstanceOf(NullPointerException.class);
 
+        assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null))
+                .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void testTopologyTemplates() {
         PfReferenceKey tttKey = new PfReferenceKey("tst", VERSION_001, "ttt");
         JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
 
@@ -75,12 +81,10 @@ public class JpaToscaTopologyTemplateTest {
         ttt.setPolicies(policies);
 
         JpaToscaTopologyTemplate tttClone0 = new JpaToscaTopologyTemplate(ttt);
-        assertEquals(ttt, tttClone0);
-        assertEquals(0, ttt.compareTo(tttClone0));
+        checkEqualsTopologyTemplate(ttt, tttClone0);
 
         JpaToscaTopologyTemplate tttClone1 = new JpaToscaTopologyTemplate(ttt);
-        assertEquals(ttt, tttClone1);
-        assertEquals(0, ttt.compareTo(tttClone1));
+        checkEqualsTopologyTemplate(ttt, tttClone1);
 
         assertEquals(-1, ttt.compareTo(null));
         assertEquals(0, ttt.compareTo(ttt));
@@ -97,11 +101,16 @@ public class JpaToscaTopologyTemplateTest {
         otherDt.setPolicies(policies);
         assertEquals(0, ttt.compareTo(otherDt));
 
-        assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null))
-                .isInstanceOf(NullPointerException.class);
-
         assertEquals(4, ttt.getKeys().size());
         assertEquals(1, new JpaToscaTopologyTemplate().getKeys().size());
+    }
+
+    @Test
+    public void testTopologyTemplateValidation() {
+        PfReferenceKey tttKey = new PfReferenceKey("tst", VERSION_001, "ttt");
+        JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
+
+        JpaToscaTopologyTemplate tttClone0 = new JpaToscaTopologyTemplate(ttt);
 
         new JpaToscaTopologyTemplate().clean();
         ttt.clean();
@@ -124,4 +133,9 @@ public class JpaToscaTopologyTemplateTest {
 
         assertThatThrownBy(() -> ttt.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
     }
+
+    private void checkEqualsTopologyTemplate(JpaToscaTopologyTemplate ttt1, JpaToscaTopologyTemplate ttt2) {
+        assertEquals(ttt1, ttt2);
+        assertEquals(0, ttt1.compareTo(ttt2));
+    }
 }
index 38be5f6..597ce0c 100644 (file)
@@ -75,7 +75,10 @@ public class JpaToscaTriggerTest {
                 .hasMessageMatching("eventType is marked .*on.*ull but is null");
 
         assertThatThrownBy(() -> new JpaToscaTrigger((JpaToscaTrigger) null)).isInstanceOf(NullPointerException.class);
+    }
 
+    @Test
+    public void testTriggerConstraints() {
         PfConceptKey tparentKey = new PfConceptKey("tParentKey", VERSION_001);
         PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0");
         JpaToscaTrigger tdt = new JpaToscaTrigger(tkey, EVENT_TYPE, ACTION);
@@ -104,12 +107,10 @@ public class JpaToscaTriggerTest {
         assertEquals(A_METHOD, tdt.getMethod());
 
         JpaToscaTrigger tdtClone0 = new JpaToscaTrigger(tdt);
-        assertEquals(tdt, tdtClone0);
-        assertEquals(0, tdt.compareTo(tdtClone0));
+        checkEqualsToscaTriggers(tdt, tdtClone0);
 
         JpaToscaTrigger tdtClone1 = new JpaToscaTrigger(tdt);
-        assertEquals(tdt, tdtClone1);
-        assertEquals(0, tdt.compareTo(tdtClone1));
+        checkEqualsToscaTriggers(tdt, tdtClone1);
 
         assertEquals(-1, tdt.compareTo(null));
         assertEquals(0, tdt.compareTo(tdt));
@@ -147,6 +148,15 @@ public class JpaToscaTriggerTest {
 
         assertEquals(4, tdt.getKeys().size());
         assertEquals(1, new JpaToscaTrigger().getKeys().size());
+    }
+
+    @Test
+    public void testCloneToscaTrigger() {
+        PfConceptKey tparentKey = new PfConceptKey("tParentKey", VERSION_001);
+        PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0");
+        JpaToscaTrigger tdt = new JpaToscaTrigger(tkey, EVENT_TYPE, ACTION);
+
+        JpaToscaTrigger tdtClone0 = new JpaToscaTrigger(tdt);
 
         new JpaToscaTrigger().clean();
         tdt.clean();
@@ -176,4 +186,9 @@ public class JpaToscaTriggerTest {
 
         assertThatThrownBy(() -> tdt.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
     }
+
+    private void checkEqualsToscaTriggers(JpaToscaTrigger tdt1, JpaToscaTrigger tdt2) {
+        assertEquals(tdt1, tdt2);
+        assertEquals(0, tdt1.compareTo(tdt2));
+    }
 }
index 07624ae..69c1839 100644 (file)
@@ -705,7 +705,7 @@ public class SimpleToscaProviderTest {
     }
 
     @Test
-    public void testNonNulls() {
+    public void testNonNullsDataType() {
         assertThatThrownBy(() -> {
             new SimpleToscaProvider().getServiceTemplate(null);
         }).hasMessageMatching(DAO_IS_NULL);
@@ -761,7 +761,10 @@ public class SimpleToscaProviderTest {
         assertThatThrownBy(() -> {
             new SimpleToscaProvider().deleteDataType(pfDao, null);
         }).hasMessageMatching("^dataTypeKey is marked .*on.*ull but is null$");
+    }
 
+    @Test
+    public void testNotNullsPolicyTypes() {
         assertThatThrownBy(() -> {
             new SimpleToscaProvider().getPolicyTypes(null, null, null);
         }).hasMessageMatching(DAO_IS_NULL);