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