Test many POJOs getters/setters 02/102402/2
authorIttay Stern <ittay.stern@att.com>
Wed, 26 Feb 2020 13:17:34 +0000 (15:17 +0200)
committerIttay Stern <ittay.stern@att.com>
Wed, 26 Feb 2020 13:47:40 +0000 (15:47 +0200)
Issue-ID: VID-768

Change-Id: I891af541bec9aaddaf4d68c8c2b0ae15690bde2c
Signed-off-by: Ittay Stern <ittay.stern@att.com>
vid-app-common/src/main/java/org/onap/vid/aai/model/AaiGetNetworkCollectionDetails/InstanceGroup.java
vid-app-common/src/main/java/org/onap/vid/aai/model/Properties.java
vid-app-common/src/main/java/org/onap/vid/model/Workflow.java
vid-app-common/src/test/java/org/onap/vid/model/PojoBeanTest.java [new file with mode: 0644]
vid-app-common/src/test/java/org/onap/vid/model/WorkflowTest.java [deleted file]
vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java

index 4bc83ce..522f77c 100644 (file)
@@ -44,10 +44,6 @@ public class InstanceGroup {
     @JsonProperty("relationship-list")
     private RelationshipList relationshipList;
 
-    public InstanceGroup(){
-        super();
-    }
-    
     @JsonIgnoreProperties(ignoreUnknown = true)
     public InstanceGroup(
                         @JsonProperty("instance-group-role")
index 0ccf73f..34daf66 100644 (file)
@@ -23,7 +23,6 @@ package org.onap.vid.aai.model;
 import com.fasterxml.jackson.annotation.JsonAnyGetter;
 import com.fasterxml.jackson.annotation.JsonAnySetter;
 import com.fasterxml.jackson.annotation.JsonProperty;
-
 import java.util.HashMap;
 import java.util.Map;
 
@@ -58,14 +57,26 @@ public class Properties {
         return interfaceName;
     }
 
+    public void setInterfaceName(String interfaceName) {
+        this.interfaceName = interfaceName;
+    }
+
     public String getInterfaceId() {
         return interfaceId;
     }
 
+    public void setInterfaceId(String interfaceId) {
+        this.interfaceId = interfaceId;
+    }
+
     public Boolean getIsPortMirrored() {
         return isPortMirrored;
     }
 
+    public void setPortMirrored(Boolean portMirrored) {
+        isPortMirrored = portMirrored;
+    }
+
     public String getTenantId() {
         return tenantId;
     }
index e11a40c..b29db5f 100644 (file)
@@ -61,7 +61,7 @@ public class Workflow {
         this.workflowName = workflowName;
     }
 
-    public void setVnfName(Collection<String> vnfNames) {
+    public void setVnfNames(Collection<String> vnfNames) {
         this.vnfNames = vnfNames;
     }
 }
diff --git a/vid-app-common/src/test/java/org/onap/vid/model/PojoBeanTest.java b/vid-app-common/src/test/java/org/onap/vid/model/PojoBeanTest.java
new file mode 100644 (file)
index 0000000..63ac889
--- /dev/null
@@ -0,0 +1,130 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.vid.model;
+
+import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding;
+import static org.assertj.core.util.Arrays.array;
+import static org.assertj.core.util.Lists.list;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSetMultimap;
+import com.google.common.reflect.ClassPath;
+import com.google.common.reflect.ClassPath.ClassInfo;
+import java.io.IOException;
+import java.lang.reflect.Modifier;
+import java.util.Collection;
+import java.util.List;
+import org.apache.commons.collections4.SetUtils;
+import org.onap.vid.testUtils.TestUtils;
+import org.springframework.util.ClassUtils;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+public class PojoBeanTest {
+
+    @Test(dataProvider = "modelClasses")
+    public void givenPojoWithNoArgsConstructor_hasValidGettersAndSetters(String name, Class<?> clazz,
+        Collection<String> propertiesToIgnore) {
+        assertThat(name, clazz, hasValidGettersAndSettersExcluding(propertiesToIgnore.toArray(new String[]{})));
+    }
+
+    @DataProvider
+    public static Object[][] modelClasses() throws IOException {
+        ClassPath classPath = ClassPath.from(PojoBeanTest.class.getClassLoader());
+        ImmutableSet<ClassInfo> classes1 = classPath.getTopLevelClassesRecursive("org.onap.vid.aai.model");
+        ImmutableSet<ClassInfo> classes2 = classPath.getTopLevelClassesRecursive("org.onap.vid.model");
+
+        return SetUtils.union(classes1, classes2).stream()
+            .filter(it -> classNotIgnored(it))
+            .filter(it -> packageNotIgnored(it))
+            .filter(it -> notTestClass(it))
+            .map(it -> it.load())
+            .filter(it -> notAbstractClass(it))
+            .filter(it -> hasNoArgsConstructor(it))
+            .map(it -> array(it.getName(), it, propertiesToIgnore(it)))
+            .toArray(Object[][]::new);
+    }
+
+    @BeforeClass
+    public void beforeClass() {
+        TestUtils.registerVidNotionsValueGenerator();
+    }
+
+    private static final ImmutableSet<String> classesToSkip = ImmutableSet.of(
+        /*
+         * Reasons vary; some are using Kotlin overrides, others are with setJson setters,
+         * and others are just not POJOs per-se.
+         */
+        "org.onap.vid.model.ProxyResponse",
+        "org.onap.vid.model.PombaInstance.ServiceInstance",
+        "org.onap.vid.model.SOWorkflowList",
+        "org.onap.vid.model.SOWorkflows",
+        "org.onap.vid.model.SOWorkflowParameterDefinitions",
+        "org.onap.vid.model.LocalWorkflowParameterDefinitions",
+        "org.onap.vid.model.aaiTree.*",
+        "org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse",
+        "org.onap.vid.aai.model.OwningEntityResponse",
+        "org.onap.vid.aai.model.PnfProperties",
+        "org.onap.vid.aai.model.PnfResult",
+        "org.onap.vid.aai.model.VnfResult",
+        "org.onap.vid.aai.model.SimpleResult"
+    );
+
+    private static final ImmutableSetMultimap<String, String> propertiesToIgnore = ImmutableSetMultimap.<String, String>builder()
+        .putAll("org.onap.vid.model.ResourceInfo", "errorMessageRaw")
+        .putAll("org.onap.vid.model.JobAuditStatus", "createdDate", "additionalProperties")
+        .putAll("org.onap.vid.aai.model.Properties", "isPortMirrored", "portMirrored", "additionalProperties")
+        .putAll("org.onap.vid.model.NewServiceModel", "serviceProxies", "configurations")
+        .putAll("org.onap.vid.model.CategoryParameter", "options")
+        .build();
+
+    private static Object propertiesToIgnore(Class<?> it) {
+        List<String> defaultPropertiesToIgnore = list("additionalProperties");
+
+        return propertiesToIgnore.containsKey(it.getName())
+            ? propertiesToIgnore.get(it.getName())
+            : defaultPropertiesToIgnore;
+    }
+
+    private static boolean hasNoArgsConstructor(Class<?> it) {
+        return ClassUtils.hasConstructor(it);
+    }
+
+    private static boolean notAbstractClass(Class<?> it) {
+        return !Modifier.isAbstract(it.getModifiers());
+    }
+
+    private static boolean notTestClass(ClassInfo it) {
+        return !it.getName().endsWith("Test");
+    }
+
+    private static boolean packageNotIgnored(ClassInfo classInfo) {
+        return !classesToSkip.contains(classInfo.getPackageName() + ".*");
+    }
+
+    private static boolean classNotIgnored(ClassInfo classInfo) {
+        return !classesToSkip.contains(classInfo.getName());
+    }
+
+
+}
\ No newline at end of file
diff --git a/vid-app-common/src/test/java/org/onap/vid/model/WorkflowTest.java b/vid-app-common/src/test/java/org/onap/vid/model/WorkflowTest.java
deleted file mode 100644 (file)
index 38a1aae..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * VID
- * ================================================================================
- * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.vid.model;
-
-import java.util.Collection;
-
-import org.junit.Test;
-
-public class WorkflowTest {
-
-       private Workflow createTestSubject() {
-               return new Workflow();
-       }
-
-       @Test
-       public void testGetId() throws Exception {
-               Workflow testSubject;
-               int result;
-
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.getId();
-       }
-
-       @Test
-       public void testGetWorkflowName() throws Exception {
-               Workflow testSubject;
-               String result;
-
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.getWorkflowName();
-       }
-
-       @Test
-       public void testGetVnfNames() throws Exception {
-               Workflow testSubject;
-               Collection<String> result;
-
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.getVnfNames();
-       }
-
-       @Test
-       public void testSetId() throws Exception {
-               Workflow testSubject;
-               int id = 0;
-
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setId(id);
-       }
-
-       @Test
-       public void testSetWorkflowName() throws Exception {
-               Workflow testSubject;
-               String workflowName = "";
-
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setWorkflowName(workflowName);
-       }
-
-       @Test
-       public void testSetVnfName() throws Exception {
-               Workflow testSubject;
-               Collection<String> vnfNames = null;
-
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setVnfName(vnfNames);
-       }
-}
index 242977f..0b79452 100644 (file)
@@ -62,6 +62,7 @@ import javax.ws.rs.core.GenericType;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.RandomUtils;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.commons.lang3.reflect.FieldUtils;
 import org.apache.commons.lang3.reflect.MethodUtils;
@@ -87,6 +88,10 @@ import org.mockito.stubbing.OngoingStubbing;
 import org.onap.portalsdk.core.util.SystemProperties;
 import org.onap.vid.asdc.AsdcCatalogException;
 import org.onap.vid.asdc.beans.Service;
+import org.onap.vid.model.VidNotions;
+import org.onap.vid.model.VidNotions.InstantiationType;
+import org.onap.vid.model.VidNotions.InstantiationUI;
+import org.onap.vid.model.VidNotions.ModelCategory;
 import org.onap.vid.mso.model.CloudConfiguration;
 import org.springframework.core.env.Environment;
 import org.testng.annotations.DataProvider;
@@ -249,6 +254,18 @@ public class TestUtils {
             ), CloudConfiguration.class);
     }
 
+    public static void registerVidNotionsValueGenerator() {
+        BeanMatchers.registerValueGenerator(() -> new VidNotions(
+            randomEnum(InstantiationUI.class), randomEnum(ModelCategory.class),
+            randomEnum(InstantiationUI.class), randomEnum(InstantiationType.class)
+        ), VidNotions.class);
+    }
+
+    private static <T> T randomEnum(Class<T> enumClass) {
+        T[] values = enumClass.getEnumConstants();
+        return values[RandomUtils.nextInt(0, values.length)];
+    }
+
     public static OngoingStubbing<InputStream> mockGetRawBodyWithStringBody(HttpResponse<String> httpResponse, String body) {
         try {
             return when(httpResponse.getRawBody()).thenReturn(IOUtils.toInputStream(body, StandardCharsets.UTF_8.name()));