2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.modelloader.restclient;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
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;
45 public class TestAaiRestClient {
47 private static final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";
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/");
56 ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
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);
63 NodeList nodesList = doc.getDocumentElement().getChildNodes();
68 String modelInvariantId =
69 getNodesStream(nodesList)
70 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_INVARIANT_ID))
72 .map(Node::getTextContent)
75 String modelId = getNodesStream(nodesList)
76 .flatMap(n -> getNodesStream(n.getChildNodes()))
77 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_VER))
79 .map(n -> n.getChildNodes().item(1).getTextContent())
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");
91 AaiRestClient aaiClient = new AaiRestClient(config);
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());
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());
104 opResult = aaiClient.getAndDeleteResource(getUrl(model, config), "example-trans-id-3");
105 assertEquals(opResult.getResultCode(), Response.Status.NO_CONTENT.getStatusCode());
106 } catch (Exception e) {
111 private Stream<Node> getNodesStream(NodeList nodeList) {
112 return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item);
115 static String readFile(String path) throws IOException {
116 byte[] encoded = Files.readAllBytes(Paths.get(path));
117 return new String(encoded);
120 private String getUrl(ModelArtifact model, ModelLoaderConfig config) {
122 if (model.getType().equals(ArtifactType.MODEL)) {
123 subUrl = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim();
125 subUrl = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim();
128 String baseUrl = config.getAaiBaseUrl().trim();
129 if (!baseUrl.endsWith("/") && !subUrl.startsWith("/")) {
130 baseUrl = baseUrl + "/";
133 if (baseUrl.endsWith("/") && subUrl.startsWith("/")) {
134 baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
137 if (!subUrl.endsWith("/")) {
138 subUrl = subUrl + "/";
141 return baseUrl + subUrl + model.getModelInvariantId();