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 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;
49 import static org.testng.AssertJUnit.assertEquals;
50 import static org.testng.AssertJUnit.assertTrue;
52 public class GetResourceNotAbstractApiTest extends ComponentBaseTest {
54 private static Logger logger = LoggerFactory.getLogger(ComponentBaseTest.class.getName());
55 protected static final int STATUS_CODE_GET_SUCCESS = 200;
57 protected Config config = Config.instance();
58 protected String contentTypeHeaderData = "application/json";
59 protected String acceptHeaderDate = "application/json";
62 public static TestName name = new TestName();
64 public GetResourceNotAbstractApiTest() {
65 super(name, GetResourceNotAbstractApiTest.class.getName());
69 public void getNotAbstractResourceList() throws Exception {
71 // remove all the not abstract resources
72 // Map<NormativeTypes, Boolean> originalState =
73 // ImportResourceCITest.removeAllNormativeTypeResources();
75 // import all the default not abstract resources
76 // ImportGenericResourceCITest.importAllNormativeTypesResources(UserRoleEnum.ADMIN);
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));
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";
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));
102 List<String> actualNotAbstarctResourcesUniqueIdArray = restResponseToListByHeader(
103 getResourceNotAbstarctResponse, "name");
105 // Collections.sort(actualNotAbstarctResourcesUniqueIdArray);
106 // Collections.sort(expectedNotAbstractResourcesUniqueIdArray);
108 List<String> toFind = new ArrayList<>();
109 toFind.add(objectStorageUid);
110 toFind.add(computeUid);
111 toFind.add(blockStorageUid);
112 toFind.add(loadBalancerUid);
115 boolean removeAll = toFind.removeAll(actualNotAbstarctResourcesUniqueIdArray);
116 logger.debug("Cannot find resources {}", toFind.toString());
118 for (String expectedResource : expectedNotAbstractResourcesUniqueIdArray) {
119 if (false == actualNotAbstarctResourcesUniqueIdArray.contains(expectedResource)) {
120 // System.out.println("Not found abstract resource " +
121 // expectedResource);
126 "Expected abstract resources list: " + expectedNotAbstractResourcesUniqueIdArray.toString()
127 + " Actual: " + actualNotAbstarctResourcesUniqueIdArray.toString(),
128 actualNotAbstarctResourcesUniqueIdArray.containsAll(expectedNotAbstractResourcesUniqueIdArray));
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]
157 // Create resource (not certified)
158 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
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";
172 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, category,
173 derivedFrom, vendorName, vendorRelease, contactId, icon);
175 ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
176 // assertEquals("Check response code after create user", 201,
177 // restResponse.getErrorCode().intValue());
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));
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"));
196 // actualNotAbstarctResourcesUniqueIdArray =
197 // restResponseToListByHeader(getResourceNotAbstarctResponse,
200 actualNotAbstarctResourcesUniqueIdArray = restResponseToListByHeader(getResourceNotAbstarctResponse, "name");
202 Collections.sort(actualNotAbstarctResourcesUniqueIdArray);
203 Collections.sort(expectedNotAbstractResourcesUniqueIdArray);
205 for (String expectedResource : expectedNotAbstractResourcesUniqueIdArray) {
206 if (false == actualNotAbstarctResourcesUniqueIdArray.contains(expectedResource)) {
207 // System.out.println("Not found abstract resource " +
208 // expectedResource);
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));
219 // restore the resources
220 // ImportResourceCITest.restoreToOriginalState(originalState);
224 protected RestResponse getNotAbstractResources() throws Exception {
225 HttpRequest httpRequest = new HttpRequest();
227 String url = String.format(Urls.GET_ALL_NOT_ABSTRACT_RESOURCES, config.getCatalogBeHost(),
228 config.getCatalogBePort());
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");
235 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
237 return getResourceNotAbstarctResponse;
240 protected List<String> restResponseToListByHeader(RestResponse restResponse, String restResponseHeader) {
241 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
242 JsonArray jsonArray = jelement.getAsJsonArray();
244 List<String> restResponseArray = new ArrayList<>();
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);
253 return restResponseArray;
257 protected boolean isAllResourcesNotAbstract(RestResponse restResponse) {
258 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
259 JsonArray jsonArray = jelement.getAsJsonArray();
261 for (int i = 0; i < jsonArray.size(); i++) {
262 JsonObject jobject = (JsonObject) jsonArray.get(i);
264 if (jobject.get("abstract").getAsBoolean()) {
273 protected boolean isEmptyList(RestResponse restResponse) {
274 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
275 JsonArray jsonArray = jelement.getAsJsonArray();
277 if (jsonArray.size() == 0) {
283 protected boolean isAllResourcesCertified(RestResponse restResponse) {
284 JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
285 JsonArray jsonArray = jelement.getAsJsonArray();
287 String certified = "CERTIFIED";
288 String lifecycleState;
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)) {
301 @Test(enabled = false)
302 public void getEmptyNonAbstractResourcesList() throws Exception {
303 // remove all the not abstract resources
304 Map<NormativeTypesEnum, Boolean> originalState = ImportGenericResourceCITest.removeAllNormativeTypeResources();
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());
312 assertTrue("Received list is not empty", isEmptyList(getResourceNotAbstarctResponse));
314 // restore the resources
315 // ImportResourceCITest.restoreToOriginalState(originalState);
316 // import the resources
317 ImportGenericResourceCITest.importAllNormativeTypesResources(UserRoleEnum.ADMIN);