Merge "Fix all blocker sonar issues and some checkstyle"
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / introspection / tools / IntrospectorValidatorTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.aai.introspection.tools;
22
23 import static junit.framework.TestCase.assertNotNull;
24 import static org.eclipse.persistence.jpa.jpql.Assert.fail;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.List;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.aai.AAISetup;
34 import org.onap.aai.introspection.Introspector;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.springframework.test.annotation.DirtiesContext;
39
40 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
41 public class IntrospectorValidatorTest extends AAISetup {
42
43     private Loader loader;
44     private Issue issue;
45     private Introspector introspector;
46     private IntrospectorValidator.Builder b;
47     private IntrospectorValidator iv;
48
49     @Before
50     public void setup() {
51         loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
52         issue = new Issue();
53         try {
54             introspector = loader.introspectorFromName("pserver");
55         } catch (Exception e) {
56             fail("Introspector instantiation call threw an exception " + e);
57         }
58         b = new IntrospectorValidator.Builder();
59         iv = b.build();
60
61         b.addResolver(new IssueResolver() {
62             @Override
63             public boolean resolveIssue(Issue issue) {
64                 return true;
65             }
66         });
67         // this method does nothing
68         iv.processPrimitiveList("TEST", introspector);
69     }
70
71     public void setupIssue(String message, IssueType type, String propName, Introspector introspector) {
72         issue.setDetail(message);
73         issue.setType(type);
74         issue.setPropName(propName);
75         issue.setIntrospector(introspector);
76     }
77
78     @Test
79     public void testIntrospectorValidatorMaxDepth() throws AAIUnknownObjectException {
80         setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
81         b.restrictDepth(4);
82         assertEquals("Maximum Depth should be 4", 4, b.getMaximumDepth());
83     }
84
85     @Test
86     public void testIntrospectorValidatorValidationRequired() throws AAIUnknownObjectException {
87         setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
88         b.validateRequired(true);
89         assertTrue("Validation should be required", b.getValidateRequired());
90     }
91
92     @Test
93     public void testIntrospectorValidatorValidatedFalse() throws AAIUnknownObjectException {
94         setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
95         try {
96             assertFalse("Not currently validated", iv.validate(introspector));
97         } catch (Exception e) {
98             fail("Introspector validate call threw an exception " + e);
99         }
100     }
101
102     @Test
103     public void testIntrospectorValidatorResolveIssues() throws AAIUnknownObjectException {
104         setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
105         assertTrue("Introspector call to resolve issues should return true", iv.resolveIssues());
106     }
107
108     @Test
109     public void testIntrospectorValidatorGetIssues() throws AAIUnknownObjectException {
110         setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
111         List<Issue> result = iv.getIssues();
112         Assert.assertNotNull(result);
113
114     }
115
116     @Test
117     public void testIntrospectorValidatorProcessComplexObject() throws AAIUnknownObjectException {
118         setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
119         iv.processComplexObj(introspector);
120         Assert.assertNotNull(introspector);
121     }
122
123     @Test
124     public void testIntrospectorValidatorCreateComplexListSize() throws AAIUnknownObjectException {
125         setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
126         assertEquals("create complex list size should return 0", 0,
127                 iv.createComplexListSize(introspector, introspector));
128     }
129
130     @Test
131     public void testIntrospectorValidatorGetResolvers() throws AAIUnknownObjectException {
132         setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
133         assertNotNull("Get resolvers should not be null", b.getResolvers());
134     }
135
136 }