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