2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.nodes;
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;
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;
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;
56 import static java.nio.charset.StandardCharsets.UTF_8;
57 import static org.hamcrest.CoreMatchers.is;
58 import static org.junit.Assert.*;
60 @RunWith(SpringRunner.class)
63 "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test-local-node.properties"})
64 @ContextConfiguration(classes = {TestUtilConfigTranslator.class, NodesConfiguration.class})
66 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
69 public class NodeIngestorLocalTest {
71 // set thrown.expect to whatever a specific test needs
72 // this establishes a default of expecting no exceptions to be thrown
74 public ExpectedException thrown = ExpectedException.none();
76 NodeIngestor nodeIngestor;
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");
87 transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, UTF_8)));
91 public void testGetContextForVersion11() {
92 DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
94 // should work bc Foo is valid in test_network_v10 schema
95 DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
97 foo10.set("fooId", "bar");
98 assertEquals("bar", foo10.get("fooId"));
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);
107 DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
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"));
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);
120 thrown.expect(IllegalArgumentException.class);
121 // should fail bc Quux not in v10 test schema
122 ctx10.newDynamicEntity("Quux");
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")));
133 public void testGetVersionFromClassName() {
134 assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
138 public void testGetVersionFromClassNameNull() {
139 assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
143 public void testGetObjectsInVersion() {
144 assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
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+", "");
158 assertThat("OXM:\n" + expected, expected, is(content));
161 private static class XSDOutputResolver extends SchemaOutputResolver {
164 public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
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());
173 result.setSystemId(temp.toURI().toURL().toString());