dca21f2de24b015ea00847a4da6d698ad0b11b7e
[aai/aai-common.git] / aai-schema-ingest / src / test / java / org / onap / aai / nodes / NodeIngestorLocalTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.nodes;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.*;
25
26 import java.io.*;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29
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;
34
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;
53
54 @RunWith(SpringRunner.class)
55 @TestPropertySource(
56         properties = {
57                 "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test-local-node.properties"})
58 @ContextConfiguration(classes = {TestUtilConfigTranslator.class, NodesConfiguration.class})
59
60 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
61
62 @SpringBootTest
63 public class NodeIngestorLocalTest {
64
65     // set thrown.expect to whatever a specific test needs
66     // this establishes a default of expecting no exceptions to be thrown
67     @Rule
68     public ExpectedException thrown = ExpectedException.none();
69     @Autowired
70     NodeIngestor nodeIngestor;
71
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");
80
81         transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
82     }
83
84     @Test
85     public void testGetContextForVersion11() {
86         DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
87
88         // should work bc Foo is valid in test_network_v10 schema
89         DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
90
91         foo10.set("fooId", "bar");
92         assertTrue("bar".equals(foo10.get("fooId")));
93
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);
100
101         DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
102
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")));
107
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);
113
114         thrown.expect(IllegalArgumentException.class);
115         // should fail bc Quux not in v10 test schema
116         ctx10.newDynamicEntity("Quux");
117     }
118
119     @Test
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")));
124     }
125
126     @Test
127     public void testGetVersionFromClassName() {
128         assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
129
130     }
131
132     @Test
133     public void testGetVersionFromClassNameNull() {
134         assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
135
136     }
137
138     @Test
139     public void testGetObjectsInVersion() {
140         assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
141
142     }
143
144     @Test
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+", "");
154
155         assertThat("OXM:\n" + expected, expected, is(content));
156     }
157
158     private class XSDOutputResolver extends SchemaOutputResolver {
159
160         @Override
161         public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
162
163             // create new file
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());
168
169             // set system id
170             result.setSystemId(temp.toURI().toURL().toString());
171
172             // return result
173             return result;
174         }
175     }
176 }