a742313aeebe1f246a4bd85e681e52e1f4da3651
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / HPACapabilityTest.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
21 package org.onap.aai.rest;
22
23 import static org.junit.Assert.assertEquals;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import com.jayway.jsonpath.JsonPath;
28
29 import java.util.*;
30
31 import javax.ws.rs.core.Response;
32
33 import org.junit.Before;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.Parameterized;
38 import org.onap.aai.AAIJunitRunner;
39 import org.onap.aai.HttpTestUtil;
40 import org.onap.aai.PayloadUtil;
41 import org.onap.aai.serialization.engines.QueryStyle;
42 import org.skyscreamer.jsonassert.JSONAssert;
43
44 @Ignore
45 @RunWith(AAIJunitRunner.class)
46 public class HPACapabilityTest {
47
48     private static EELFLogger logger = EELFManager.getInstance().getLogger(HPACapabilityTest.class);
49     private HttpTestUtil httpTestUtil;
50     private Map<String, String> templateValuesMap;
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[][] {{QueryStyle.TRAVERSAL}});
58     }
59
60     @Before
61     public void setup() {
62         httpTestUtil = new HttpTestUtil(queryStyle);
63         templateValuesMap = new HashMap<>();
64     }
65
66     @Test
67     public void testPutHPACapabilitiesInFlavorAndCheckIfDeleteIsSuccessful() throws Exception {
68
69         templateValuesMap.put("cloud-region-id", UUID.randomUUID().toString());
70         templateValuesMap.put("cloud-owner", UUID.randomUUID().toString());
71         templateValuesMap.put("tenant-id", UUID.randomUUID().toString());
72         templateValuesMap.put("vserver-id", UUID.randomUUID().toString());
73         templateValuesMap.put("flavor-id1", UUID.randomUUID().toString());
74         templateValuesMap.put("flavor-id2", UUID.randomUUID().toString());
75         templateValuesMap.put("hpa-capability-id1", UUID.randomUUID().toString());
76         templateValuesMap.put("hpa-capability-id2", UUID.randomUUID().toString());
77         templateValuesMap.put("hpa-capability-id3", UUID.randomUUID().toString());
78         templateValuesMap.put("hpa-capability-id4", UUID.randomUUID().toString());
79         templateValuesMap.put("hpa-capability-id5", UUID.randomUUID().toString());
80         templateValuesMap.put("hpa-capability-id6", UUID.randomUUID().toString());
81         templateValuesMap.put("hpa-capability-id7", UUID.randomUUID().toString());
82         templateValuesMap.put("hpa-capability-id8", UUID.randomUUID().toString());
83
84         String cloudRegionPayload = PayloadUtil.getTemplatePayload("hpa.json", templateValuesMap);
85         String cloudRegionUri = String.format("/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/%s/%s",
86                 templateValuesMap.get("cloud-owner"), templateValuesMap.get("cloud-region-id"));
87
88         Response response = httpTestUtil.doPut(cloudRegionUri, cloudRegionPayload);
89         assertEquals("Expected the cloud region to be created", 201, response.getStatus());
90
91         response = httpTestUtil.doGet(cloudRegionUri);
92         assertEquals("Expected the cloud region to be found", 200, response.getStatus());
93         String jsonResponse = response.getEntity().toString();
94         System.out.println("#########################jsonResponse#########################");
95         System.out.println(jsonResponse);
96         System.out.println("#########################jsonResponse#########################");
97
98         JSONAssert.assertEquals(cloudRegionPayload, jsonResponse, false);
99
100         deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id1"));
101         deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id2"));
102         deleteTenant(cloudRegionUri);
103     }
104
105     private void deleteTenant(String cloudRegionUri) throws Exception {
106         String tenantUri = cloudRegionUri + "/tenants/tenant/" + templateValuesMap.get("tenant-id");
107         deleteVserver(tenantUri);
108
109         Response tntResponse = httpTestUtil.doGet(tenantUri);
110         assertEquals("Expected to GET Tenant info from cloud-region", 200, tntResponse.getStatus());
111         String responseStr = tntResponse.getEntity().toString();
112
113         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
114
115         tntResponse = httpTestUtil.doDelete(tenantUri, resourceVersion);
116         assertEquals("Expected to DELETE Tenant info from cloud-region", 204, tntResponse.getStatus());
117     }
118
119     private void deleteVserver(String tenantUri) throws Exception {
120         String uri = tenantUri + "/vservers/vserver/" + templateValuesMap.get("vserver-id");
121
122         Response tntResponse = httpTestUtil.doGet(uri);
123         assertEquals("Expected to GET Vserver", 200, tntResponse.getStatus());
124         String responseStr = tntResponse.getEntity().toString();
125
126         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
127
128         tntResponse = httpTestUtil.doDelete(uri, resourceVersion);
129         assertEquals("Expected to DELETE Vserver", 204, tntResponse.getStatus());
130     }
131
132     private void deleteFlavor(String cloudRegionUri, String flavorId) throws Exception {
133         String flavorUri = cloudRegionUri + "/flavors/flavor/" + flavorId;
134
135         Response response = httpTestUtil.doGet(flavorUri);
136         assertEquals("Expected to GET Flavors info from cloud-region", 200, response.getStatus());
137         String jsonResponse = response.getEntity().toString();
138         System.out.println("#########################Flavor Response#########################");
139         System.out.println(jsonResponse);
140         System.out.println("#########################Flavor Response#########################");
141         String responseStr = response.getEntity().toString();
142
143         List<String> capabilityIds =
144                 JsonPath.read(responseStr, "$.hpa-capabilities.hpa-capability[*].hpa-capability-id");
145         for (String capabilityId : capabilityIds) {
146             deleteHPACapability(flavorUri, capabilityId);
147         }
148
149         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
150         response = httpTestUtil.doDelete(flavorUri, resourceVersion);
151         assertEquals("Expected to DELETE Flavor info from cloud-region", 204, response.getStatus());
152     }
153
154     private void deleteHPACapability(String flavorUri, String capabilityId) throws Exception {
155         String uri = flavorUri + "/hpa-capabilities/hpa-capability/" + capabilityId;
156
157         Response response = httpTestUtil.doGet(uri);
158         assertEquals("Expected to GET HPA info from flavors", 200, response.getStatus());
159         String jsonResponse = response.getEntity().toString();
160         System.out.println("#########################HPA Response#########################");
161         System.out.println(jsonResponse);
162         System.out.println("#########################HPA Response#########################");
163         String responseStr = response.getEntity().toString();
164
165         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
166
167         response = httpTestUtil.doDelete(uri, resourceVersion);
168         assertEquals("Expected to DELETE HPA info from flavors", 204, response.getStatus());
169     }
170 }