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