Update schema ingest library call schema service
[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 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.onap.aai.testutils.TestUtilConfigTranslator;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.test.context.SpringBootTest;
35 import org.springframework.test.annotation.DirtiesContext;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.springframework.test.context.TestPropertySource;
38 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
39 import org.springframework.test.context.junit4.SpringRunner;
40 import org.w3c.dom.Document;
41
42 import javax.xml.bind.SchemaOutputResolver;
43 import javax.xml.transform.*;
44 import javax.xml.transform.dom.DOMSource;
45 import javax.xml.transform.stream.StreamResult;
46 import java.io.*;
47 import java.nio.file.Files;
48 import java.nio.file.Paths;
49
50 import static org.hamcrest.CoreMatchers.is;
51 import static org.junit.Assert.*;
52
53 @RunWith(SpringRunner.class)
54 @TestPropertySource(properties = { "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test-local-node.properties" })
55 @ContextConfiguration(classes = {TestUtilConfigTranslator.class, NodesConfiguration.class})
56
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58
59 @SpringBootTest
60 public class NodeIngestorLocalTest {
61
62     //set thrown.expect to whatever a specific test needs
63     //this establishes a default of expecting no exceptions to be thrown
64     @Rule
65     public ExpectedException thrown = ExpectedException.none();
66     @Autowired
67     NodeIngestor nodeIngestor;
68
69     public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
70         TransformerFactory tf = TransformerFactory.newInstance();
71         Transformer transformer = tf.newTransformer();
72         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
73         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
74         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
75         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
76         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
77
78         transformer.transform(new DOMSource(doc),
79             new StreamResult(new OutputStreamWriter(out, "UTF-8")));
80     }
81
82     @Test
83     public void testGetContextForVersion11() {
84         DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
85
86         //should work bc Foo is valid in test_network_v10 schema
87         DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
88
89         foo10.set("fooId", "bar");
90         assertTrue("bar".equals(foo10.get("fooId")));
91
92         //should work bc Bar is valid in test_business_v10 schema
93         DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
94         bar10.set("barId", "bar2");
95         assertTrue("bar2".equals(bar10.get("barId")));
96         XSDOutputResolver outputResolver10 = new XSDOutputResolver();
97         ctx10.generateSchema(outputResolver10);
98
99         DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
100
101         //should work bc Foo.quantity is valid in test_network_v11 schema
102         DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
103         foo11.set("quantity", "12");
104         assertTrue("12".equals(foo11.get("quantity")));
105
106         DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
107         quux11.set("qManagerName", "some guy");
108         assertTrue("some guy".equals(quux11.get("qManagerName")));
109         XSDOutputResolver outputResolver11 = new XSDOutputResolver();
110         ctx11.generateSchema(outputResolver11);
111
112
113         thrown.expect(IllegalArgumentException.class);
114         //should fail bc Quux not in v10 test schema
115         ctx10.newDynamicEntity("Quux");
116     }
117
118     @Test
119     public void testHasNodeType() {
120         assertTrue(nodeIngestor.hasNodeType("foo", new SchemaVersion("v11")));
121         assertTrue(nodeIngestor.hasNodeType("quux", new SchemaVersion("v11")));
122         assertFalse(nodeIngestor.hasNodeType("quux", new SchemaVersion("v10")));
123     }
124
125     @Test
126     public void testGetVersionFromClassName() {
127         assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
128
129     }
130
131     @Test
132     public void testGetVersionFromClassNameNull() {
133         assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
134
135     }
136
137     @Test
138     public void testGetObjectsInVersion() {
139         assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
140
141     }
142
143     @Test
144     public void testCombinedSchema() throws TransformerException, IOException {
145         DynamicJAXBContext ctx13 = nodeIngestor.getContextForVersion(new SchemaVersion("v13"));
146         XSDOutputResolver outputResolver13 = new XSDOutputResolver();
147         ctx13.generateSchema(outputResolver13);
148         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
149         printDocument(nodeIngestor.getSchema(new SchemaVersion("v13")), buffer);
150         String content = new String(Files.readAllBytes(Paths.get("src/test/resources/forWiringTests/aai_oxm_v13.xml")));
151         content = content.replaceAll("\\s+", "");
152         String expected = buffer.toString().replaceAll("\\s+", "");
153
154         assertThat("OXM:\n" + expected, expected, is(content));
155     }
156
157     private class XSDOutputResolver extends SchemaOutputResolver {
158
159         @Override
160         public Result createOutput(String namespaceUri, String suggestedFileName)
161             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 }