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 org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.*;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
30 import javax.xml.bind.SchemaOutputResolver;
31 import javax.xml.transform.*;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
35 import org.eclipse.persistence.dynamic.DynamicEntity;
36 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.junit.runner.RunWith;
41 import org.onap.aai.config.NodesConfiguration;
42 import org.onap.aai.restclient.MockProvider;
43 import org.onap.aai.setup.SchemaVersion;
44 import org.onap.aai.testutils.TestUtilConfigTranslator;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.test.annotation.DirtiesContext;
48 import org.springframework.test.context.ContextConfiguration;
49 import org.springframework.test.context.TestPropertySource;
50 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
51 import org.springframework.test.context.junit4.SpringRunner;
52 import org.w3c.dom.Document;
54 @RunWith(SpringRunner.class)
57 "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test-local-node.properties"})
58 @ContextConfiguration(classes = {TestUtilConfigTranslator.class, NodesConfiguration.class})
60 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
63 public class NodeIngestorLocalTest {
65 // set thrown.expect to whatever a specific test needs
66 // this establishes a default of expecting no exceptions to be thrown
68 public ExpectedException thrown = ExpectedException.none();
70 NodeIngestor nodeIngestor;
72 public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
73 TransformerFactory tf = TransformerFactory.newInstance();
74 Transformer transformer = tf.newTransformer();
75 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
76 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
77 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
78 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
79 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
81 transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
85 public void testGetContextForVersion11() {
86 DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
88 // should work bc Foo is valid in test_network_v10 schema
89 DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
91 foo10.set("fooId", "bar");
92 assertTrue("bar".equals(foo10.get("fooId")));
94 // should work bc Bar is valid in test_business_v10 schema
95 DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
96 bar10.set("barId", "bar2");
97 assertTrue("bar2".equals(bar10.get("barId")));
98 XSDOutputResolver outputResolver10 = new XSDOutputResolver();
99 ctx10.generateSchema(outputResolver10);
101 DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
103 // should work bc Foo.quantity is valid in test_network_v11 schema
104 DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
105 foo11.set("quantity", "12");
106 assertTrue("12".equals(foo11.get("quantity")));
108 DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
109 quux11.set("qManagerName", "some guy");
110 assertTrue("some guy".equals(quux11.get("qManagerName")));
111 XSDOutputResolver outputResolver11 = new XSDOutputResolver();
112 ctx11.generateSchema(outputResolver11);
114 thrown.expect(IllegalArgumentException.class);
115 // should fail bc Quux not in v10 test schema
116 ctx10.newDynamicEntity("Quux");
120 public void testHasNodeType() {
121 assertTrue(nodeIngestor.hasNodeType("foo", new SchemaVersion("v11")));
122 assertTrue(nodeIngestor.hasNodeType("quux", new SchemaVersion("v11")));
123 assertFalse(nodeIngestor.hasNodeType("quux", new SchemaVersion("v10")));
127 public void testGetVersionFromClassName() {
128 assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
133 public void testGetVersionFromClassNameNull() {
134 assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
139 public void testGetObjectsInVersion() {
140 assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
145 public void testCombinedSchema() throws TransformerException, IOException {
146 DynamicJAXBContext ctx13 = nodeIngestor.getContextForVersion(new SchemaVersion("v13"));
147 XSDOutputResolver outputResolver13 = new XSDOutputResolver();
148 ctx13.generateSchema(outputResolver13);
149 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
150 printDocument(nodeIngestor.getSchema(new SchemaVersion("v13")), buffer);
151 String content = new String(Files.readAllBytes(Paths.get("src/test/resources/forWiringTests/aai_oxm_v13.xml")));
152 content = content.replaceAll("\\s+", "");
153 String expected = buffer.toString().replaceAll("\\s+", "");
155 assertThat("OXM:\n" + expected, expected, is(content));
158 private class XSDOutputResolver extends SchemaOutputResolver {
161 public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
164 // create stream result
165 File temp = File.createTempFile("schema", ".xsd");
166 StreamResult result = new StreamResult(temp);
167 System.out.println("Schema file: " + temp.getAbsolutePath());
170 result.setSystemId(temp.toURI().toURL().toString());