Merge "DB Schema Change for SO Catalog"
[so.git] / so-monitoring / so-monitoring-handler / src / test / java / org / onap / so / montoring / configuration / PojoClassesTests.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.monitoring.configuration;
21
22 import static org.junit.Assert.assertFalse;
23
24 import java.util.Set;
25 import java.util.regex.Pattern;
26
27 import org.junit.Test;
28 import org.onap.so.openpojo.rules.ToStringTester;
29 import org.springframework.beans.factory.config.BeanDefinition;
30 import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
31 import org.springframework.core.type.filter.RegexPatternTypeFilter;
32
33 import com.openpojo.reflection.filters.FilterPackageInfo;
34 import com.openpojo.validation.Validator;
35 import com.openpojo.validation.ValidatorBuilder;
36 import com.openpojo.validation.test.impl.GetterTester;
37 import com.openpojo.validation.test.impl.SetterTester;
38
39 import nl.jqno.equalsverifier.EqualsVerifier;
40 import nl.jqno.equalsverifier.Warning;
41
42 /**
43  * @author waqas.ikram@ericsson.com
44  */
45 public class PojoClassesTests {
46
47     @Test
48     public void test_camunda_module_pojo_classes() throws ClassNotFoundException {
49         test("org.onap.so.monitoring.camunda.model");
50         assertEqualMethod("org.onap.so.monitoring.camunda.model");
51     }
52
53     @Test
54     public void test_so_monitoring_pojo_classes() throws ClassNotFoundException {
55         test("org.onap.so.monitoring.model");
56         assertEqualMethod("org.onap.so.monitoring.model");
57     }
58
59     public void assertEqualMethod(final String pojoPackage) throws ClassNotFoundException {
60         final Set<BeanDefinition> classes = getBeanDefinition(pojoPackage);
61         assertFalse(classes.isEmpty());
62         for (final BeanDefinition bean : classes) {
63             final Class<?> clazz = Class.forName(bean.getBeanClassName());
64             if (!clazz.getName().endsWith("Builder")) {
65                 EqualsVerifier.forClass(clazz).suppress(Warning.STRICT_INHERITANCE, Warning.NONFINAL_FIELDS).verify();
66             }
67         }
68     }
69
70     private Set<BeanDefinition> getBeanDefinition(final String pojoPackage) {
71         final ClassPathScanningCandidateComponentProvider provider =
72                 new ClassPathScanningCandidateComponentProvider(false);
73         provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(pojoPackage + ".*")));
74         return provider.findCandidateComponents(pojoPackage);
75     }
76
77     private void test(final String pojoPackage) {
78         final Validator validator = ValidatorBuilder.create().with(new SetterTester()).with(new GetterTester())
79                 .with(new ToStringTester()).build();
80         validator.validate(pojoPackage, new FilterPackageInfo());
81     }
82 }