7035affdcffc17d72a4346c80d19e49c4744e0be
[aai/aai-common.git] / aai-schema-ingest / src / test / java / org / onap / aai / nodes / NodeIngestorTest.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.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;
51
52 @RunWith(SpringJUnit4ClassRunner.class)
53 @TestPropertySource(
54         properties = {"schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-ss-wiring-test.properties"})
55
56 @ContextConfiguration(classes = {MockProvider.class, NodesConfiguration.class})
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 @SpringBootTest
59 public class NodeIngestorTest {
60     @Autowired
61     NodeIngestor nodeIngestor;
62
63     // set thrown.expect to whatever a specific test needs
64     // this establishes a default of expecting no exceptions to be thrown
65     @Rule
66     public ExpectedException thrown = ExpectedException.none();
67
68     @Test
69     public void testGetContextForVersion() {
70         DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
71
72         // should work bc Foo is valid in test_network_v10 schema
73         DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
74
75         foo10.set("fooId", "bar");
76         assertTrue("bar".equals(foo10.get("fooId")));
77
78         // should work bc Bar is valid in test_business_v10 schema
79         DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
80         bar10.set("barId", "bar2");
81         assertTrue("bar2".equals(bar10.get("barId")));
82         XSDOutputResolver outputResolver10 = new XSDOutputResolver();
83         ctx10.generateSchema(outputResolver10);
84
85         DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
86
87         // should work bc Foo.quantity is valid in test_network_v11 schema
88         DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
89         foo11.set("quantity", "12");
90         assertTrue("12".equals(foo11.get("quantity")));
91
92         DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
93         quux11.set("qManagerName", "some guy");
94         assertTrue("some guy".equals(quux11.get("qManagerName")));
95         XSDOutputResolver outputResolver11 = new XSDOutputResolver();
96         ctx11.generateSchema(outputResolver11);
97
98         thrown.expect(IllegalArgumentException.class);
99         // should fail bc Quux not in v10 test schema
100         ctx10.newDynamicEntity("Quux");
101     }
102
103     @Test
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")));
109     }
110
111     @Test
112     public void testGetVersionFromClassName() {
113         assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
114
115     }
116
117     @Test
118     public void testGetVersionFromClassNameNull() {
119         assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
120
121     }
122
123     @Test
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);
128
129     }
130
131     @Test
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+", "");
141
142         assertThat("OXM:\n" + expected, expected, is(content));
143     }
144
145     public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
146         TransformerFactory tf = TransformerFactory.newInstance();
147         Transformer transformer = tf.newTransformer();
148         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
149         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
150         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
151         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
152         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
153
154         transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
155     }
156
157     private class XSDOutputResolver extends SchemaOutputResolver {
158
159         @Override
160         public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
161
162             // create new file
163             // create stream result
164             File temp = File.createTempFile("schema", ".xsd");
165             StreamResult result = new StreamResult(temp);
166             System.out.println("Schema file: " + temp.getAbsolutePath());
167
168             // set system id
169             result.setSystemId(temp.toURI().toURL().toString());
170
171             // return result
172             return result;
173         }
174     }
175
176 }