Fix minor sonar issues
[aai/aai-common.git] / aai-schema-ingest / src / test / java / org / onap / aai / nodes / NodeIngestorLocalTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-18 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.nodes;
22
23 import org.eclipse.persistence.dynamic.DynamicEntity;
24 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.onap.aai.config.NodesConfiguration;
30 import org.onap.aai.setup.SchemaVersion;
31 import org.onap.aai.testutils.TestUtilConfigTranslator;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.test.annotation.DirtiesContext;
35 import org.springframework.test.context.ContextConfiguration;
36 import org.springframework.test.context.TestPropertySource;
37 import org.springframework.test.context.junit4.SpringRunner;
38 import org.w3c.dom.Document;
39
40 import javax.xml.bind.SchemaOutputResolver;
41 import javax.xml.transform.OutputKeys;
42 import javax.xml.transform.Result;
43 import javax.xml.transform.Transformer;
44 import javax.xml.transform.TransformerException;
45 import javax.xml.transform.TransformerFactory;
46 import javax.xml.transform.dom.DOMSource;
47 import javax.xml.transform.stream.StreamResult;
48 import java.io.ByteArrayOutputStream;
49 import java.io.File;
50 import java.io.IOException;
51 import java.io.OutputStream;
52 import java.io.OutputStreamWriter;
53 import java.nio.file.Files;
54 import java.nio.file.Paths;
55
56 import static java.nio.charset.StandardCharsets.UTF_8;
57 import static org.hamcrest.CoreMatchers.is;
58 import static org.junit.Assert.*;
59
60 @RunWith(SpringRunner.class)
61 @TestPropertySource(
62         properties = {
63                 "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test-local-node.properties"})
64 @ContextConfiguration(classes = {TestUtilConfigTranslator.class, NodesConfiguration.class})
65
66 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
67
68 @SpringBootTest
69 public class NodeIngestorLocalTest {
70
71     // set thrown.expect to whatever a specific test needs
72     // this establishes a default of expecting no exceptions to be thrown
73     @Rule
74     public ExpectedException thrown = ExpectedException.none();
75     @Autowired
76     NodeIngestor nodeIngestor;
77
78     public static void printDocument(Document doc, OutputStream out) throws TransformerException {
79         TransformerFactory tf = TransformerFactory.newInstance();
80         Transformer transformer = tf.newTransformer();
81         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
82         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
83         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
84         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
85         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
86
87         transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, UTF_8)));
88     }
89
90     @Test
91     public void testGetContextForVersion11() {
92         DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
93
94         // should work bc Foo is valid in test_network_v10 schema
95         DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
96
97         foo10.set("fooId", "bar");
98         assertEquals("bar", foo10.get("fooId"));
99
100         // should work bc Bar is valid in test_business_v10 schema
101         DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
102         bar10.set("barId", "bar2");
103         assertEquals("bar2", bar10.get("barId"));
104         XSDOutputResolver outputResolver10 = new XSDOutputResolver();
105         ctx10.generateSchema(outputResolver10);
106
107         DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
108
109         // should work bc Foo.quantity is valid in test_network_v11 schema
110         DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
111         foo11.set("quantity", "12");
112         assertEquals("12", foo11.get("quantity"));
113
114         DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
115         quux11.set("qManagerName", "some guy");
116         assertEquals("some guy", quux11.get("qManagerName"));
117         XSDOutputResolver outputResolver11 = new XSDOutputResolver();
118         ctx11.generateSchema(outputResolver11);
119
120         thrown.expect(IllegalArgumentException.class);
121         // should fail bc Quux not in v10 test schema
122         ctx10.newDynamicEntity("Quux");
123     }
124
125     @Test
126     public void testHasNodeType() {
127         assertTrue(nodeIngestor.hasNodeType("foo", new SchemaVersion("v11")));
128         assertTrue(nodeIngestor.hasNodeType("quux", new SchemaVersion("v11")));
129         assertFalse(nodeIngestor.hasNodeType("quux", new SchemaVersion("v10")));
130     }
131
132     @Test
133     public void testGetVersionFromClassName() {
134         assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
135     }
136
137     @Test
138     public void testGetVersionFromClassNameNull() {
139         assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
140     }
141
142     @Test
143     public void testGetObjectsInVersion() {
144         assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
145     }
146
147     @Test
148     public void testCombinedSchema() throws TransformerException, IOException {
149         DynamicJAXBContext ctx13 = nodeIngestor.getContextForVersion(new SchemaVersion("v13"));
150         XSDOutputResolver outputResolver13 = new XSDOutputResolver();
151         ctx13.generateSchema(outputResolver13);
152         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
153         printDocument(nodeIngestor.getSchema(new SchemaVersion("v13")), buffer);
154         String content = new String(Files.readAllBytes(Paths.get("src/test/resources/forWiringTests/aai_oxm_v13.xml")));
155         content = content.replaceAll("\\s+", "");
156         String expected = buffer.toString().replaceAll("\\s+", "");
157
158         assertThat("OXM:\n" + expected, expected, is(content));
159     }
160
161     private static class XSDOutputResolver extends SchemaOutputResolver {
162
163         @Override
164         public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
165
166             // create new file
167             // create stream result
168             File temp = File.createTempFile("schema", ".xsd");
169             StreamResult result = new StreamResult(temp);
170             System.out.println("Schema file: " + temp.getAbsolutePath());
171
172             // set system id
173             result.setSystemId(temp.toURI().toURL().toString());
174
175             // return result
176             return result;
177         }
178     }
179 }