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