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