Remove unused system properties from unit tests
[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 European Software Marketing Ltd.
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.DocumentBuilderFactory;
35 import org.onap.aai.modelloader.config.ModelLoaderConfig;
36 import org.onap.aai.modelloader.entity.ArtifactType;
37 import org.onap.aai.modelloader.entity.model.ModelArtifact;
38 import org.onap.aai.modelloader.entity.model.ModelArtifactParser;
39 import org.onap.aai.restclient.client.OperationResult;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Node;
42 import org.w3c.dom.NodeList;
43
44 public class TestAaiRestClient {
45
46     private static final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml";
47
48     // This test requires a running A&AI system. To test locally, annotate with org.junit.Test
49     public void testRestClient() throws Exception {
50         Properties props = new Properties();
51         props.setProperty("ml.distribution.ARTIFACT_TYPES", "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG");
52         props.setProperty("ml.aai.BASE_URL", "https://localhost:8443");
53         props.setProperty("ml.aai.MODEL_URL", "/aai/v9/service-design-and-creation/models/model/");
54
55         ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
56
57         File xmlFile = new File(MODEL_FILE);
58         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
59         dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
60         Document doc = dbFactory.newDocumentBuilder().parse(xmlFile);
61
62         NodeList nodesList = doc.getDocumentElement().getChildNodes();
63
64         // Get the model IDs
65
66         // @formatter:off
67         String modelInvariantId =
68                 getNodesStream(nodesList)
69                         .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_INVARIANT_ID))
70                         .findFirst()
71                         .map(Node::getTextContent)
72                         .orElse(null);
73
74         String modelId = getNodesStream(nodesList)
75                 .flatMap(n -> getNodesStream(n.getChildNodes()))
76                 .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_VER))
77                 .findFirst()
78                 .map(n -> n.getChildNodes().item(1).getTextContent())
79                 .orElse(null);
80         // @formatter:on
81
82         try {
83             // Build the model artifact
84             ModelArtifact model = new ModelArtifact();
85             model.setModelInvariantId(modelInvariantId);
86             model.setModelVerId(modelId);
87             model.setPayload(readFile(MODEL_FILE));
88             model.setModelNamespace("http://org.openecomp.aai.inventory/v9");
89
90             AaiRestClient aaiClient = new AaiRestClient(config);
91
92             // GET model
93             OperationResult opResult =
94                     aaiClient.getResource(getUrl(model, config), "example-trans-id-0", MediaType.APPLICATION_XML_TYPE);
95             assertTrue(opResult.getResultCode() == Response.Status.NOT_FOUND.getStatusCode());
96
97             // PUT the model
98             opResult = aaiClient.putResource(getUrl(model, config), model.getPayload(), "example-trans-id-1",
99                     MediaType.APPLICATION_XML_TYPE);
100             assertTrue(opResult.getResultCode() == Response.Status.CREATED.getStatusCode());
101
102             // DELETE the model
103             opResult = aaiClient.getAndDeleteResource(getUrl(model, config), "example-trans-id-3");
104             assertTrue(opResult.getResultCode() == Response.Status.NO_CONTENT.getStatusCode());
105         } catch (Exception e) {
106             e.printStackTrace();
107         }
108     }
109
110     private Stream<Node> getNodesStream(NodeList nodeList) {
111         return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item);
112     }
113
114     static String readFile(String path) throws IOException {
115         byte[] encoded = Files.readAllBytes(Paths.get(path));
116         return new String(encoded);
117     }
118
119     private String getUrl(ModelArtifact model, ModelLoaderConfig config) {
120         String subUrl;
121         if (model.getType().equals(ArtifactType.MODEL)) {
122             subUrl = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim();
123         } else {
124             subUrl = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim();
125         }
126
127         String baseUrl = config.getAaiBaseUrl().trim();
128         if (!baseUrl.endsWith("/") && !subUrl.startsWith("/")) {
129             baseUrl = baseUrl + "/";
130         }
131
132         if (baseUrl.endsWith("/") && subUrl.startsWith("/")) {
133             baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
134         }
135
136         if (!subUrl.endsWith("/")) {
137             subUrl = subUrl + "/";
138         }
139
140         return baseUrl + subUrl + model.getModelInvariantId();
141     }
142 }