2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.schemagen;
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;
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;
50 import javax.xml.parsers.ParserConfigurationException;
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;
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 {
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;
73 YAMLfromOXM yamlFromOxm;
76 HTMLfromOXM htmlFromOxm;
79 SchemaConfigVersions schemaConfigVersions;
82 public static void setUpBeforeClass() throws Exception {
83 XSDElementTest x = new XSDElementTest();
85 testXML = x.getTestXML();
86 logger.debug(testXML);
87 // Write test XML to file (OXM)
88 BufferedWriter bw = new BufferedWriter(new FileWriter(OXMFILENAME));
91 // Write Edge Rules to file
92 BufferedWriter bw1 = new BufferedWriter(new FileWriter(EDGEFILENAME));
93 bw1.write(YAMLfromOXMTest.EdgeDefs());
98 public void setUp() throws Exception {
99 XSDElementTest x = new XSDElementTest();
101 testXML = x.getTestXML();
106 public void test_generateSwaggerFromOxmFile() {
107 SchemaVersion v = schemaConfigVersions.getAppRootVersion();
108 String apiVersion = v.toString();
110 yamlFromOxm.setXmlVersion(testXML, v);
111 String fileContent = yamlFromOxm.process();
113 assertThat(fileContent, is(new YAMLfromOXMTest().YAMLresult()));
118 public void test_generateXSDFromOxmFile() {
119 SchemaVersion v = schemaConfigVersions.getAppRootVersion();
121 htmlFromOxm.setXmlVersion(testXML, v);
122 String fileContent = htmlFromOxm.process();
124 assertThat(fileContent, is(new HTMLfromOXMTest().HTMLresult()));
128 public void testGetAPIVersion() {
129 GenerateXsd.apiVersion = schemaConfigVersions.getAppRootVersion().toString();
130 assertThat(GenerateXsd.getAPIVersion(), is("v11"));
134 public void testGetYamlDir() {
135 assertThat(GenerateXsd.getYamlDir(),
136 is("aai-schema/src/main/resources/onap/aai_swagger_yaml"));
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);
146 boolean result = (boolean) validVersionMethod.invoke(null, "ALL");
147 assertTrue(result, "\"ALL\" should be considered a valid version.");
150 // Test for versionSupportsSwagger method using Reflection
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);
157 // Test version >= v1
158 boolean result = (boolean) versionSupportsSwaggerMethod.invoke(null, "v1");
159 assertTrue(result, "\"v1\" should be supported for Swagger.");
161 result = (boolean) versionSupportsSwaggerMethod.invoke(null, "v5");
162 assertTrue(result, "\"v5\" should be supported for Swagger.");
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);
172 boolean result = (boolean) versionSupportsSwaggerMethod.invoke(null, "v0");
173 assertFalse(result, "\"v0\" should not be supported for Swagger.");
177 public void mainMethod() throws IOException {
178 System.setProperty("gen_version","v12");
179 System.setProperty("gen_type","xsd");
180 GenerateXsd.main(new String[]{});