Upgrade spring-boot to 2.7.X in model-loader
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / restclient / TestAaiRestClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.modelloader.restclient;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.util.Properties;
30 import java.util.stream.IntStream;
31 import java.util.stream.Stream;
32
33 import javax.xml.parsers.DocumentBuilderFactory;
34
35 import org.onap.aai.modelloader.config.ModelLoaderConfig;
36 import org.onap.aai.modelloader.entity.ArtifactType;
37 import org.onap.aai.modelloader.entity.model.ModelArtifact;
38 import org.onap.aai.modelloader.entity.model.ModelArtifactParser;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.client.RestTemplate;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Node;
45 import org.w3c.dom.NodeList;
46
47 public class TestAaiRestClient {
48
49     private static final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";
50
51     // This test requires a running A&AI system. To test locally, annotate with org.junit.Test
52     public void testRestClient() throws Exception {
53         Properties props = new Properties();
54         props.setProperty("ml.distribution.ARTIFACT_TYPES", "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");
55         props.setProperty("ml.aai.BASE_URL", "https://localhost:8443");
56         props.setProperty("ml.aai.MODEL_URL", "/aai/v9/service-design-and-creation/models/model/");
57
58         ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
59
60         File xmlFile = new File(MODEL_FILE);
61         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
62         dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
63         Document doc = dbFactory.newDocumentBuilder().parse(xmlFile);
64
65         NodeList nodesList = doc.getDocumentElement().getChildNodes();
66
67         // Get the model IDs
68
69         // @formatter:off
70         String modelInvariantId =
71                 getNodesStream(nodesList)
72                         .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_INVARIANT_ID))
73                         .findFirst()
74                         .map(Node::getTextContent)
75                         .orElse(null);
76
77         String modelId = getNodesStream(nodesList)
78                 .flatMap(n -> getNodesStream(n.getChildNodes()))
79                 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_VER))
80                 .findFirst()
81                 .map(n -> n.getChildNodes().item(1).getTextContent())
82                 .orElse(null);
83         // @formatter:on
84
85         try {
86             // Build the model artifact
87             ModelArtifact model = new ModelArtifact();
88             model.setModelInvariantId(modelInvariantId);
89             model.setModelVerId(modelId);
90             model.setPayload(readFile(MODEL_FILE));
91             model.setModelNamespace("http://org.openecomp.aai.inventory/v9");
92
93             AaiRestClient aaiClient = new AaiRestClient(config, new RestTemplate());
94
95             // GET model
96             ResponseEntity opResult =
97                     aaiClient.getResource(getUrl(model, config), "example-trans-id-0", MediaType.APPLICATION_XML, String.class);
98             assertEquals(opResult.getStatusCode(), HttpStatus.NOT_FOUND);
99
100             // PUT the model
101             opResult = aaiClient.putResource(getUrl(model, config), model.getPayload(), "example-trans-id-1",
102                     MediaType.APPLICATION_XML, String.class);
103             assertEquals(opResult.getStatusCode(), HttpStatus.CREATED);
104
105             // DELETE the model
106             opResult = aaiClient.getAndDeleteResource(getUrl(model, config), "example-trans-id-3");
107             assertEquals(opResult.getStatusCode(), HttpStatus.NO_CONTENT);
108         } catch (Exception e) {
109             e.printStackTrace();
110         }
111     }
112
113     private Stream<Node> getNodesStream(NodeList nodeList) {
114         return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item);
115     }
116
117     static String readFile(String path) throws IOException {
118         byte[] encoded = Files.readAllBytes(Paths.get(path));
119         return new String(encoded);
120     }
121
122     private String getUrl(ModelArtifact model, ModelLoaderConfig config) {
123         String subUrl;
124         if (model.getType().equals(ArtifactType.MODEL)) {
125             subUrl = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim();
126         } else {
127             subUrl = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim();
128         }
129
130         String baseUrl = config.getAaiBaseUrl().trim();
131         if (!baseUrl.endsWith("/") && !subUrl.startsWith("/")) {
132             baseUrl = baseUrl + "/";
133         }
134
135         if (baseUrl.endsWith("/") && subUrl.startsWith("/")) {
136             baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
137         }
138
139         if (!subUrl.endsWith("/")) {
140             subUrl = subUrl + "/";
141         }
142
143         return baseUrl + subUrl + model.getModelInvariantId();
144     }
145 }