Fix minor sonar issues
[aai/aai-common.git] / aai-schema-ingest / src / test / java / org / onap / aai / nodes / NodeIngestorTest.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.restclient.MockProvider;
31 import org.onap.aai.setup.SchemaVersion;
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.SpringJUnit4ClassRunner;
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(SpringJUnit4ClassRunner.class)
61 @TestPropertySource(
62         properties = {
63             "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-ss-wiring-test.properties",
64             "spring.main.allow-bean-definition-overriding=true"
65         })
66
67 @ContextConfiguration(classes = {MockProvider.class, NodesConfiguration.class})
68 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
69 @SpringBootTest
70 public class NodeIngestorTest {
71     @Autowired
72     NodeIngestor nodeIngestor;
73
74     // set thrown.expect to whatever a specific test needs
75     // this establishes a default of expecting no exceptions to be thrown
76     @Rule
77     public ExpectedException thrown = ExpectedException.none();
78
79     @Test
80     public void testGetContextForVersion() {
81         DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
82
83         // should work bc Foo is valid in test_network_v10 schema
84         DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
85
86         foo10.set("fooId", "bar");
87         assertEquals("bar", foo10.get("fooId"));
88
89         // should work bc Bar is valid in test_business_v10 schema
90         DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
91         bar10.set("barId", "bar2");
92         assertEquals("bar2", bar10.get("barId"));
93         XSDOutputResolver outputResolver10 = new XSDOutputResolver();
94         ctx10.generateSchema(outputResolver10);
95
96         DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
97
98         // should work bc Foo.quantity is valid in test_network_v11 schema
99         DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
100         foo11.set("quantity", "12");
101         assertEquals("12", foo11.get("quantity"));
102
103         DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
104         quux11.set("qManagerName", "some guy");
105         assertEquals("some guy", quux11.get("qManagerName"));
106         XSDOutputResolver outputResolver11 = new XSDOutputResolver();
107         ctx11.generateSchema(outputResolver11);
108
109         thrown.expect(IllegalArgumentException.class);
110         // should fail bc Quux not in v10 test schema
111         ctx10.newDynamicEntity("Quux");
112     }
113
114     @Test
115     public void testHasNodeType() {
116         // TODO remove for integration tests
117         assertTrue(nodeIngestor.hasNodeType("foo", new SchemaVersion("v11")));
118         assertTrue(nodeIngestor.hasNodeType("quux", new SchemaVersion("v11")));
119         assertFalse(nodeIngestor.hasNodeType("quux", new SchemaVersion("v10")));
120     }
121
122     @Test
123     public void testGetVersionFromClassName() {
124         assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
125
126     }
127
128     @Test
129     public void testGetVersionFromClassNameNull() {
130         assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
131
132     }
133
134     @Test
135     public void testGetObjectsInVersion() {
136         assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
137         // comment for IntegrationTest
138         // assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 229);
139
140     }
141
142     @Test
143     public void testCombinedSchema() throws TransformerException, IOException {
144         DynamicJAXBContext ctx13 = nodeIngestor.getContextForVersion(new SchemaVersion("v13"));
145         XSDOutputResolver outputResolver13 = new XSDOutputResolver();
146         ctx13.generateSchema(outputResolver13);
147         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
148         printDocument(nodeIngestor.getSchema(new SchemaVersion("v13")), buffer);
149         String content = new String(Files.readAllBytes(Paths.get("src/test/resources/forWiringTests/aai_oxm_v13.xml")));
150         content = content.replaceAll("\\s+", "");
151         String expected = buffer.toString().replaceAll("\\s+", "");
152
153         assertThat("OXM:\n" + expected, expected, is(content));
154     }
155
156     public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
157         TransformerFactory tf = TransformerFactory.newInstance();
158         Transformer transformer = tf.newTransformer();
159         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
160         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
161         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
162         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
163         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
164
165         transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, UTF_8)));
166     }
167
168     private static class XSDOutputResolver extends SchemaOutputResolver {
169
170         @Override
171         public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
172             File temp = File.createTempFile("schema", ".xsd");
173             StreamResult result = new StreamResult(temp);
174             System.out.println("Schema file: " + temp.getAbsolutePath());
175
176             result.setSystemId(temp.toURI().toURL().toString());
177
178             return result;
179         }
180     }
181
182 }