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