Implement support for v10 model entities.
[aai/model-loader.git] / src / test / java / org / openecomp / modelloader / restclient / AaiRestClientTest.java
diff --git a/src/test/java/org/openecomp/modelloader/restclient/AaiRestClientTest.java b/src/test/java/org/openecomp/modelloader/restclient/AaiRestClientTest.java
new file mode 100644 (file)
index 0000000..dc6f9d6
--- /dev/null
@@ -0,0 +1,121 @@
+/**\r
+ * ============LICENSE_START=======================================================\r
+ * Model Loader\r
+ * ================================================================================\r
+ * Copyright © 2017 AT&T Intellectual Property.\r
+ * Copyright © 2017 Amdocs\r
+ * All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ *\r
+ * ECOMP and OpenECOMP are trademarks\r
+ * and service marks of AT&T Intellectual Property.\r
+ */\r
+package org.openecomp.modelloader.restclient;\r
+\r
+import java.io.IOException;\r
+import java.nio.file.Files;\r
+import java.nio.file.Paths;\r
+\r
+import org.openecomp.modelloader.config.ModelLoaderConfig;\r
+import org.openecomp.modelloader.entity.ArtifactType;\r
+import org.openecomp.modelloader.entity.model.ModelArtifact;\r
+\r
+public class AaiRestClientTest {\r
+\r
+  // This test requires a running A&AI system. Uncomment to test locally.\r
+  /*\r
+   * @Test public void testRestClient() throws Exception { final String\r
+   * MODEL_FILE = "src/test/resources/models/vnf-model.xml";\r
+   * \r
+   * Properties props = new Properties();\r
+   * props.setProperty("ml.distribution.ARTIFACT_TYPES",\r
+   * "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");\r
+   * props.setProperty("ml.aai.BASE_URL", "https://127.0.0.1:4321");\r
+   * props.setProperty("ml.aai.MODEL_URL",\r
+   * "/aai/v8/service-design-and-creation/models/model/");\r
+   * props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12");\r
+   * props.setProperty("ml.aai.KEYSTORE_PASSWORD",\r
+   * "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o");\r
+   * \r
+   * ModelLoaderConfig config = new ModelLoaderConfig(props, "");\r
+   * \r
+   * String payload = readFile(MODEL_FILE); System.out.println("FILE:" +\r
+   * payload);\r
+   * \r
+   * File xmlFile = new File(MODEL_FILE); DocumentBuilderFactory dbFactory =\r
+   * DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder =\r
+   * dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile);\r
+   * \r
+   * // Get the ID of the model String modelId = null; NodeList nodeList =\r
+   * doc.getDocumentElement().getChildNodes(); for (int i = 0; i <\r
+   * nodeList.getLength(); i++) { Node currentNode = nodeList.item(i); if\r
+   * (currentNode.getNodeName().equals("model-name-version-id")) { modelId =\r
+   * currentNode.getTextContent(); break; } }\r
+   * \r
+   * // Add the model try { ModelArtifact model = new ModelArtifact();\r
+   * model.setNameVersionId(modelId); model.setType(ArtifactType.MODEL);\r
+   * model.setPayload(payload);\r
+   * \r
+   * AAIRestClient aaiClient = new AAIRestClient(config);\r
+   * \r
+   * // GET model System.out.println("Calling GET API ..."); ClientResponse\r
+   * getResponse = aaiClient.getResource(getURL(model, config),\r
+   * "example-trans-id-0", AAIRestClient.MimeType.XML); System.out.println(\r
+   * "GET result: " + getResponse.getStatus());\r
+   * assertTrue(getResponse.getStatus() ==\r
+   * Response.Status.NOT_FOUND.getStatusCode());\r
+   * \r
+   * // Add the model System.out.println("Calling PUT API ..."); ClientResponse\r
+   * res = aaiClient.putResource(getURL(model, config), model.getPayload(),\r
+   * "example-trans-id-1", AAIRestClient.MimeType.XML); System.out.println(\r
+   * "PUT result: " + res.getStatus()); assertTrue(res.getStatus() ==\r
+   * Response.Status.CREATED.getStatusCode());\r
+   * \r
+   * // Delete the model System.out.println("Calling DELETE API ..."); res =\r
+   * aaiClient.getAndDeleteResource(getURL(model, config),\r
+   * "example-trans-id-3"); System.out.println("DELETE result: " +\r
+   * res.getStatus()); assertTrue(res.getStatus() ==\r
+   * Response.Status.NO_CONTENT.getStatusCode()); } catch (Exception e) {\r
+   * e.printStackTrace(); } }\r
+   */\r
+\r
+  static String readFile(String path) throws IOException {\r
+    byte[] encoded = Files.readAllBytes(Paths.get(path));\r
+    return new String(encoded);\r
+  }\r
+\r
+  private String getURL(ModelArtifact model, ModelLoaderConfig config) {\r
+    String baseURL = config.getAaiBaseUrl().trim();\r
+    String subURL = null;\r
+    if (model.getType().equals(ArtifactType.MODEL)) {\r
+      subURL = config.getAaiModelUrl().trim();\r
+    } else {\r
+      subURL = config.getAaiNamedQueryUrl().trim();\r
+    }\r
+\r
+    if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {\r
+      baseURL = baseURL + "/";\r
+    }\r
+\r
+    if (baseURL.endsWith("/") && subURL.startsWith("/")) {\r
+      baseURL = baseURL.substring(0, baseURL.length() - 1);\r
+    }\r
+\r
+    if (!subURL.endsWith("/")) {\r
+      subURL = subURL + "/";\r
+    }\r
+\r
+    String url = baseURL + subURL + model.getNameVersionId();\r
+    return url;\r
+  }\r
+}\r