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