Update the license for 2017-2018 license
[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.onap.aai.AAIJunitRunner;
29 import org.onap.aai.HttpTestUtil;
30 import org.onap.aai.PayloadUtil;
31 import org.onap.aai.introspection.*;
32 import org.skyscreamer.jsonassert.JSONAssert;
33
34 import javax.ws.rs.core.Response;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertNotNull;
40
41 @RunWith(AAIJunitRunner.class)
42 public class PserverTest {
43
44     private static EELFLogger logger = EELFManager.getInstance().getLogger(PserverTest.class);
45     private HttpTestUtil httpTestUtil;
46     private Map<String, String> relationshipMap;
47
48     @Before
49     public void setup(){
50         httpTestUtil = new HttpTestUtil();
51         relationshipMap = new HashMap<>();
52     }
53
54     @Test
55     public void testPutPServerCreateGetAndDeleteAndCreateRelationshipBetweenPserverAndCloudRegion() throws Exception {
56
57         logger.info("Starting the pserver testPutServerCreateGetAndDelete");
58
59         String pserverUri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver";
60         String cloudRegionUri = "/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/test1/test2";
61         String cloudRegionRelationshipUri = cloudRegionUri + "/relationship-list/relationship";
62
63         Response response = httpTestUtil.doGet(pserverUri);
64         assertNotNull("Expected the response to be not null", response);
65         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
66         logger.info("Verifying that the pserver is already not in the database successfully");
67
68         Map<String, String> templateValueMap = new HashMap<>();
69         templateValueMap.put("hostname", "test-pserver");
70         String pserverPayload = PayloadUtil.getTemplatePayload("pserver.json", templateValueMap);
71
72         response = httpTestUtil.doPut(pserverUri, pserverPayload);
73         assertNotNull("Expected the response to be not null", response);
74         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
75         logger.info("Successfully created the pserver into db");
76
77         response = httpTestUtil.doGet(pserverUri);
78         assertNotNull("Expected the response to be not null", response);
79         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
80
81         JSONAssert.assertEquals(pserverPayload, response.getEntity().toString(), false);
82         logger.info("Successfully retrieved the created pserver from db and verified with put data");
83
84         response = httpTestUtil.doPut(cloudRegionUri, "{}");
85         assertNotNull("Expected the response to be not null", response);
86         assertEquals("Expect the cloud region to be created", 201, response.getStatus());
87         logger.info("Successfully able to create the cloud region with payload that has no keys to be retrieved from uri");
88
89         response = httpTestUtil.doGet(cloudRegionUri);
90         assertNotNull("Expected the response to be not null", response);
91         assertEquals("Expecting the cloud region to be found", 200, response.getStatus());
92         logger.info("Successfully retrieved the cloud region from db");
93
94         relationshipMap.put("related-to", "pserver");
95         relationshipMap.put("related-link", pserverUri);
96
97         String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
98         // Creates the relationship between cloud region and pserver
99         response = httpTestUtil.doPut(cloudRegionRelationshipUri, pserverRelationshipPayload);
100         assertNotNull("Expected the response to be not null", response);
101         assertEquals("Expect the cloud region relationship to pserver to be created", 200, response.getStatus());
102         logger.info("Successfully created the relationship between cloud region and pserver");
103
104         response =  httpTestUtil.doGet(cloudRegionUri);
105         assertNotNull("Expected the response to be not null", response);
106         assertEquals("Expect the cloud region to be created", 200, response.getStatus());
107         logger.info("Successfully retrieved the cloud region from db");
108
109         Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.getLatest());
110         Introspector in = loader.unmarshal("cloud-region", response.getEntity().toString());
111
112         System.out.println(in.marshal(true));
113         String resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
114
115         response = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
116         assertNotNull("Expected the response to be not null", response);
117         assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
118         logger.info("Successfully deleted the cloud region from db");
119
120         response = httpTestUtil.doGet(pserverUri);
121         assertNotNull("Expected the response to be not null", response);
122         assertEquals("Expecting the pserver to be not found", 200, response.getStatus());
123         resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
124         logger.info("Successfully retrieved the cloud region from db to get the latest resource version");
125
126         response = httpTestUtil.doDelete(pserverUri, resourceVersion);
127         assertNotNull("Expected the response to be not null", response);
128         assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
129         logger.info("Successfully deleted the pserver from db");
130
131         logger.info("Ending the pserver testPutServerCreateGetAndDelete");
132     }
133
134 }