2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property.
\r
6 * Copyright © 2017 Amdocs
\r
7 * All rights reserved.
\r
8 * ================================================================================
\r
9 * Licensed under the Apache License, Version 2.0 (the "License");
\r
10 * you may not use this file except in compliance with the License.
\r
11 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
20 * ECOMP and OpenECOMP are trademarks
\r
21 * and service marks of AT&T Intellectual Property.
\r
23 package org.openecomp.modelloader.restclient;
\r
25 import java.io.IOException;
\r
26 import java.nio.file.Files;
\r
27 import java.nio.file.Paths;
\r
29 import org.openecomp.modelloader.config.ModelLoaderConfig;
\r
30 import org.openecomp.modelloader.entity.ArtifactType;
\r
31 import org.openecomp.modelloader.entity.model.ModelArtifact;
\r
33 public class AaiRestClientTest {
\r
35 // This test requires a running A&AI system. Uncomment to test locally.
\r
37 * @Test public void testRestClient() throws Exception { final String
\r
38 * MODEL_FILE = "src/test/resources/models/vnf-model.xml";
\r
40 * Properties props = new Properties();
\r
41 * props.setProperty("ml.distribution.ARTIFACT_TYPES",
\r
42 * "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");
\r
43 * props.setProperty("ml.aai.BASE_URL", "https://127.0.0.1:4321");
\r
44 * props.setProperty("ml.aai.MODEL_URL",
\r
45 * "/aai/v8/service-design-and-creation/models/model/");
\r
46 * props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12");
\r
47 * props.setProperty("ml.aai.KEYSTORE_PASSWORD",
\r
48 * "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o");
\r
50 * ModelLoaderConfig config = new ModelLoaderConfig(props, "");
\r
52 * String payload = readFile(MODEL_FILE); System.out.println("FILE:" +
\r
55 * File xmlFile = new File(MODEL_FILE); DocumentBuilderFactory dbFactory =
\r
56 * DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder =
\r
57 * dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile);
\r
59 * // Get the ID of the model String modelId = null; NodeList nodeList =
\r
60 * doc.getDocumentElement().getChildNodes(); for (int i = 0; i <
\r
61 * nodeList.getLength(); i++) { Node currentNode = nodeList.item(i); if
\r
62 * (currentNode.getNodeName().equals("model-name-version-id")) { modelId =
\r
63 * currentNode.getTextContent(); break; } }
\r
65 * // Add the model try { ModelArtifact model = new ModelArtifact();
\r
66 * model.setNameVersionId(modelId); model.setType(ArtifactType.MODEL);
\r
67 * model.setPayload(payload);
\r
69 * AAIRestClient aaiClient = new AAIRestClient(config);
\r
71 * // GET model System.out.println("Calling GET API ..."); ClientResponse
\r
72 * getResponse = aaiClient.getResource(getURL(model, config),
\r
73 * "example-trans-id-0", AAIRestClient.MimeType.XML); System.out.println(
\r
74 * "GET result: " + getResponse.getStatus());
\r
75 * assertTrue(getResponse.getStatus() ==
\r
76 * Response.Status.NOT_FOUND.getStatusCode());
\r
78 * // Add the model System.out.println("Calling PUT API ..."); ClientResponse
\r
79 * res = aaiClient.putResource(getURL(model, config), model.getPayload(),
\r
80 * "example-trans-id-1", AAIRestClient.MimeType.XML); System.out.println(
\r
81 * "PUT result: " + res.getStatus()); assertTrue(res.getStatus() ==
\r
82 * Response.Status.CREATED.getStatusCode());
\r
84 * // Delete the model System.out.println("Calling DELETE API ..."); res =
\r
85 * aaiClient.getAndDeleteResource(getURL(model, config),
\r
86 * "example-trans-id-3"); System.out.println("DELETE result: " +
\r
87 * res.getStatus()); assertTrue(res.getStatus() ==
\r
88 * Response.Status.NO_CONTENT.getStatusCode()); } catch (Exception e) {
\r
89 * e.printStackTrace(); } }
\r
92 static String readFile(String path) throws IOException {
\r
93 byte[] encoded = Files.readAllBytes(Paths.get(path));
\r
94 return new String(encoded);
\r
97 private String getURL(ModelArtifact model, ModelLoaderConfig config) {
\r
98 String baseURL = config.getAaiBaseUrl().trim();
\r
99 String subURL = null;
\r
100 if (model.getType().equals(ArtifactType.MODEL)) {
\r
101 subURL = config.getAaiModelUrl().trim();
\r
103 subURL = config.getAaiNamedQueryUrl().trim();
\r
106 if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {
\r
107 baseURL = baseURL + "/";
\r
110 if (baseURL.endsWith("/") && subURL.startsWith("/")) {
\r
111 baseURL = baseURL.substring(0, baseURL.length() - 1);
\r
114 if (!subURL.endsWith("/")) {
\r
115 subURL = subURL + "/";
\r
118 String url = baseURL + subURL + model.getNameVersionId();
\r