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