Allow handling of legact model artifacts
[aai/model-loader.git] / src / test / java / org / openecomp / modelloader / restclient / AaiRestClientTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Model Loader
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  * 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=========================================================
19  *
20  * ECOMP and OpenECOMP are trademarks
21  * and service marks of AT&T Intellectual Property.
22  */
23 package org.openecomp.modelloader.restclient;
24
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28
29 import org.openecomp.modelloader.config.ModelLoaderConfig;
30 import org.openecomp.modelloader.entity.ArtifactType;
31 import org.openecomp.modelloader.entity.model.ModelArtifact;
32
33 public class AaiRestClientTest {
34
35   // This test requires a running A&AI system. Uncomment to test locally.
36   /*
37    * @Test public void testRestClient() throws Exception { final String
38    * MODEL_FILE = "src/test/resources/models/vnf-model.xml";
39    * 
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");
49    * 
50    * ModelLoaderConfig config = new ModelLoaderConfig(props, "");
51    * 
52    * String payload = readFile(MODEL_FILE); System.out.println("FILE:" +
53    * payload);
54    * 
55    * File xmlFile = new File(MODEL_FILE); DocumentBuilderFactory dbFactory =
56    * DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder =
57    * dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile);
58    * 
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; } }
64    * 
65    * // Add the model try { ModelArtifact model = new ModelArtifact();
66    * model.setNameVersionId(modelId); model.setType(ArtifactType.MODEL);
67    * model.setPayload(payload);
68    * 
69    * AAIRestClient aaiClient = new AAIRestClient(config);
70    * 
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());
77    * 
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());
83    * 
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(); } }
90    */
91
92   static String readFile(String path) throws IOException {
93     byte[] encoded = Files.readAllBytes(Paths.get(path));
94     return new String(encoded);
95   }
96
97   private String getURL(ModelArtifact model, ModelLoaderConfig config) {
98     String baseURL = config.getAaiBaseUrl().trim();
99     String subURL = null;
100     if (model.getType().equals(ArtifactType.MODEL)) {
101       subURL = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim();
102     } else {
103       subURL = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim();
104     }
105
106     if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {
107       baseURL = baseURL + "/";
108     }
109
110     if (baseURL.endsWith("/") && subURL.startsWith("/")) {
111       baseURL = baseURL.substring(0, baseURL.length() - 1);
112     }
113
114     if (!subURL.endsWith("/")) {
115       subURL = subURL + "/";
116     }
117
118     String url = baseURL + subURL + model.getUniqueIdentifier();
119     return url;
120   }
121 }