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.be.components.impl;
23 import static org.assertj.core.api.Java6Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.anyString;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.when;
35 import fj.data.Either;
36 import java.io.IOException;
37 import java.lang.reflect.Method;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.EnumMap;
41 import java.util.HashMap;
42 import java.util.List;
44 import java.util.Optional;
45 import java.util.stream.Collectors;
46 import java.util.stream.Stream;
47 import javax.servlet.ServletContext;
48 import org.apache.commons.lang3.tuple.ImmutablePair;
49 import org.junit.Assert;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.mockito.InjectMocks;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.openecomp.sdc.ElementOperationMock;
56 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
57 import org.openecomp.sdc.be.components.ArtifactsResolver;
58 import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
59 import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
60 import org.openecomp.sdc.be.components.csar.CsarInfo;
61 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
62 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
63 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
64 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
65 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
66 import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
67 import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
68 import org.openecomp.sdc.be.components.validation.UserValidations;
69 import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
70 import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
71 import org.openecomp.sdc.be.components.validation.component.ComponentFieldValidator;
72 import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator;
73 import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
74 import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator;
75 import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator;
76 import org.openecomp.sdc.be.components.validation.component.ComponentValidator;
77 import org.openecomp.sdc.be.config.ConfigurationManager;
78 import org.openecomp.sdc.be.dao.api.ActionStatus;
79 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
80 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
81 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
82 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
83 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
84 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
85 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
86 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
87 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
88 import org.openecomp.sdc.be.facade.operations.CatalogOperation;
89 import org.openecomp.sdc.be.impl.ComponentsUtils;
90 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
91 import org.openecomp.sdc.be.model.ArtifactDefinition;
92 import org.openecomp.sdc.be.model.Component;
93 import org.openecomp.sdc.be.model.DataTypeDefinition;
94 import org.openecomp.sdc.be.model.GroupDefinition;
95 import org.openecomp.sdc.be.model.InputDefinition;
96 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
97 import org.openecomp.sdc.be.model.LifecycleStateEnum;
98 import org.openecomp.sdc.be.model.PropertyDefinition;
99 import org.openecomp.sdc.be.model.Resource;
100 import org.openecomp.sdc.be.model.User;
101 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
102 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
103 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
104 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
105 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation;
106 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
107 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
108 import org.openecomp.sdc.be.model.operations.StorageException;
109 import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation;
110 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
111 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
112 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
113 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
114 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
115 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
116 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
117 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
118 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
119 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
120 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
121 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
122 import org.openecomp.sdc.be.tosca.CsarUtils;
123 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
124 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
125 import org.openecomp.sdc.be.user.Role;
126 import org.openecomp.sdc.be.user.UserBusinessLogic;
127 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
128 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
129 import org.openecomp.sdc.common.api.ConfigurationSource;
130 import org.openecomp.sdc.common.api.Constants;
131 import org.openecomp.sdc.common.impl.ExternalConfiguration;
132 import org.openecomp.sdc.common.impl.FSConfigurationSource;
133 import org.openecomp.sdc.common.util.GeneralUtility;
134 import org.openecomp.sdc.common.util.ValidationUtils;
135 import org.openecomp.sdc.exception.ResponseFormat;
136 import org.slf4j.Logger;
137 import org.slf4j.LoggerFactory;
138 import org.springframework.web.context.WebApplicationContext;
140 public class ResourceBusinessLogicTest {
142 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
143 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
144 private static final Logger log = LoggerFactory.getLogger(ResourceBusinessLogicTest.class);
145 private static final String RESOURCE_CATEGORY1 = "Network Layer 2-3";
146 private static final String RESOURCE_SUBCATEGORY = "Router";
148 private static final String UPDATED_SUBCATEGORY = "Gateway";
150 private String resourceId = "resourceId1";
151 private String operationId = "uniqueId1";
152 Resource resourceUpdate;
154 private static final String RESOURCE_NAME = "My-Resource_Name with space";
155 private static final String RESOURCE_TOSCA_NAME = "My-Resource_Tosca_Name";
156 private static final String GENERIC_ROOT_NAME = "tosca.nodes.Root";
157 private static final String GENERIC_VF_NAME = "org.openecomp.resource.abstract.nodes.VF";
158 private static final String GENERIC_CR_NAME = "org.openecomp.resource.abstract.nodes.CR";
159 private static final String GENERIC_PNF_NAME = "org.openecomp.resource.abstract.nodes.PNF";
161 final ServletContext servletContext = Mockito.mock(ServletContext.class);
162 IElementOperation mockElementDao;
163 JanusGraphDao mockJanusGraphDao = Mockito.mock(JanusGraphDao.class);
164 UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
165 ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
166 NodeTypeOperation nodeTypeOperation = Mockito.mock(NodeTypeOperation.class);
167 NodeTemplateOperation nodeTemplateOperation = Mockito.mock(NodeTemplateOperation.class);
168 TopologyTemplateOperation topologyTemplateOperation = Mockito.mock(TopologyTemplateOperation.class);
169 final LifecycleBusinessLogic lifecycleBl = Mockito.mock(LifecycleBusinessLogic.class);
170 final CatalogOperation catalogOperation = Mockito.mock(CatalogOperation.class);
171 final ICapabilityTypeOperation capabilityTypeOperation = Mockito.mock(ICapabilityTypeOperation.class);
172 final PropertyOperation propertyOperation = Mockito.mock(PropertyOperation.class);
173 final ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
174 WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
175 UserValidations userValidations = Mockito.mock(UserValidations.class);
176 WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
177 IInterfaceLifecycleOperation interfaceTypeOperation = Mockito.mock(IInterfaceLifecycleOperation.class);
178 ArtifactCassandraDao artifactCassandraDao = Mockito.mock(ArtifactCassandraDao.class);
179 IElementOperation elementDao = new ElementOperationMock();
181 CsarUtils csarUtils = Mockito.mock(CsarUtils.class);
182 UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class);
183 IGroupOperation groupOperation = Mockito.mock(IGroupOperation.class);
184 IGroupInstanceOperation groupInstanceOperation = Mockito.mock(IGroupInstanceOperation.class);
185 IGroupTypeOperation groupTypeOperation = Mockito.mock(IGroupTypeOperation.class);
186 GroupBusinessLogic groupBusinessLogic = Mockito.mock(GroupBusinessLogic.class);
187 InterfaceOperation interfaceOperation = Mockito.mock(InterfaceOperation.class);
188 ArtifactsOperations artifactToscaOperation = Mockito.mock(ArtifactsOperations.class);
189 private PropertyBusinessLogic propertyBusinessLogic = Mockito.mock(PropertyBusinessLogic.class);
190 ArtifactsResolver artifactsResolver = Mockito.mock(ArtifactsResolver.class);
191 InterfaceLifecycleOperation interfaceLifecycleTypeOperation = Mockito.mock(InterfaceLifecycleOperation.class);
192 ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
193 ResourceImportManager resourceImportManager = Mockito.mock(ResourceImportManager.class);
194 InputsBusinessLogic inputsBusinessLogic = Mockito.mock(InputsBusinessLogic.class);
195 CompositionBusinessLogic compositionBusinessLogic = Mockito.mock(CompositionBusinessLogic.class);
196 ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic = Mockito.mock(ResourceDataMergeBusinessLogic.class);
197 CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic = Mockito.mock(CsarArtifactsAndGroupsBusinessLogic.class);
198 MergeInstanceUtils mergeInstanceUtils = Mockito.mock(MergeInstanceUtils.class);
199 UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class);
200 ToscaExportHandler toscaExportHandler = Mockito.mock(ToscaExportHandler.class);
201 PolicyBusinessLogic policyBusinessLogic = Mockito.mock(PolicyBusinessLogic.class);
205 ResponseFormatManager responseManager = null;
206 GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
208 Resource resourceResponse = null;
209 Resource genericVF = null;
210 Resource genericCR = null;
211 Resource genericVFC = null;
212 Resource genericPNF = null;
213 Resource rootType = null;
214 ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
215 ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic(artifactCassandraDao, toscaExportHandler, csarUtils, lifecycleBl,
216 userBusinessLogic, artifactsResolver, elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
217 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
218 CsarOperation csarOperation = Mockito.mock(CsarOperation.class);
220 CsarBusinessLogic csarBusinessLogic ;
221 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<>();
222 List<Resource> reslist;
223 private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
224 protected ComponentDescriptionValidator componentDescriptionValidator = new ComponentDescriptionValidator(componentsUtils);
225 protected ComponentProjectCodeValidator componentProjectCodeValidator = new ComponentProjectCodeValidator(componentsUtils);
226 protected ComponentIconValidator componentIconValidator = new ComponentIconValidator(componentsUtils);
227 protected ComponentContactIdValidator componentContactIdValidator = new ComponentContactIdValidator(componentsUtils);
228 protected ComponentTagsValidator componentTagsValidator = new ComponentTagsValidator(componentsUtils);
229 protected ComponentNameValidator componentNameValidator = new ComponentNameValidator(componentsUtils, toscaOperationFacade);
230 private ComponentValidator componentValidator = createComponentValidator();
231 private SoftwareInformationBusinessLogic softwareInformationBusinessLogic = Mockito.mock(SoftwareInformationBusinessLogic.class);
233 private ComponentValidator createComponentValidator() {
234 List<ComponentFieldValidator> componentFieldValidators = Arrays.asList(componentNameValidator,
235 componentDescriptionValidator, componentProjectCodeValidator,
236 componentIconValidator, componentContactIdValidator,
237 componentTagsValidator);
238 return new ComponentValidator(componentsUtils,componentFieldValidators);
241 ResourceBusinessLogic bl;
242 public ResourceBusinessLogicTest() {
246 public void setup() {
247 MockitoAnnotations.initMocks(this);
248 Mockito.reset(propertyOperation);
251 mockElementDao = new ElementOperationMock();
254 // User data and management
256 user.setUserId("jh0003");
257 user.setFirstName("Jimmi");
258 user.setLastName("Hendrix");
259 user.setRole(Role.ADMIN.name());
261 when(mockUserAdmin.getUser("jh0003", false)).thenReturn(user);
262 when(userValidations.validateUserExists(eq(user.getUserId()))).thenReturn(user);
263 when(userValidations.validateUserNotEmpty(eq(user), anyString())).thenReturn(user);
264 // Servlet Context attributes
265 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
266 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))
267 .thenReturn(webAppContextWrapper);
268 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
269 when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
271 Either<Boolean, StorageOperationStatus> eitherFalse = Either.left(true);
272 when(toscaOperationFacade.validateComponentNameExists("tosca.nodes.Root", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
273 .thenReturn(eitherFalse);
276 Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
277 when(toscaOperationFacade.validateComponentNameExists("alreadyExists", ResourceTypeEnum.VFC,
278 ComponentTypeEnum.RESOURCE)).thenReturn(eitherCountExist);
280 Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
281 when(toscaOperationFacade.validateComponentNameExists(eq(RESOURCE_NAME), any(ResourceTypeEnum.class),
282 eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherCount);
284 * when(toscaOperationFacade.validateComponentNameExists(RESOURCE_NAME,
285 * ResourceTypeEnum.VF,
286 when(interfaceOperation.updateInterface(anyString(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
287 * ComponentTypeEnum.RESOURCE)).thenReturn(eitherCount);
288 * when(toscaOperationFacade.validateComponentNameExists(RESOURCE_NAME,
289 * ResourceTypeEnum.PNF,
290 * ComponentTypeEnum.RESOURCE)).thenReturn(eitherCount);
291 * when(toscaOperationFacade.validateComponentNameExists(RESOURCE_NAME,
292 * ResourceTypeEnum.CR,
293 * ComponentTypeEnum.RESOURCE)).thenReturn(eitherCount);
295 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
296 when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(validateDerivedExists);
298 Either<Boolean, StorageOperationStatus> validateDerivedNotExists = Either.left(false);
299 when(toscaOperationFacade.validateToscaResourceNameExists("kuku")).thenReturn(validateDerivedNotExists);
300 when(graphLockOperation.lockComponent(anyString(), eq(NodeTypeEnum.Resource)))
301 .thenReturn(StorageOperationStatus.OK);
302 when(graphLockOperation.lockComponentByName(anyString(), eq(NodeTypeEnum.Resource)))
303 .thenReturn(StorageOperationStatus.OK);
306 resourceResponse = createResourceObject(true);
307 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(resourceResponse);
308 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
309 when(catalogOperation.updateCatalog(Mockito.any(), Mockito.any())).thenReturn(ActionStatus.OK);
310 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<>();
311 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(emptyDataTypes));
312 when(mockJanusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
315 artifactManager.setNodeTemplateOperation(nodeTemplateOperation);
316 bl = new ResourceBusinessLogic(mockElementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic,
317 interfaceOperation, interfaceLifecycleTypeOperation, artifactManager, componentInstanceBusinessLogic,
318 resourceImportManager, inputsBusinessLogic, compositionBusinessLogic, resourceDataMergeBusinessLogic,
319 csarArtifactsAndGroupsBusinessLogic, mergeInstanceUtils, uiComponentDataConverter, csarBusinessLogic,
320 artifactToscaOperation, propertyBusinessLogic, componentContactIdValidator, componentNameValidator,
321 componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator,
322 componentDescriptionValidator, policyBusinessLogic);
323 bl.setElementDao(mockElementDao);
324 bl.setUserAdmin(mockUserAdmin);
325 bl.setCapabilityTypeOperation(capabilityTypeOperation);
326 bl.setComponentsUtils(componentsUtils);
327 bl.setLifecycleManager(lifecycleBl);
328 bl.setGraphLockOperation(graphLockOperation);
329 bl.setArtifactsManager(artifactManager);
330 bl.setPropertyOperation(propertyOperation);
331 bl.setJanusGraphDao(mockJanusGraphDao);
332 bl.setApplicationDataTypeCache(applicationDataTypeCache);
333 bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
334 bl.setCatalogOperations(catalogOperation);
335 toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
336 toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
337 bl.setToscaOperationFacade(toscaOperationFacade);
338 bl.setUserValidations(userValidations);
339 bl.setInterfaceTypeOperation(interfaceTypeOperation);
341 csarBusinessLogic.setCsarOperation(csarOperation);
342 Resource resourceCsar = createResourceObjectCsar(true);
343 setCanWorkOnResource(resourceCsar);
344 Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
345 when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
346 responseManager = ResponseFormatManager.getInstance();
347 bl.setComponentIconValidator(componentIconValidator);
348 bl.setComponentNameValidator(componentNameValidator);
349 bl.setComponentDescriptionValidator(componentDescriptionValidator);
350 bl.setComponentTagsValidator(componentTagsValidator);
351 bl.setComponentContactIdValidator(componentContactIdValidator);
352 bl.setComponentProjectCodeValidator(componentProjectCodeValidator);
353 bl.setComponentValidator(componentValidator);
354 reslist = new ArrayList<Resource>();
355 reslist.add(resourceResponse);
356 reslist.add(genericVF);
357 reslist.add(genericCR);
358 reslist.add(genericVFC);
359 reslist.add(genericPNF);
360 Either<List<Resource>, StorageOperationStatus> returneval= Either.left(reslist);
361 when(toscaOperationFacade.getAllCertifiedResources(true, true)).thenReturn(returneval);
362 when(toscaOperationFacade.validateComponentNameUniqueness("Resource", ResourceTypeEnum.CR, ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(true));
363 Either<List<Resource>, StorageOperationStatus> returnevalexception= Either.right(StorageOperationStatus.BAD_REQUEST);
364 when(toscaOperationFacade.getAllCertifiedResources(false, false)).thenReturn(returnevalexception);
368 private Resource createResourceObject(boolean afterCreate) {
369 Resource resource = new Resource();
370 resource.setName(RESOURCE_NAME);
371 resource.setToscaResourceName(RESOURCE_TOSCA_NAME);
372 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
373 resource.setDescription("My short description");
374 List<String> tgs = new ArrayList<>();
376 tgs.add(resource.getName());
377 resource.setTags(tgs);
378 List<String> template = new ArrayList<>();
379 template.add("tosca.nodes.Root");
380 resource.setDerivedFrom(template);
381 resource.setVendorName("Motorola");
382 resource.setVendorRelease("1.0.0");
383 resource.setContactId("ya5467");
384 resource.setIcon("defaulticon");
387 resource.setName(resource.getName());
388 resource.setVersion("0.1");
389 resource.setUniqueId(resource.getName()
390 .toLowerCase() + ":" + resource.getVersion());
391 resource.setCreatorUserId(user.getUserId());
392 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
393 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
398 private Resource createResourceObjectCsar(boolean afterCreate) {
399 Resource resource = new Resource();
400 resource.setName(RESOURCE_NAME);
401 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
402 resource.setDescription("My short description");
403 List<String> tgs = new ArrayList<>();
405 tgs.add(resource.getName());
406 resource.setTags(tgs);
407 List<String> template = new ArrayList<>();
408 template.add("tosca.nodes.Root");
409 resource.setDerivedFrom(template);
410 resource.setVendorName("Motorola");
411 resource.setVendorRelease("1.0.0");
412 resource.setResourceVendorModelNumber("");
413 resource.setContactId("ya5467");
414 resource.setIcon("MyIcon");
415 resource.setCsarUUID("valid_vf.csar");
416 resource.setCsarVersion("1");
419 resource.setName(resource.getName());
420 resource.setVersion("0.1");
422 resource.setUniqueId(resource.getName()
423 .toLowerCase() + ":" + resource.getVersion());
424 resource.setCreatorUserId(user.getUserId());
425 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
426 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
431 private Resource setCanWorkOnResource(Resource resource) {
432 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
433 resource.setLastUpdaterUserId(user.getUserId());
438 public void testHappyScenario() {
439 validateUserRoles(Role.ADMIN, Role.DESIGNER);
440 Resource resource = createResourceObject(false);
441 Resource createdResource = null;
443 createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
444 assertThat(createResourceObject(true)).isEqualTo(createdResource);
445 } catch (ComponentException e) {
446 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
452 public void testUpdateHappyScenario() {
453 Resource resource = createResourceObjectCsar(true);
454 setCanWorkOnResource(resource);
455 validateUserRoles(Role.ADMIN, Role.DESIGNER);
456 Either<Resource, StorageOperationStatus> resourceLinkedToCsarRes = Either.left(resource);
457 when(toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, resource.getCsarUUID(),
458 resource.getSystemName())).thenReturn(resourceLinkedToCsarRes);
459 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
460 when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(validateDerivedExists);
461 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
462 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
463 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
464 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
465 Resource createdResource = null;
467 createdResource = bl.validateAndUpdateResourceFromCsar(resource, user, null, null, resource.getUniqueId());
468 assertThat(resource.getUniqueId()).isEqualTo(createdResource.getUniqueId());
469 } catch (ComponentException e) {
470 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
475 /* CREATE validations - start ***********************/
476 // Resource name - start
479 public void testFailedResourceValidations() {
480 testResourceNameExist();
481 testResourceNameEmpty();
482 // testResourceNameExceedsLimit();
483 testResourceNameWrongFormat();
484 testResourceDescExceedsLimitCreate();
485 testResourceDescNotEnglish();
486 testResourceDescriptionEmpty();
487 testResourceDescriptionMissing();
488 testResourceIconMissing();
489 testResourceIconInvalid();
490 testResourceIconExceedsLimit();
491 testResourceTagNotExist();
492 testResourceTagEmpty();
493 testTagsExceedsLimitCreate();
494 testTagsNoServiceName();
497 testContactIdTooLong();
498 testContactIdWrongFormatCreate();
499 testResourceContactIdEmpty();
500 testResourceContactIdMissing();
501 testVendorNameExceedsLimit();
502 testVendorNameWrongFormatCreate();
503 testVendorReleaseWrongFormat();
504 testVendorReleaseExceedsLimitCreate();
505 testResourceVendorModelNumberExceedsLimit();
506 testResourceVendorNameMissing();
507 testResourceVendorReleaseMissing();
508 testResourceCategoryExist();
509 testResourceBadCategoryCreate();
510 testHappyScenarioCostLicenseType();
511 testCostWrongFormatCreate();
512 testLicenseTypeWrongFormatCreate();
513 testResourceTemplateNotExist();
514 testResourceTemplateEmpty();
515 testResourceTemplateInvalid();
518 private void testResourceNameExist() {
519 String resourceName = "alreadyExists";
520 Resource resourceExist = createResourceObject(false);
521 resourceExist.setName(resourceName);
522 resourceExist.getTags()
524 validateUserRoles(Role.ADMIN, Role.DESIGNER);
526 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
527 } catch (ComponentException e) {
528 assertComponentException(e, ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
529 ComponentTypeEnum.RESOURCE.getValue(), resourceName);
533 private void testResourceNameEmpty() {
534 Resource resourceExist = createResourceObject(false);
535 resourceExist.setName(null);
538 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
539 } catch (ComponentException e) {
540 assertComponentException(e, ActionStatus.MISSING_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
544 private void testResourceNameExceedsLimit() {
545 Resource resourceExccedsNameLimit = createResourceObject(false);
546 // 51 chars, the limit is 50
547 String tooLongResourceName = "zCRCAWjqte0DtgcAAMmcJcXeNubeX1p1vOZNTShAHOYNAHvV3iK";
548 resourceExccedsNameLimit.setName(tooLongResourceName);
551 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
552 } catch (ComponentException e) {
553 assertComponentException(e, ActionStatus.COMPONENT_NAME_EXCEEDS_LIMIT,
554 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_NAME_MAX_LENGTH);
558 private void testResourceNameWrongFormat() {
559 Resource resource = createResourceObject(false);
561 String nameWrongFormat = "ljg?fd";
562 resource.setName(nameWrongFormat);
565 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
566 } catch (ComponentException e) {
567 assertComponentException(e, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
571 // Resource name - end
572 // Resource description - start
573 private void testResourceDescExceedsLimitCreate() {
574 Resource resourceExccedsDescLimit = createResourceObject(false);
575 // 1025 chars, the limit is 1024
576 String tooLongResourceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
577 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
578 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
579 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
580 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
581 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
582 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
583 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
585 resourceExccedsDescLimit.setDescription(tooLongResourceDesc);
587 bl.createResource(resourceExccedsDescLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
588 } catch (ComponentException e) {
589 assertComponentException(e, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT,
590 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
594 private void testResourceDescNotEnglish() {
595 Resource notEnglish = createResourceObject(false);
597 String notEnglishDesc = "\uC2B5";
598 notEnglish.setDescription(notEnglishDesc);
601 bl.createResource(notEnglish, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
602 } catch (ComponentException e) {
603 assertComponentException(e, ActionStatus.COMPONENT_INVALID_DESCRIPTION,
604 ComponentTypeEnum.RESOURCE.getValue());
608 private void testResourceDescriptionEmpty() {
609 Resource resourceExist = createResourceObject(false);
610 resourceExist.setDescription("");
613 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
614 } catch (ComponentException e) {
615 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION,
616 ComponentTypeEnum.RESOURCE.getValue());
620 private void testResourceDescriptionMissing() {
621 Resource resourceExist = createResourceObject(false);
622 resourceExist.setDescription(null);
625 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
626 } catch (ComponentException e) {
627 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION,
628 ComponentTypeEnum.RESOURCE.getValue());
631 // Resource description - end
632 // Resource icon start
634 private void testResourceIconMissing() {
635 Resource resourceExist = createResourceObject(false);
636 resourceExist.setIcon(null);
639 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
640 } catch (ComponentException e) {
641 assertComponentException(e, ActionStatus.COMPONENT_MISSING_ICON, ComponentTypeEnum.RESOURCE.getValue());
645 private void testResourceIconInvalid() {
646 Resource resourceExist = createResourceObject(false);
647 resourceExist.setIcon("kjk3453^&");
650 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
651 } catch (ComponentException e) {
652 assertComponentException(e, ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.RESOURCE.getValue());
656 private void testResourceIconExceedsLimit() {
657 Resource resourceExist = createResourceObject(false);
658 resourceExist.setIcon("dsjfhskdfhskjdhfskjdhkjdhfkshdfksjsdkfhsdfsdfsdfsfsdfsf");
660 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
661 } catch (ComponentException e) {
662 assertComponentException(e, ActionStatus.COMPONENT_ICON_EXCEEDS_LIMIT,
663 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.ICON_MAX_LENGTH);
668 // Resource tags - start
669 private void testResourceTagNotExist() {
670 Resource resourceExist = createResourceObject(false);
671 resourceExist.setTags(null);
673 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
674 } catch (ComponentException e) {
675 assertComponentException(e, ActionStatus.COMPONENT_MISSING_TAGS);
679 private void testResourceTagEmpty() {
680 Resource resourceExist = createResourceObject(false);
681 resourceExist.setTags(new ArrayList<>());
683 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
684 } catch (ComponentException e) {
685 assertComponentException(e, ActionStatus.COMPONENT_MISSING_TAGS);
689 private void testTagsExceedsLimitCreate() {
690 Resource resourceExccedsNameLimit = createResourceObject(false);
691 String tag1 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjQ";
692 String tag2 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjW";
693 String tag3 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjE";
694 String tag4 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjb";
695 String tag5 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjr";
696 String tag6 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
697 String tag7 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
698 String tag8 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjd";
699 String tag9 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
700 String tag10 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
701 String tag11 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjh";
702 String tag12 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjj";
703 String tag13 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjk";
704 String tag14 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjs";
705 String tag15 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjz";
706 String tag16 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjx";
707 String tag17 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj2";
708 String tag18 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj3";
709 String tag19 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj4";
710 String tag20 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj5";
711 String tag21 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj0";
713 List<String> tagsList = new ArrayList<>();
735 tagsList.add(resourceExccedsNameLimit.getName());
737 resourceExccedsNameLimit.setTags(tagsList);
739 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
740 } catch (ComponentException e) {
741 assertComponentException(e, ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT,
742 "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
746 private void testTagsSingleExceedsLimit() {
747 Resource resourceExccedsNameLimit = createResourceObject(false);
748 String tag1 = "afzs2qLBb5X6tZhiunkcEwiFX1qRQY8YZl3y3Du5M5xeQY5Nq9afcFHDZ9HaURw43gH27nAUWM36bMbMylwTFSzzNV8NO4v4ripe6Q15Vc2nPOFI";
749 String tag2 = resourceExccedsNameLimit.getName();
750 List<String> tagsList = new ArrayList<>();
754 resourceExccedsNameLimit.setTags(tagsList);
756 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
757 } catch (ComponentException e) {
758 assertComponentException(e, ActionStatus.COMPONENT_SINGLE_TAG_EXCEED_LIMIT,
759 "" + ValidationUtils.TAG_MAX_LENGTH);
763 private void testTagsNoServiceName() {
764 Resource serviceExccedsNameLimit = createResourceObject(false);
765 String tag1 = "afzs2qLBb";
766 List<String> tagsList = new ArrayList<>();
768 serviceExccedsNameLimit.setTags(tagsList);
770 bl.createResource(serviceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
771 } catch (ComponentException e) {
772 assertComponentException(e, ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
776 private void testInvalidTag() {
777 Resource serviceExccedsNameLimit = createResourceObject(false);
778 String tag1 = "afzs2qLBb%#%";
779 List<String> tagsList = new ArrayList<>();
781 serviceExccedsNameLimit.setTags(tagsList);
783 bl.createResource(serviceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
784 } catch (ComponentException e) {
785 assertComponentException(e, ActionStatus.INVALID_FIELD_FORMAT, new String[] { "Resource", "tag" });
789 // Resource tags - stop
790 // Resource contact start
792 private void testContactIdTooLong() {
793 Resource resourceContactId = createResourceObject(false);
794 // 59 chars instead of 50
795 String contactIdTooLong = "thisNameIsVeryLongAndExeccedsTheNormalLengthForContactId";
796 resourceContactId.setContactId(contactIdTooLong);
799 bl.createResource(resourceContactId, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
800 } catch (ComponentException e) {
801 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
805 private void testContactIdWrongFormatCreate() {
806 Resource resourceContactId = createResourceObject(false);
807 // 3 letters and 3 digits and special characters
808 String contactIdFormatWrong = "yrt134!!!";
809 resourceContactId.setContactId(contactIdFormatWrong);
811 bl.createResource(resourceContactId, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
812 } catch (ComponentException e) {
813 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
817 private void testResourceContactIdEmpty() {
818 Resource resourceExist = createResourceObject(false);
819 resourceExist.setContactId("");
821 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
822 } catch (ComponentException e) {
823 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
827 private void testResourceContactIdMissing() {
828 Resource resourceExist = createResourceObject(false);
829 resourceExist.setContactId(null);
831 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
832 } catch (ComponentException e) {
833 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
837 private void testVendorNameExceedsLimit() {
838 Resource resourceExccedsVendorNameLimit = createResourceObject(false);
839 String tooLongVendorName = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
840 resourceExccedsVendorNameLimit.setVendorName(tooLongVendorName);
842 bl.createResource(resourceExccedsVendorNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
843 } catch (ComponentException e) {
844 assertComponentException(e, ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT,
845 "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
849 private void testResourceVendorModelNumberExceedsLimit() {
850 Resource resourceExccedsVendorModelNumberLimit = createResourceObject(false);
851 String tooLongVendorModelNumber = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
852 resourceExccedsVendorModelNumberLimit.setResourceVendorModelNumber(tooLongVendorModelNumber);
854 bl.createResource(resourceExccedsVendorModelNumberLimit, AuditingActionEnum.CREATE_RESOURCE, user, null,
856 } catch (ComponentException e) {
857 assertComponentException(e, ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
858 "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
862 private void testVendorNameWrongFormatCreate() {
863 Resource resource = createResourceObject(false);
865 String nameWrongFormat = "ljg*fd";
866 resource.setVendorName(nameWrongFormat);
868 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
869 } catch (ComponentException e) {
870 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
874 private void testVendorReleaseWrongFormat() {
875 Resource resource = createResourceObject(false);
877 String vendorReleaseWrongFormat = "1>2";
878 resource.setVendorRelease(vendorReleaseWrongFormat);
880 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
881 } catch (ComponentException e) {
882 assertComponentException(e, ActionStatus.INVALID_VENDOR_RELEASE, vendorReleaseWrongFormat);
886 private void testVendorReleaseExceedsLimitCreate() {
887 Resource resourceExccedsNameLimit = createResourceObject(false);
888 String tooLongVendorRelease = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
889 resourceExccedsNameLimit.setVendorRelease(tooLongVendorRelease);
891 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
892 } catch (ComponentException e) {
893 assertComponentException(e, ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT,
894 "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
898 private void testResourceVendorNameMissing() {
899 Resource resourceExist = createResourceObject(false);
900 resourceExist.setVendorName(null);
902 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
903 } catch (ComponentException e) {
904 assertComponentException(e, ActionStatus.MISSING_VENDOR_NAME);
908 private void testResourceVendorReleaseMissing() {
909 Resource resourceExist = createResourceObject(false);
910 resourceExist.setVendorRelease(null);
912 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
913 } catch (ComponentException e) {
914 assertComponentException(e, ActionStatus.MISSING_VENDOR_RELEASE);
918 // Resource vendor name/release stop
920 private void testResourceCategoryExist() {
921 Resource resourceExist = createResourceObject(false);
922 resourceExist.setCategories(null);
924 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
925 } catch (ComponentException e) {
926 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
930 private void testResourceBadCategoryCreate() {
932 Resource resourceExist = createResourceObject(false);
933 resourceExist.setCategories(null);
934 resourceExist.addCategory("koko", "koko");
936 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
937 } catch (ComponentException e) {
938 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
944 private void testHappyScenarioCostLicenseType() {
945 Resource createResourceObject = createResourceObject(false);
946 Resource createResourceObjectAfterCreate = createResourceObject(true);
947 // Adding cost and licenseType to basic mock
948 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(createResourceObjectAfterCreate);
949 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
951 String cost = "123.456";
952 String licenseType = "User";
953 createResourceObject.setCost(cost);
954 createResourceObject.setLicenseType(licenseType);
955 Resource createdResource;
957 createdResource = bl.createResource(createResourceObject, AuditingActionEnum.CREATE_RESOURCE, user, null,
959 createResourceObjectAfterCreate.setCost(cost);
960 createResourceObjectAfterCreate.setLicenseType(licenseType);
961 assertThat(createResourceObjectAfterCreate).isEqualTo(createdResource);
962 } catch (ComponentException e) {
963 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
968 private void testCostWrongFormatCreate() {
969 Resource resourceCost = createResourceObject(false);
970 // Comma instead of fullstop
971 String cost = "12356,464";
972 resourceCost.setCost(cost);
974 bl.createResource(resourceCost, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
975 } catch (ComponentException e) {
976 assertComponentException(e, ActionStatus.INVALID_CONTENT);
981 // License type start
982 private void testLicenseTypeWrongFormatCreate() {
983 Resource resourceLicenseType = createResourceObject(false);
985 String licenseType = "cpu";
986 resourceLicenseType.setLicenseType(licenseType);
988 bl.createResource(resourceLicenseType, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
989 } catch (ComponentException e) {
990 assertComponentException(e, ActionStatus.INVALID_CONTENT);
995 // Derived from start
996 private void testResourceTemplateNotExist() {
997 Resource resourceExist = createResourceObject(false);
998 List<String> list = null;
999 resourceExist.setDerivedFrom(list);
1001 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1002 } catch (ComponentException e) {
1003 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1007 private void testResourceTemplateEmpty() {
1008 Resource resourceExist = createResourceObject(false);
1009 resourceExist.setDerivedFrom(new ArrayList<>());
1011 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1012 } catch (ComponentException e) {
1013 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1017 private void testResourceTemplateInvalid() {
1018 Resource resourceExist = createResourceObject(false);
1019 ArrayList<String> derivedFrom = new ArrayList<>();
1020 derivedFrom.add("kuku");
1021 resourceExist.setDerivedFrom(derivedFrom);
1023 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1024 } catch (ComponentException e) {
1025 assertComponentException(e, ActionStatus.PARENT_RESOURCE_NOT_FOUND);
1029 // Derived from stop
1030 private void assertComponentException(ComponentException e, ActionStatus expectedStatus, String... variables) {
1031 ResponseFormat actualResponse = e.getResponseFormat() != null ? e.getResponseFormat()
1032 : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
1033 assertResponse(actualResponse, expectedStatus, variables);
1036 private void assertResponse(ResponseFormat actualResponse, ActionStatus expectedStatus, String... variables) {
1037 ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables);
1038 assertThat(expectedResponse.getStatus()).isEqualTo(actualResponse.getStatus());
1039 assertThat(expectedResponse.getFormattedMessage()).isEqualTo(actualResponse.getFormattedMessage());
1042 private void assertResponse(Either<Resource, ResponseFormat> createResponse, ActionStatus expectedStatus,
1043 String... variables) {
1044 assertResponse(createResponse.right()
1045 .value(), expectedStatus, variables);
1048 // UPDATE tests - start
1051 public void testResourceNameWrongFormat_UPDATE() {
1052 Resource resource = createResourceObject(true);
1053 Resource updatedResource = createResourceObject(true);
1055 // this is in order to prevent failing with 403 earlier
1056 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1057 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1059 String nameWrongFormat = "ljg*fd";
1060 updatedResource.setName(nameWrongFormat);
1062 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1063 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1065 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1066 } catch (ComponentException e) {
1067 assertComponentException(e, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
1072 public void testResourceNameAfterCertify_UPDATE() {
1073 Resource resource = createResourceObject(true);
1074 Resource updatedResource = createResourceObject(true);
1076 // this is in order to prevent failing with 403 earlier
1077 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1078 // when(resourceOperation.getResource_tx(resource.getUniqueId(),false)).thenReturn(eitherUpdate);
1079 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1081 String name = "ljg";
1082 updatedResource.setName(name);
1083 resource.setVersion("1.0");
1085 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1086 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1088 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1089 } catch (ComponentException e) {
1090 assertComponentException(e, ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED);
1095 public void testResourceNameAlreadyExist_UPDATE() {
1096 Resource resource = createResourceObject(true);
1097 Resource updatedResource = createResourceObject(true);
1099 // this is in order to prevent failing with 403 earlier
1100 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1101 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1103 String resourceName = "alreadyExists";
1104 updatedResource.setName(resourceName);
1105 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(updatedResource);
1106 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1108 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1109 } catch (ComponentException e) {
1110 assertComponentException(e, ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
1111 ComponentTypeEnum.RESOURCE.getValue(), resourceName);
1118 public void testResourceDescExceedsLimit_UPDATE() {
1119 Resource resource = createResourceObject(true);
1120 Resource updatedResource = createResourceObject(true);
1122 // this is in order to prevent failing with 403 earlier
1123 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1124 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1126 // 1025 chars, the limit is 1024
1127 String tooLongResourceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
1128 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
1129 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
1130 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
1131 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
1132 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
1133 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
1134 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
1135 updatedResource.setDescription(tooLongResourceDesc);
1136 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1137 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1139 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1140 } catch (ComponentException e) {
1141 assertComponentException(e, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT,
1142 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
1147 public void testIconWrongFormat_UPDATE() {
1148 Resource resource = createResourceObject(true);
1149 Resource updatedResource = createResourceObject(true);
1151 // this is in order to prevent failing with 403 earlier
1152 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1153 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1156 String icon = "icon.jpg";
1157 updatedResource.setIcon(icon);
1158 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1159 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1161 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1162 } catch (ComponentException e) {
1163 assertComponentException(e, ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.RESOURCE.getValue());
1168 public void testIconAfterCertify_UPDATE() {
1169 Resource resource = createResourceObject(true);
1170 Resource updatedResource = createResourceObject(true);
1172 // this is in order to prevent failing with 403 earlier
1173 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1174 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1177 String icon = "icon";
1178 updatedResource.setIcon(icon);
1180 resource.setVersion("1.0");
1182 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1183 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1185 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1186 } catch (ComponentException e) {
1187 assertComponentException(e, ActionStatus.RESOURCE_ICON_CANNOT_BE_CHANGED);
1192 public void testTagsExceedsLimit_UPDATE() {
1193 Resource resource = createResourceObject(true);
1194 Resource updatedResource = createResourceObject(true);
1196 // this is in order to prevent failing with 403 earlier
1197 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1198 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1200 String tag1 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjQ";
1201 String tag2 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjW";
1202 String tag3 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjE";
1203 String tag4 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjb";
1204 String tag5 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjr";
1205 String tag6 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
1206 String tag7 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
1207 String tag8 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjd";
1208 String tag9 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
1209 String tag10 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
1210 String tag11 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjh";
1211 String tag12 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjj";
1212 String tag13 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjk";
1213 String tag14 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjs";
1214 String tag15 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjz";
1215 String tag16 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjx";
1216 String tag17 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj2";
1217 String tag18 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj3";
1218 String tag19 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj4";
1219 String tag20 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj5";
1220 String tag21 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj0";
1222 List<String> tagsList = new ArrayList<>();
1232 tagsList.add(tag10);
1233 tagsList.add(tag11);
1234 tagsList.add(tag12);
1235 tagsList.add(tag13);
1236 tagsList.add(tag14);
1237 tagsList.add(tag15);
1238 tagsList.add(tag16);
1239 tagsList.add(tag17);
1240 tagsList.add(tag18);
1241 tagsList.add(tag19);
1242 tagsList.add(tag20);
1243 tagsList.add(tag21);
1244 tagsList.add(resource.getName());
1246 updatedResource.setTags(tagsList);
1247 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1248 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1250 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1251 } catch (ComponentException e) {
1252 assertComponentException(e, ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT,
1253 "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
1258 public void testVendorNameWrongFormat_UPDATE() {
1259 Resource resource = createResourceObject(true);
1260 Resource updatedResource = createResourceObject(true);
1262 // this is in order to prevent failing with 403 earlier
1263 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1264 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1267 String nameWrongFormat = "ljg*fd";
1268 updatedResource.setVendorName(nameWrongFormat);
1269 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1270 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1272 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1273 } catch (ComponentException e) {
1274 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
1279 public void testVendorNameWrongFormat() {
1280 Resource resource = createResourceObject(true);
1281 Resource updatedResource = createResourceObject(true);
1283 // this is in order to prevent failing with 403 earlier
1284 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1285 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1288 String nameWrongFormat = "ljg*fd";
1289 updatedResource.setVendorName(nameWrongFormat);
1290 resource.setVersion("1.0");
1292 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1293 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1295 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1296 } catch (ComponentException e) {
1297 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
1302 public void testVendorReleaseExceedsLimit_UPDATE() {
1303 Resource resource = createResourceObject(true);
1304 Resource updatedResource = createResourceObject(true);
1306 // this is in order to prevent failing with 403 earlier
1307 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1308 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1309 // 129 chars, the limit is 128
1310 String tooLongVendorRelease = "h1KSyJh9EspI8SPwAGu4VETfqWejeanuB1PCJBxJmJncYnrW0lnsEFFVRIukRJkwlOVnZCy8p38tjhANeZq3BGMHIawWR6ICl8Wi9mikRYALWgvJug00JrlQ0iPVKPLxy";
1311 updatedResource.setVendorRelease(tooLongVendorRelease);
1312 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1313 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1315 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1316 } catch (ComponentException e) {
1317 assertComponentException(e, ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT,
1318 "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
1323 public void testResourceBadCategory_UPDATE() {
1324 Resource resource = createResourceObject(true);
1325 Resource updatedResource = createResourceObject(true);
1327 // this is in order to prevent failing with 403 earlier
1328 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1329 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1331 String resourceId = resource.getUniqueId();
1332 String badCategory = "ddfds";
1333 updatedResource.setCategories(null);
1334 updatedResource.addCategory(badCategory, "fikt");
1335 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1336 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1338 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1339 } catch (ComponentException e) {
1340 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
1345 public void testResourceCategoryAfterCertify_UPDATE() {
1346 Resource resource = createResourceObject(true);
1347 Resource updatedResource = createResourceObject(true);
1349 // this is in order to prevent failing with 403 earlier
1350 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1351 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1353 String resourceId = resource.getUniqueId();
1354 updatedResource.setCategories(null);
1355 updatedResource.addCategory(RESOURCE_CATEGORY1, UPDATED_SUBCATEGORY);
1356 resource.setVersion("1.0");
1358 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1359 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1361 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1362 } catch (ComponentException e) {
1363 assertComponentException(e, ActionStatus.RESOURCE_CATEGORY_CANNOT_BE_CHANGED);
1367 // Derived from start
1369 public void testResourceTemplateNotExist_UPDATE() {
1370 Resource resource = createResourceObject(true);
1371 Resource updatedResource = createResourceObject(true);
1373 // this is in order to prevent failing with 403 earlier
1374 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1375 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1376 String resourceId = resource.getUniqueId();
1378 List<String> list = null;
1379 updatedResource.setDerivedFrom(list);
1380 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1381 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1383 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1384 } catch (ComponentException e) {
1385 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1390 public void testResourceTemplateEmpty_UPDATE() {
1391 Resource resource = createResourceObject(true);
1392 Resource updatedResource = createResourceObject(true);
1393 String resourceId = resource.getUniqueId();
1395 // this is in order to prevent failing with 403 earlier
1396 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1397 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1399 updatedResource.setDerivedFrom(new ArrayList<>());
1400 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1401 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1403 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1404 } catch (ComponentException e) {
1405 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1410 public void testResourceTemplateInvalid_UPDATE() {
1411 Resource resource = createResourceObject(true);
1412 Resource updatedResource = createResourceObject(true);
1413 String resourceId = resource.getUniqueId();
1415 // this is in order to prevent failing with 403 earlier
1416 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1417 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1419 ArrayList<String> derivedFrom = new ArrayList<>();
1420 derivedFrom.add("kuku");
1421 updatedResource.setDerivedFrom(derivedFrom);
1422 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1423 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1425 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1426 } catch (ComponentException e) {
1427 assertComponentException(e, ActionStatus.PARENT_RESOURCE_NOT_FOUND);
1432 public void testResourceTemplateCertify_UPDATE_HAPPY() {
1433 Resource resource = createResourceObject(true);
1434 Resource updatedResource = createResourceObject(true);
1435 String resourceId = resource.getUniqueId();
1437 // this is in order to prevent failing with 403 earlier
1438 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1439 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1441 Either<Boolean, StorageOperationStatus> isToscaNameExtending = Either.left(true);
1442 when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString()))
1443 .thenReturn(isToscaNameExtending);
1445 Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = Either
1446 .left(new HashMap<>());
1447 when(propertyOperation.deleteAllPropertiesAssociatedToNode(any(NodeTypeEnum.class), anyString()))
1448 .thenReturn(findPropertiesOfNode);
1450 resource.setVersion("1.0");
1452 ArrayList<String> derivedFrom = new ArrayList<>();
1453 derivedFrom.add("tosca.nodes.Root");
1454 updatedResource.setDerivedFrom(derivedFrom);
1455 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(updatedResource);
1456 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1457 Resource createdResource = bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1458 assertThat(createdResource).isNotNull();
1462 public void testResourceTemplateCertify_UPDATE_SAD() {
1463 Resource resource = createResourceObject(true);
1464 Resource updatedResource = createResourceObject(true);
1465 String resourceId = resource.getUniqueId();
1467 // this is in order to prevent failing with 403 earlier
1468 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1469 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1471 Either<Boolean, StorageOperationStatus> isToscaNameExtending = Either.left(false);
1472 when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString()))
1473 .thenReturn(isToscaNameExtending);
1475 resource.setVersion("1.0");
1477 ArrayList<String> derivedFrom = new ArrayList<>();
1478 derivedFrom.add("tosca.nodes.Root");
1479 updatedResource.setDerivedFrom(derivedFrom);
1480 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1481 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1482 Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = Either
1483 .left(new HashMap<>());
1484 when(propertyOperation.deleteAllPropertiesAssociatedToNode(any(NodeTypeEnum.class), anyString()))
1485 .thenReturn(findPropertiesOfNode);
1488 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1489 } catch (ComponentException e) {
1490 assertComponentException(e, ActionStatus.PARENT_RESOURCE_DOES_NOT_EXTEND);
1493 // Derived from stop
1496 public void createOrUpdateResourceAlreadyCheckout() {
1498 Resource resourceExist = createResourceObject(false);
1499 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1500 Resource createdResource = bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null,
1502 createdResource.setLastUpdaterUserId(user.getUserId());
1503 assertThat(createdResource).isNotNull();
1504 Either<Resource, StorageOperationStatus> getLatestResult = Either.left(createdResource);
1505 Either<Component, StorageOperationStatus> getCompLatestResult = Either.left(createdResource);
1506 when(toscaOperationFacade.getLatestByToscaResourceName(resourceExist.getToscaResourceName()))
1507 .thenReturn(getCompLatestResult);
1508 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1509 .thenReturn(getLatestResult);
1511 Resource resourceToUpdtae = createResourceObject(false);
1513 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1514 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1515 assertNotNull(createOrUpdateResource);
1517 Mockito.verify(toscaOperationFacade, Mockito.times(1))
1518 .overrideComponent(any(Resource.class), any(Resource.class));
1519 Mockito.verify(lifecycleBl, Mockito.times(0))
1520 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1521 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1526 public void createOrUpdateResourceCertified() {
1528 Resource resourceExist = createResourceObject(false);
1529 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1530 Resource createdResource = bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null,
1533 assertThat(createdResource).isNotNull();
1534 createdResource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1535 createdResource.setVersion("1.0");
1537 Either<Resource, StorageOperationStatus> getLatestResult = Either.left(createdResource);
1538 Either<Component, StorageOperationStatus> getCompLatestResult = Either.left(createdResource);
1539 when(toscaOperationFacade.getLatestByToscaResourceName(resourceExist.getToscaResourceName()))
1540 .thenReturn(getCompLatestResult);
1541 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1542 .thenReturn(getLatestResult);
1544 when(lifecycleBl.changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1545 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean()))
1546 .thenReturn(Either.left(createdResource));
1548 Resource resourceToUpdtae = createResourceObject(false);
1550 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1551 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1552 assertNotNull(createOrUpdateResource);
1554 Mockito.verify(toscaOperationFacade, Mockito.times(1))
1555 .overrideComponent(any(Resource.class), any(Resource.class));
1556 Mockito.verify(lifecycleBl, Mockito.times(1))
1557 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1558 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1563 public void createOrUpdateResourceNotExist() {
1564 Resource resourceToUpdtae = createResourceObject(false);
1566 Either<Component, StorageOperationStatus> getLatestResult = Either.right(StorageOperationStatus.NOT_FOUND);
1567 when(toscaOperationFacade.getLatestByName(resourceToUpdtae.getName())).thenReturn(getLatestResult);
1569 Either<Component, StorageOperationStatus> getLatestToscaNameResult = Either
1570 .right(StorageOperationStatus.NOT_FOUND);
1571 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdtae.getToscaResourceName()))
1572 .thenReturn(getLatestToscaNameResult);
1574 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1575 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1576 assertThat(createOrUpdateResource).isNotNull();
1578 Mockito.verify(toscaOperationFacade, times(1))
1579 .createToscaComponent(eq(resourceToUpdtae));
1580 Mockito.verify(toscaOperationFacade, Mockito.times(0))
1581 .overrideComponent(any(Resource.class), any(Resource.class));
1582 Mockito.verify(lifecycleBl, Mockito.times(0))
1583 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1584 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1589 public void testIfNodeTypeNameHasValidPrefix() {
1590 final List<String> definedNodeTypeNamespaceList = ConfigurationManager.getConfigurationManager()
1591 .getConfiguration().getDefinedResourceNamespace();
1593 definedNodeTypeNamespaceList.parallelStream().forEach(validNodeTypePrefix -> {
1594 final String nodeName = validNodeTypePrefix + "." + "abc";
1595 final Optional<String> result = bl.validateNodeTypeNamePrefix(nodeName, definedNodeTypeNamespaceList);
1596 assertTrue(result.isPresent());
1601 public void updateNestedResource_typeIsNew() throws IOException {
1602 Resource resourceToUpdate = createResourceObject(false);
1603 String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + "." + "abc";
1604 String jsonContent = ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml");
1605 CsarInfo csarInfo = new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", jsonContent,
1607 String nestedResourceName = bl.buildNestedToscaResourceName(resourceToUpdate.getResourceType()
1608 .name(), csarInfo.getVfResourceName(), nodeName)
1610 when(toscaOperationFacade.getLatestByName(resourceToUpdate.getName()))
1611 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1612 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdate.getToscaResourceName()))
1613 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1614 when(toscaOperationFacade.getLatestByToscaResourceName(nestedResourceName))
1615 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1617 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1618 .createOrUpdateResourceByImport(resourceToUpdate, user, false, false, false, csarInfo, nodeName, false);
1619 assertThat(createOrUpdateResource).isNotNull();
1621 Mockito.verify(toscaOperationFacade, times(1))
1622 .createToscaComponent(eq(resourceToUpdate));
1623 Mockito.verify(toscaOperationFacade, times(0))
1624 .overrideComponent(any(Resource.class), any(Resource.class));
1625 Mockito.verify(lifecycleBl, times(0))
1626 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1627 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1631 public void updateNestedResource_typeExists() throws IOException {
1633 Resource resourceToUpdate = createResourceObject(false);
1634 setCanWorkOnResource(resourceResponse);
1635 String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + "." + "abc";
1636 String jsonContent = ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml");
1637 CsarInfo csarInfo = new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", jsonContent,
1639 String nestedResourceName = bl.buildNestedToscaResourceName(resourceToUpdate.getResourceType()
1640 .name(), csarInfo.getVfResourceName(), nodeName)
1642 when(toscaOperationFacade.getLatestByName(resourceToUpdate.getName()))
1643 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1644 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdate.getToscaResourceName()))
1645 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1646 when(toscaOperationFacade.getLatestByToscaResourceName(nestedResourceName))
1647 .thenReturn(Either.left(resourceResponse));
1648 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1649 .thenReturn(Either.left(resourceResponse));
1651 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1652 .createOrUpdateResourceByImport(resourceToUpdate, user, false, false, false, csarInfo, nodeName, false);
1653 assertThat(createOrUpdateResource).isNotNull();
1654 Mockito.verify(toscaOperationFacade, times(1))
1655 .overrideComponent(any(Resource.class), any(Resource.class));
1656 Mockito.verify(lifecycleBl, times(0))
1657 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1658 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1662 public void testValidatePropertiesDefaultValues_SuccessfullWithoutProperties() {
1663 Resource basic = createResourceObject(true);
1665 Boolean validatePropertiesDefaultValues = bl.validatePropertiesDefaultValues(basic);
1666 assertTrue(validatePropertiesDefaultValues);
1670 public void testValidatePropertiesDefaultValues_SuccessfullWithProperties() {
1671 Resource basic = createResourceObject(true);
1672 PropertyDefinition property = new PropertyDefinition();
1673 property.setName("myProperty");
1674 property.setType(ToscaPropertyType.INTEGER.getType());
1675 property.setDefaultValue("1");
1676 List<PropertyDefinition> properties = new ArrayList<>();
1677 properties.add(property);
1678 basic.setProperties(properties);
1679 when(propertyOperation.isPropertyTypeValid(property)).thenReturn(true);
1680 when(propertyOperation.isPropertyDefaultValueValid(property, emptyDataTypes)).thenReturn(true);
1681 Boolean validatePropertiesDefaultValues = bl.validatePropertiesDefaultValues(basic);
1682 assertTrue(validatePropertiesDefaultValues);
1685 @Test(expected = ComponentException.class)
1686 public void testValidatePropertiesDefaultValues_FailedWithProperties() {
1687 Resource basic = createResourceObject(true);
1688 PropertyDefinition property = new PropertyDefinition();
1689 property.setName("myProperty");
1690 property.setType(ToscaPropertyType.INTEGER.getType());
1691 property.setDefaultValue("1.5");
1692 List<PropertyDefinition> properties = new ArrayList<>();
1693 properties.add(property);
1694 basic.setProperties(properties);
1696 when(propertyOperation.isPropertyDefaultValueValid(property, emptyDataTypes)).thenReturn(false);
1697 bl.validatePropertiesDefaultValues(basic);
1701 // public void testDeleteMarkedResourcesNoResources() {
1702 // List<GraphVertex> ids = new ArrayList<>();
1703 // Either<List<GraphVertex>, StorageOperationStatus> eitherNoResources =
1704 // Either.left(ids);
1705 // when(topologyTemplateOperation.getAllComponentsMarkedForDeletion(ComponentTypeEnum.RESOURCE)).thenReturn(eitherNoResources);
1707 // Either<List<String>, ResponseFormat> deleteMarkedResources =
1708 // bl.deleteMarkedComponents();
1709 // assertTrue(deleteMarkedResources.isLeft());
1710 // assertTrue(deleteMarkedResources.left().value().isEmpty());
1712 // Mockito.verify(artifactManager,
1713 // Mockito.times(0)).deleteAllComponentArtifactsIfNotOnGraph(Mockito.anyList());
1718 // public void testDeleteMarkedResources() {
1719 // List<String> ids = new ArrayList<String>();
1720 // String resourceInUse = "123";
1721 // ids.add(resourceInUse);
1722 // String resourceFree = "456";
1723 // ids.add(resourceFree);
1724 // Either<List<String>, StorageOperationStatus> eitherNoResources =
1725 // Either.left(ids);
1726 // when(toscaOperationFacade.getAllComponentsMarkedForDeletion()).thenReturn(eitherNoResources);
1728 // Either<Boolean, StorageOperationStatus> resourceInUseResponse =
1729 // Either.left(true);
1730 // Either<Boolean, StorageOperationStatus> resourceFreeResponse =
1731 // Either.left(false);
1733 // List<ArtifactDefinition> artifacts = new ArrayList<ArtifactDefinition>();
1734 // Either<List<ArtifactDefinition>, StorageOperationStatus>
1735 // getArtifactsResponse = Either.left(artifacts);
1736 // when(toscaOperationFacade.getComponentArtifactsForDelete(resourceFree,
1737 // NodeTypeEnum.Resource, true)).thenReturn(getArtifactsResponse);
1739 // when(toscaOperationFacade.isComponentInUse(resourceFree)).thenReturn(resourceFreeResponse);
1740 // when(toscaOperationFacade.isComponentInUse(resourceInUse)).thenReturn(resourceInUseResponse);
1742 // Either<Component, StorageOperationStatus> eitherDelete = Either.left(new
1744 // when(toscaOperationFacade.deleteToscaComponent(resourceFree)).thenReturn(eitherDelete);
1746 // when(artifactManager.deleteAllComponentArtifactsIfNotOnGraph(artifacts)).thenReturn(StorageOperationStatus.OK);
1747 // List<String> deletedComponents = new ArrayList<>();
1748 // deletedComponents.add(resourceFree);
1749 // when(toscaOperationFacade.deleteMarkedElements(ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(deletedComponents));
1751 // Either<List<String>, ResponseFormat> deleteMarkedResources =
1752 // bl.deleteMarkedComponents();
1753 // assertTrue(deleteMarkedResources.isLeft());
1754 // List<String> resourceIdList = deleteMarkedResources.left().value();
1755 // assertFalse(resourceIdList.isEmpty());
1756 // assertTrue(resourceIdList.contains(resourceFree));
1757 // assertFalse(resourceIdList.contains(resourceInUse));
1759 // Mockito.verify(artifactManager,
1760 // Mockito.times(1)).deleteAllComponentArtifactsIfNotOnGraph(artifacts);
1763 @SuppressWarnings("unchecked")
1765 public void testFindVfCsarArtifactsToHandle() {
1767 Class<ResourceBusinessLogic> targetClass = ResourceBusinessLogic.class;
1768 String methodName = "findVfCsarArtifactsToHandle";
1769 Resource resource = new Resource();
1770 String deploymentArtifactToUpdateFileName = "deploymentArtifactToUpdate.yaml";
1771 String deploymentArtifactToDeleteFileName = "deploymentArtifactToDelete.yaml";
1772 String deploymentArtifactToCreateFileName = "deploymentArtifactToCreate.yaml";
1774 String artifactInfoToUpdateFileName = "infoArtifactToUpdate.yaml";
1775 String artifactInfoToDeleteFileName = "infoArtifactToDelete.yaml";
1776 String artifactInfoToNotDeleteFileName = "infoArtifactNotToDelete.yaml";
1777 String artifactInfoToCreateFileName = "infoArtifactToCreate.yaml";
1779 byte[] oldPayloadData = "oldPayloadData".getBytes();
1780 byte[] newPayloadData = "newPayloadData".getBytes();
1781 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
1783 ArtifactDefinition deploymentArtifactToUpdate = new ArtifactDefinition();
1784 deploymentArtifactToUpdate.setMandatory(false);
1785 deploymentArtifactToUpdate.setArtifactName(deploymentArtifactToUpdateFileName);
1786 deploymentArtifactToUpdate.setArtifactType("SNMP_POLL");
1787 deploymentArtifactToUpdate.setPayload(oldPayloadData);
1788 deploymentArtifactToUpdate
1789 .setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1791 ArtifactDefinition deploymentArtifactToDelete = new ArtifactDefinition();
1792 deploymentArtifactToDelete.setMandatory(false);
1793 deploymentArtifactToDelete.setArtifactName(deploymentArtifactToDeleteFileName);
1794 deploymentArtifactToDelete.setArtifactType("SNMP_TRAP");
1795 deploymentArtifactToDelete.setPayload(oldPayloadData);
1796 deploymentArtifactToDelete
1797 .setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1799 ArtifactDefinition deploymentArtifactToIgnore = new ArtifactDefinition();
1801 deploymentArtifacts.put(ValidationUtils.normalizeArtifactLabel(deploymentArtifactToUpdate.getArtifactName()),
1802 deploymentArtifactToUpdate);
1803 deploymentArtifacts.put(ValidationUtils.normalizeArtifactLabel(deploymentArtifactToDelete.getArtifactName()),
1804 deploymentArtifactToDelete);
1805 deploymentArtifacts.put("ignore", deploymentArtifactToIgnore);
1807 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
1809 ArtifactDefinition artifactToUpdate = new ArtifactDefinition();
1810 artifactToUpdate.setMandatory(false);
1811 artifactToUpdate.setArtifactName(artifactInfoToUpdateFileName);
1812 artifactToUpdate.setArtifactType("SNMP_POLL");
1813 artifactToUpdate.setPayload(oldPayloadData);
1814 artifactToUpdate.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1816 ArtifactDefinition artifactToDelete = new ArtifactDefinition();
1817 artifactToDelete.setMandatory(false);
1818 artifactToDelete.setArtifactName(artifactInfoToDeleteFileName);
1819 artifactToDelete.setArtifactType("SNMP_TRAP");
1820 artifactToDelete.setPayload(oldPayloadData);
1821 artifactToDelete.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1822 artifactToDelete.setIsFromCsar(true);
1824 ArtifactDefinition artifactToNotDelete = new ArtifactDefinition();
1825 artifactToNotDelete.setMandatory(false);
1826 artifactToNotDelete.setArtifactName(artifactInfoToNotDeleteFileName);
1827 artifactToNotDelete.setArtifactType("SNMP_TRAP");
1828 artifactToNotDelete.setPayload(oldPayloadData);
1829 artifactToNotDelete.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1830 artifactToNotDelete.setIsFromCsar(false);
1832 ArtifactDefinition artifactToIgnore = new ArtifactDefinition();
1834 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToUpdate.getArtifactName()), artifactToUpdate);
1835 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToDelete.getArtifactName()), artifactToDelete);
1836 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToNotDelete.getArtifactName()),
1837 artifactToNotDelete);
1838 artifacts.put("ignore", artifactToIgnore);
1840 resource.setDeploymentArtifacts(deploymentArtifacts);
1841 resource.setArtifacts(artifacts);
1843 List<NonMetaArtifactInfo> artifactPathAndNameList = new ArrayList<>();
1844 NonMetaArtifactInfo deploymentArtifactInfoToUpdate = new NonMetaArtifactInfo(
1845 deploymentArtifactToUpdate.getArtifactName(), null,
1846 ArtifactTypeEnum.findType(deploymentArtifactToUpdate.getArtifactType()),
1847 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, deploymentArtifactToUpdate.getArtifactName(), false);
1849 NonMetaArtifactInfo informationalArtifactInfoToUpdate = new NonMetaArtifactInfo(
1850 artifactToUpdate.getArtifactName(), null, ArtifactTypeEnum.findType(artifactToUpdate.getArtifactType()),
1851 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, artifactToUpdate.getArtifactName(), false);
1853 NonMetaArtifactInfo informationalArtifactInfoToUpdateFromCsar = new NonMetaArtifactInfo(
1854 artifactToUpdate.getArtifactName(), null, ArtifactTypeEnum.findType(artifactToUpdate.getArtifactType()),
1855 ArtifactGroupTypeEnum.INFORMATIONAL, newPayloadData, artifactToUpdate.getArtifactName(), true);
1857 NonMetaArtifactInfo deploymentArtifactInfoToUpdateFromCsar = new NonMetaArtifactInfo(
1858 artifactToUpdate.getArtifactName(), null, ArtifactTypeEnum.findType(artifactToUpdate.getArtifactType()),
1859 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, artifactToUpdate.getArtifactName(), true);
1861 NonMetaArtifactInfo deploymentArtifactInfoToCreate = new NonMetaArtifactInfo(deploymentArtifactToCreateFileName,
1862 null, ArtifactTypeEnum.OTHER, ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData,
1863 deploymentArtifactToCreateFileName, false);
1865 NonMetaArtifactInfo informationalArtifactInfoToCreate = new NonMetaArtifactInfo(artifactInfoToCreateFileName,
1866 null, ArtifactTypeEnum.OTHER, ArtifactGroupTypeEnum.INFORMATIONAL, newPayloadData,
1867 artifactInfoToCreateFileName, false);
1869 artifactPathAndNameList.add(deploymentArtifactInfoToUpdate);
1870 artifactPathAndNameList.add(informationalArtifactInfoToUpdate);
1871 artifactPathAndNameList.add(deploymentArtifactInfoToCreate);
1872 artifactPathAndNameList.add(informationalArtifactInfoToCreate);
1873 artifactPathAndNameList.add(informationalArtifactInfoToUpdateFromCsar);
1874 artifactPathAndNameList.add(deploymentArtifactInfoToUpdateFromCsar);
1876 Object[] argObjects = { resource, artifactPathAndNameList, user };
1877 Class[] argClasses = { Resource.class, List.class, User.class };
1879 Method method = targetClass.getDeclaredMethod(methodName, argClasses);
1880 method.setAccessible(true);
1881 Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = (Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat>) method
1882 .invoke(bl, argObjects);
1883 assertTrue(findVfCsarArtifactsToHandleRes.isLeft());
1884 EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> foundVfArtifacts = findVfCsarArtifactsToHandleRes
1887 assertEquals(4, foundVfArtifacts.get(ArtifactOperationEnum.CREATE)
1889 assertEquals(4, foundVfArtifacts.get(ArtifactOperationEnum.UPDATE)
1891 assertEquals(1, foundVfArtifacts.get(ArtifactOperationEnum.DELETE)
1894 } catch (Exception e) {
1895 e.printStackTrace();
1900 public void testVFGeneratedInputs() {
1901 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1902 Resource resource = createVF();
1903 List<InputDefinition> inputs = resource.getInputs();
1904 assertEquals(6, inputs.size());
1905 for (InputDefinition input : inputs) {
1906 assertThat(input.getOwnerId()).isNotNull();
1908 assertEquals(resource.getDerivedFromGenericType(), genericVF.getToscaResourceName());
1909 assertEquals(resource.getDerivedFromGenericVersion(), genericVF.getVersion());
1913 public void testCRGeneratedInputs() {
1914 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1915 Resource resource = createCR();
1916 List<InputDefinition> inputs = resource.getInputs();
1917 assertEquals(3, inputs.size());
1918 for (InputDefinition input : inputs) {
1919 assertThat(input.getOwnerId()).isNotNull();
1921 assertEquals(resource.getDerivedFromGenericType(), genericCR.getToscaResourceName());
1922 assertEquals(resource.getDerivedFromGenericVersion(), genericCR.getVersion());
1926 public void testVFUpdateGenericInputsToLatestOnCheckout() {
1927 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1928 // create a VF that is derived from generic version 1.0
1929 Resource resource = createVF();
1930 // create a new generic version without properties
1931 genericVF.setVersion("2.0");
1932 genericVF.setProperties(null);
1933 String currentDerivedFromVersion = resource.getDerivedFromGenericVersion();
1934 List<InputDefinition> currentInputs = resource.getInputs();
1935 // verify previous inputs ownerId fields exist - user may not delete
1937 assertEquals(6, currentInputs.stream()
1938 .filter(p -> null != p.getOwnerId())
1939 .collect(Collectors.toList())
1941 Either<Boolean, ResponseFormat> upgradeToLatestGeneric = bl.shouldUpgradeToLatestGeneric(resource);
1943 assertTrue(upgradeToLatestGeneric.isLeft());
1944 // verify update required and valid
1945 assertTrue(upgradeToLatestGeneric.left()
1947 // verify version was upgraded
1948 assertNotEquals(resource.getDerivedFromGenericVersion(), currentDerivedFromVersion);
1949 // verify inputs were not deleted
1950 assertEquals(6, resource.getInputs()
1952 // verify inputs ownerId fields were removed - user may delete/edit
1954 assertEquals(6, resource.getInputs()
1956 .filter(p -> null == p.getOwnerId())
1957 .collect(Collectors.toList())
1962 public void testVFUpdateGenericInputsToLatestOnCheckoutNotPerformed() {
1964 // create a VF that is derived from generic version 1.0
1965 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1966 Resource resource = createVF();
1968 // add an input to the VF
1969 PropertyDefinition newProp = new PropertyDefinition();
1970 newProp.setType("integer");
1971 newProp.setName("newProp");
1972 resource.getInputs()
1973 .add(new InputDefinition(newProp));
1975 // create a new generic version with a new property which has the same
1976 // name as a user defined input on the VF with a different type
1977 genericVF.setVersion("2.0");
1978 newProp.setType("string");
1979 genericVF.setProperties(new ArrayList<>());
1980 genericVF.getProperties()
1982 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericVF));
1983 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericVF.getProperties(),
1984 genericVF.getUniqueId())).thenCallRealMethod();
1985 String currentDerivedFromVersion = resource.getDerivedFromGenericVersion();
1986 assertEquals(6, resource.getInputs()
1988 .filter(p -> null != p.getOwnerId())
1989 .collect(Collectors.toList())
1991 Either<Boolean, ResponseFormat> upgradeToLatestGeneric = bl.shouldUpgradeToLatestGeneric(resource);
1993 assertTrue(upgradeToLatestGeneric.isLeft());
1994 // verify update is invalid an void
1995 assertFalse(upgradeToLatestGeneric.left()
1997 // verify version was not upgraded
1998 assertEquals(resource.getDerivedFromGenericVersion(), currentDerivedFromVersion);
1999 // verify inputs were not removed
2000 assertEquals(7, resource.getInputs()
2002 // verify user defined input exists
2003 assertEquals(1, resource.getInputs()
2005 .filter(p -> null == p.getOwnerId())
2006 .collect(Collectors.toList())
2008 assertEquals("integer", resource.getInputs()
2010 .filter(p -> null == p.getOwnerId())
2017 public void testPNFGeneratedInputsNoGeneratedInformationalArtifacts() {
2018 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2019 Resource resource = createPNF();
2020 List<InputDefinition> inputs = resource.getInputs();
2021 assertEquals(3, inputs.size());
2022 for (InputDefinition input : inputs) {
2023 assertThat(input.getOwnerId()).isNotNull();
2025 assertEquals(resource.getDerivedFromGenericType(), genericPNF.getToscaResourceName());
2026 assertEquals(resource.getDerivedFromGenericVersion(), genericPNF.getVersion());
2027 assertEquals(0, resource.getArtifacts()
2031 private Resource createVF() {
2033 genericVF = setupGenericTypeMock(GENERIC_VF_NAME);
2034 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_VF_NAME))
2035 .thenReturn(Either.left(genericVF));
2036 Resource resource = createResourceObject(true);
2037 resource.setDerivedFrom(null);
2038 resource.setResourceType(ResourceTypeEnum.VF);
2039 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2040 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericVF));
2041 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericVF)).thenCallRealMethod();
2042 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericVF.getProperties(),
2043 resource.getUniqueId())).thenCallRealMethod();
2044 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2045 assertThat(createdResource).isNotNull();
2046 return createdResource;
2049 private Resource createRoot() {
2050 rootType = setupGenericTypeMock(GENERIC_ROOT_NAME);
2051 when(toscaOperationFacade.getLatestByToscaResourceName(GENERIC_ROOT_NAME))
2052 .thenReturn(Either.left(rootType));
2056 private Resource createCR() {
2058 genericCR = setupGenericTypeMock(GENERIC_CR_NAME);
2059 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_CR_NAME))
2060 .thenReturn(Either.left(genericCR));
2061 Resource resource = createResourceObject(true);
2062 resource.setDerivedFrom(null);
2063 resource.setResourceType(ResourceTypeEnum.CR);
2064 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2065 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericCR));
2066 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericCR)).thenCallRealMethod();
2067 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericCR.getProperties(),
2068 resource.getUniqueId())).thenCallRealMethod();
2069 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2070 assertThat(createdResource).isNotNull();
2071 return createdResource;
2074 private Resource createPNF() {
2076 genericPNF = setupGenericTypeMock(GENERIC_PNF_NAME);
2077 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_PNF_NAME))
2078 .thenReturn(Either.left(genericPNF));
2079 Resource resource = createResourceObject(true);
2080 resource.setDerivedFrom(null);
2081 resource.setResourceType(ResourceTypeEnum.PNF);
2082 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2083 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericPNF));
2084 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericPNF)).thenCallRealMethod();
2085 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericPNF.getProperties(),
2086 resource.getUniqueId())).thenCallRealMethod();
2087 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2088 assertThat(createdResource).isNotNull();
2089 return createdResource;
2092 private Map<String, String> getGenericPropertiesByToscaName(String toscaName) {
2093 HashMap<String, String> PNFProps = new HashMap<String, String>() {
2095 put("nf_function", "string");
2096 put("nf_role", "string");
2097 put("nf_type", "string");
2101 HashMap<String, String> CRProps = new HashMap<String, String>() {
2103 put("cr_function", "string");
2104 put("cr_role", "string");
2105 put("cr_type", "string");
2109 HashMap<String, String> VFProps = new HashMap<String, String>() {
2112 put("availability_zone_max_count", "integer");
2113 put("min_instances", "integer");
2114 put("max_instances", "integer");
2118 if (toscaName.contains("PNF"))
2120 if (toscaName.contains("CR"))
2122 if (toscaName.contains("VF"))
2125 return new HashMap<>();
2128 private Resource setupGenericTypeMock(String toscaName) {
2130 Resource genericType = createResourceObject(true);
2131 genericType.setVersion("1.0");
2132 genericType.setToscaResourceName(toscaName);
2133 genericType.setAbstract(true);
2134 List<PropertyDefinition> genericProps = new ArrayList<>();
2135 Map<String, String> genericPropsMap = getGenericPropertiesByToscaName(toscaName);
2136 genericPropsMap.forEach((name, type) -> {
2137 PropertyDefinition prop = new PropertyDefinition();
2140 genericProps.add(prop);
2143 genericType.setProperties(genericProps);
2147 private void validateUserRoles(Role... roles) {
2148 List<Role> listOfRoles = Stream.of(roles)
2149 .collect(Collectors.toList());
2153 public void testUpdateVolumeGroup() {
2154 Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
2155 bl.updateVolumeGroup(resource);
2156 assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(true));
2160 public void testUpdateVolumeGroupNull() {
2161 Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
2162 resource.setGroups(null);
2163 bl.updateVolumeGroup(resource);
2167 public void testUpdateVolumeGroupFail() {
2168 Resource resource = getResourceWithType("NON_EXIST_HEAT", "org.openecomp.groups.VfModule");
2169 bl.updateVolumeGroup(resource);
2170 assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(false));
2173 private Resource getResourceWithType(String artifactType, String groupDefinitionType) {
2174 ArtifactDefinition artifactToUpdate = new ArtifactDefinition();
2175 List<GroupDefinition> groups = new ArrayList<>();
2176 GroupDefinition gd = new GroupDefinition();
2177 List<PropertyDataDefinition> properties = new ArrayList<>();
2178 PropertyDataDefinition pdd = new PropertyDataDefinition();
2179 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
2180 List<String> artifactsList = new ArrayList<>();
2182 artifactToUpdate.setArtifactType(artifactType);
2183 artifactToUpdate.setArtifactName(artifactType);
2184 artifactToUpdate.setUniqueId(artifactType);
2185 Resource resource = createResourceObjectCsar(true);
2186 artifactsList.add(artifactToUpdate.getArtifactName());
2189 pdd.setName("volume_group");
2190 pdd.setValue("true");
2191 pdd.setType(ToscaPropertyType.BOOLEAN.getType());
2193 artifacts.put(artifactToUpdate.getArtifactName(), artifactToUpdate);
2195 properties.add(pdd);
2196 gd.setType(groupDefinitionType);
2197 gd.setProperties(properties);
2198 gd.setArtifacts(artifactsList);
2201 resource.setGroups(groups);
2202 resource.setDeploymentArtifacts(artifacts);
2208 public void testgetAllCertifiedResources() throws Exception {
2209 List<Resource> list = bl.getAllCertifiedResources(true, HighestFilterEnum.HIGHEST_ONLY, "USER");
2210 Assert.assertEquals(reslist,list);
2213 @Test(expected = StorageException.class)
2214 public void testgetAllCertifiedResources_exception() throws Exception {
2215 List<Resource> list = bl.getAllCertifiedResources(false, HighestFilterEnum.NON_HIGHEST_ONLY, "USER");
2216 Assert.assertEquals(reslist,list);
2220 public void testvalidateResourceNameExists() throws Exception {
2221 Either<Map<String, Boolean>, ResponseFormat> res = bl.validateResourceNameExists("Resource", ResourceTypeEnum.CR, "jh0003");
2222 Assert.assertEquals(true,res.isLeft());