Merge "[AAI] Improve test coverage for A&AI component aai-schema-service"
[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  * Modifications Copyright © 2025 Deutsche Telekom.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * <p>
13  * http://www.apache.org/licenses/LICENSE-2.0
14  * <p>
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.aai.schemagen;
23
24 import org.junit.jupiter.api.BeforeAll;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.onap.aai.edges.EdgeIngestor;
28 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
29 import org.onap.aai.nodes.NodeIngestor;
30 import org.onap.aai.schemagen.genxsd.*;
31 import org.onap.aai.schemagen.testutils.TestUtilConfigTranslatorforBusiness;
32 import org.onap.aai.setup.SchemaConfigVersions;
33 import org.onap.aai.setup.SchemaLocationsBean;
34 import org.onap.aai.setup.SchemaVersion;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
39 import org.springframework.test.context.TestPropertySource;
40 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
41 import org.xml.sax.SAXException;
42 import lombok.SneakyThrows;
43
44 import java.io.BufferedWriter;
45 import java.io.FileNotFoundException;
46 import java.io.FileWriter;
47 import java.io.IOException;
48 import java.lang.reflect.Method;
49
50 import javax.xml.parsers.ParserConfigurationException;
51
52 import static org.hamcrest.CoreMatchers.is;
53 import static org.hamcrest.MatcherAssert.assertThat;
54 import static org.junit.jupiter.api.Assertions.assertFalse;
55 import static org.junit.jupiter.api.Assertions.assertNull;
56 import static org.junit.jupiter.api.Assertions.assertTrue;
57
58 @SpringJUnitConfig(
59     classes = {SchemaLocationsBean.class, TestUtilConfigTranslatorforBusiness.class,
60         EdgeIngestor.class, NodeIngestor.class, SwaggerGenerationConfiguration.class,
61         SchemaConfigVersions.class})
62 @TestPropertySource(properties = {"schema.uri.base.path = /aai", "schema.xsd.maxoccurs = 5000"})
63 public class GenerateXsdTest {
64
65     private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class");
66     private static final String OXMFILENAME = "src/test/resources/oxm/business_oxm_v11.xml";
67     private static final String EDGEFILENAME =
68         "src/test/resources/dbedgerules/DbEdgeBusinessRules_test.json";
69     public static AnnotationConfigApplicationContext ctx = null;
70     private static String testXML;
71
72     @Autowired
73     YAMLfromOXM yamlFromOxm;
74
75     @Autowired
76     HTMLfromOXM htmlFromOxm;
77
78     @Autowired
79     SchemaConfigVersions schemaConfigVersions;
80
81     @BeforeAll
82     public static void setUpBeforeClass() throws Exception {
83         XSDElementTest x = new XSDElementTest();
84         x.setUp();
85         testXML = x.getTestXML();
86         logger.debug(testXML);
87         // Write test XML to file (OXM)
88         BufferedWriter bw = new BufferedWriter(new FileWriter(OXMFILENAME));
89         bw.write(testXML);
90         bw.close();
91         // Write Edge Rules to file
92         BufferedWriter bw1 = new BufferedWriter(new FileWriter(EDGEFILENAME));
93         bw1.write(YAMLfromOXMTest.EdgeDefs());
94         bw1.close();
95     }
96
97     @BeforeEach
98     public void setUp() throws Exception {
99         XSDElementTest x = new XSDElementTest();
100         x.setUp();
101         testXML = x.getTestXML();
102     }
103
104     @Test
105     @SneakyThrows
106     public void test_generateSwaggerFromOxmFile() {
107         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
108         String apiVersion = v.toString();
109
110         yamlFromOxm.setXmlVersion(testXML, v);
111         String fileContent = yamlFromOxm.process();
112
113         assertThat(fileContent, is(new YAMLfromOXMTest().YAMLresult()));
114     }
115
116     @Test
117     @SneakyThrows
118     public void test_generateXSDFromOxmFile() {
119         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
120
121         htmlFromOxm.setXmlVersion(testXML, v);
122         String fileContent = htmlFromOxm.process();
123
124         assertThat(fileContent, is(new HTMLfromOXMTest().HTMLresult()));
125     }
126
127     @Test
128     public void testGetAPIVersion() {
129         GenerateXsd.apiVersion = schemaConfigVersions.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 testValidVersionWithAll() throws Exception {
141         // Access the private method using reflection
142         Method validVersionMethod = GenerateXsd.class.getDeclaredMethod("validVersion", String.class);
143         validVersionMethod.setAccessible(true);
144
145         // Test "ALL"
146         boolean result = (boolean) validVersionMethod.invoke(null, "ALL");
147         assertTrue(result, "\"ALL\" should be considered a valid version.");
148     }
149
150     // Test for versionSupportsSwagger method using Reflection
151     @Test
152     public void testVersionSupportsSwaggerWithValidVersion() throws Exception {
153         // Accessing the private method using reflection
154         Method versionSupportsSwaggerMethod = GenerateXsd.class.getDeclaredMethod("versionSupportsSwagger", String.class);
155         versionSupportsSwaggerMethod.setAccessible(true);
156
157         // Test version >= v1
158         boolean result = (boolean) versionSupportsSwaggerMethod.invoke(null, "v1");
159         assertTrue(result, "\"v1\" should be supported for Swagger.");
160
161         result = (boolean) versionSupportsSwaggerMethod.invoke(null, "v5");
162         assertTrue(result, "\"v5\" should be supported for Swagger.");
163     }
164
165     @Test
166     public void testVersionSupportsSwaggerWithInvalidVersion() throws Exception {
167         // Accessing the private method using reflection
168         Method versionSupportsSwaggerMethod = GenerateXsd.class.getDeclaredMethod("versionSupportsSwagger", String.class);
169         versionSupportsSwaggerMethod.setAccessible(true);
170
171         // Test version < v1
172         boolean result = (boolean) versionSupportsSwaggerMethod.invoke(null, "v0");
173         assertFalse(result, "\"v0\" should not be supported for Swagger.");
174     }
175
176     @Test
177     public void mainMethod() throws IOException {
178         System.setProperty("gen_version","v12");
179         System.setProperty("gen_type","xsd");
180         GenerateXsd.main(new String[]{});
181     }
182
183 }