re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / imports / ImportGenericResourceCITest.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.imports;
22
23 import com.google.gson.Gson;
24 import fj.data.Either;
25 import org.apache.http.HttpStatus;
26 import org.apache.http.client.ClientProtocolException;
27 import org.apache.http.client.methods.CloseableHttpResponse;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.entity.ContentType;
30 import org.apache.http.entity.mime.MultipartEntityBuilder;
31 import org.apache.http.entity.mime.content.FileBody;
32 import org.apache.http.entity.mime.content.StringBody;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClients;
35 import org.junit.Rule;
36 import org.junit.rules.TestName;
37 import org.openecomp.sdc.be.model.LifecycleStateEnum;
38 import org.openecomp.sdc.be.model.category.CategoryDefinition;
39 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
40 import org.openecomp.sdc.ci.tests.api.Urls;
41 import org.openecomp.sdc.ci.tests.config.Config;
42 import org.openecomp.sdc.ci.tests.datatypes.enums.*;
43 import org.openecomp.sdc.ci.tests.datatypes.expected.ExpectedResourceAuditJavaObject;
44 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
45 import org.openecomp.sdc.ci.tests.execute.TODO.ImportCapabilityTypeCITest;
46 import org.openecomp.sdc.ci.tests.utils.DbUtils;
47 import org.openecomp.sdc.ci.tests.utils.Utils;
48 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
49 import org.openecomp.sdc.ci.tests.utils.rest.ImportRestUtils;
50 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
51 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
52 import org.openecomp.sdc.ci.tests.utils.validation.AuditValidationUtils;
53 import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.testng.annotations.BeforeClass;
57 import org.testng.annotations.Test;
58
59 import java.io.File;
60 import java.io.IOException;
61 import java.nio.file.FileSystems;
62 import java.nio.file.Files;
63 import java.util.*;
64 import java.util.Map.Entry;
65
66 import static org.testng.AssertJUnit.*;
67
68 public class ImportGenericResourceCITest extends ComponentBaseTest {
69         private static Logger log = LoggerFactory.getLogger(ImportGenericResourceCITest.class.getName());
70         private static final String FILE_NAME_MY_COMPUTE = "tosca.nodes.MyCompute";
71         private static final String RESOURCE_NAME_UPDATE_COMPUTE = "userUpdateCompute";
72         private static final String RESOURCE_NAME_MY_COMPUTE = "myCompute";
73         private static final String RESOURCE_NAME_USER_COMPUTE = "userCompute";
74         private static final String FILE_NAME_USER_COMPUTE = "tosca.nodes.userCompute";
75         @Rule
76         public static TestName name = new TestName();
77
78         public ImportGenericResourceCITest() {
79                 super(name, ImportGenericResourceCITest.class.getName());
80         }
81
82         @BeforeClass
83         public static void beforeImportClass() throws IOException {
84                 ImportCapabilityTypeCITest.importAllCapabilityTypes();
85                 // removeAllNormativeTypeResources();
86                 // importAllNormativeTypesResources(UserRoleEnum.ADMIN);
87         }
88
89         static Config config = Config.instance();
90
91         public static Map<NormativeTypesEnum, Boolean> removeAllNormativeTypeResources() throws ClientProtocolException, IOException {
92                 Map<NormativeTypesEnum, Boolean> normativeExistInDB = new HashMap<>();
93
94                 for (NormativeTypesEnum current : NormativeTypesEnum.values()) {
95                         Boolean existedBeforeDelete = ImportRestUtils.removeNormativeTypeResource(current);
96                         normativeExistInDB.put(current, existedBeforeDelete);
97                 }
98                 return normativeExistInDB;
99         }
100
101         public static Either<String, Boolean> getNormativeTypeResource(NormativeTypesEnum current) throws ClientProtocolException, IOException {
102                 return getResource(current.getNormativeName(), "1.0");
103         }
104
105         @Test
106         public void importAllTestResources() throws Exception {
107                 for (ImportTestTypesEnum currResource : ImportTestTypesEnum.values()) {
108                         DbUtils.cleanAllAudits();
109
110                         RestResponse importResponse = ImportRestUtils.importTestResource(currResource, UserRoleEnum.ADMIN);
111                         // System.err.println("import Resource
112                         // "+"<"+currResource+">"+"response:
113                         // "+importResponse.getErrorCode());
114                         ImportRestUtils.validateImportTestTypesResp(currResource, importResponse);
115                         if (currResource.getvalidateAudit() == true) {
116                                 // validate audit
117                                 String baseVersion = "1.0";
118                                 ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(currResource.getActionStatus().name());
119                                 ExpectedResourceAuditJavaObject expectedResourceAuditJavaObject = new ExpectedResourceAuditJavaObject();
120                                 String auditAction = "ResourceImport";
121                                 expectedResourceAuditJavaObject.setAction(auditAction);
122                                 expectedResourceAuditJavaObject.setModifierUid(UserRoleEnum.ADMIN.getUserId());
123                                 expectedResourceAuditJavaObject.setModifierName(UserRoleEnum.ADMIN.getUserName());
124                                 expectedResourceAuditJavaObject.setResourceName(currResource.getNormativeName());
125                                 expectedResourceAuditJavaObject.setResourceType("Resource");
126                                 expectedResourceAuditJavaObject.setPrevVersion("");
127                                 expectedResourceAuditJavaObject.setCurrVersion(baseVersion);
128                                 expectedResourceAuditJavaObject.setPrevState("");
129                                 expectedResourceAuditJavaObject.setCurrState(LifecycleStateEnum.CERTIFIED.toString());
130                                 expectedResourceAuditJavaObject.setComment(null);
131                                 expectedResourceAuditJavaObject.setStatus(errorInfo.getCode().toString());
132                                 List<String> variables = (currResource.getErrorParams() != null ? currResource.getErrorParams() : new ArrayList<String>());
133                                 String auditDesc = AuditValidationUtils.buildAuditDescription(errorInfo, variables);
134                                 expectedResourceAuditJavaObject.setDesc(auditDesc);
135                                 AuditValidationUtils.validateAuditImport(expectedResourceAuditJavaObject, auditAction);
136                         }
137                 }
138         }
139
140         // -----------------------------------------------------------------------------------
141         protected void validateMyComputeCapabilities(Map<String, Object> map) {
142                 assertTrue(map.containsKey("capabilities"));
143                 Map<String, Object> capabilities = (Map<String, Object>) map.get("capabilities");
144                 assertTrue(capabilities.containsKey("tosca.capabilities.Container"));
145                 List<Object> hostCapList = (List<Object>) capabilities.get("tosca.capabilities.Container");
146                 assertFalse(hostCapList.isEmpty());
147                 Map<String, Object> hostCap = (Map<String, Object>) hostCapList.get(0);
148                 validateField(hostCap, "type", "tosca.capabilities.Container");
149                 validateField(hostCap, "name", "host");
150                 validateField(hostCap, "validSourceTypes", Arrays.asList(new String[] { "tosca.nodes.SoftwareComponent" }));
151
152                 assertTrue(capabilities.containsKey("tosca.capabilities.Endpoint.Admin"));
153                 List<Object> endPointCapList = (List<Object>) capabilities.get("tosca.capabilities.Endpoint.Admin");
154                 assertFalse(endPointCapList.isEmpty());
155                 Map<String, Object> endPointCap = (Map<String, Object>) endPointCapList.get(0);
156                 validateField(endPointCap, "name", "endpoint");
157                 validateField(endPointCap, "type", "tosca.capabilities.Endpoint.Admin");
158
159                 assertTrue(capabilities.containsKey("tosca.capabilities.OperatingSystem"));
160                 List<Object> osCapList = (List<Object>) capabilities.get("tosca.capabilities.OperatingSystem");
161                 assertFalse(osCapList.isEmpty());
162                 Map<String, Object> osCap = (Map<String, Object>) osCapList.get(0);
163                 validateField(osCap, "name", "os");
164                 validateField(osCap, "type", "tosca.capabilities.OperatingSystem");
165
166                 assertTrue(capabilities.containsKey("tosca.capabilities.Scalable"));
167                 List<Object> scalableCapList = (List<Object>) capabilities.get("tosca.capabilities.Scalable");
168                 assertFalse(scalableCapList.isEmpty());
169                 Map<String, Object> scalableCap = (Map<String, Object>) scalableCapList.get(0);
170                 validateField(scalableCap, "name", "scalable");
171                 validateField(scalableCap, "type", "tosca.capabilities.Scalable");
172
173                 assertTrue(capabilities.containsKey("tosca.capabilities.network.Bindable"));
174                 List<Object> bindingCapList = (List<Object>) capabilities.get("tosca.capabilities.network.Bindable");
175                 assertFalse(bindingCapList.isEmpty());
176                 Map<String, Object> bindingCap = (Map<String, Object>) bindingCapList.get(0);
177                 validateField(bindingCap, "name", "binding");
178                 validateField(bindingCap, "type", "tosca.capabilities.network.Bindable");
179
180         }
181
182         protected void validateMyComputeResource(String resourceName, String resourceVersion, String expectedState) throws ClientProtocolException, IOException {
183                 Either<String, Boolean> eitherMyCompute = getResource(resourceName, resourceVersion);
184                 assertTrue(eitherMyCompute.isLeft());
185                 String testComputeYml = eitherMyCompute.left().value();
186
187                 Map<String, Object> map = new HashMap<String, Object>();
188                 map = (Map<String, Object>) new Gson().fromJson(testComputeYml, map.getClass());
189
190                 validateMyComputeBasicFields(map, resourceName, resourceVersion, expectedState);
191
192                 validateMyComputeCapabilities(map);
193
194                 validateMyComputeRequirements(map);
195                 validateField(map, RespJsonKeysEnum.RESOURCE_VERSION.getRespJsonKeyName(), resourceVersion);
196
197         }
198
199         protected void validateMyComputeResource(String uid, String resourceName, String resourceVersion, String expectedState) throws ClientProtocolException, IOException {
200                 RestResponse resourceResponse = ResourceRestUtils.getResource(uid);
201                 ResourceRestUtils.checkSuccess(resourceResponse);
202                 String testComputeYml = resourceResponse.getResponse();
203
204                 // Either<String, Boolean> eitherMyCompute = getResource(resourceName,
205                 // resourceVersion);
206                 // assertTrue( eitherMyCompute.isLeft() );
207                 // String testComputeYml = eitherMyCompute.left().value();
208
209                 Map<String, Object> map = new HashMap<String, Object>();
210                 map = (Map<String, Object>) new Gson().fromJson(testComputeYml, map.getClass());
211
212                 validateMyComputeBasicFields(map, resourceName, resourceVersion, expectedState);
213
214                 validateMyComputeCapabilities(map);
215
216                 validateMyComputeRequirements(map);
217                 validateField(map, RespJsonKeysEnum.RESOURCE_VERSION.getRespJsonKeyName(), resourceVersion);
218
219         }
220
221         protected void validateMyComputeResourceAfterUpdate(String uid, String resourceName, String resourceVersion, String expectedState) throws ClientProtocolException, IOException {
222                 RestResponse resourceResponse = ResourceRestUtils.getResource(uid);
223                 ResourceRestUtils.checkSuccess(resourceResponse);
224                 String testComputeYml = resourceResponse.getResponse();
225
226                 // Either<String, Boolean> eitherMyCompute = getResource(resourceName,
227                 // resourceVersion);
228                 // assertTrue( eitherMyCompute.isLeft() );
229
230                 // String testComputeYml = eitherMyCompute.left().value();
231
232                 Map<String, Object> map = new HashMap<String, Object>();
233                 map = (Map<String, Object>) new Gson().fromJson(testComputeYml, map.getClass());
234
235                 validateMyComputeBasicFields(map, resourceName, resourceVersion, expectedState);
236                 validateField(map, RespJsonKeysEnum.DESCRIPTION.getRespJsonKeyName(), "Short description");
237                 validateField(map, RespJsonKeysEnum.VENDOR_NAME.getRespJsonKeyName(), "UserVendor");
238                 validateField(map, RespJsonKeysEnum.VENDOR_RELEASE.getRespJsonKeyName(), "1.1.2");
239
240                 // validateMyComputeCapabilities(map);
241                 // AssertJUnit.assertTrue(map.containsKey("capabilities"));
242                 // Map<String, Object> capabilities = (Map<String, Object>)
243                 // map.get("capabilities");
244                 // AssertJUnit.assertTrue(capabilities.containsKey("host"));
245                 // Map<String, Object> hostCap = (Map<String, Object>)
246                 // capabilities.get("host");
247                 // validateField(hostCap, "type", "tosca.capabilities.Container");
248                 // validateField(hostCap, "validSourceTypes", Arrays.asList(new
249                 // String[]{"tosca.nodes.SoftwareComponent"}));
250                 //
251                 // AssertJUnit.assertTrue(capabilities.containsKey("endpoint"));
252                 // Map<String, Object> endPointCap = (Map<String, Object>)
253                 // capabilities.get("endpoint");
254                 // validateField(endPointCap, "type",
255                 // "tosca.capabilities.Endpoint.Admin");
256
257                 assertTrue(map.containsKey("capabilities"));
258                 Map<String, Object> capabilities = (Map<String, Object>) map.get("capabilities");
259                 assertTrue(capabilities.containsKey("tosca.capabilities.Container"));
260                 List<Object> hostCapList = (List<Object>) capabilities.get("tosca.capabilities.Container");
261                 assertFalse(hostCapList.isEmpty());
262                 Map<String, Object> hostCap = (Map<String, Object>) hostCapList.get(0);
263                 validateField(hostCap, "type", "tosca.capabilities.Container");
264                 validateField(hostCap, "name", "host");
265                 validateField(hostCap, "validSourceTypes", Arrays.asList(new String[] { "tosca.nodes.SoftwareComponent" }));
266
267                 assertTrue(capabilities.containsKey("tosca.capabilities.Endpoint.Admin"));
268                 List<Object> endPointCapList = (List<Object>) capabilities.get("tosca.capabilities.Endpoint.Admin");
269                 assertFalse(endPointCapList.isEmpty());
270                 Map<String, Object> endPointCap = (Map<String, Object>) endPointCapList.get(0);
271                 validateField(endPointCap, "name", "endpoint");
272                 validateField(endPointCap, "type", "tosca.capabilities.Endpoint.Admin");
273
274                 validateMyComputeRequirements(map);
275                 validateField(map, RespJsonKeysEnum.RESOURCE_VERSION.getRespJsonKeyName(), resourceVersion);
276
277         }
278
279         protected void validateMyComputeRequirements(Map<String, Object> map) {
280                 assertTrue(map.containsKey("requirements"));
281                 Map<String, Object> requirements = (Map<String, Object>) map.get("requirements");
282
283                 assertTrue(requirements.containsKey("tosca.capabilities.Attachment"));
284                 List<Object> localStorageReqList = (List<Object>) requirements.get("tosca.capabilities.Attachment");
285                 assertFalse(localStorageReqList.isEmpty());
286                 Map<String, Object> localStorageReq = (Map<String, Object>) localStorageReqList.get(0);
287                 validateField(localStorageReq, "capability", "tosca.capabilities.Attachment");
288                 validateField(localStorageReq, "node", "tosca.nodes.BlockStorage");
289                 validateField(localStorageReq, "relationship", "tosca.relationships.AttachesTo");
290                 validateField(localStorageReq, "name", "local_storage");
291         }
292
293         protected void validateMyComputeBasicFields(Map<String, Object> map, String resourceName, String resourceVersion, String expectedState) {
294                 validateField(map, RespJsonKeysEnum.IS_ABSTRACT.getRespJsonKeyName(), false);
295                 // validateField(map, RespJsonKeysEnum.CATEGORIES.getRespJsonKeyName(),
296                 // categoryDefinition);
297                 // validateField(map, RespJsonKeysEnum.UNIQUE_ID.getRespJsonKeyName(),
298                 // UniqueIdBuilder.buildResourceUniqueId(resourceName,
299                 // resourceVersion));
300                 validateField(map, RespJsonKeysEnum.RESOURCE_NAME.getRespJsonKeyName(), resourceName);
301                 validateField(map, RespJsonKeysEnum.TAGS.getRespJsonKeyName(), Arrays.asList(new String[] { resourceName }));
302                 validateField(map, RespJsonKeysEnum.LIFE_CYCLE_STATE.getRespJsonKeyName(), expectedState);
303
304                 validateField(map, RespJsonKeysEnum.DERIVED_FROM.getRespJsonKeyName(), Arrays.asList(new String[] { "tosca.nodes.Root" }));
305         }
306
307         protected static void validateField(Map<String, Object> map, String jsonField, Object expectedValue) {
308                 if (expectedValue == null) {
309                         assertTrue(!map.containsKey(jsonField));
310                 } else {
311                         assertTrue("map does not contain field " + jsonField, map.containsKey(jsonField));
312                         Object foundValue = map.get(jsonField);
313                         compareElements(expectedValue, foundValue);
314                 }
315         }
316
317         protected static void compareElements(Object expectedValue, Object foundValue) {
318                 if (expectedValue instanceof String) {
319                         assertTrue(foundValue instanceof String);
320                         assertTrue(foundValue.equals(expectedValue));
321                 }
322
323                 else if (expectedValue instanceof Boolean) {
324                         assertTrue(foundValue instanceof Boolean);
325                         assertTrue(foundValue == expectedValue);
326                 } else if (expectedValue instanceof Map) {
327                         assertTrue(foundValue instanceof Map);
328                         Map<String, Object> foundMap = (Map<String, Object>) foundValue;
329                         Map<String, Object> excpectedMap = (Map<String, Object>) expectedValue;
330                         assertTrue(foundMap.size() == excpectedMap.size());
331                         Iterator<String> foundkeyItr = foundMap.keySet().iterator();
332                         while (foundkeyItr.hasNext()) {
333                                 String foundKey = foundkeyItr.next();
334                                 assertTrue(excpectedMap.containsKey(foundKey));
335                                 compareElements(excpectedMap.get(foundKey), foundMap.get(foundKey));
336                         }
337
338                 } else if (expectedValue instanceof List) {
339                         assertTrue(foundValue instanceof List);
340                         List<Object> foundList = (List<Object>) foundValue;
341                         List<Object> excpectedList = (List<Object>) expectedValue;
342                         assertTrue(foundList.size() == excpectedList.size());
343                         for (int i = 0; i < foundList.size(); i++) {
344                                 compareElements(excpectedList.get(i), foundList.get(i));
345                         }
346
347                 } else if (expectedValue instanceof CategoryDefinition) {
348                         assertTrue(foundValue instanceof Map);
349                         CategoryDefinition expCat = (CategoryDefinition) expectedValue;
350                         Map<String, Object> actCat = (Map<String, Object>) foundValue;
351                         assertEquals(expCat.getName(), actCat.get("name"));
352
353                         // assertEquals(expCat.getSubcategories().get(0).getName(),
354                         // actCat.get("subcategories").getName());
355                 } else {
356                         assertTrue(foundValue.equals(expectedValue));
357                 }
358         }
359
360         public static void restoreToOriginalState(Map<NormativeTypesEnum, Boolean> originalState, UserRoleEnum userRole) throws IOException {
361                 removeAllNormativeTypeResources();
362
363                 Iterator<Entry<NormativeTypesEnum, Boolean>> iterator = originalState.entrySet().iterator();
364                 while (iterator.hasNext()) {
365                         Entry<NormativeTypesEnum, Boolean> entry = iterator.next();
366                         Boolean isExistBeforeDelete = entry.getValue();
367                         if (isExistBeforeDelete) {
368                                 importNormativeResource(entry.getKey(), userRole);
369                         }
370                 }
371
372         }
373
374         public static void importAllNormativeTypesResources(UserRoleEnum userRole) throws IOException {
375                 for (NormativeTypesEnum currResource : NormativeTypesEnum.values()) {
376                         Either<String, Boolean> resource = getResource(currResource.getNormativeName(), "1.0");
377                         if (resource.isRight()) {
378                                 importNormativeResource(currResource, userRole);
379                         }
380                 }
381
382         }
383
384         protected static Integer importNormativeResource(NormativeTypesEnum resource, UserRoleEnum userRole) throws IOException {
385                 return importResource(resource.getFolderName(), userRole, true);
386         }
387
388         protected static Integer importResource(String folderName, UserRoleEnum userRole, boolean isNormative) throws IOException {
389                 Config config = Utils.getConfig();
390                 CloseableHttpResponse response = null;
391                 MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create();
392
393                 mpBuilder.addPart("resourceZip", new FileBody(getZipFile(folderName)));
394                 mpBuilder.addPart("resourceMetadata", new StringBody(getJsonStringOfFile(folderName, folderName + ".json"), ContentType.APPLICATION_JSON));
395
396                 String url = String.format(Urls.IMPORT_RESOURCE_NORMATIVE, config.getCatalogBeHost(), config.getCatalogBePort());
397                 if (!isNormative) {
398                         url = String.format(Urls.IMPORT_USER_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
399                 }
400
401                 CloseableHttpClient client = HttpClients.createDefault();
402                 try {
403                         HttpPost httpPost = new HttpPost(url);
404                         httpPost.addHeader("USER_ID", userRole.getUserId());
405                         httpPost.setEntity(mpBuilder.build());
406                         response = client.execute(httpPost);
407                         return response.getStatusLine().getStatusCode();
408                 } finally {
409                         closeResponse(response);
410                         closeHttpClient(client);
411
412                 }
413         }
414
415         public static void closeHttpClient(CloseableHttpClient client) {
416                 try {
417                         if (client != null) {
418                                 client.close();
419                         }
420                 } catch (IOException e) {
421                         log.debug("failed to close client or response: ", e);
422                 }
423         }
424
425         public static void closeResponse(CloseableHttpResponse response) {
426                 try {
427                         if (response != null) {
428                                 response.close();
429                         }
430                 } catch (IOException e) {
431                         log.debug("failed to close client or response: ", e);
432                 }
433         }
434
435         protected static String getJsonStringOfFile(String folderName, String fileName) throws IOException {
436                 String sourceDir = config.getImportResourceConfigDir();
437                 sourceDir += File.separator + "normative-types";
438
439                 java.nio.file.Path filePath = FileSystems.getDefault().getPath(sourceDir + File.separator + folderName, fileName);
440                 byte[] fileContent = Files.readAllBytes(filePath);
441                 String content = new String(fileContent);
442                 return content;
443         }
444
445         protected static File getZipFile(String elementName) throws IOException {
446                 String sourceDir = config.getImportResourceConfigDir();
447                 sourceDir += File.separator + "normative-types";
448
449                 java.nio.file.Path filePath = FileSystems.getDefault().getPath(sourceDir + File.separator + elementName, "normative-types-new-" + elementName + ".zip");
450                 return filePath.toFile();
451         }
452
453         protected static String getTestJsonStringOfFile(String folderName, String fileName) throws IOException {
454                 String sourceDir = config.getImportResourceTestsConfigDir();
455                 java.nio.file.Path filePath = FileSystems.getDefault().getPath(sourceDir + File.separator + folderName, fileName);
456                 byte[] fileContent = Files.readAllBytes(filePath);
457                 String content = new String(fileContent);
458                 return content;
459         }
460
461         protected static File getTestZipFile(String elementName) throws IOException {
462                 String sourceDir = config.getImportResourceTestsConfigDir();
463
464                 java.nio.file.Path filePath = FileSystems.getDefault().getPath(sourceDir + File.separator + elementName, "normative-types-new-" + elementName + ".zip");
465                 return filePath.toFile();
466         }
467
468         protected static Either<String, Boolean> getResource(String name, String version) throws IOException {
469                 RestResponse resource = ResourceRestUtils.getResourceByNameAndVersion(UserRoleEnum.DESIGNER.getUserId(), name, version);
470                 if (resource.getErrorCode() == ImportRestUtils.STATUS_CODE_GET_SUCCESS) {
471                         return Either.left(resource.getResponse());
472                         // return Either.right(true);
473
474                 }
475                 return Either.right(false);
476         }
477
478         @Test
479         public void testImportWithRequirmentsAndCapabilities() throws IOException {
480                 String fileName = FILE_NAME_MY_COMPUTE;
481                 RestResponse response = ImportRestUtils.importNormativeResourceByName(RESOURCE_NAME_MY_COMPUTE, UserRoleEnum.ADMIN);
482                 Integer statusCode = response.getErrorCode();
483                 assertTrue(statusCode == ImportRestUtils.STATUS_CODE_IMPORT_SUCCESS);
484                 String uid = ResponseParser.getUniqueIdFromResponse(response);
485                 validateMyComputeResource(uid, fileName, "1.0", "CERTIFIED");
486         }
487
488         @Test
489         public void testImportWithUpdateNormativeType() throws IOException {
490                 String fileName = FILE_NAME_MY_COMPUTE;
491                 RestResponse response = ImportRestUtils.importNormativeResourceByName(RESOURCE_NAME_MY_COMPUTE, UserRoleEnum.ADMIN);
492                 Integer statusCode = response.getErrorCode();
493                 assertTrue(statusCode == BaseRestUtils.STATUS_CODE_IMPORT_SUCCESS);
494                 String uid = ResponseParser.getUniqueIdFromResponse(response);
495                 validateMyComputeResource(uid, fileName, "1.0", "CERTIFIED");
496
497                 // update
498                 response = ImportRestUtils.importNormativeResourceByName(RESOURCE_NAME_MY_COMPUTE, UserRoleEnum.ADMIN);
499                 statusCode = response.getErrorCode();
500                 assertTrue(statusCode == ImportRestUtils.STATUS_CODE_UPDATE_SUCCESS);
501                 uid = ResponseParser.getUniqueIdFromResponse(response);
502                 validateMyComputeResource(uid, fileName, "2.0", "CERTIFIED");
503
504         }
505
506         @Test
507         public void testImportWithInvalidDefaultValue() throws IOException {
508                 RestResponse response = ImportRestUtils.importNewResourceByName("portInvalidDefaultValue", UserRoleEnum.DESIGNER);
509                 assertTrue(response.getErrorCode() == HttpStatus.SC_BAD_REQUEST);
510         }
511
512         @Test
513         public void testImportUserResource() throws IOException {
514                 String fileName = FILE_NAME_USER_COMPUTE;
515                 RestResponse response = ImportRestUtils.importNewResourceByName(RESOURCE_NAME_USER_COMPUTE, UserRoleEnum.DESIGNER);
516                 Integer statusCode = response.getErrorCode();
517                 assertTrue(statusCode == ImportRestUtils.STATUS_CODE_IMPORT_SUCCESS);
518                 String uid = ResponseParser.getUniqueIdFromResponse(response);
519                 validateMyComputeResource(uid, fileName, "0.1", "NOT_CERTIFIED_CHECKOUT");
520
521         }
522
523         @Test
524         public void testImportAndUpdateUserResource() throws IOException {
525                 String fileName = FILE_NAME_USER_COMPUTE;
526                 RestResponse response = ImportRestUtils.importNewResourceByName(RESOURCE_NAME_USER_COMPUTE, UserRoleEnum.DESIGNER);
527                 Integer statusCode = response.getErrorCode();
528                 assertTrue(statusCode == ImportRestUtils.STATUS_CODE_IMPORT_SUCCESS);
529                 String uid = ResponseParser.getUniqueIdFromResponse(response);
530                 validateMyComputeResource(uid, fileName, "0.1", "NOT_CERTIFIED_CHECKOUT");
531                 response = ImportRestUtils.importNewResourceByName(RESOURCE_NAME_UPDATE_COMPUTE, UserRoleEnum.DESIGNER);
532                 statusCode = response.getErrorCode();
533                 assertTrue(statusCode == ImportRestUtils.STATUS_CODE_UPDATE_SUCCESS);
534                 uid = ResponseParser.getUniqueIdFromResponse(response);
535                 validateMyComputeResourceAfterUpdate(uid, fileName, "0.1", "NOT_CERTIFIED_CHECKOUT");
536
537         }
538
539         @Test
540         public void testImportAndUpdateChangesUserResource() throws IOException {
541                 String fileName = FILE_NAME_USER_COMPUTE;
542                 RestResponse response = ImportRestUtils.importNewResourceByName(RESOURCE_NAME_USER_COMPUTE, UserRoleEnum.DESIGNER);
543                 Integer statusCode = response.getErrorCode();
544                 assertTrue(statusCode == ImportRestUtils.STATUS_CODE_IMPORT_SUCCESS);
545                 String uid = ResponseParser.getUniqueIdFromResponse(response);
546                 validateMyComputeResource(uid, fileName, "0.1", "NOT_CERTIFIED_CHECKOUT");
547                 // Either<String, Boolean> resource = getResource(fileName, "0.1");
548                 // assertTrue(resource.isLeft());
549
550                 response = ImportRestUtils.importNewResourceByName(RESOURCE_NAME_UPDATE_COMPUTE, UserRoleEnum.DESIGNER);
551                 statusCode = response.getErrorCode();
552                 assertTrue(statusCode == ImportRestUtils.STATUS_CODE_UPDATE_SUCCESS);
553                 validateMyComputeResourceAfterUpdate(uid, fileName, "0.1", "NOT_CERTIFIED_CHECKOUT");
554
555         }
556
557         @Test
558         public void testImportCheckoutAndUpdateUserResource() throws IOException {
559                 String fileName = FILE_NAME_USER_COMPUTE;
560                 RestResponse response = ImportRestUtils.importNormativeResourceByName(RESOURCE_NAME_USER_COMPUTE, UserRoleEnum.ADMIN);
561                 Integer statusCode = response.getErrorCode();
562                 assertTrue(statusCode == ImportRestUtils.STATUS_CODE_IMPORT_SUCCESS);
563                 String uid = ResponseParser.getUniqueIdFromResponse(response);
564                 validateMyComputeResource(uid, fileName, "1.0", "CERTIFIED");
565
566                 response = ImportRestUtils.importNewResourceByName(RESOURCE_NAME_USER_COMPUTE, UserRoleEnum.DESIGNER);
567                 statusCode = response.getErrorCode();
568                 assertEquals("check response code after update resource", ImportRestUtils.STATUS_CODE_UPDATE_SUCCESS, statusCode.intValue());
569                 uid = ResponseParser.getUniqueIdFromResponse(response);
570                 validateMyComputeResource(uid, fileName, "1.1", "NOT_CERTIFIED_CHECKOUT");
571
572         }
573
574         @Test
575         public void importNormativeTypesTesterUserRole() throws Exception {
576                 Integer statusCode = ImportRestUtils.importNormativeResourceByName(RESOURCE_NAME_MY_COMPUTE, UserRoleEnum.TESTER).getErrorCode();
577                 assertTrue(statusCode == ImportRestUtils.RESTRICTED_OPERATION);
578         }
579
580         @Test
581         public void importNormativeTypesDesignerUserRole() throws Exception {
582                 Integer statusCode = ImportRestUtils.importNormativeResourceByName(RESOURCE_NAME_MY_COMPUTE, UserRoleEnum.DESIGNER).getErrorCode();
583                 assertTrue(statusCode == 409);
584         }
585
586 }