Update nexus url from openecomp to onap
[aai/model-loader.git] / src / test / java / org / openecomp / modelloader / restclient / AaiRestClientTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * Model Loader\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
19  *\r
20  * ECOMP and OpenECOMP are trademarks\r
21  * and service marks of AT&T Intellectual Property.\r
22  */\r
23 package org.openecomp.modelloader.restclient;\r
24 \r
25 import java.io.IOException;\r
26 import java.nio.file.Files;\r
27 import java.nio.file.Paths;\r
28 \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
32 \r
33 public class AaiRestClientTest {\r
34 \r
35   // This test requires a running A&AI system. Uncomment to test locally.\r
36   /*\r
37    * @Test public void testRestClient() throws Exception { final String\r
38    * MODEL_FILE = "src/test/resources/models/vnf-model.xml";\r
39    * \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
49    * \r
50    * ModelLoaderConfig config = new ModelLoaderConfig(props, "");\r
51    * \r
52    * String payload = readFile(MODEL_FILE); System.out.println("FILE:" +\r
53    * payload);\r
54    * \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
58    * \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
64    * \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
68    * \r
69    * AAIRestClient aaiClient = new AAIRestClient(config);\r
70    * \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
77    * \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
83    * \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
90    */\r
91 \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
95   }\r
96 \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
102     } else {\r
103       subURL = config.getAaiNamedQueryUrl().trim();\r
104     }\r
105 \r
106     if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {\r
107       baseURL = baseURL + "/";\r
108     }\r
109 \r
110     if (baseURL.endsWith("/") && subURL.startsWith("/")) {\r
111       baseURL = baseURL.substring(0, baseURL.length() - 1);\r
112     }\r
113 \r
114     if (!subURL.endsWith("/")) {\r
115       subURL = subURL + "/";\r
116     }\r
117 \r
118     String url = baseURL + subURL + model.getNameVersionId();\r
119     return url;\r
120   }\r
121 }\r