2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
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.DocumentBuilder;
35 import javax.xml.parsers.DocumentBuilderFactory;
36 import org.junit.Ignore;
37 import org.onap.aai.modelloader.config.ModelLoaderConfig;
38 import org.onap.aai.modelloader.entity.model.ModelArtifact;
39 import org.onap.aai.modelloader.entity.model.ModelArtifactParser;
40 import org.onap.aai.modelloader.restclient.AaiRestClient;
41 import org.onap.aai.modelloader.entity.ArtifactType;
42 import org.onap.aai.restclient.client.OperationResult;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Node;
45 import org.w3c.dom.NodeList;
47 public class TestAaiRestClient {
49 // This test requires a running A&AI system. To test locally, annotate with org.junit.Test
51 public void testRestClient() throws Exception {
52 final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";
54 Properties props = new Properties();
55 props.setProperty("ml.distribution.ARTIFACT_TYPES", "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");
56 props.setProperty("ml.aai.BASE_URL", "https://localhost:8443");
57 props.setProperty("ml.aai.MODEL_URL", "/aai/v9/service-design-and-creation/models/model/");
58 props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12");
59 props.setProperty("ml.aai.KEYSTORE_PASSWORD", "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o");
61 ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
63 File xmlFile = new File(MODEL_FILE);
64 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
65 dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
66 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
67 Document doc = dBuilder.parse(xmlFile);
69 NodeList nodesList = doc.getDocumentElement().getChildNodes();
74 String modelInvariantId =
75 getNodesStream(nodesList)
76 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_INVARIANT_ID))
78 .map(Node::getTextContent)
81 String modelId = getNodesStream(nodesList)
82 .flatMap(n -> getNodesStream(n.getChildNodes()))
83 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_VER))
85 .map(n -> n.getChildNodes().item(1).getTextContent())
90 // Build the model artifact
91 ModelArtifact model = new ModelArtifact();
92 model.setModelInvariantId(modelInvariantId);
93 model.setModelVerId(modelId);
94 model.setPayload(readFile(MODEL_FILE));
95 model.setModelNamespace("http://org.openecomp.aai.inventory/v9");
97 AaiRestClient aaiClient = new AaiRestClient(config);
100 OperationResult opResult =
101 aaiClient.getResource(getURL(model, config), "example-trans-id-0", MediaType.APPLICATION_XML_TYPE);
102 assertTrue(opResult.getResultCode() == Response.Status.NOT_FOUND.getStatusCode());
105 opResult = aaiClient.putResource(getURL(model, config), model.getPayload(), "example-trans-id-1",
106 MediaType.APPLICATION_XML_TYPE);
107 assertTrue(opResult.getResultCode() == Response.Status.CREATED.getStatusCode());
110 opResult = aaiClient.getAndDeleteResource(getURL(model, config), "example-trans-id-3");
111 assertTrue(opResult.getResultCode() == Response.Status.NO_CONTENT.getStatusCode());
112 } catch (Exception e) {
117 private Stream<Node> getNodesStream(NodeList nodeList) {
118 return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item);
121 static String readFile(String path) throws IOException {
122 byte[] encoded = Files.readAllBytes(Paths.get(path));
123 return new String(encoded);
126 private String getURL(ModelArtifact model, ModelLoaderConfig config) {
127 String baseURL = config.getAaiBaseUrl().trim();
128 String subURL = null;
129 if (model.getType().equals(ArtifactType.MODEL)) {
130 subURL = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim();
132 subURL = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim();
135 if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {
136 baseURL = baseURL + "/";
139 if (baseURL.endsWith("/") && subURL.startsWith("/")) {
140 baseURL = baseURL.substring(0, baseURL.length() - 1);
143 if (!subURL.endsWith("/")) {
144 subURL = subURL + "/";
147 return baseURL + subURL + model.getModelInvariantId();