AAI-1523 checkstyle warnings for aai-schema-ingest
[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.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
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.Paths;
35
36 import javax.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.JAXBContext;
47 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.junit.rules.ExpectedException;
51 import org.junit.runner.RunWith;
52 import org.onap.aai.setup.SchemaLocationsBean;
53 import org.onap.aai.setup.SchemaVersion;
54 import org.onap.aai.setup.SchemaVersions;
55 import org.onap.aai.testutils.TestUtilConfigTranslator;
56 import org.springframework.test.context.ContextConfiguration;
57 import org.springframework.test.context.TestPropertySource;
58 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
59 import org.w3c.dom.Document;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.boot.test.context.SpringBootTest;
62
63 @RunWith(SpringJUnit4ClassRunner.class)
64 @ContextConfiguration(classes = {SchemaLocationsBean.class, SchemaVersions.class, TestUtilConfigTranslator.class, NodeIngestor.class})
65 @TestPropertySource(properties = { "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test.properties" })
66 @SpringBootTest
67 public class NodeIngestorTest {
68     @Autowired
69     NodeIngestor ni;
70     
71     //set thrown.expect to whatever a specific test needs
72     //this establishes a default of expecting no exceptions to be thrown
73     @Rule
74     public ExpectedException thrown = ExpectedException.none();
75     
76     @Test
77     public void testGetContextForVersion() {
78         DynamicJAXBContext ctx10 = ni.getContextForVersion(new SchemaVersion("v10"));
79         
80         //should work bc Foo is valid in test_network_v10 schema
81         DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
82
83         foo10.set("fooId","bar");
84         assertTrue("bar".equals(foo10.get("fooId")));
85         
86         //should work bc Bar is valid in test_business_v10 schema
87         DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
88         bar10.set("barId","bar2");
89         assertTrue("bar2".equals(bar10.get("barId")));
90         XSDOutputResolver outputResolver10 = new XSDOutputResolver();
91         ctx10.generateSchema(outputResolver10);
92         
93         DynamicJAXBContext ctx11 = ni.getContextForVersion(new SchemaVersion("v11"));
94         
95         //should work bc Foo.quantity is valid in test_network_v11 schema
96         DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
97         foo11.set("quantity","12");
98         assertTrue("12".equals(foo11.get("quantity")));
99         
100         DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
101         quux11.set("qManagerName","some guy");
102         assertTrue("some guy".equals(quux11.get("qManagerName")));
103         XSDOutputResolver outputResolver11 = new XSDOutputResolver();
104         ctx11.generateSchema(outputResolver11);
105
106         
107         thrown.expect(IllegalArgumentException.class);
108         //should fail bc Quux not in v10 test schema
109         ctx10.newDynamicEntity("Quux");
110     }
111     
112     @Test
113     public void testHasNodeType() {
114         assertTrue(ni.hasNodeType("foo", new SchemaVersion("v11")));
115         assertTrue(ni.hasNodeType("quux", new SchemaVersion("v11")));
116         assertFalse(ni.hasNodeType("quux", new SchemaVersion("v10")));
117     }
118     @Test
119     public void testCombinedSchema() throws TransformerException, IOException {
120         DynamicJAXBContext ctx13 = ni.getContextForVersion(new SchemaVersion("v13"));
121         XSDOutputResolver outputResolver13 = new XSDOutputResolver();
122         ctx13.generateSchema(outputResolver13);
123         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
124         printDocument(ni.getSchema(new SchemaVersion("v13")),buffer);
125         String content = new String(Files.readAllBytes(Paths.get("src/test/resources/forWiringTests/aai_oxm_v13.xml")));
126         content = content.replaceAll("\\s+", "");
127         String expected = buffer.toString().replaceAll("\\s+", "");
128         assertThat("OXM:\n"+expected,expected, is(content));
129     }
130
131     public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
132         TransformerFactory tf = TransformerFactory.newInstance();
133         Transformer transformer = tf.newTransformer();
134         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
135         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
136         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
137         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
138         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
139
140         transformer.transform(new DOMSource(doc), 
141              new StreamResult(new OutputStreamWriter(out, "UTF-8")));
142     }
143     
144     private class XSDOutputResolver extends SchemaOutputResolver {
145
146            @Override
147            public Result createOutput(String namespaceUri, String suggestedFileName)
148               throws IOException {
149               
150               // create new file
151               // create stream result
152               File temp = File.createTempFile("schema", ".xsd"); 
153               StreamResult result = new StreamResult(temp);
154               System.out.println("Schema file: "+temp.getAbsolutePath());
155               
156               // set system id
157               result.setSystemId(temp.toURI().toURL().toString());
158               
159               // return result
160               return result;
161            }
162     }
163 }
164
165