2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.model;
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;
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;
43 public class PojoBeanTest {
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[]{})));
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");
57 return SetUtils.union(classes1, classes2).stream()
58 .filter(it -> classNotIgnored(it))
59 .filter(it -> packageNotIgnored(it))
60 .filter(it -> notTestClass(it))
62 .filter(it -> notAbstractClass(it))
63 .filter(it -> hasNoArgsConstructor(it))
64 .map(it -> array(it.getName(), it, propertiesToIgnore(it)))
65 .toArray(Object[][]::new);
69 public void beforeClass() {
70 TestUtils.registerVidNotionsValueGenerator();
73 private static final ImmutableSet<String> classesToSkip = ImmutableSet.of(
75 * Reasons vary; some are using Kotlin overrides, others are with setJson setters,
76 * and others are just not POJOs per-se.
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"
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")
101 private static Object propertiesToIgnore(Class<?> it) {
102 List<String> defaultPropertiesToIgnore = list("additionalProperties");
104 return propertiesToIgnore.containsKey(it.getName())
105 ? propertiesToIgnore.get(it.getName())
106 : defaultPropertiesToIgnore;
109 private static boolean hasNoArgsConstructor(Class<?> it) {
110 return ClassUtils.hasConstructor(it);
113 private static boolean notAbstractClass(Class<?> it) {
114 return !Modifier.isAbstract(it.getModifiers());
117 private static boolean notTestClass(ClassInfo it) {
118 return !it.getName().endsWith("Test");
121 private static boolean packageNotIgnored(ClassInfo classInfo) {
122 return !classesToSkip.contains(classInfo.getPackageName() + ".*");
125 private static boolean classNotIgnored(ClassInfo classInfo) {
126 return !classesToSkip.contains(classInfo.getName());