Revisions made to the Model Loader to use Babel
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / restclient / TestAaiRestClient.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  */
21 package org.onap.aai.modelloader.restclient;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.util.Properties;
30 import java.util.stream.IntStream;
31 import java.util.stream.Stream;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import javax.xml.parsers.DocumentBuilder;
35 import javax.xml.parsers.DocumentBuilderFactory;
36 import org.junit.Ignore;
37 import org.onap.aai.modelloader.config.ModelLoaderConfig;
38 import org.onap.aai.modelloader.entity.model.ModelArtifact;
39 import org.onap.aai.modelloader.entity.model.ModelArtifactParser;
40 import org.onap.aai.modelloader.restclient.AaiRestClient;
41 import org.onap.aai.modelloader.entity.ArtifactType;
42 import org.onap.aai.restclient.client.OperationResult;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Node;
45 import org.w3c.dom.NodeList;
46
47 public class TestAaiRestClient {
48
49     // This test requires a running A&AI system. To test locally, annotate with org.junit.Test
50     @Ignore
51     public void testRestClient() throws Exception {
52         final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";
53
54         Properties props = new Properties();
55         props.setProperty("ml.distribution.ARTIFACT_TYPES", "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");
56         props.setProperty("ml.aai.BASE_URL", "https://localhost:8443");
57         props.setProperty("ml.aai.MODEL_URL", "/aai/v9/service-design-and-creation/models/model/");
58         props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12");
59         props.setProperty("ml.aai.KEYSTORE_PASSWORD", "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o");
60
61         ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
62
63         File xmlFile = new File(MODEL_FILE);
64         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
65         dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
66         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
67         Document doc = dBuilder.parse(xmlFile);
68
69         NodeList nodesList = doc.getDocumentElement().getChildNodes();
70
71         // Get the model IDs
72
73         // @formatter:off
74         String modelInvariantId =
75                 getNodesStream(nodesList)
76                         .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_INVARIANT_ID))
77                         .findFirst()
78                         .map(Node::getTextContent)
79                         .orElse(null);
80
81         String modelId = getNodesStream(nodesList)
82                 .flatMap(n -> getNodesStream(n.getChildNodes()))
83                 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_VER))
84                 .findFirst()
85                 .map(n -> n.getChildNodes().item(1).getTextContent())
86                 .orElse(null);
87         // @formatter:on
88
89         try {
90             // Build the model artifact
91             ModelArtifact model = new ModelArtifact();
92             model.setModelInvariantId(modelInvariantId);
93             model.setModelVerId(modelId);
94             model.setPayload(readFile(MODEL_FILE));
95             model.setModelNamespace("http://org.openecomp.aai.inventory/v9");
96
97             AaiRestClient aaiClient = new AaiRestClient(config);
98
99             // GET model
100             OperationResult opResult =
101                     aaiClient.getResource(getURL(model, config), "example-trans-id-0", MediaType.APPLICATION_XML_TYPE);
102             assertTrue(opResult.getResultCode() == Response.Status.NOT_FOUND.getStatusCode());
103
104             // PUT the model
105             opResult = aaiClient.putResource(getURL(model, config), model.getPayload(), "example-trans-id-1",
106                     MediaType.APPLICATION_XML_TYPE);
107             assertTrue(opResult.getResultCode() == Response.Status.CREATED.getStatusCode());
108
109             // DELETE the model
110             opResult = aaiClient.getAndDeleteResource(getURL(model, config), "example-trans-id-3");
111             assertTrue(opResult.getResultCode() == Response.Status.NO_CONTENT.getStatusCode());
112         } catch (Exception e) {
113             e.printStackTrace();
114         }
115     }
116
117     private Stream<Node> getNodesStream(NodeList nodeList) {
118         return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item);
119     }
120
121     static String readFile(String path) throws IOException {
122         byte[] encoded = Files.readAllBytes(Paths.get(path));
123         return new String(encoded);
124     }
125
126     private String getURL(ModelArtifact model, ModelLoaderConfig config) {
127         String baseURL = config.getAaiBaseUrl().trim();
128         String subURL = null;
129         if (model.getType().equals(ArtifactType.MODEL)) {
130             subURL = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim();
131         } else {
132             subURL = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim();
133         }
134
135         if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {
136             baseURL = baseURL + "/";
137         }
138
139         if (baseURL.endsWith("/") && subURL.startsWith("/")) {
140             baseURL = baseURL.substring(0, baseURL.length() - 1);
141         }
142
143         if (!subURL.endsWith("/")) {
144             subURL = subURL + "/";
145         }
146
147         return baseURL + subURL + model.getModelInvariantId();
148     }
149 }