From e51ebedbdead7413d8e76de7da2f8fb39f22e5c4 Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Wed, 26 Feb 2020 15:17:34 +0200 Subject: Test many POJOs getters/setters Issue-ID: VID-768 Change-Id: I891af541bec9aaddaf4d68c8c2b0ae15690bde2c Signed-off-by: Ittay Stern --- .../test/java/org/onap/vid/model/PojoBeanTest.java | 130 +++++++++++++++++++++ .../test/java/org/onap/vid/model/WorkflowTest.java | 92 --------------- .../java/org/onap/vid/testUtils/TestUtils.java | 17 +++ 3 files changed, 147 insertions(+), 92 deletions(-) create mode 100644 vid-app-common/src/test/java/org/onap/vid/model/PojoBeanTest.java delete mode 100644 vid-app-common/src/test/java/org/onap/vid/model/WorkflowTest.java (limited to 'vid-app-common/src/test') 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 index 000000000..63ac889d0 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/model/PojoBeanTest.java @@ -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 propertiesToIgnore) { + assertThat(name, clazz, hasValidGettersAndSettersExcluding(propertiesToIgnore.toArray(new String[]{}))); + } + + @DataProvider + public static Object[][] modelClasses() throws IOException { + ClassPath classPath = ClassPath.from(PojoBeanTest.class.getClassLoader()); + ImmutableSet classes1 = classPath.getTopLevelClassesRecursive("org.onap.vid.aai.model"); + ImmutableSet 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 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 propertiesToIgnore = ImmutableSetMultimap.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 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 index 38a1aae66..000000000 --- a/vid-app-common/src/test/java/org/onap/vid/model/WorkflowTest.java +++ /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 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 vnfNames = null; - - // default test - testSubject = createTestSubject(); - testSubject.setVnfName(vnfNames); - } -} diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java index 242977f59..0b7945268 100644 --- a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java +++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java @@ -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 randomEnum(Class enumClass) { + T[] values = enumClass.getEnumConstants(); + return values[RandomUtils.nextInt(0, values.length)]; + } + public static OngoingStubbing mockGetRawBodyWithStringBody(HttpResponse httpResponse, String body) { try { return when(httpResponse.getRawBody()).thenReturn(IOUtils.toInputStream(body, StandardCharsets.UTF_8.name())); -- cgit 1.2.3-korg