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