[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / resource / GetResourceNotAbstractApiTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.sdc.ci.tests.execute.resource;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.apache.log4j.lf5.util.ResourceUtils;
34 import org.junit.Rule;
35 import org.junit.rules.TestName;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
38 import org.openecomp.sdc.ci.tests.api.Urls;
39 import org.openecomp.sdc.ci.tests.config.Config;
40 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
41 import org.openecomp.sdc.ci.tests.datatypes.enums.NormativeTypesEnum;
42 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
45 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
46 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
47 import org.openecomp.sdc.ci.tests.execute.imports.ImportGenericResourceCITest;
48 import org.openecomp.sdc.ci.tests.utils.Utils;
49 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
50 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.testng.annotations.Test;
54
55 import com.google.gson.JsonArray;
56 import com.google.gson.JsonElement;
57 import com.google.gson.JsonObject;
58 import com.google.gson.JsonParser;
59
60 public class GetResourceNotAbstractApiTest extends ComponentBaseTest {
61
62         private static Logger logger = LoggerFactory.getLogger(ComponentBaseTest.class.getName());
63         protected static final int STATUS_CODE_GET_SUCCESS = 200;
64
65         protected Config config = Config.instance();
66         protected String contentTypeHeaderData = "application/json";
67         protected String acceptHeaderDate = "application/json";
68
69         @Rule
70         public static TestName name = new TestName();
71
72         public GetResourceNotAbstractApiTest() {
73                 super(name, GetResourceNotAbstractApiTest.class.getName());
74         }
75
76         @Test
77         public void getNotAbstractResourceList() throws Exception {
78
79                 // remove all the not abstract resources
80                 // Map<NormativeTypes, Boolean> originalState =
81                 // ImportResourceCITest.removeAllNormativeTypeResources();
82
83                 // import all the default not abstract resources
84                 // ImportGenericResourceCITest.importAllNormativeTypesResources(UserRoleEnum.ADMIN);
85
86                 // Get not abstract resources
87                 RestResponse getResourceNotAbstarctResponse = getNotAbstractResources();
88                 // Check that received 200.
89                 assertEquals("Check response code after get abstract resources", STATUS_CODE_GET_SUCCESS,
90                                 getResourceNotAbstarctResponse.getErrorCode().intValue());
91                 // Verify that all the resources not abstract
92                 assertTrue("One or more resources are abstract", isAllResourcesNotAbstract(getResourceNotAbstarctResponse));
93                 // Verify that all the resources are certified
94                 assertTrue("Not all the resources are certified", isAllResourcesCertified(getResourceNotAbstarctResponse));
95
96                 String objectStorageUid = "ObjectStorage";
97                 String computeUid = "Compute";
98                 String blockStorageUid = "BlockStorage";
99                 String loadBalancerUid = "LoadBalancer";
100                 // String portUid = "tosca.nodes.Network.Port";
101                 String portUid = "Port";
102                 String networkUid = "Network";
103                 String databaseUid = "Database";
104
105                 // Compare expected list of abstract resources to actual list of
106                 // abstract resources.
107                 List<String> expectedNotAbstractResourcesUniqueIdArray = new ArrayList<String>(Arrays.asList(computeUid,
108                                 databaseUid, objectStorageUid, blockStorageUid, loadBalancerUid, portUid, networkUid));
109
110                 List<String> actualNotAbstarctResourcesUniqueIdArray = restResponseToListByHeader(
111                                 getResourceNotAbstarctResponse, "name");
112
113                 // Collections.sort(actualNotAbstarctResourcesUniqueIdArray);
114                 // Collections.sort(expectedNotAbstractResourcesUniqueIdArray);
115
116                 List<String> toFind = new ArrayList<>();
117                 toFind.add(objectStorageUid);
118                 toFind.add(computeUid);
119                 toFind.add(blockStorageUid);
120                 toFind.add(loadBalancerUid);
121                 toFind.add(portUid);
122
123                 boolean removeAll = toFind.removeAll(actualNotAbstarctResourcesUniqueIdArray);
124                 logger.debug("Cannot find resources {}", toFind.toString());
125
126                 for (String expectedResource : expectedNotAbstractResourcesUniqueIdArray) {
127                         if (false == actualNotAbstarctResourcesUniqueIdArray.contains(expectedResource)) {
128                                 // System.out.println("Not found abstract resource " +
129                                 // expectedResource);
130                         }
131                 }
132
133                 assertTrue(
134                                 "Expected abstract resources list: " + expectedNotAbstractResourcesUniqueIdArray.toString()
135                                                 + " Actual: " + actualNotAbstarctResourcesUniqueIdArray.toString(),
136                                 actualNotAbstarctResourcesUniqueIdArray.containsAll(expectedNotAbstractResourcesUniqueIdArray));
137
138                 /*
139                  * java.lang.AssertionError: Expected abstract resources list:
140                  * [tosca.nodes.Compute, tosca.nodes.ObjectStorage,
141                  * tosca.nodes.BlockStorage, tosca.nodes.LoadBalancer,
142                  * tosca.nodes.Network.Port] Actual: [resourceforproperty216,
143                  * tosca.nodes.Compute, tosca.nodes.Database, resourceforproperty217,
144                  * resourceforproperty217, tosca.nodes.ObjectStorage,
145                  * tosca.nodes.BlockStorage, tosca.nodes.LoadBalancer,
146                  * tosca.nodes.network.Port, tosca.nodes.network.Network,
147                  * resourceforproperty217, resourceforproperty217,
148                  * resourceforproperty217, resourceforproperty217,
149                  * resourceforproperty217, resourceforproperty217,
150                  * resourceforproperty217, resourceforproperty217,
151                  * resourceforproperty217, resourceforproperty317,
152                  * resourceforproperty317, resourceforproperty317,
153                  * resourceforproperty317, resourceforproperty317,
154                  * resourceforproperty317, resourceforproperty317,
155                  * resourceforproperty317, resourceforproperty317,
156                  * resourceforproperty317, resourceforproperty317,
157                  * resourceforproperty317, resourceforproperty317,
158                  * resourceforproperty317, resourceforproperty317,
159                  * resourceforproperty317, resourceforproperty317,
160                  * resourceforproperty317, resourceforproperty317,
161                  * resourceforproperty317, resourceforproperty317,
162                  * resourceforproperty317, resourceforproperty317]
163                  */
164
165                 // Create resource (not certified)
166                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
167
168                 String resourceName = "TestResource";
169                 String description = "description";
170                 ArrayList<String> resourceTags = new ArrayList<String>();
171                 resourceTags.add(resourceName);
172                 String category = ServiceCategoriesEnum.MOBILITY.getValue();
173                 ArrayList<String> derivedFrom = new ArrayList<String>();
174                 derivedFrom.add("tosca.nodes.root");
175                 String vendorName = "Oracle";
176                 String vendorRelease = "1.0";
177                 String contactId = "Peter";
178                 String icon = "myICON";
179
180                 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, category,
181                                 derivedFrom, vendorName, vendorRelease, contactId, icon);
182
183                 ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
184                 // assertEquals("Check response code after create user", 201,
185                 // restResponse.getErrorCode().intValue());
186
187                 // Get not abstract resources
188                 getResourceNotAbstarctResponse = getNotAbstractResources();
189                 // Check that received 200.
190                 assertEquals("Check response code after get abstract resources", STATUS_CODE_GET_SUCCESS,
191                                 getResourceNotAbstarctResponse.getErrorCode().intValue());
192                 // Verify that all the resources not abstract
193                 assertTrue("One or more resources are abstract", isAllResourcesNotAbstract(getResourceNotAbstarctResponse));
194                 // Verify that all the resources are certified
195                 assertTrue("Not all the resources are certified", isAllResourcesCertified(getResourceNotAbstarctResponse));
196
197                 // Compare expected list of abstract resources to actual list of
198                 // abstract resources.
199                 // expectedNotAbstractResourcesUniqueIdArray = new
200                 // ArrayList<String>(Arrays.asList("tosca.nodes.compute.1.0",
201                 // "tosca.nodes.objectstorage.1.0", "tosca.nodes.blockstorage.1.0",
202                 // "tosca.nodes.loadbalancer.1.0", "tosca.nodes.network.port.1.0"));
203
204                 // actualNotAbstarctResourcesUniqueIdArray =
205                 // restResponseToListByHeader(getResourceNotAbstarctResponse,
206                 // "uniqueId");
207
208                 actualNotAbstarctResourcesUniqueIdArray = restResponseToListByHeader(getResourceNotAbstarctResponse, "name");
209
210                 Collections.sort(actualNotAbstarctResourcesUniqueIdArray);
211                 Collections.sort(expectedNotAbstractResourcesUniqueIdArray);
212
213                 for (String expectedResource : expectedNotAbstractResourcesUniqueIdArray) {
214                         if (false == actualNotAbstarctResourcesUniqueIdArray.contains(expectedResource)) {
215                                 // System.out.println("Not found abstract resource " +
216                                 // expectedResource);
217                         }
218                 }
219                 assertTrue(
220                                 "Expected abstract resources list: " + expectedNotAbstractResourcesUniqueIdArray.toString()
221                                                 + " Actual: " + actualNotAbstarctResourcesUniqueIdArray.toString(),
222                                 actualNotAbstarctResourcesUniqueIdArray.containsAll(expectedNotAbstractResourcesUniqueIdArray));
223                 // assertTrue("Expected abstract resources list: "+
224                 // expectedNotAbstractResourcesUniqueIdArray.toString()+ " Actual:
225                 // "+actualNotAbstarctResourcesUniqueIdArray.toString(),expectedNotAbstractResourcesUniqueIdArray.equals(actualNotAbstarctResourcesUniqueIdArray));
226
227                 // restore the resources
228                 // ImportResourceCITest.restoreToOriginalState(originalState);
229
230         }
231
232         protected RestResponse getNotAbstractResources() throws Exception {
233                 HttpRequest httpRequest = new HttpRequest();
234
235                 String url = String.format(Urls.GET_ALL_NOT_ABSTRACT_RESOURCES, config.getCatalogBeHost(),
236                                 config.getCatalogBePort());
237
238                 Map<String, String> headersMap = new HashMap<String, String>();
239                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
240                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
241                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
242
243                 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
244
245                 return getResourceNotAbstarctResponse;
246         }
247
248         protected List<String> restResponseToListByHeader(RestResponse restResponse, String restResponseHeader) {
249                 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
250                 JsonArray jsonArray = jelement.getAsJsonArray();
251
252                 List<String> restResponseArray = new ArrayList<>();
253
254                 for (int i = 0; i < jsonArray.size(); i++) {
255                         JsonObject jobject = (JsonObject) jsonArray.get(i);
256                         String header = jobject.get(restResponseHeader).toString();
257                         header = header.replace("\"", "");
258                         restResponseArray.add(header);
259                 }
260
261                 return restResponseArray;
262
263         }
264
265         protected boolean isAllResourcesNotAbstract(RestResponse restResponse) {
266                 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
267                 JsonArray jsonArray = jelement.getAsJsonArray();
268
269                 for (int i = 0; i < jsonArray.size(); i++) {
270                         JsonObject jobject = (JsonObject) jsonArray.get(i);
271
272                         if (jobject.get("abstract").getAsBoolean()) {
273                                 return false;
274                         }
275
276                 }
277                 return true;
278
279         }
280
281         protected boolean isEmptyList(RestResponse restResponse) {
282                 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
283                 JsonArray jsonArray = jelement.getAsJsonArray();
284
285                 if (jsonArray.size() == 0) {
286                         return true;
287                 }
288                 return false;
289         }
290
291         protected boolean isAllResourcesCertified(RestResponse restResponse) {
292                 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
293                 JsonArray jsonArray = jelement.getAsJsonArray();
294
295                 String certified = "CERTIFIED";
296                 String lifecycleState;
297
298                 for (int i = 0; i < jsonArray.size(); i++) {
299                         JsonObject jobject = (JsonObject) jsonArray.get(i);
300                         lifecycleState = jobject.get("lifecycleState").getAsString();
301                         if (!lifecycleState.equals(certified)) {
302                                 return false;
303                         }
304
305                 }
306                 return true;
307         }
308
309         @Test(enabled = false)
310         public void getEmptyNonAbstractResourcesList() throws Exception {
311                 // remove all the not abstract resources
312                 Map<NormativeTypesEnum, Boolean> originalState = ImportGenericResourceCITest.removeAllNormativeTypeResources();
313
314                 // Get not abstract resources
315                 RestResponse getResourceNotAbstarctResponse = getNotAbstractResources();
316                 // Check that received 200.
317                 assertEquals("Check response code after get abstract resources", STATUS_CODE_GET_SUCCESS,
318                                 getResourceNotAbstarctResponse.getErrorCode().intValue());
319                 // Verify empty list
320                 assertTrue("Received list is not empty", isEmptyList(getResourceNotAbstarctResponse));
321
322                 // restore the resources
323                 // ImportResourceCITest.restoreToOriginalState(originalState);
324                 // import the resources
325                 ImportGenericResourceCITest.importAllNormativeTypesResources(UserRoleEnum.ADMIN);
326         }
327
328 }