2a9b510672234eca2be2ec9c4cafe2f66afbf04f
[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 © 2018 Intel Corporation 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  * <p>
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * <p>
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.List;
37 import java.util.Map;
38 import java.util.UUID;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotNull;
42
43 @RunWith(AAIJunitRunner.class)
44 public class HPACapabilityTest {
45
46     private static EELFLogger logger = EELFManager.getInstance().getLogger(HPACapabilityTest.class);
47     private HttpTestUtil httpTestUtil;
48     private Map<String, String> templateValuesMap;
49
50     @Before
51     public void setup() {
52         httpTestUtil = new HttpTestUtil();
53         templateValuesMap = new HashMap<>();
54     }
55
56     @Test
57     public void testPutHPACapabilitiesInFlavorAndCheckIfDeleteIsSuccessful() throws Exception {
58
59         templateValuesMap.put("cloud-region-id", UUID.randomUUID().toString());
60         templateValuesMap.put("cloud-owner", UUID.randomUUID().toString());
61         templateValuesMap.put("tenant-id", UUID.randomUUID().toString());
62         templateValuesMap.put("vserver-id", UUID.randomUUID().toString());
63         templateValuesMap.put("flavor-id1", UUID.randomUUID().toString());
64         templateValuesMap.put("flavor-id2", UUID.randomUUID().toString());
65         templateValuesMap.put("hpa-capability-id1", UUID.randomUUID().toString());
66         templateValuesMap.put("hpa-capability-id2", UUID.randomUUID().toString());
67         templateValuesMap.put("hpa-capability-id3", UUID.randomUUID().toString());
68         templateValuesMap.put("hpa-capability-id4", UUID.randomUUID().toString());
69         templateValuesMap.put("hpa-capability-id5", UUID.randomUUID().toString());
70         templateValuesMap.put("hpa-capability-id6", UUID.randomUUID().toString());
71         templateValuesMap.put("hpa-capability-id7", UUID.randomUUID().toString());
72         templateValuesMap.put("hpa-capability-id8", UUID.randomUUID().toString());
73
74         String cloudRegionPayload = PayloadUtil.getTemplatePayload("hpa.json", templateValuesMap);
75         String cloudRegionUri = String.format("/aai/v13/cloud-infrastructure/cloud-regions/cloud-region/%s/%s",
76                 templateValuesMap.get("cloud-owner"),
77                 templateValuesMap.get("cloud-region-id")
78         );
79
80         Response response = httpTestUtil.doPut(cloudRegionUri, cloudRegionPayload);
81         assertEquals("Expected the cloud region to be created", 201, response.getStatus());
82
83         response = httpTestUtil.doGet(cloudRegionUri);
84         assertEquals("Expected the cloud region to be found", 200, response.getStatus());
85         String jsonResponse = response.getEntity().toString();
86         System.out.println("#########################jsonResponse#########################");
87         System.out.println(jsonResponse);
88         System.out.println("#########################jsonResponse#########################");
89
90         JSONAssert.assertEquals(cloudRegionPayload, jsonResponse, false);
91
92         deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id1"));
93         deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id2"));
94         deleteTenant(cloudRegionUri);
95     }
96
97     private void deleteTenant(String cloudRegionUri) throws Exception {
98         String tenantUri = cloudRegionUri + "/tenants/tenant/" + templateValuesMap.get("tenant-id");
99         deleteVserver(tenantUri);
100
101         Response tntResponse = httpTestUtil.doGet(tenantUri);
102         assertEquals("Expected to GET Tenant info from cloud-region", 200, tntResponse.getStatus());
103         String responseStr = tntResponse.getEntity().toString();
104
105         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
106
107         tntResponse = httpTestUtil.doDelete(tenantUri, resourceVersion);
108         assertEquals("Expected to DELETE Tenant info from cloud-region", 204, tntResponse.getStatus());
109     }
110
111     private void deleteVserver(String tenantUri) throws Exception {
112         String uri = tenantUri + "/vservers/vserver/" + templateValuesMap.get("vserver-id");
113
114         Response tntResponse = httpTestUtil.doGet(uri);
115         assertEquals("Expected to GET Vserver", 200, tntResponse.getStatus());
116         String responseStr = tntResponse.getEntity().toString();
117
118         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
119
120         tntResponse = httpTestUtil.doDelete(uri, resourceVersion);
121         assertEquals("Expected to DELETE Vserver", 204, tntResponse.getStatus());
122     }
123
124     private void deleteFlavor(String cloudRegionUri, String flavorId) throws Exception {
125         String flavorUri = cloudRegionUri + "/flavors/flavor/" + flavorId;
126
127         Response response = httpTestUtil.doGet(flavorUri);
128         assertEquals("Expected to GET Flavors info from cloud-region", 200, response.getStatus());
129         String jsonResponse = response.getEntity().toString();
130         System.out.println("#########################Flavor Response#########################");
131         System.out.println(jsonResponse);
132         System.out.println("#########################Flavor Response#########################");
133         String responseStr = response.getEntity().toString();
134
135         List<String> capabilityIds = JsonPath.read(responseStr,
136                 "$.hpa-capabilities.hpa-capability[*].hpa-capability-id");
137         for(String capabilityId : capabilityIds) {
138             deleteHPACapability(flavorUri, capabilityId);
139         }
140
141         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
142         response = httpTestUtil.doDelete(flavorUri, resourceVersion);
143         assertEquals("Expected to DELETE Flavor info from cloud-region", 204, response.getStatus());
144     }
145
146     private void deleteHPACapability(String flavorUri, String capabilityId) throws Exception {
147         String uri = flavorUri + "/hpa-capabilities/hpa-capability/" + capabilityId;
148
149         Response response = httpTestUtil.doGet(uri);
150         assertEquals("Expected to GET HPA info from flavors", 200, response.getStatus());
151         String jsonResponse = response.getEntity().toString();
152         System.out.println("#########################HPA Response#########################");
153         System.out.println(jsonResponse);
154         System.out.println("#########################HPA Response#########################");
155         String responseStr = response.getEntity().toString();
156
157         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
158
159         response = httpTestUtil.doDelete(uri, resourceVersion);
160         assertEquals("Expected to DELETE HPA info from flavors", 204, response.getStatus());
161     }
162 }