2188380bbd0e94bc8ea9e6dba44fdc2d0eda75cc
[so.git] / mso-catalog-db / src / test / java / org / onap / so / db / catalog / beans / BeansTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.so.db.catalog.beans;
22
23 import static org.hamcrest.CoreMatchers.allOf;
24 import static org.hamcrest.CoreMatchers.equalTo;
25 import static org.onap.so.openpojo.rules.HasAnnotationMatcher.hasAnnotation;
26 import static org.onap.so.openpojo.rules.HasAnnotationPropertyWithValueMatcher.hasAnnotationPropertyWithValue;
27
28 import javax.persistence.Column;
29 import javax.persistence.GeneratedValue;
30 import javax.persistence.Temporal;
31
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.onap.so.openpojo.rules.CustomSetterMustExistRule;
35 import org.onap.so.openpojo.rules.EqualsAndHashCodeTester;
36 import org.onap.so.openpojo.rules.HasEqualsAndHashCodeRule;
37 import org.onap.so.openpojo.rules.HasToStringRule;
38 import org.onap.so.openpojo.rules.ToStringTester;
39
40 import com.openpojo.reflection.PojoClass;
41 import com.openpojo.reflection.PojoClassFilter;
42 import com.openpojo.reflection.filters.FilterEnum;
43 import com.openpojo.reflection.filters.FilterNonConcrete;
44 import com.openpojo.reflection.filters.FilterPackageInfo;
45 import com.openpojo.validation.Validator;
46 import com.openpojo.validation.ValidatorBuilder;
47 import com.openpojo.validation.rule.impl.BusinessKeyMustExistRule;
48 import com.openpojo.validation.rule.impl.GetterMustExistRule;
49 import com.openpojo.validation.rule.impl.NoNestedClassRule;
50 import com.openpojo.validation.rule.impl.NoPrimitivesRule;
51 import com.openpojo.validation.rule.impl.NoPublicFieldsExceptStaticFinalRule;
52 import com.openpojo.validation.rule.impl.NoStaticExceptFinalRule;
53 import com.openpojo.validation.rule.impl.SerializableMustHaveSerialVersionUIDRule;
54 import com.openpojo.validation.test.impl.GetterTester;
55 import com.openpojo.validation.test.impl.SetterTester;
56
57
58 public class BeansTest {
59
60         private PojoClassFilter filterTestClasses = new FilterTestClasses();
61         
62         private PojoClassFilter  enumFilter = new FilterEnum();
63         
64         
65
66         @Test
67         public void pojoStructure() {   
68                 test("org.onap.so.db.catalog.beans");           
69                 test("org.onap.so.db.catalog.beans.macro");             
70         }
71
72         private void test(String pojoPackage) {
73                 Validator validator = ValidatorBuilder.create()
74                                 .with(new GetterMustExistRule())
75                                 .with(new NoPrimitivesRule())
76                             .with(new NoNestedClassRule())
77                             .with(new NoStaticExceptFinalRule())
78                             .with(new SerializableMustHaveSerialVersionUIDRule())
79                                 .with(new HasToStringRule())
80                                 .with(new EqualsAndHashCodeTester())
81                             .with(new NoPublicFieldsExceptStaticFinalRule())
82                                 .with(new CustomSetterMustExistRule().exclude(
83                                                 allOf(hasAnnotationPropertyWithValue(Column.class, "updatable", equalTo(false)), hasAnnotation(GeneratedValue.class)),
84                                                 hasAnnotation(Temporal.class)))
85                                 
86                                 .with(new SetterTester())
87                                 .with(new GetterTester())
88                                 .with(new ToStringTester())
89                                 .with(new HasEqualsAndHashCodeRule())
90                              
91                                 .build();
92                 
93         
94                 validator.validate(pojoPackage, new FilterPackageInfo(), filterTestClasses,enumFilter,new FilterNonConcrete());
95         }
96         private static class FilterTestClasses implements PojoClassFilter {
97                 public boolean include(PojoClass pojoClass) {
98                         return !pojoClass.getSourcePath().contains("/test-classes/");
99                 }
100         }
101 }