Test many POJOs getters/setters
[vid.git] / vid-app-common / src / test / java / org / onap / vid / model / PojoBeanTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.model;
22
23 import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding;
24 import static org.assertj.core.util.Arrays.array;
25 import static org.assertj.core.util.Lists.list;
26 import static org.hamcrest.MatcherAssert.assertThat;
27
28 import com.google.common.collect.ImmutableSet;
29 import com.google.common.collect.ImmutableSetMultimap;
30 import com.google.common.reflect.ClassPath;
31 import com.google.common.reflect.ClassPath.ClassInfo;
32 import java.io.IOException;
33 import java.lang.reflect.Modifier;
34 import java.util.Collection;
35 import java.util.List;
36 import org.apache.commons.collections4.SetUtils;
37 import org.onap.vid.testUtils.TestUtils;
38 import org.springframework.util.ClassUtils;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.DataProvider;
41 import org.testng.annotations.Test;
42
43 public class PojoBeanTest {
44
45     @Test(dataProvider = "modelClasses")
46     public void givenPojoWithNoArgsConstructor_hasValidGettersAndSetters(String name, Class<?> clazz,
47         Collection<String> propertiesToIgnore) {
48         assertThat(name, clazz, hasValidGettersAndSettersExcluding(propertiesToIgnore.toArray(new String[]{})));
49     }
50
51     @DataProvider
52     public static Object[][] modelClasses() throws IOException {
53         ClassPath classPath = ClassPath.from(PojoBeanTest.class.getClassLoader());
54         ImmutableSet<ClassInfo> classes1 = classPath.getTopLevelClassesRecursive("org.onap.vid.aai.model");
55         ImmutableSet<ClassInfo> classes2 = classPath.getTopLevelClassesRecursive("org.onap.vid.model");
56
57         return SetUtils.union(classes1, classes2).stream()
58             .filter(it -> classNotIgnored(it))
59             .filter(it -> packageNotIgnored(it))
60             .filter(it -> notTestClass(it))
61             .map(it -> it.load())
62             .filter(it -> notAbstractClass(it))
63             .filter(it -> hasNoArgsConstructor(it))
64             .map(it -> array(it.getName(), it, propertiesToIgnore(it)))
65             .toArray(Object[][]::new);
66     }
67
68     @BeforeClass
69     public void beforeClass() {
70         TestUtils.registerVidNotionsValueGenerator();
71     }
72
73     private static final ImmutableSet<String> classesToSkip = ImmutableSet.of(
74         /*
75          * Reasons vary; some are using Kotlin overrides, others are with setJson setters,
76          * and others are just not POJOs per-se.
77          */
78         "org.onap.vid.model.ProxyResponse",
79         "org.onap.vid.model.PombaInstance.ServiceInstance",
80         "org.onap.vid.model.SOWorkflowList",
81         "org.onap.vid.model.SOWorkflows",
82         "org.onap.vid.model.SOWorkflowParameterDefinitions",
83         "org.onap.vid.model.LocalWorkflowParameterDefinitions",
84         "org.onap.vid.model.aaiTree.*",
85         "org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse",
86         "org.onap.vid.aai.model.OwningEntityResponse",
87         "org.onap.vid.aai.model.PnfProperties",
88         "org.onap.vid.aai.model.PnfResult",
89         "org.onap.vid.aai.model.VnfResult",
90         "org.onap.vid.aai.model.SimpleResult"
91     );
92
93     private static final ImmutableSetMultimap<String, String> propertiesToIgnore = ImmutableSetMultimap.<String, String>builder()
94         .putAll("org.onap.vid.model.ResourceInfo", "errorMessageRaw")
95         .putAll("org.onap.vid.model.JobAuditStatus", "createdDate", "additionalProperties")
96         .putAll("org.onap.vid.aai.model.Properties", "isPortMirrored", "portMirrored", "additionalProperties")
97         .putAll("org.onap.vid.model.NewServiceModel", "serviceProxies", "configurations")
98         .putAll("org.onap.vid.model.CategoryParameter", "options")
99         .build();
100
101     private static Object propertiesToIgnore(Class<?> it) {
102         List<String> defaultPropertiesToIgnore = list("additionalProperties");
103
104         return propertiesToIgnore.containsKey(it.getName())
105             ? propertiesToIgnore.get(it.getName())
106             : defaultPropertiesToIgnore;
107     }
108
109     private static boolean hasNoArgsConstructor(Class<?> it) {
110         return ClassUtils.hasConstructor(it);
111     }
112
113     private static boolean notAbstractClass(Class<?> it) {
114         return !Modifier.isAbstract(it.getModifiers());
115     }
116
117     private static boolean notTestClass(ClassInfo it) {
118         return !it.getName().endsWith("Test");
119     }
120
121     private static boolean packageNotIgnored(ClassInfo classInfo) {
122         return !classesToSkip.contains(classInfo.getPackageName() + ".*");
123     }
124
125     private static boolean classNotIgnored(ClassInfo classInfo) {
126         return !classesToSkip.contains(classInfo.getName());
127     }
128
129
130 }