Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / PserverTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.rest;
22
23 import static junit.framework.TestCase.fail;
24 import static org.hamcrest.CoreMatchers.containsString;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import com.jayway.jsonpath.JsonPath;
30
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.Map;
35
36 import javax.ws.rs.core.Response;
37
38 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
39 import org.janusgraph.core.JanusGraphTransaction;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.Parameterized;
45 import org.onap.aai.AAISetup;
46 import org.onap.aai.HttpTestUtil;
47 import org.onap.aai.PayloadUtil;
48 import org.onap.aai.dbmap.AAIGraph;
49 import org.onap.aai.introspection.Introspector;
50 import org.onap.aai.introspection.Loader;
51 import org.onap.aai.introspection.ModelType;
52 import org.onap.aai.serialization.engines.QueryStyle;
53 import org.skyscreamer.jsonassert.JSONAssert;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.test.annotation.DirtiesContext;
57
58 @RunWith(value = Parameterized.class)
59 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
60 public class PserverTest extends AAISetup {
61
62     private static Logger logger = LoggerFactory.getLogger(PserverTest.class);
63     private HttpTestUtil httpTestUtil;
64     private Map<String, String> relationshipMap;
65
66     @Parameterized.Parameter(value = 0)
67     public QueryStyle queryStyle;
68
69     @Parameterized.Parameters(name = "QueryStyle.{0}")
70     public static Collection<Object[]> data() {
71         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
72     }
73
74     @Before
75     public void setUp() {
76         httpTestUtil = new HttpTestUtil(queryStyle);
77         relationshipMap = new HashMap<>();
78     }
79
80     @Test
81     public void testPutPserverCreateGetInXmlForFormats() throws Exception {
82         httpTestUtil = new HttpTestUtil(queryStyle, "application/xml");
83         String pserverUri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver-xml";
84         String cloudRegionUri =
85                 "/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/cloud-region-random1/cloud-region-random1-region";
86
87         Response response = httpTestUtil.doGet(pserverUri);
88         assertNotNull("Expected the response to be not null", response);
89         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
90
91         response = httpTestUtil.doPut(pserverUri, "{}");
92         assertNotNull("Expected the response to be not null", response);
93         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
94
95         response = httpTestUtil.doPut(cloudRegionUri, "{}");
96         assertNotNull("Expected the response to be not null", response);
97         assertEquals("Expecting the cloud-region to be created", 201, response.getStatus());
98
99         relationshipMap.put("related-to", "pserver");
100         relationshipMap.put("related-link", pserverUri);
101
102         String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
103         // Creates the relationship between cloud region and pserver
104         response = httpTestUtil.doPut(cloudRegionUri + "/relationship-list/relationship", pserverRelationshipPayload);
105         assertNotNull("Expected the response to be not null", response);
106         assertEquals("Expecting the cloud-region to pserver relationship to be created", 200, response.getStatus());
107
108         response = httpTestUtil.doGet(pserverUri, "0", "raw");
109         assertNotNull("Expected the response to be not null", response);
110         assertEquals("Expecting the pserver to be created", 200, response.getStatus());
111         assertThat(response.getEntity().toString(), containsString(
112                 "<related-to><node><relationship-label>org.onap.relationships.inventory.LocatedIn</relationship-label><node-type>cloud-region</node-type>"));
113     }
114
115     @Test
116     public void testPutPServerCreateGetAndDeleteAndCreateRelationshipBetweenPserverAndCloudRegion() throws Exception {
117
118         logger.info("Starting the pserver testPutServerCreateGetAndDelete");
119
120         String pserverUri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver";
121         String cloudRegionUri = "/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/test1/test2";
122         String cloudRegionRelationshipUri = cloudRegionUri + "/relationship-list/relationship";
123
124         Response response = httpTestUtil.doGet(pserverUri);
125         assertNotNull("Expected the response to be not null", response);
126         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
127         logger.info("Verifying that the pserver is already not in the database successfully");
128
129         Map<String, String> templateValueMap = new HashMap<>();
130         templateValueMap.put("hostname", "test-pserver");
131         String pserverPayload = PayloadUtil.getTemplatePayload("pserver.json", templateValueMap);
132
133         response = httpTestUtil.doPut(pserverUri, pserverPayload);
134         assertNotNull("Expected the response to be not null", response);
135         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
136         logger.info("Successfully created the pserver into db");
137
138         response = httpTestUtil.doGet(pserverUri);
139         assertNotNull("Expected the response to be not null", response);
140         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
141
142         JSONAssert.assertEquals(pserverPayload, response.getEntity().toString(), false);
143         logger.info("Successfully retrieved the created pserver from db and verified with put data");
144
145         response = httpTestUtil.doPut(cloudRegionUri, "{}");
146         assertNotNull("Expected the response to be not null", response);
147         assertEquals("Expect the cloud region to be created", 201, response.getStatus());
148         logger.info(
149                 "Successfully able to create the cloud region with payload that has no keys to be retrieved from uri");
150
151         response = httpTestUtil.doGet(cloudRegionUri);
152         assertNotNull("Expected the response to be not null", response);
153         assertEquals("Expecting the cloud region to be found", 200, response.getStatus());
154         logger.info("Successfully retrieved the cloud region from db");
155
156         relationshipMap.put("related-to", "pserver");
157         relationshipMap.put("related-link", pserverUri);
158
159         String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
160         // Creates the relationship between cloud region and pserver
161         response = httpTestUtil.doPut(cloudRegionRelationshipUri, pserverRelationshipPayload);
162         assertNotNull("Expected the response to be not null", response);
163         assertEquals("Expect the cloud region relationship to pserver to be created", 200, response.getStatus());
164         logger.info("Successfully created the relationship between cloud region and pserver");
165
166         response = httpTestUtil.doGet(cloudRegionUri);
167         assertNotNull("Expected the response to be not null", response);
168         assertEquals("Expect the cloud region to be created", 200, response.getStatus());
169         logger.info("Successfully retrieved the cloud region from db");
170
171         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
172         Introspector in = loader.unmarshal("cloud-region", response.getEntity().toString());
173
174         String resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
175
176         response = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
177         assertNotNull("Expected the response to be not null", response);
178         assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
179         logger.info("Successfully deleted the cloud region from db");
180
181         response = httpTestUtil.doGet(pserverUri);
182         assertNotNull("Expected the response to be not null", response);
183         assertEquals("Expecting the pserver to be not found", 200, response.getStatus());
184         resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
185         logger.info("Successfully retrieved the cloud region from db to get the latest resource version");
186
187         response = httpTestUtil.doDelete(pserverUri, resourceVersion);
188         assertNotNull("Expected the response to be not null", response);
189         assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
190         logger.info("Successfully deleted the pserver from db");
191
192         logger.info("Ending the pserver testPutServerCreateGetAndDelete");
193     }
194
195     @After
196     public void tearDown() {
197
198         JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
199         boolean success = true;
200
201         try {
202
203             GraphTraversalSource g = transaction.traversal();
204
205             g.V().has("source-of-truth", "JUNIT").toList().forEach(v -> v.remove());
206
207         } catch (Exception ex) {
208             success = false;
209             logger.error("Unable to remove the vertexes", ex);
210         } finally {
211             if (success) {
212                 transaction.commit();
213             } else {
214                 transaction.rollback();
215                 fail("Unable to teardown the graph");
216             }
217         }
218
219     }
220 }