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