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.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.test.annotation.DirtiesContext;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.test.context.TestPropertySource;
49 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
50 import org.w3c.dom.Document;
52 @RunWith(SpringJUnit4ClassRunner.class)
55 "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-ss-wiring-test.properties",
56 "spring.main.allow-bean-definition-overriding=true"
59 @ContextConfiguration(classes = {MockProvider.class, NodesConfiguration.class})
60 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
62 public class NodeIngestorTest {
64 NodeIngestor nodeIngestor;
66 // set thrown.expect to whatever a specific test needs
67 // this establishes a default of expecting no exceptions to be thrown
69 public ExpectedException thrown = ExpectedException.none();
72 public void testGetContextForVersion() {
73 DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
75 // should work bc Foo is valid in test_network_v10 schema
76 DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
78 foo10.set("fooId", "bar");
79 assertTrue("bar".equals(foo10.get("fooId")));
81 // should work bc Bar is valid in test_business_v10 schema
82 DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
83 bar10.set("barId", "bar2");
84 assertTrue("bar2".equals(bar10.get("barId")));
85 XSDOutputResolver outputResolver10 = new XSDOutputResolver();
86 ctx10.generateSchema(outputResolver10);
88 DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
90 // should work bc Foo.quantity is valid in test_network_v11 schema
91 DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
92 foo11.set("quantity", "12");
93 assertTrue("12".equals(foo11.get("quantity")));
95 DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
96 quux11.set("qManagerName", "some guy");
97 assertTrue("some guy".equals(quux11.get("qManagerName")));
98 XSDOutputResolver outputResolver11 = new XSDOutputResolver();
99 ctx11.generateSchema(outputResolver11);
101 thrown.expect(IllegalArgumentException.class);
102 // should fail bc Quux not in v10 test schema
103 ctx10.newDynamicEntity("Quux");
107 public void testHasNodeType() {
108 // TODO remove for integration tests
109 assertTrue(nodeIngestor.hasNodeType("foo", new SchemaVersion("v11")));
110 assertTrue(nodeIngestor.hasNodeType("quux", new SchemaVersion("v11")));
111 assertFalse(nodeIngestor.hasNodeType("quux", new SchemaVersion("v10")));
115 public void testGetVersionFromClassName() {
116 assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
121 public void testGetVersionFromClassNameNull() {
122 assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
127 public void testGetObjectsInVersion() {
128 assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
129 // comment for IntegrationTest
130 // assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 229);
135 public void testCombinedSchema() throws TransformerException, IOException {
136 DynamicJAXBContext ctx13 = nodeIngestor.getContextForVersion(new SchemaVersion("v13"));
137 XSDOutputResolver outputResolver13 = new XSDOutputResolver();
138 ctx13.generateSchema(outputResolver13);
139 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
140 printDocument(nodeIngestor.getSchema(new SchemaVersion("v13")), buffer);
141 String content = new String(Files.readAllBytes(Paths.get("src/test/resources/forWiringTests/aai_oxm_v13.xml")));
142 content = content.replaceAll("\\s+", "");
143 String expected = buffer.toString().replaceAll("\\s+", "");
145 assertThat("OXM:\n" + expected, expected, is(content));
148 public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
149 TransformerFactory tf = TransformerFactory.newInstance();
150 Transformer transformer = tf.newTransformer();
151 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
152 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
153 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
154 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
155 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
157 transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
160 private class XSDOutputResolver extends SchemaOutputResolver {
163 public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
166 // create stream result
167 File temp = File.createTempFile("schema", ".xsd");
168 StreamResult result = new StreamResult(temp);
169 System.out.println("Schema file: " + temp.getAbsolutePath());
172 result.setSystemId(temp.toURI().toURL().toString());