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