8ff56839edd352bd9d78271508bd3820376ce7ce
[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 package org.onap.aai.rest;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import com.jayway.jsonpath.JsonPath;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Parameterized;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.HttpTestUtil;
31 import org.onap.aai.PayloadUtil;
32 import org.onap.aai.introspection.*;
33
34 import org.onap.aai.serialization.engines.QueryStyle;
35 import org.skyscreamer.jsonassert.JSONAssert;
36
37 import javax.ws.rs.core.Response;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.HashMap;
41 import java.util.Map;
42
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertNotNull;
45
46 @RunWith(value = Parameterized.class)
47 public class PserverTest extends AAISetup{
48
49     private static EELFLogger logger = EELFManager.getInstance().getLogger(PserverTest.class);
50     private HttpTestUtil httpTestUtil;
51     private Map<String, String> relationshipMap;
52
53     @Parameterized.Parameter(value = 0)
54     public QueryStyle queryStyle;
55
56     @Parameterized.Parameters(name = "QueryStyle.{0}")
57     public static Collection<Object[]> data() {
58         return Arrays.asList(new Object[][]{
59                 {QueryStyle.TRAVERSAL},
60                 {QueryStyle.TRAVERSAL_URI}
61         });
62     }
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("Successfully able to create the cloud region with payload that has no keys to be retrieved from uri");
105
106         response = httpTestUtil.doGet(cloudRegionUri);
107         assertNotNull("Expected the response to be not null", response);
108         assertEquals("Expecting the cloud region to be found", 200, response.getStatus());
109         logger.info("Successfully retrieved the cloud region from db");
110
111         relationshipMap.put("related-to", "pserver");
112         relationshipMap.put("related-link", pserverUri);
113
114         String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
115         // Creates the relationship between cloud region and pserver
116         response = httpTestUtil.doPut(cloudRegionRelationshipUri, pserverRelationshipPayload);
117         assertNotNull("Expected the response to be not null", response);
118         assertEquals("Expect the cloud region relationship to pserver to be created", 200, response.getStatus());
119         logger.info("Successfully created the relationship between cloud region and pserver");
120
121         response =  httpTestUtil.doGet(cloudRegionUri);
122         assertNotNull("Expected the response to be not null", response);
123         assertEquals("Expect the cloud region to be created", 200, response.getStatus());
124         logger.info("Successfully retrieved the cloud region from db");
125
126         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
127         Introspector in = loader.unmarshal("cloud-region", response.getEntity().toString());
128
129         String resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
130
131         response = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
132         assertNotNull("Expected the response to be not null", response);
133         assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
134         logger.info("Successfully deleted the cloud region from db");
135
136         response = httpTestUtil.doGet(pserverUri);
137         assertNotNull("Expected the response to be not null", response);
138         assertEquals("Expecting the pserver to be not found", 200, response.getStatus());
139         resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
140         logger.info("Successfully retrieved the cloud region from db to get the latest resource version");
141
142         response = httpTestUtil.doDelete(pserverUri, resourceVersion);
143         assertNotNull("Expected the response to be not null", response);
144         assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
145         logger.info("Successfully deleted the pserver from db");
146
147         logger.info("Ending the pserver testPutServerCreateGetAndDelete");
148     }
149
150 }