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