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