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