91aa182d5bc7315daf8dc50c3a4463177c6f708c
[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.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.Properties;
31 import java.util.stream.IntStream;
32 import java.util.stream.Stream;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import javax.xml.parsers.DocumentBuilderFactory;
36 import org.onap.aai.modelloader.config.ModelLoaderConfig;
37 import org.onap.aai.modelloader.entity.ArtifactType;
38 import org.onap.aai.modelloader.entity.model.ModelArtifact;
39 import org.onap.aai.modelloader.entity.model.ModelArtifactParser;
40 import org.onap.aai.restclient.client.OperationResult;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Node;
43 import org.w3c.dom.NodeList;
44
45 public class TestAaiRestClient {
46
47     private static final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";
48
49     // This test requires a running A&AI system. To test locally, annotate with org.junit.Test
50     public void testRestClient() throws Exception {
51         Properties props = new Properties();
52         props.setProperty("ml.distribution.ARTIFACT_TYPES", "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");
53         props.setProperty("ml.aai.BASE_URL", "https://localhost:8443");
54         props.setProperty("ml.aai.MODEL_URL", "/aai/v9/service-design-and-creation/models/model/");
55
56         ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
57
58         File xmlFile = new File(MODEL_FILE);
59         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
60         dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
61         Document doc = dbFactory.newDocumentBuilder().parse(xmlFile);
62
63         NodeList nodesList = doc.getDocumentElement().getChildNodes();
64
65         // Get the model IDs
66
67         // @formatter:off
68         String modelInvariantId =
69                 getNodesStream(nodesList)
70                         .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_INVARIANT_ID))
71                         .findFirst()
72                         .map(Node::getTextContent)
73                         .orElse(null);
74
75         String modelId = getNodesStream(nodesList)
76                 .flatMap(n -> getNodesStream(n.getChildNodes()))
77                 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_VER))
78                 .findFirst()
79                 .map(n -> n.getChildNodes().item(1).getTextContent())
80                 .orElse(null);
81         // @formatter:on
82
83         try {
84             // Build the model artifact
85             ModelArtifact model = new ModelArtifact();
86             model.setModelInvariantId(modelInvariantId);
87             model.setModelVerId(modelId);
88             model.setPayload(readFile(MODEL_FILE));
89             model.setModelNamespace("http://org.openecomp.aai.inventory/v9");
90
91             AaiRestClient aaiClient = new AaiRestClient(config);
92
93             // GET model
94             OperationResult opResult =
95                     aaiClient.getResource(getUrl(model, config), "example-trans-id-0", MediaType.APPLICATION_XML_TYPE);
96             assertEquals(opResult.getResultCode(), Response.Status.NOT_FOUND.getStatusCode());
97
98             // PUT the model
99             opResult = aaiClient.putResource(getUrl(model, config), model.getPayload(), "example-trans-id-1",
100                     MediaType.APPLICATION_XML_TYPE);
101             assertEquals(opResult.getResultCode(), Response.Status.CREATED.getStatusCode());
102
103             // DELETE the model
104             opResult = aaiClient.getAndDeleteResource(getUrl(model, config), "example-trans-id-3");
105             assertEquals(opResult.getResultCode(), Response.Status.NO_CONTENT.getStatusCode());
106         } catch (Exception e) {
107             e.printStackTrace();
108         }
109     }
110
111     private Stream<Node> getNodesStream(NodeList nodeList) {
112         return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item);
113     }
114
115     static String readFile(String path) throws IOException {
116         byte[] encoded = Files.readAllBytes(Paths.get(path));
117         return new String(encoded);
118     }
119
120     private String getUrl(ModelArtifact model, ModelLoaderConfig config) {
121         String subUrl;
122         if (model.getType().equals(ArtifactType.MODEL)) {
123             subUrl = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim();
124         } else {
125             subUrl = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim();
126         }
127
128         String baseUrl = config.getAaiBaseUrl().trim();
129         if (!baseUrl.endsWith("/") && !subUrl.startsWith("/")) {
130             baseUrl = baseUrl + "/";
131         }
132
133         if (baseUrl.endsWith("/") && subUrl.startsWith("/")) {
134             baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
135         }
136
137         if (!subUrl.endsWith("/")) {
138             subUrl = subUrl + "/";
139         }
140
141         return baseUrl + subUrl + model.getModelInvariantId();
142     }
143 }