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 static java.nio.charset.StandardCharsets.UTF_8;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.*;
27 import java.io.ByteArrayOutputStream;
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;
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;
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.setup.SchemaVersion;
52 import org.onap.aai.testutils.TestUtilConfigTranslator;
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.SpringRunner;
59 import org.w3c.dom.Document;
61 @RunWith(SpringRunner.class)
64 "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test-local-node.properties"})
65 @ContextConfiguration(classes = {TestUtilConfigTranslator.class, NodesConfiguration.class})
67 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
70 public class NodeIngestorLocalTest {
72 // set thrown.expect to whatever a specific test needs
73 // this establishes a default of expecting no exceptions to be thrown
75 public ExpectedException thrown = ExpectedException.none();
77 NodeIngestor nodeIngestor;
79 public static void printDocument(Document doc, OutputStream out) throws TransformerException {
80 TransformerFactory tf = TransformerFactory.newInstance();
81 Transformer transformer = tf.newTransformer();
82 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
83 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
84 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
85 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
86 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
88 transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, UTF_8)));
92 public void testGetContextForVersion11() {
93 DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
95 // should work bc Foo is valid in test_network_v10 schema
96 DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
98 foo10.set("fooId", "bar");
99 assertEquals("bar", foo10.get("fooId"));
101 // should work bc Bar is valid in test_business_v10 schema
102 DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
103 bar10.set("barId", "bar2");
104 assertEquals("bar2", bar10.get("barId"));
105 XSDOutputResolver outputResolver10 = new XSDOutputResolver();
106 ctx10.generateSchema(outputResolver10);
108 DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
110 // should work bc Foo.quantity is valid in test_network_v11 schema
111 DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
112 foo11.set("quantity", "12");
113 assertEquals("12", foo11.get("quantity"));
115 DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
116 quux11.set("qManagerName", "some guy");
117 assertEquals("some guy", quux11.get("qManagerName"));
118 XSDOutputResolver outputResolver11 = new XSDOutputResolver();
119 ctx11.generateSchema(outputResolver11);
121 thrown.expect(IllegalArgumentException.class);
122 // should fail bc Quux not in v10 test schema
123 ctx10.newDynamicEntity("Quux");
127 public void testHasNodeType() {
128 assertTrue(nodeIngestor.hasNodeType("foo", new SchemaVersion("v11")));
129 assertTrue(nodeIngestor.hasNodeType("quux", new SchemaVersion("v11")));
130 assertFalse(nodeIngestor.hasNodeType("quux", new SchemaVersion("v10")));
134 public void testGetVersionFromClassName() {
135 assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
139 public void testGetVersionFromClassNameNull() {
140 assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
144 public void testGetObjectsInVersion() {
145 assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
149 public void testCombinedSchema() throws TransformerException, IOException {
150 DynamicJAXBContext ctx13 = nodeIngestor.getContextForVersion(new SchemaVersion("v13"));
151 XSDOutputResolver outputResolver13 = new XSDOutputResolver();
152 ctx13.generateSchema(outputResolver13);
153 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
154 printDocument(nodeIngestor.getSchema(new SchemaVersion("v13")), buffer);
155 String content = new String(Files.readAllBytes(Paths.get("src/test/resources/forWiringTests/aai_oxm_v13.xml")));
156 content = content.replaceAll("\\s+", "");
157 String expected = buffer.toString().replaceAll("\\s+", "");
159 assertThat("OXM:\n" + expected, expected, is(content));
162 private static class XSDOutputResolver extends SchemaOutputResolver {
165 public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
168 // create stream result
169 File temp = File.createTempFile("schema", ".xsd");
170 StreamResult result = new StreamResult(temp);
171 System.out.println("Schema file: " + temp.getAbsolutePath());
174 result.setSystemId(temp.toURI().toURL().toString());