Add instructions to invoke the linter and code formatter plugins to the README and...
[aai/schema-service.git] / aai-schema-gen / src / test / java / org / onap / aai / schemagen / GenerateXsdTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.schemagen;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertThat;
26
27 import java.io.BufferedWriter;
28 import java.io.FileWriter;
29
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.onap.aai.edges.EdgeIngestor;
35 import org.onap.aai.nodes.NodeIngestor;
36 import org.onap.aai.schemagen.genxsd.*;
37 import org.onap.aai.schemagen.testutils.TestUtilConfigTranslatorforBusiness;
38 import org.onap.aai.setup.SchemaLocationsBean;
39 import org.onap.aai.setup.SchemaVersion;
40 import org.onap.aai.setup.SchemaVersions;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.TestPropertySource;
47 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48
49 @RunWith(SpringJUnit4ClassRunner.class)
50 @ContextConfiguration(
51     classes = {SchemaLocationsBean.class, TestUtilConfigTranslatorforBusiness.class,
52         EdgeIngestor.class, NodeIngestor.class, SwaggerGenerationConfiguration.class,
53         SchemaVersions.class})
54 @TestPropertySource(properties = {"schema.uri.base.path = /aai", "schema.xsd.maxoccurs = 5000"})
55 public class GenerateXsdTest {
56     private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class");
57     private static final String OXMFILENAME = "src/test/resources/oxm/business_oxm_v11.xml";
58     private static final String EDGEFILENAME =
59         "src/test/resources/dbedgerules/DbEdgeBusinessRules_test.json";
60     public static AnnotationConfigApplicationContext ctx = null;
61     private static String testXML;
62
63     @Autowired
64     YAMLfromOXM yamlFromOxm;
65
66     @Autowired
67     HTMLfromOXM htmlFromOxm;
68
69     @Autowired
70     SchemaVersions schemaVersions;
71
72     @BeforeClass
73     public static void setUpBeforeClass() throws Exception {
74         XSDElementTest x = new XSDElementTest();
75         x.setUp();
76         testXML = x.getTestXML();
77         logger.debug(testXML);
78         BufferedWriter bw = new BufferedWriter(new FileWriter(OXMFILENAME));
79         bw.write(testXML);
80         bw.close();
81         BufferedWriter bw1 = new BufferedWriter(new FileWriter(EDGEFILENAME));
82         bw1.write(YAMLfromOXMTest.EdgeDefs());
83         bw1.close();
84
85     }
86
87     @Before
88     public void setUp() throws Exception {
89         // PowerMockito.mockStatic(GenerateXsd.class);
90         XSDElementTest x = new XSDElementTest();
91         x.setUp();
92         testXML = x.getTestXML();
93         // logger.info(testXML);
94     }
95
96     @Test
97     public void test_generateSwaggerFromOxmFile() {
98
99         SchemaVersion v = schemaVersions.getAppRootVersion();
100         String apiVersion = v.toString();
101         String fileContent = null;
102         try {
103
104             yamlFromOxm.setXmlVersion(testXML, v);
105             fileContent = yamlFromOxm.process();
106         } catch (Exception e) {
107             e.printStackTrace();
108         }
109         assertThat(fileContent, is(new YAMLfromOXMTest().YAMLresult()));
110     }
111
112     @Test
113     public void test_generateXSDFromOxmFile() {
114
115         SchemaVersion v = schemaVersions.getAppRootVersion();
116         String fileContent = null;
117         try {
118             htmlFromOxm.setXmlVersion(testXML, v);
119             fileContent = htmlFromOxm.process();
120         } catch (Exception e) {
121             e.printStackTrace();
122         }
123         // logger.debug(fileContent);
124         assertThat(fileContent, is(new HTMLfromOXMTest().HTMLresult()));
125     }
126
127     @Test
128     public void testGetAPIVersion() {
129         GenerateXsd.apiVersion = schemaVersions.getAppRootVersion().toString();
130         assertThat(GenerateXsd.getAPIVersion(), is("v11"));
131     }
132
133     @Test
134     public void testGetYamlDir() {
135         assertThat(GenerateXsd.getYamlDir(),
136             is("aai-schema/src/main/resources/onap/aai_swagger_yaml"));
137     }
138
139     @Test
140     public void testGetResponsesUrl() {
141         assertNull(GenerateXsd.getResponsesUrl());
142     }
143 }