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.restclient.MockProvider;
30 import org.onap.aai.config.NodesConfiguration;
31 import org.onap.aai.setup.SchemaVersion;
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.SpringJUnit4ClassRunner;
38 import org.w3c.dom.Document;
40 import javax.xml.bind.SchemaOutputResolver;
41 import javax.xml.transform.*;
42 import javax.xml.transform.dom.DOMSource;
43 import javax.xml.transform.stream.StreamResult;
45 import java.nio.file.Files;
46 import java.nio.file.Paths;
48 import static org.hamcrest.CoreMatchers.is;
49 import static org.junit.Assert.*;
52 @RunWith(SpringJUnit4ClassRunner.class)
53 @TestPropertySource(properties = { "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-ss-wiring-test.properties" })
55 @ContextConfiguration(classes = { MockProvider.class, NodesConfiguration.class})
56 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 public class NodeIngestorTest {
60 NodeIngestor nodeIngestor;
62 //set thrown.expect to whatever a specific test needs
63 //this establishes a default of expecting no exceptions to be thrown
65 public ExpectedException thrown = ExpectedException.none();
68 public void testGetContextForVersion() {
69 DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
71 //should work bc Foo is valid in test_network_v10 schema
72 DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
74 foo10.set("fooId","bar");
75 assertTrue("bar".equals(foo10.get("fooId")));
77 //should work bc Bar is valid in test_business_v10 schema
78 DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
79 bar10.set("barId","bar2");
80 assertTrue("bar2".equals(bar10.get("barId")));
81 XSDOutputResolver outputResolver10 = new XSDOutputResolver();
82 ctx10.generateSchema(outputResolver10);
84 DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
86 //should work bc Foo.quantity is valid in test_network_v11 schema
87 DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
88 foo11.set("quantity","12");
89 assertTrue("12".equals(foo11.get("quantity")));
91 DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
92 quux11.set("qManagerName","some guy");
93 assertTrue("some guy".equals(quux11.get("qManagerName")));
94 XSDOutputResolver outputResolver11 = new XSDOutputResolver();
95 ctx11.generateSchema(outputResolver11);
98 thrown.expect(IllegalArgumentException.class);
99 //should fail bc Quux not in v10 test schema
100 ctx10.newDynamicEntity("Quux");
104 public void testHasNodeType() {
105 //TODO remove for integration tests
106 assertTrue(nodeIngestor.hasNodeType("foo", new SchemaVersion("v11")));
107 assertTrue(nodeIngestor.hasNodeType("quux", new SchemaVersion("v11")));
108 assertFalse(nodeIngestor.hasNodeType("quux", new SchemaVersion("v10")));
112 public void testGetVersionFromClassName() {
113 assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"),new SchemaVersion("v13"));
118 public void testGetVersionFromClassNameNull() {
119 assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
124 public void testGetObjectsInVersion() {
125 assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
126 //comment for IntegrationTest
127 //assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 229);
132 public void testCombinedSchema() throws TransformerException, IOException {
133 DynamicJAXBContext ctx13 = nodeIngestor.getContextForVersion(new SchemaVersion("v13"));
134 XSDOutputResolver outputResolver13 = new XSDOutputResolver();
135 ctx13.generateSchema(outputResolver13);
136 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
137 printDocument(nodeIngestor.getSchema(new SchemaVersion("v13")),buffer);
138 String content = new String(Files.readAllBytes(Paths.get("src/test/resources/forWiringTests/aai_oxm_v13.xml")));
139 content = content.replaceAll("\\s+", "");
140 String expected = buffer.toString().replaceAll("\\s+", "");
143 assertThat("OXM:\n"+expected,expected, is(content));
146 public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
147 TransformerFactory tf = TransformerFactory.newInstance();
148 Transformer transformer = tf.newTransformer();
149 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
150 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
151 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
152 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
153 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
155 transformer.transform(new DOMSource(doc),
156 new StreamResult(new OutputStreamWriter(out, "UTF-8")));
159 private class XSDOutputResolver extends SchemaOutputResolver {
162 public Result createOutput(String namespaceUri, String suggestedFileName)
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());