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.assertTrue;
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 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import javax.xml.parsers.DocumentBuilderFactory;
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.onap.aai.restclient.client.OperationResult;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Node;
42 import org.w3c.dom.NodeList;
44 public class TestAaiRestClient {
46 private static final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";
48 // This test requires a running A&AI system. To test locally, annotate with org.junit.Test
49 public void testRestClient() throws Exception {
50 Properties props = new Properties();
51 props.setProperty("ml.distribution.ARTIFACT_TYPES", "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");
52 props.setProperty("ml.aai.BASE_URL", "https://localhost:8443");
53 props.setProperty("ml.aai.MODEL_URL", "/aai/v9/service-design-and-creation/models/model/");
54 props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12");
55 props.setProperty("ml.aai.KEYSTORE_PASSWORD", "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o");
57 ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
59 File xmlFile = new File(MODEL_FILE);
60 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
61 dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
62 Document doc = dbFactory.newDocumentBuilder().parse(xmlFile);
64 NodeList nodesList = doc.getDocumentElement().getChildNodes();
69 String modelInvariantId =
70 getNodesStream(nodesList)
71 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_INVARIANT_ID))
73 .map(Node::getTextContent)
76 String modelId = getNodesStream(nodesList)
77 .flatMap(n -> getNodesStream(n.getChildNodes()))
78 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_VER))
80 .map(n -> n.getChildNodes().item(1).getTextContent())
85 // Build the model artifact
86 ModelArtifact model = new ModelArtifact();
87 model.setModelInvariantId(modelInvariantId);
88 model.setModelVerId(modelId);
89 model.setPayload(readFile(MODEL_FILE));
90 model.setModelNamespace("http://org.openecomp.aai.inventory/v9");
92 AaiRestClient aaiClient = new AaiRestClient(config);
95 OperationResult opResult =
96 aaiClient.getResource(getUrl(model, config), "example-trans-id-0", MediaType.APPLICATION_XML_TYPE);
97 assertTrue(opResult.getResultCode() == Response.Status.NOT_FOUND.getStatusCode());
100 opResult = aaiClient.putResource(getUrl(model, config), model.getPayload(), "example-trans-id-1",
101 MediaType.APPLICATION_XML_TYPE);
102 assertTrue(opResult.getResultCode() == Response.Status.CREATED.getStatusCode());
105 opResult = aaiClient.getAndDeleteResource(getUrl(model, config), "example-trans-id-3");
106 assertTrue(opResult.getResultCode() == Response.Status.NO_CONTENT.getStatusCode());
107 } catch (Exception e) {
112 private Stream<Node> getNodesStream(NodeList nodeList) {
113 return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item);
116 static String readFile(String path) throws IOException {
117 byte[] encoded = Files.readAllBytes(Paths.get(path));
118 return new String(encoded);
121 private String getUrl(ModelArtifact model, ModelLoaderConfig config) {
123 if (model.getType().equals(ArtifactType.MODEL)) {
124 subUrl = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim();
126 subUrl = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim();
129 String baseUrl = config.getAaiBaseUrl().trim();
130 if (!baseUrl.endsWith("/") && !subUrl.startsWith("/")) {
131 baseUrl = baseUrl + "/";
134 if (baseUrl.endsWith("/") && subUrl.startsWith("/")) {
135 baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
138 if (!subUrl.endsWith("/")) {
139 subUrl = subUrl + "/";
142 return baseUrl + subUrl + model.getModelInvariantId();