aboutsummaryrefslogtreecommitdiffstats
path: root/aai-core/src/test/java/org/onap/aai/introspection/tools/IntrospectorValidatorTest.java
blob: bc222eccf4f1268cbbb1479d517aa839f4bf2cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.introspection.tools;

import static junit.framework.TestCase.assertNotNull;
import static org.eclipse.persistence.jpa.jpql.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.onap.aai.AAISetup;
import org.onap.aai.introspection.Introspector;
import org.onap.aai.introspection.Loader;
import org.onap.aai.introspection.ModelType;
import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
import org.springframework.test.annotation.DirtiesContext;

@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
public class IntrospectorValidatorTest extends AAISetup {

    private Loader loader;
    private Issue issue;
    private Introspector introspector;
    private IntrospectorValidator.Builder b;
    private IntrospectorValidator iv;

    @Before
    public void setup() {
        loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
        issue = new Issue();
        try {
            introspector = loader.introspectorFromName("pserver");
        } catch (Exception e) {
            fail("Introspector instantiation call threw an exception " + e);
        }
        b = new IntrospectorValidator.Builder();
        iv = b.build();

        b.addResolver(new IssueResolver() {
            @Override
            public boolean resolveIssue(Issue issue) {
                return true;
            }
        });
        // this method does nothing
        iv.processPrimitiveList("TEST", introspector);
    }

    public void setupIssue(String message, IssueType type, String propName, Introspector introspector) {
        issue.setDetail(message);
        issue.setType(type);
        issue.setPropName(propName);
        issue.setIntrospector(introspector);
    }

    @Test
    public void testIntrospectorValidatorMaxDepth() throws AAIUnknownObjectException {
        setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
        b.restrictDepth(4);
        assertEquals("Maximum Depth should be 4", 4, b.getMaximumDepth());
    }

    @Test
    public void testIntrospectorValidatorValidationRequired() throws AAIUnknownObjectException {
        setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
        b.validateRequired(true);
        assertTrue("Validation should be required", b.getValidateRequired());
    }

    @Test
    public void testIntrospectorValidatorValidatedFalse() throws AAIUnknownObjectException {
        setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
        try {
            assertFalse("Not currently validated", iv.validate(introspector));
        } catch (Exception e) {
            fail("Introspector validate call threw an exception " + e);
        }
    }

    @Test
    public void testIntrospectorValidatorResolveIssues() throws AAIUnknownObjectException {
        setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
        assertTrue("Introspector call to resolve issues should return true", iv.resolveIssues());
    }

    @Test
    public void testIntrospectorValidatorGetIssues() throws AAIUnknownObjectException {
        setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
        List<Issue> result = iv.getIssues();
        Assert.assertNotNull(result);

    }

    @Test
    public void testIntrospectorValidatorProcessComplexObject() throws AAIUnknownObjectException {
        setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
        iv.processComplexObj(introspector);
        Assert.assertNotNull(introspector);
    }

    @Test
    public void testIntrospectorValidatorCreateComplexListSize() throws AAIUnknownObjectException {
        setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
        assertEquals("create complex list size should return 0", 0,
                iv.createComplexListSize(introspector, introspector));
    }

    @Test
    public void testIntrospectorValidatorGetResolvers() throws AAIUnknownObjectException {
        setupIssue("Some message", IssueType.MISSING_REQUIRED_PROP, "hostname", introspector);
        assertNotNull("Get resolvers should not be null", b.getResolvers());
    }

}