8ede3c3233083c6da253e141466e245436483d19
[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 org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.jayway.jsonpath.JsonPath;
29
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import javax.ws.rs.core.Response;
36
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.junit.runners.Parameterized;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.HttpTestUtil;
43 import org.onap.aai.PayloadUtil;
44 import org.onap.aai.introspection.*;
45 import org.onap.aai.serialization.engines.QueryStyle;
46 import org.skyscreamer.jsonassert.JSONAssert;
47 import org.springframework.test.annotation.DirtiesContext;
48
49 @RunWith(value = Parameterized.class)
50 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
51 public class PserverTest extends AAISetup {
52
53     private static EELFLogger logger = EELFManager.getInstance().getLogger(PserverTest.class);
54     private HttpTestUtil httpTestUtil;
55     private Map<String, String> relationshipMap;
56
57     @Parameterized.Parameter(value = 0)
58     public QueryStyle queryStyle;
59
60     @Parameterized.Parameters(name = "QueryStyle.{0}")
61     public static Collection<Object[]> data() {
62         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
63     }
64
65     @Before
66     public void setUp() {
67         httpTestUtil = new HttpTestUtil(queryStyle);
68         relationshipMap = new HashMap<>();
69     }
70
71     @Test
72     public void testPutPServerCreateGetAndDeleteAndCreateRelationshipBetweenPserverAndCloudRegion() throws Exception {
73
74         logger.info("Starting the pserver testPutServerCreateGetAndDelete");
75
76         String pserverUri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver";
77         String cloudRegionUri = "/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/test1/test2";
78         String cloudRegionRelationshipUri = cloudRegionUri + "/relationship-list/relationship";
79
80         Response response = httpTestUtil.doGet(pserverUri);
81         assertNotNull("Expected the response to be not null", response);
82         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
83         logger.info("Verifying that the pserver is already not in the database successfully");
84
85         Map<String, String> templateValueMap = new HashMap<>();
86         templateValueMap.put("hostname", "test-pserver");
87         String pserverPayload = PayloadUtil.getTemplatePayload("pserver.json", templateValueMap);
88
89         response = httpTestUtil.doPut(pserverUri, pserverPayload);
90         assertNotNull("Expected the response to be not null", response);
91         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
92         logger.info("Successfully created the pserver into db");
93
94         response = httpTestUtil.doGet(pserverUri);
95         assertNotNull("Expected the response to be not null", response);
96         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
97
98         JSONAssert.assertEquals(pserverPayload, response.getEntity().toString(), false);
99         logger.info("Successfully retrieved the created pserver from db and verified with put data");
100
101         response = httpTestUtil.doPut(cloudRegionUri, "{}");
102         assertNotNull("Expected the response to be not null", response);
103         assertEquals("Expect the cloud region to be created", 201, response.getStatus());
104         logger.info(
105                 "Successfully able to create the cloud region with payload that has no keys to be retrieved from uri");
106
107         response = httpTestUtil.doGet(cloudRegionUri);
108         assertNotNull("Expected the response to be not null", response);
109         assertEquals("Expecting the cloud region to be found", 200, response.getStatus());
110         logger.info("Successfully retrieved the cloud region from db");
111
112         relationshipMap.put("related-to", "pserver");
113         relationshipMap.put("related-link", pserverUri);
114
115         String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
116         // Creates the relationship between cloud region and pserver
117         response = httpTestUtil.doPut(cloudRegionRelationshipUri, pserverRelationshipPayload);
118         assertNotNull("Expected the response to be not null", response);
119         assertEquals("Expect the cloud region relationship to pserver to be created", 200, response.getStatus());
120         logger.info("Successfully created the relationship between cloud region and pserver");
121
122         response = httpTestUtil.doGet(cloudRegionUri);
123         assertNotNull("Expected the response to be not null", response);
124         assertEquals("Expect the cloud region to be created", 200, response.getStatus());
125         logger.info("Successfully retrieved the cloud region from db");
126
127         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
128         Introspector in = loader.unmarshal("cloud-region", response.getEntity().toString());
129
130         String resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
131
132         response = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
133         assertNotNull("Expected the response to be not null", response);
134         assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
135         logger.info("Successfully deleted the cloud region from db");
136
137         response = httpTestUtil.doGet(pserverUri);
138         assertNotNull("Expected the response to be not null", response);
139         assertEquals("Expecting the pserver to be not found", 200, response.getStatus());
140         resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
141         logger.info("Successfully retrieved the cloud region from db to get the latest resource version");
142
143         response = httpTestUtil.doDelete(pserverUri, resourceVersion);
144         assertNotNull("Expected the response to be not null", response);
145         assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
146         logger.info("Successfully deleted the pserver from db");
147
148         logger.info("Ending the pserver testPutServerCreateGetAndDelete");
149     }
150
151 }