d87bdf7f78ecdd3289866b1cf8d87991b470da5f
[aai/schema-service.git] / aai-schema-gen / src / test / java / org / onap / aai / schemagen / genxsd / NodesYAMLfromOXMTest.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.genxsd;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertThat;
25
26 import java.io.BufferedWriter;
27 import java.io.File;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.nio.charset.Charset;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.aai.edges.EdgeIngestor;
40 import org.onap.aai.nodes.NodeIngestor;
41 import org.onap.aai.schemagen.SwaggerGenerationConfiguration;
42 import org.onap.aai.schemagen.testutils.TestUtilConfigTranslatorforEdges;
43 import org.onap.aai.setup.SchemaConfigVersions;
44 import org.onap.aai.setup.SchemaLocationsBean;
45 import org.onap.aai.setup.SchemaVersion;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
50 import org.springframework.test.context.ContextConfiguration;
51 import org.springframework.test.context.TestPropertySource;
52 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
53 import org.w3c.dom.Element;
54
55 @RunWith(SpringJUnit4ClassRunner.class)
56 @ContextConfiguration(
57     classes = {SchemaConfigVersions.class, SchemaLocationsBean.class,
58         TestUtilConfigTranslatorforEdges.class, EdgeIngestor.class, NodeIngestor.class,
59         SwaggerGenerationConfiguration.class
60
61     })
62 @TestPropertySource(properties = {"schema.uri.base.path = /aai", "schema.xsd.maxoccurs = 5000"})
63 public class NodesYAMLfromOXMTest {
64     // public class NodesYAMLfromOXMTest extends AAISetup {
65     private static final Logger logger = LoggerFactory.getLogger("NodesYAMLfromOXMTest.class");
66     private static final String OXMFILENAME = "src/test/resources/oxm/business_v11.xml";
67     private static final String EDGEFILENAME =
68         "src/test/resources/dbedgerules/EdgeDescriptionRules_test.json";
69     public static AnnotationConfigApplicationContext ctx = null;
70     private static String testXML;
71
72     @Autowired
73     NodesYAMLfromOXM nodesYamlFromOxm;
74     @Autowired
75     SchemaConfigVersions schemaConfigVersions;
76
77     @BeforeClass
78     public static void setUpBeforeClass() throws Exception {
79
80         XSDElementTest x = new XSDElementTest();
81         x.setUp();
82         testXML = x.testXML;
83         logger.debug(testXML);
84         BufferedWriter bw = new BufferedWriter(new FileWriter(OXMFILENAME));
85         bw.write(testXML);
86         bw.close();
87         BufferedWriter bw1 = new BufferedWriter(new FileWriter(EDGEFILENAME));
88         bw1.write(YAMLfromOXMTest.EdgeDefs());
89         bw1.close();
90
91     }
92
93     @Before
94     public void setUp() throws Exception {
95
96         NodeGetOperation.checklist.clear();
97         XSDElementTest x = new XSDElementTest();
98         x.setUp();
99         testXML = x.testXML;
100
101         logger.debug(testXML);
102     }
103
104     @Test
105     public void testGetDocumentHeader() {
106         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
107         String apiVersion = v.toString();
108         String header = null;
109         try {
110             nodesYamlFromOxm.setXmlVersion(testXML, v);
111             nodesYamlFromOxm.process();
112             header = nodesYamlFromOxm.getDocumentHeader();
113         } catch (Exception e) {
114             e.printStackTrace();
115         }
116         assertThat("Header:\n" + header, header, is(YAMLheader()));
117     }
118
119     @Test
120     public void testProcess() {
121
122         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
123         String apiVersion = v.toString();
124         String fileContent = null;
125         try {
126             nodesYamlFromOxm.setXmlVersion(testXML, v);
127             fileContent = nodesYamlFromOxm.process();
128         } catch (Exception e) {
129             e.printStackTrace();
130         }
131         assertThat("FileContent-I:\n" + fileContent, fileContent, is(YAMLresult()));
132     }
133
134     @Test
135     public void testNodesYAMLfromOXMFileVersionFile() throws IOException {
136         String outfileName = "testXML.xml";
137         File XMLfile = new File(outfileName);
138         XMLfile.createNewFile();
139         BufferedWriter bw = null;
140         Charset charset = Charset.forName("UTF-8");
141         Path path = Paths.get(outfileName);
142         bw = Files.newBufferedWriter(path, charset);
143         bw.write(testXML);
144         bw.close();
145         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
146         String apiVersion = v.toString();
147         String fileContent = null;
148         try {
149             nodesYamlFromOxm.setXmlVersion(testXML, v);
150             fileContent = nodesYamlFromOxm.process();
151         } catch (Exception e) {
152             e.printStackTrace();
153         }
154         XMLfile.delete();
155         assertThat("FileContent:\n" + fileContent, fileContent, is(YAMLresult()));
156     }
157
158     @Test
159     public void testNodesYAMLfromOXMStringVersionFile() {
160         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
161         String apiVersion = v.toString();
162         String fileContent = null;
163         try {
164             nodesYamlFromOxm.setXmlVersion(testXML, v);
165             fileContent = nodesYamlFromOxm.process();
166         } catch (Exception e) {
167             e.printStackTrace();
168         }
169         assertThat("FileContent-II:\n" + fileContent, fileContent, is(YAMLresult()));
170     }
171
172     @Test
173     public void testAppendDefinitions() {
174         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
175         String apiVersion = v.toString();
176         String definitions = null;
177         try {
178             nodesYamlFromOxm.setXmlVersion(testXML, v);
179             nodesYamlFromOxm.process();
180             definitions = nodesYamlFromOxm.appendDefinitions();
181         } catch (Exception e) {
182             e.printStackTrace();
183         }
184         assertThat("Definitions:\n" + definitions, definitions, is(YAMLgetDefs()));
185     }
186
187     @Test
188     public void testGetXMLRootElementName() {
189         String target = "RootElement=customer";
190         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
191         String apiVersion = v.toString();
192         Element customer = null;
193         String root = null;
194         try {
195             nodesYamlFromOxm.setXmlVersion(testXML, v);
196             nodesYamlFromOxm.process();
197             customer = nodesYamlFromOxm.getJavaTypeElementSwagger("Customer");
198             root = nodesYamlFromOxm.getXMLRootElementName(customer);
199         } catch (Exception e) {
200             e.printStackTrace();
201         }
202         assertThat("RootElement=" + root, is(target));
203     }
204
205     @Test
206     public void testGetXmlRootElementName() {
207         String target = "RootElement=customer";
208         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
209         String apiVersion = v.toString();
210         String root = null;
211         try {
212             nodesYamlFromOxm.setXmlVersion(testXML, v);
213             nodesYamlFromOxm.process();
214             root = nodesYamlFromOxm.getXmlRootElementName("Customer");
215         } catch (Exception e) {
216             e.printStackTrace();
217         }
218         assertThat("RootElement=" + root, is(target));
219     }
220
221     @Test
222     public void testGetJavaTypeElementSwagger() {
223         String target = "Element=java-type/Customer";
224         SchemaVersion v = schemaConfigVersions.getAppRootVersion();
225         String apiVersion = v.toString();
226         Element customer = null;
227         try {
228             nodesYamlFromOxm.setXmlVersion(testXML, v);
229             nodesYamlFromOxm.process();
230             customer = nodesYamlFromOxm.getJavaTypeElementSwagger("Customer");
231         } catch (Exception e) {
232             e.printStackTrace();
233         }
234         assertThat("Element=" + customer.getNodeName() + "/" + customer.getAttribute("name"),
235             is(target));
236     }
237
238     public String YAMLresult() {
239         StringBuilder sb = new StringBuilder(32368);
240         sb.append(YAMLheader());
241         sb.append(YAMLops());
242         sb.append(YAMLgetDefs());
243         return sb.toString();
244     }
245
246     public String YAMLheader() {
247         StringBuilder sb = new StringBuilder(1500);
248         sb.append("#").append(OxmFileProcessor.LINE_SEPARATOR).append(
249             "# ============LICENSE_START=======================================================")
250             .append(OxmFileProcessor.LINE_SEPARATOR).append("# org.onap.aai")
251             .append(OxmFileProcessor.LINE_SEPARATOR)
252             .append(
253                 "# ================================================================================")
254             .append(OxmFileProcessor.LINE_SEPARATOR)
255             .append("# Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.")
256             .append(OxmFileProcessor.LINE_SEPARATOR)
257             .append(
258                 "# ================================================================================")
259             .append(OxmFileProcessor.LINE_SEPARATOR)
260             .append(
261                 "# Licensed under the Creative Commons License, Attribution 4.0 Intl. (the \"License\");")
262             .append(OxmFileProcessor.LINE_SEPARATOR)
263             .append("# you may not use this file except in compliance with the License.")
264             .append(OxmFileProcessor.LINE_SEPARATOR)
265             .append("# You may obtain a copy of the License at")
266             .append(OxmFileProcessor.LINE_SEPARATOR).append("# <p>")
267             .append(OxmFileProcessor.LINE_SEPARATOR)
268             .append("# https://creativecommons.org/licenses/by/4.0/")
269             .append(OxmFileProcessor.LINE_SEPARATOR).append("# <p>")
270             .append(OxmFileProcessor.LINE_SEPARATOR)
271             .append("# Unless required by applicable law or agreed to in writing, software")
272             .append(OxmFileProcessor.LINE_SEPARATOR)
273             .append("# distributed under the License is distributed on an \"AS IS\" BASIS,")
274             .append(OxmFileProcessor.LINE_SEPARATOR)
275             .append("# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.")
276             .append(OxmFileProcessor.LINE_SEPARATOR)
277             .append("# See the License for the specific language governing permissions and")
278             .append(OxmFileProcessor.LINE_SEPARATOR).append("# limitations under the License.")
279             .append(OxmFileProcessor.LINE_SEPARATOR)
280             .append(
281                 "# ============LICENSE_END=========================================================")
282             .append(OxmFileProcessor.LINE_SEPARATOR).append("#")
283             .append(OxmFileProcessor.LINE_SEPARATOR).append(OxmFileProcessor.LINE_SEPARATOR);
284         sb.append("swagger: \"2.0\"\n");
285         sb.append("info:" + OxmFileProcessor.LINE_SEPARATOR);
286         sb.append("  description: |\n");
287         sb.append(
288             "    [Differences versus the previous schema version](apidocs/aai/aai_swagger_v11.diff)"
289                 + OxmFileProcessor.DOUBLE_LINE_SEPARATOR);
290         sb.append(
291             "    This document is best viewed with Firefox or Chrome. Nodes can be found by opening the models link below and finding the node-type. Edge definitions can be found with the node definitions."
292                 + OxmFileProcessor.LINE_SEPARATOR);
293         sb.append("  version: \"v11\"" + OxmFileProcessor.LINE_SEPARATOR);
294         sb.append(
295             "  title: Active and Available Inventory REST API" + OxmFileProcessor.LINE_SEPARATOR);
296         sb.append("  license:" + OxmFileProcessor.LINE_SEPARATOR);
297         sb.append("    name: Apache 2.0" + OxmFileProcessor.LINE_SEPARATOR);
298         sb.append("    url: http://www.apache.org/licenses/LICENSE-2.0.html"
299             + OxmFileProcessor.LINE_SEPARATOR);
300         sb.append("host: localhost" + OxmFileProcessor.LINE_SEPARATOR);
301         sb.append("basePath: /aai/v11" + OxmFileProcessor.LINE_SEPARATOR);
302         sb.append("schemes:" + OxmFileProcessor.LINE_SEPARATOR);
303         sb.append("  - https\n");
304         sb.append("paths:" + OxmFileProcessor.LINE_SEPARATOR);
305         return sb.toString();
306     }
307
308     public String YAMLops() {
309         StringBuilder sb = new StringBuilder(16384);
310         sb.append("  /nodes/customers/customer/{global-customer-id}:\n");
311         sb.append("    get:\n");
312         sb.append("      tags:\n");
313         sb.append("        - Operations\n");
314         sb.append("      summary: returns customer\n");
315         sb.append("      description: returns customer\n");
316         sb.append("      operationId: getBusinessCustomersCustomer\n");
317         sb.append("      produces:\n");
318         sb.append("        - application/json\n");
319         sb.append("        - application/xml\n");
320         sb.append("      responses:\n");
321         sb.append("        \"200\":\n");
322         sb.append("          description: successful operation\n");
323         sb.append("          schema:\n");
324         sb.append("              $ref: \"#/definitions/customer\"\n");
325         sb.append("        \"default\":\n");
326         sb.append("          null\n      parameters:\n");
327         sb.append("        - name: global-customer-id\n");
328         sb.append("          in: path\n");
329         sb.append(
330             "          description: Global customer id used across to uniquely identify customer.\n");
331         sb.append("          required: true\n");
332         sb.append("          type: string\n");
333         sb.append("  /nodes/customers?parameter=value[&parameter2=value2]:\n");
334         sb.append("    get:\n");
335         sb.append("      tags:\n");
336         sb.append("        - Operations\n");
337         sb.append("      summary: returns customers\n");
338         sb.append("      description: returns customers\n");
339         sb.append("      operationId: getBusinessCustomers\n");
340         sb.append("      produces:\n");
341         sb.append("        - application/json\n");
342         sb.append("        - application/xml\n");
343         sb.append("      responses:\n");
344         sb.append("        \"200\":\n");
345         sb.append("          description: successful operation\n");
346         sb.append("          schema:\n");
347         sb.append("              $ref: \"#/definitions/customers\"\n");
348         sb.append("        \"default\":\n");
349         sb.append("          null\n      parameters:\n");
350         sb.append("        - name: global-customer-id\n");
351         sb.append("          in: query\n");
352         sb.append("          required: false\n");
353         sb.append("          type: string\n");
354         sb.append("        - name: subscriber-name\n");
355         sb.append("          in: query\n");
356         sb.append("          required: false\n");
357         sb.append("          type: string\n");
358         sb.append("        - name: subscriber-type\n");
359         sb.append("          in: query\n");
360         sb.append("          required: false\n");
361         sb.append("          type: string\n");
362         sb.append("  /nodes/service-subscriptions/service-subscription/{service-type}:\n");
363         sb.append("    get:\n");
364         sb.append("      tags:\n");
365         sb.append("        - Operations\n");
366         sb.append("      summary: returns service-subscription\n");
367         sb.append("      description: returns service-subscription\n");
368         sb.append(
369             "      operationId: getBusinessCustomersCustomerServiceSubscriptionsServiceSubscription\n");
370         sb.append("      produces:\n");
371         sb.append("        - application/json\n");
372         sb.append("        - application/xml\n");
373         sb.append("      responses:\n");
374         sb.append("        \"200\":\n");
375         sb.append("          description: successful operation\n");
376         sb.append("          schema:\n");
377         sb.append("              $ref: \"#/definitions/service-subscription\"\n");
378         sb.append("        \"default\":\n");
379         sb.append("          null\n      parameters:\n");
380         sb.append("        - name: service-type\n");
381         sb.append("          in: path\n");
382         sb.append(
383             "          description: Value defined by orchestration to identify this service.\n");
384         sb.append("          required: true\n");
385         sb.append("          type: string\n");
386         sb.append("  /nodes/service-subscriptions?parameter=value[&parameter2=value2]:\n");
387         sb.append("    get:\n");
388         sb.append("      tags:\n");
389         sb.append("        - Operations\n");
390         sb.append("      summary: returns service-subscriptions\n");
391         sb.append("      description: returns service-subscriptions\n");
392         sb.append("      operationId: getBusinessCustomersCustomerServiceSubscriptions\n");
393         sb.append("      produces:\n");
394         sb.append("        - application/json\n");
395         sb.append("        - application/xml\n");
396         sb.append("      responses:\n");
397         sb.append("        \"200\":\n");
398         sb.append("          description: successful operation\n");
399         sb.append("          schema:\n");
400         sb.append("              $ref: \"#/definitions/service-subscriptions\"\n");
401         sb.append("        \"default\":\n");
402         sb.append("          null\n      parameters:\n");
403         sb.append("        - name: service-type\n");
404         sb.append("          in: query\n");
405         sb.append("          required: false\n");
406         sb.append("          type: string\n");
407         return sb.toString();
408     }
409
410     public String YAMLgetDefs() {
411         StringBuilder sb = new StringBuilder(8092);
412         sb.append("definitions:\n");
413         sb.append("  business:\n");
414         sb.append("    description: |\n");
415         sb.append("      Namespace for business related constructs\n");
416         sb.append("    properties:\n");
417         sb.append("      customers:\n");
418         sb.append("        type: array\n");
419         sb.append("        items:\n");
420         sb.append("          $ref: \"#/definitions/customer\"\n");
421         sb.append("  customer:\n");
422         sb.append("    description: |\n");
423         sb.append("      customer identifiers to provide linkage back to BSS information.\n");
424         sb.append("      ###### Related Nodes\n");
425         sb.append(
426             "      - FROM service-subscription (CHILD of customer, service-subscription BelongsTo customer, MANY2ONE)(1)\n");
427         sb.append("\n");
428         sb.append("      -(1) IF this CUSTOMER node is deleted, this FROM node is DELETED also\n");
429         sb.append("    required:\n");
430         sb.append("    - global-customer-id\n");
431         sb.append("    - subscriber-name\n");
432         sb.append("    - subscriber-type\n");
433         sb.append("    properties:\n");
434         sb.append("      global-customer-id:\n");
435         sb.append("        type: string\n");
436         sb.append(
437             "        description: Global customer id used across to uniquely identify customer.\n");
438         sb.append("      subscriber-name:\n");
439         sb.append("        type: string\n");
440         sb.append(
441             "        description: Subscriber name, an alternate way to retrieve a customer.\n");
442         sb.append("      subscriber-type:\n");
443         sb.append("        type: string\n");
444         sb.append(
445             "        description: Subscriber type, a way to provide VID with only the INFRA customers.\n");
446         sb.append("      resource-version:\n");
447         sb.append("        type: string\n");
448         sb.append(
449             "        description: Used for optimistic concurrency.  Must be empty on create, valid on update and delete.\n");
450         sb.append("      service-subscriptions:\n");
451         sb.append("        type: array\n");
452         sb.append("        items:\n");
453         sb.append("          $ref: \"#/definitions/service-subscription\"\n");
454         sb.append("  customers:\n");
455         sb.append("    description: |\n");
456         sb.append(
457             "      Collection of customer identifiers to provide linkage back to BSS information.\n");
458         sb.append("    properties:\n");
459         sb.append("      customer:\n");
460         sb.append("        type: array\n");
461         sb.append("        items:          \n");
462         sb.append("          $ref: \"#/definitions/customer\"\n");
463         sb.append("  inventory:\n");
464         sb.append("    properties:\n");
465         sb.append("      business:\n");
466         sb.append("        type: object\n");
467         sb.append("        $ref: \"#/definitions/business\"\n");
468         sb.append("  nodes:" + OxmFileProcessor.LINE_SEPARATOR);
469         sb.append("    properties:" + OxmFileProcessor.LINE_SEPARATOR);
470         sb.append("      inventory-item-data:" + OxmFileProcessor.LINE_SEPARATOR);
471         sb.append("        type: array" + OxmFileProcessor.LINE_SEPARATOR);
472         sb.append("        items:" + OxmFileProcessor.LINE_SEPARATOR);
473         sb.append("          $ref: \"#/definitions/inventory-item-data\""
474             + OxmFileProcessor.LINE_SEPARATOR);
475         sb.append("  service-subscription:\n");
476         sb.append("    description: |\n");
477         sb.append("      Object that group service instances.\n");
478         sb.append("      ###### Related Nodes\n");
479         sb.append(
480             "      - TO customer (PARENT of service-subscription, service-subscription BelongsTo customer, MANY2ONE)(4)\n");
481         sb.append("      - TO tenant( service-subscription Uses tenant, MANY2MANY)\n");
482         sb.append(
483             "      - FROM service-instance (CHILD of service-subscription, service-instance BelongsTo service-subscription, MANY2ONE)(1)\n");
484         sb.append("\n");
485         sb.append(
486             "      -(1) IF this SERVICE-SUBSCRIPTION node is deleted, this FROM node is DELETED also\n");
487         sb.append(
488             "      -(4) IF this TO node is deleted, this SERVICE-SUBSCRIPTION is DELETED also\n");
489         sb.append("    required:\n");
490         sb.append("    - service-type\n");
491         sb.append("    properties:\n");
492         sb.append("      service-type:\n");
493         sb.append("        type: string\n");
494         sb.append(
495             "        description: Value defined by orchestration to identify this service.\n");
496         sb.append("      temp-ub-sub-account-id:\n");
497         sb.append("        type: string\n");
498         sb.append(
499             "        description: This property will be deleted from A&AI in the near future. Only stop gap solution.\n");
500         sb.append("      resource-version:\n");
501         sb.append("        type: string\n");
502         sb.append(
503             "        description: Used for optimistic concurrency.  Must be empty on create, valid on update and delete.\n");
504         sb.append("  service-subscriptions:\n");
505         sb.append("    description: |\n");
506         sb.append("      Collection of objects that group service instances.\n");
507         sb.append("    properties:\n");
508         sb.append("      service-subscription:\n");
509         sb.append("        type: array\n");
510         sb.append("        items:          \n");
511         sb.append("          $ref: \"#/definitions/service-subscription\"\n");
512         return sb.toString();
513     }
514 }