Spring-boot 3.1 update
[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 java.nio.charset.StandardCharsets.UTF_8;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.junit.jupiter.api.Assertions.*;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.io.OutputStreamWriter;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35
36 import jakarta.xml.bind.SchemaOutputResolver;
37 import javax.xml.transform.OutputKeys;
38 import javax.xml.transform.Result;
39 import javax.xml.transform.Transformer;
40 import javax.xml.transform.TransformerException;
41 import javax.xml.transform.TransformerFactory;
42 import javax.xml.transform.dom.DOMSource;
43 import javax.xml.transform.stream.StreamResult;
44
45 import org.eclipse.persistence.dynamic.DynamicEntity;
46 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
47 import org.junit.jupiter.api.Test;
48 import org.onap.aai.config.NodesConfiguration;
49 import org.onap.aai.restclient.MockProvider;
50 import org.onap.aai.setup.SchemaVersion;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.test.annotation.DirtiesContext;
54 import org.springframework.test.context.ContextConfiguration;
55 import org.springframework.test.context.TestPropertySource;
56 import org.w3c.dom.Document;
57
58 @TestPropertySource(
59         properties = {"schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-ss-wiring-test.properties",
60                 "spring.main.allow-bean-definition-overriding=true"})
61
62 @ContextConfiguration(classes = {MockProvider.class, NodesConfiguration.class})
63 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
64 @SpringBootTest
65 public class NodeIngestorTest {
66     @Autowired
67     NodeIngestor nodeIngestor;
68
69     @Test
70     public void testGetContextForVersion() {
71         assertThrows(IllegalArgumentException.class, () -> {
72             DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
73
74             // should work bc Foo is valid in test_network_v10 schema
75             DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
76
77             foo10.set("fooId", "bar");
78             assertEquals("bar", foo10.get("fooId"));
79
80             // should work bc Bar is valid in test_business_v10 schema
81             DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
82             bar10.set("barId", "bar2");
83             assertEquals("bar2", bar10.get("barId"));
84             XSDOutputResolver outputResolver10 = new XSDOutputResolver();
85             ctx10.generateSchema(outputResolver10);
86
87             DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
88
89             // should work bc Foo.quantity is valid in test_network_v11 schema
90             DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
91             foo11.set("quantity", "12");
92             assertEquals("12", foo11.get("quantity"));
93
94             DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
95             quux11.set("qManagerName", "some guy");
96             assertEquals("some guy", quux11.get("qManagerName"));
97             XSDOutputResolver outputResolver11 = new XSDOutputResolver();
98             ctx11.generateSchema(outputResolver11);
99             // should fail bc Quux not in v10 test schema
100             ctx10.newDynamicEntity("Quux");
101         });
102     }
103
104     @Test
105     public void testHasNodeType() {
106         // TODO remove for integration tests
107         assertTrue(nodeIngestor.hasNodeType("foo", new SchemaVersion("v11")));
108         assertTrue(nodeIngestor.hasNodeType("quux", new SchemaVersion("v11")));
109         assertFalse(nodeIngestor.hasNodeType("quux", new SchemaVersion("v10")));
110     }
111
112     @Test
113     public void testGetVersionFromClassName() {
114         assertEquals(nodeIngestor.getVersionFromClassName("inventory.aai.onap.org.v13.Evc"), new SchemaVersion("v13"));
115
116     }
117
118     @Test
119     public void testGetVersionFromClassNameNull() {
120         assertEquals(nodeIngestor.getVersionFromClassName("blah"), new SchemaVersion("v15"));
121
122     }
123
124     @Test
125     public void testGetObjectsInVersion() {
126         assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 148);
127         // comment for IntegrationTest
128         // assertEquals(nodeIngestor.getObjectsInVersion(new SchemaVersion("v13")).size(), 229);
129
130     }
131
132     @Test
133     public void testCombinedSchema() throws TransformerException, IOException {
134         DynamicJAXBContext ctx13 = nodeIngestor.getContextForVersion(new SchemaVersion("v13"));
135         XSDOutputResolver outputResolver13 = new XSDOutputResolver();
136         ctx13.generateSchema(outputResolver13);
137         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
138         printDocument(nodeIngestor.getSchema(new SchemaVersion("v13")), buffer);
139         String content = new String(Files.readAllBytes(Path.of("src/test/resources/forWiringTests/aai_oxm_v13.xml")));
140         content = content.replaceAll("\\s+", "");
141         String expected = buffer.toString().replaceAll("\\s+", "");
142
143         assertThat("OXM:\n" + expected, expected, is(content));
144     }
145
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");
154
155         transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, UTF_8)));
156     }
157
158     private static class XSDOutputResolver extends SchemaOutputResolver {
159
160         @Override
161         public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
162             File temp = File.createTempFile("schema", ".xsd");
163             StreamResult result = new StreamResult(temp);
164             System.out.println("Schema file: " + temp.getAbsolutePath());
165
166             result.setSystemId(temp.toURI().toURL().toString());
167
168             return result;
169         }
170     }
171
172 }