2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.execute.resource;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
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;
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;
53 import com.google.gson.JsonArray;
54 import com.google.gson.JsonElement;
55 import com.google.gson.JsonObject;
56 import com.google.gson.JsonParser;
58 public class GetResourceNotAbstractApiTest extends ComponentBaseTest {
60 private static Logger logger = LoggerFactory.getLogger(ComponentBaseTest.class.getName());
61 protected static final int STATUS_CODE_GET_SUCCESS = 200;
63 protected Config config = Config.instance();
64 protected String contentTypeHeaderData = "application/json";
65 protected String acceptHeaderDate = "application/json";
68 public static TestName name = new TestName();
70 public GetResourceNotAbstractApiTest() {
71 super(name, GetResourceNotAbstractApiTest.class.getName());
75 public void getNotAbstractResourceList() throws Exception {
77 // remove all the not abstract resources
78 // Map<NormativeTypes, Boolean> originalState =
79 // ImportResourceCITest.removeAllNormativeTypeResources();
81 // import all the default not abstract resources
82 // ImportGenericResourceCITest.importAllNormativeTypesResources(UserRoleEnum.ADMIN);
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));
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";
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));
108 List<String> actualNotAbstarctResourcesUniqueIdArray = restResponseToListByHeader(
109 getResourceNotAbstarctResponse, "name");
111 // Collections.sort(actualNotAbstarctResourcesUniqueIdArray);
112 // Collections.sort(expectedNotAbstractResourcesUniqueIdArray);
114 List<String> toFind = new ArrayList<>();
115 toFind.add(objectStorageUid);
116 toFind.add(computeUid);
117 toFind.add(blockStorageUid);
118 toFind.add(loadBalancerUid);
121 boolean removeAll = toFind.removeAll(actualNotAbstarctResourcesUniqueIdArray);
122 logger.debug("Cannot find resources {}",toFind.toString());
124 for (String expectedResource : expectedNotAbstractResourcesUniqueIdArray) {
125 if (false == actualNotAbstarctResourcesUniqueIdArray.contains(expectedResource)) {
126 // System.out.println("Not found abstract resource " +
127 // expectedResource);
132 "Expected abstract resources list: " + expectedNotAbstractResourcesUniqueIdArray.toString()
133 + " Actual: " + actualNotAbstarctResourcesUniqueIdArray.toString(),
134 actualNotAbstarctResourcesUniqueIdArray.containsAll(expectedNotAbstractResourcesUniqueIdArray));
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]
163 // Create resource (not certified)
164 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
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";
178 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, category,
179 derivedFrom, vendorName, vendorRelease, contactId, icon);
181 ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
182 // assertEquals("Check response code after create user", 201,
183 // restResponse.getErrorCode().intValue());
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));
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"));
202 // actualNotAbstarctResourcesUniqueIdArray =
203 // restResponseToListByHeader(getResourceNotAbstarctResponse,
206 actualNotAbstarctResourcesUniqueIdArray = restResponseToListByHeader(getResourceNotAbstarctResponse, "name");
208 Collections.sort(actualNotAbstarctResourcesUniqueIdArray);
209 Collections.sort(expectedNotAbstractResourcesUniqueIdArray);
211 for (String expectedResource : expectedNotAbstractResourcesUniqueIdArray) {
212 if (false == actualNotAbstarctResourcesUniqueIdArray.contains(expectedResource)) {
213 // System.out.println("Not found abstract resource " +
214 // expectedResource);
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));
225 // restore the resources
226 // ImportResourceCITest.restoreToOriginalState(originalState);
230 protected RestResponse getNotAbstractResources() throws Exception {
231 HttpRequest httpRequest = new HttpRequest();
233 String url = String.format(Urls.GET_ALL_NOT_ABSTRACT_RESOURCES, config.getCatalogBeHost(),
234 config.getCatalogBePort());
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");
241 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
243 return getResourceNotAbstarctResponse;
246 protected List<String> restResponseToListByHeader(RestResponse restResponse, String restResponseHeader) {
247 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
248 JsonArray jsonArray = jelement.getAsJsonArray();
250 List<String> restResponseArray = new ArrayList<>();
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);
259 return restResponseArray;
263 protected boolean isAllResourcesNotAbstract(RestResponse restResponse) {
264 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
265 JsonArray jsonArray = jelement.getAsJsonArray();
267 for (int i = 0; i < jsonArray.size(); i++) {
268 JsonObject jobject = (JsonObject) jsonArray.get(i);
270 if (jobject.get("abstract").getAsBoolean()) {
279 protected boolean isEmptyList(RestResponse restResponse) {
280 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
281 JsonArray jsonArray = jelement.getAsJsonArray();
283 if (jsonArray.size() == 0) {
289 protected boolean isAllResourcesCertified(RestResponse restResponse) {
290 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
291 JsonArray jsonArray = jelement.getAsJsonArray();
293 String certified = "CERTIFIED";
294 String lifecycleState;
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)) {
307 @Test(enabled = false)
308 public void getEmptyNonAbstractResourcesList() throws Exception {
309 // remove all the not abstract resources
310 Map<NormativeTypesEnum, Boolean> originalState = ImportGenericResourceCITest.removeAllNormativeTypeResources();
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());
318 assertTrue("Received list is not empty", isEmptyList(getResourceNotAbstarctResponse));
320 // restore the resources
321 // ImportResourceCITest.restoreToOriginalState(originalState);
322 // import the resources
323 ImportGenericResourceCITest.importAllNormativeTypesResources(UserRoleEnum.ADMIN);