Initial OpenECOMP A&AI Model Loader commit
[aai/model-loader.git] / src / test / java / org / openecomp / modelloader / restclient / AAIRestClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * MODEL LOADER SERVICE
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.modelloader.restclient;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26
27 import org.openecomp.modelloader.config.ModelLoaderConfig;
28 import org.openecomp.modelloader.entity.ArtifactType;
29 import org.openecomp.modelloader.entity.model.ModelArtifact;
30
31 public class AAIRestClientTest {
32
33   // This test requires a running A&AI system. Uncomment to test locally.
34   /*
35    * @Test public void testRestClient() throws Exception { final String
36    * MODEL_FILE = "src/test/resources/models/vnf-model.xml";
37    * 
38    * Properties props = new Properties();
39    * props.setProperty("ml.distribution.ARTIFACT_TYPES",
40    * "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");
41    * props.setProperty("ml.aai.BASE_URL", "https://127.0.0.1:4321");
42    * props.setProperty("ml.aai.MODEL_URL",
43    * "/aai/v8/service-design-and-creation/models/model/");
44    * props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12");
45    * props.setProperty("ml.aai.KEYSTORE_PASSWORD",
46    * "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o");
47    * 
48    * ModelLoaderConfig config = new ModelLoaderConfig(props, "");
49    * 
50    * String payload = readFile(MODEL_FILE); System.out.println("FILE:" +
51    * payload);
52    * 
53    * File xmlFile = new File(MODEL_FILE); DocumentBuilderFactory dbFactory =
54    * DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder =
55    * dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile);
56    * 
57    * // Get the ID of the model String modelId = null; NodeList nodeList =
58    * doc.getDocumentElement().getChildNodes(); for (int i = 0; i <
59    * nodeList.getLength(); i++) { Node currentNode = nodeList.item(i); if
60    * (currentNode.getNodeName().equals("model-name-version-id")) { modelId =
61    * currentNode.getTextContent(); break; } }
62    * 
63    * // Add the model try { ModelArtifact model = new ModelArtifact();
64    * model.setNameVersionId(modelId); model.setType(ArtifactType.MODEL);
65    * model.setPayload(payload);
66    * 
67    * AAIRestClient aaiClient = new AAIRestClient(config);
68    * 
69    * // GET model System.out.println("Calling GET API ..."); ClientResponse
70    * getResponse = aaiClient.getResource(getURL(model, config),
71    * "example-trans-id-0", AAIRestClient.MimeType.XML); System.out.println(
72    * "GET result: " + getResponse.getStatus());
73    * assertTrue(getResponse.getStatus() ==
74    * Response.Status.NOT_FOUND.getStatusCode());
75    * 
76    * // Add the model System.out.println("Calling PUT API ..."); ClientResponse
77    * res = aaiClient.putResource(getURL(model, config), model.getPayload(),
78    * "example-trans-id-1", AAIRestClient.MimeType.XML); System.out.println(
79    * "PUT result: " + res.getStatus()); assertTrue(res.getStatus() ==
80    * Response.Status.CREATED.getStatusCode());
81    * 
82    * // Delete the model System.out.println("Calling DELETE API ..."); res =
83    * aaiClient.getAndDeleteResource(getURL(model, config),
84    * "example-trans-id-3"); System.out.println("DELETE result: " +
85    * res.getStatus()); assertTrue(res.getStatus() ==
86    * Response.Status.NO_CONTENT.getStatusCode()); } catch (Exception e) {
87    * e.printStackTrace(); } }
88    */
89
90   static String readFile(String path) throws IOException {
91     byte[] encoded = Files.readAllBytes(Paths.get(path));
92     return new String(encoded);
93   }
94
95   private String getURL(ModelArtifact model, ModelLoaderConfig config) {
96     String baseURL = config.getAaiBaseUrl().trim();
97     String subURL = null;
98     if (model.getType().equals(ArtifactType.MODEL)) {
99       subURL = config.getAaiModelUrl().trim();
100     } else {
101       subURL = config.getAaiNamedQueryUrl().trim();
102     }
103
104     if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {
105       baseURL = baseURL + "/";
106     }
107
108     if (baseURL.endsWith("/") && subURL.startsWith("/")) {
109       baseURL = baseURL.substring(0, baseURL.length() - 1);
110     }
111
112     if (!subURL.endsWith("/")) {
113       subURL = subURL + "/";
114     }
115
116     String url = baseURL + subURL + model.getNameVersionId();
117     return url;
118   }
119 }