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