2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * 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=========================================================
21 package org.onap.aai.rest;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import com.jayway.jsonpath.JsonPath;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.janusgraph.core.JanusGraphTransaction;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.onap.aai.AAISetup;
34 import org.onap.aai.HttpTestUtil;
35 import org.onap.aai.PayloadUtil;
36 import org.onap.aai.dbmap.AAIGraph;
37 import org.onap.aai.introspection.Introspector;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.ModelType;
40 import org.onap.aai.serialization.engines.QueryStyle;
41 import org.skyscreamer.jsonassert.JSONAssert;
42 import org.springframework.test.annotation.DirtiesContext;
44 import javax.ws.rs.core.Response;
45 import java.util.Arrays;
46 import java.util.Collection;
47 import java.util.HashMap;
50 import static junit.framework.TestCase.fail;
51 import static org.hamcrest.CoreMatchers.containsString;
52 import static org.hamcrest.MatcherAssert.assertThat;
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertNotNull;
56 @RunWith(value = Parameterized.class)
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 public class PserverTest extends AAISetup {
60 private static Logger logger = LoggerFactory.getLogger(PserverTest.class);
61 private HttpTestUtil httpTestUtil;
62 private Map<String, String> relationshipMap;
64 @Parameterized.Parameter(value = 0)
65 public QueryStyle queryStyle;
67 @Parameterized.Parameters(name = "QueryStyle.{0}")
68 public static Collection<Object[]> data() {
69 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
74 httpTestUtil = new HttpTestUtil(queryStyle);
75 relationshipMap = new HashMap<>();
79 public void testPutPserverCreateGetInXmlForFormats() throws Exception {
80 httpTestUtil = new HttpTestUtil(queryStyle, "application/xml");
81 String pserverUri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver-xml";
82 String cloudRegionUri = "/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/cloud-region-random1/cloud-region-random1-region";
84 Response response = httpTestUtil.doGet(pserverUri);
85 assertNotNull("Expected the response to be not null", response);
86 assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
88 response = httpTestUtil.doPut(pserverUri, "{}");
89 assertNotNull("Expected the response to be not null", response);
90 assertEquals("Expecting the pserver to be created", 201, response.getStatus());
92 response = httpTestUtil.doPut(cloudRegionUri, "{}");
93 assertNotNull("Expected the response to be not null", response);
94 assertEquals("Expecting the cloud-region to be created", 201, response.getStatus());
96 relationshipMap.put("related-to", "pserver");
97 relationshipMap.put("related-link", pserverUri);
99 String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
100 // Creates the relationship between cloud region and pserver
101 response = httpTestUtil.doPut(cloudRegionUri + "/relationship-list/relationship", pserverRelationshipPayload);
102 assertNotNull("Expected the response to be not null", response);
103 assertEquals("Expecting the cloud-region to pserver relationship to be created", 200, response.getStatus());
105 response = httpTestUtil.doGet(pserverUri , "0", "raw");
106 assertNotNull("Expected the response to be not null", response);
107 assertEquals("Expecting the pserver to be created", 200, response.getStatus());
108 assertThat(response.getEntity().toString(), containsString("<related-to><node><relationship-label>org.onap.relationships.inventory.LocatedIn</relationship-label><node-type>cloud-region</node-type>"));
112 public void testPutPServerCreateGetAndDeleteAndCreateRelationshipBetweenPserverAndCloudRegion() throws Exception {
114 logger.info("Starting the pserver testPutServerCreateGetAndDelete");
116 String pserverUri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver";
117 String cloudRegionUri = "/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/test1/test2";
118 String cloudRegionRelationshipUri = cloudRegionUri + "/relationship-list/relationship";
120 Response response = httpTestUtil.doGet(pserverUri);
121 assertNotNull("Expected the response to be not null", response);
122 assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
123 logger.info("Verifying that the pserver is already not in the database successfully");
125 Map<String, String> templateValueMap = new HashMap<>();
126 templateValueMap.put("hostname", "test-pserver");
127 String pserverPayload = PayloadUtil.getTemplatePayload("pserver.json", templateValueMap);
129 response = httpTestUtil.doPut(pserverUri, pserverPayload);
130 assertNotNull("Expected the response to be not null", response);
131 assertEquals("Expecting the pserver to be created", 201, response.getStatus());
132 logger.info("Successfully created the pserver into db");
134 response = httpTestUtil.doGet(pserverUri);
135 assertNotNull("Expected the response to be not null", response);
136 assertEquals("Expecting the pserver to be found", 200, response.getStatus());
138 JSONAssert.assertEquals(pserverPayload, response.getEntity().toString(), false);
139 logger.info("Successfully retrieved the created pserver from db and verified with put data");
141 response = httpTestUtil.doPut(cloudRegionUri, "{}");
142 assertNotNull("Expected the response to be not null", response);
143 assertEquals("Expect the cloud region to be created", 201, response.getStatus());
145 "Successfully able to create the cloud region with payload that has no keys to be retrieved from uri");
147 response = httpTestUtil.doGet(cloudRegionUri);
148 assertNotNull("Expected the response to be not null", response);
149 assertEquals("Expecting the cloud region to be found", 200, response.getStatus());
150 logger.info("Successfully retrieved the cloud region from db");
152 relationshipMap.put("related-to", "pserver");
153 relationshipMap.put("related-link", pserverUri);
155 String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
156 // Creates the relationship between cloud region and pserver
157 response = httpTestUtil.doPut(cloudRegionRelationshipUri, pserverRelationshipPayload);
158 assertNotNull("Expected the response to be not null", response);
159 assertEquals("Expect the cloud region relationship to pserver to be created", 200, response.getStatus());
160 logger.info("Successfully created the relationship between cloud region and pserver");
162 response = httpTestUtil.doGet(cloudRegionUri);
163 assertNotNull("Expected the response to be not null", response);
164 assertEquals("Expect the cloud region to be created", 200, response.getStatus());
165 logger.info("Successfully retrieved the cloud region from db");
167 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
168 Introspector in = loader.unmarshal("cloud-region", response.getEntity().toString());
170 String resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
172 response = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
173 assertNotNull("Expected the response to be not null", response);
174 assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
175 logger.info("Successfully deleted the cloud region from db");
177 response = httpTestUtil.doGet(pserverUri);
178 assertNotNull("Expected the response to be not null", response);
179 assertEquals("Expecting the pserver to be not found", 200, response.getStatus());
180 resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
181 logger.info("Successfully retrieved the cloud region from db to get the latest resource version");
183 response = httpTestUtil.doDelete(pserverUri, resourceVersion);
184 assertNotNull("Expected the response to be not null", response);
185 assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
186 logger.info("Successfully deleted the pserver from db");
188 logger.info("Ending the pserver testPutServerCreateGetAndDelete");
192 public void tearDown() {
194 JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
195 boolean success = true;
199 GraphTraversalSource g = transaction.traversal();
201 g.V().has("source-of-truth", "JUNIT").toList().forEach(v -> v.remove());
203 } catch (Exception ex) {
205 logger.error("Unable to remove the vertexes", ex);
208 transaction.commit();
210 transaction.rollback();
211 fail("Unable to teardown the graph");