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