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