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 fj.data.Either;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.ElementOperationMock;
32 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
33 import org.openecomp.sdc.be.components.ArtifactsResolver;
34 import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
35 import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
36 import org.openecomp.sdc.be.components.csar.CsarInfo;
37 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
38 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
39 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
40 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
41 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
42 import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
43 import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
44 import org.openecomp.sdc.be.components.validation.UserValidations;
45 import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
46 import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
47 import org.openecomp.sdc.be.components.validation.component.ComponentFieldValidator;
48 import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator;
49 import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
50 import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator;
51 import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator;
52 import org.openecomp.sdc.be.components.validation.component.ComponentValidator;
53 import org.openecomp.sdc.be.config.ConfigurationManager;
54 import org.openecomp.sdc.be.dao.api.ActionStatus;
55 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
56 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
57 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
58 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
59 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
60 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
61 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
62 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
63 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
64 import org.openecomp.sdc.be.facade.operations.CatalogOperation;
65 import org.openecomp.sdc.be.impl.ComponentsUtils;
66 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
67 import org.openecomp.sdc.be.model.ArtifactDefinition;
68 import org.openecomp.sdc.be.model.Component;
69 import org.openecomp.sdc.be.model.DataTypeDefinition;
70 import org.openecomp.sdc.be.model.GroupDefinition;
71 import org.openecomp.sdc.be.model.InputDefinition;
72 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
73 import org.openecomp.sdc.be.model.LifecycleStateEnum;
74 import org.openecomp.sdc.be.model.PropertyDefinition;
75 import org.openecomp.sdc.be.model.Resource;
76 import org.openecomp.sdc.be.model.User;
77 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
78 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
79 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
80 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
81 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation;
82 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
83 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
84 import org.openecomp.sdc.be.model.operations.StorageException;
85 import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation;
86 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
87 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
88 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
89 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
90 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
91 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
92 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
93 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
94 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
95 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
96 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
97 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
98 import org.openecomp.sdc.be.tosca.CsarUtils;
99 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
100 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
101 import org.openecomp.sdc.be.user.Role;
102 import org.openecomp.sdc.be.user.UserBusinessLogic;
103 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
104 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
105 import org.openecomp.sdc.common.api.ConfigurationSource;
106 import org.openecomp.sdc.common.api.Constants;
107 import org.openecomp.sdc.common.impl.ExternalConfiguration;
108 import org.openecomp.sdc.common.impl.FSConfigurationSource;
109 import org.openecomp.sdc.common.util.GeneralUtility;
110 import org.openecomp.sdc.common.util.ValidationUtils;
111 import org.openecomp.sdc.exception.ResponseFormat;
112 import org.slf4j.Logger;
113 import org.slf4j.LoggerFactory;
114 import org.springframework.web.context.WebApplicationContext;
116 import javax.servlet.ServletContext;
117 import java.io.IOException;
118 import java.lang.reflect.Method;
119 import java.util.ArrayList;
120 import java.util.Arrays;
121 import java.util.EnumMap;
122 import java.util.HashMap;
123 import java.util.List;
124 import java.util.Map;
125 import java.util.stream.Collectors;
126 import java.util.stream.Stream;
128 import static org.assertj.core.api.Java6Assertions.assertThat;
129 import static org.junit.Assert.assertEquals;
130 import static org.junit.Assert.assertFalse;
131 import static org.junit.Assert.assertNotEquals;
132 import static org.junit.Assert.assertNotNull;
133 import static org.junit.Assert.assertTrue;
134 import static org.mockito.ArgumentMatchers.any;
135 import static org.mockito.ArgumentMatchers.anyString;
136 import static org.mockito.ArgumentMatchers.eq;
137 import static org.mockito.Mockito.times;
138 import static org.mockito.Mockito.when;
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);
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, componentDescriptionValidator);
322 bl.setElementDao(mockElementDao);
323 bl.setUserAdmin(mockUserAdmin);
324 bl.setCapabilityTypeOperation(capabilityTypeOperation);
325 bl.setComponentsUtils(componentsUtils);
326 bl.setLifecycleManager(lifecycleBl);
327 bl.setGraphLockOperation(graphLockOperation);
328 bl.setArtifactsManager(artifactManager);
329 bl.setPropertyOperation(propertyOperation);
330 bl.setJanusGraphDao(mockJanusGraphDao);
331 bl.setApplicationDataTypeCache(applicationDataTypeCache);
332 bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
333 bl.setCatalogOperations(catalogOperation);
334 toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
335 toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
336 bl.setToscaOperationFacade(toscaOperationFacade);
337 bl.setUserValidations(userValidations);
338 bl.setInterfaceTypeOperation(interfaceTypeOperation);
340 csarBusinessLogic.setCsarOperation(csarOperation);
341 Resource resourceCsar = createResourceObjectCsar(true);
342 setCanWorkOnResource(resourceCsar);
343 Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
344 when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
345 responseManager = ResponseFormatManager.getInstance();
346 bl.setComponentIconValidator(componentIconValidator);
347 bl.setComponentNameValidator(componentNameValidator);
348 bl.setComponentDescriptionValidator(componentDescriptionValidator);
349 bl.setComponentTagsValidator(componentTagsValidator);
350 bl.setComponentContactIdValidator(componentContactIdValidator);
351 bl.setComponentProjectCodeValidator(componentProjectCodeValidator);
352 bl.setComponentValidator(componentValidator);
353 reslist = new ArrayList<Resource>();
354 reslist.add(resourceResponse);
355 reslist.add(genericVF);
356 reslist.add(genericCR);
357 reslist.add(genericVFC);
358 reslist.add(genericPNF);
359 Either<List<Resource>, StorageOperationStatus> returneval= Either.left(reslist);
360 when(toscaOperationFacade.getAllCertifiedResources(true, true)).thenReturn(returneval);
361 when(toscaOperationFacade.validateComponentNameUniqueness("Resource", ResourceTypeEnum.CR, ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(true));
362 Either<List<Resource>, StorageOperationStatus> returnevalexception= Either.right(StorageOperationStatus.BAD_REQUEST);
363 when(toscaOperationFacade.getAllCertifiedResources(false, false)).thenReturn(returnevalexception);
367 private Resource createResourceObject(boolean afterCreate) {
368 Resource resource = new Resource();
369 resource.setName(RESOURCE_NAME);
370 resource.setToscaResourceName(RESOURCE_TOSCA_NAME);
371 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
372 resource.setDescription("My short description");
373 List<String> tgs = new ArrayList<>();
375 tgs.add(resource.getName());
376 resource.setTags(tgs);
377 List<String> template = new ArrayList<>();
378 template.add("tosca.nodes.Root");
379 resource.setDerivedFrom(template);
380 resource.setVendorName("Motorola");
381 resource.setVendorRelease("1.0.0");
382 resource.setContactId("ya5467");
383 resource.setIcon("defaulticon");
386 resource.setName(resource.getName());
387 resource.setVersion("0.1");
388 resource.setUniqueId(resource.getName()
389 .toLowerCase() + ":" + resource.getVersion());
390 resource.setCreatorUserId(user.getUserId());
391 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
392 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
397 private Resource createResourceObjectCsar(boolean afterCreate) {
398 Resource resource = new Resource();
399 resource.setName(RESOURCE_NAME);
400 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
401 resource.setDescription("My short description");
402 List<String> tgs = new ArrayList<>();
404 tgs.add(resource.getName());
405 resource.setTags(tgs);
406 List<String> template = new ArrayList<>();
407 template.add("tosca.nodes.Root");
408 resource.setDerivedFrom(template);
409 resource.setVendorName("Motorola");
410 resource.setVendorRelease("1.0.0");
411 resource.setResourceVendorModelNumber("");
412 resource.setContactId("ya5467");
413 resource.setIcon("MyIcon");
414 resource.setCsarUUID("valid_vf.csar");
415 resource.setCsarVersion("1");
418 resource.setName(resource.getName());
419 resource.setVersion("0.1");
421 resource.setUniqueId(resource.getName()
422 .toLowerCase() + ":" + resource.getVersion());
423 resource.setCreatorUserId(user.getUserId());
424 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
425 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
430 private Resource setCanWorkOnResource(Resource resource) {
431 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
432 resource.setLastUpdaterUserId(user.getUserId());
437 public void testHappyScenario() {
438 validateUserRoles(Role.ADMIN, Role.DESIGNER);
439 Resource resource = createResourceObject(false);
440 Resource createdResource = null;
442 createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
443 assertThat(createResourceObject(true)).isEqualTo(createdResource);
444 } catch (ComponentException e) {
445 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
451 public void testUpdateHappyScenario() {
452 Resource resource = createResourceObjectCsar(true);
453 setCanWorkOnResource(resource);
454 validateUserRoles(Role.ADMIN, Role.DESIGNER);
455 Either<Resource, StorageOperationStatus> resourceLinkedToCsarRes = Either.left(resource);
456 when(toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, resource.getCsarUUID(),
457 resource.getSystemName())).thenReturn(resourceLinkedToCsarRes);
458 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
459 when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(validateDerivedExists);
460 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
461 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
462 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
463 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
464 Resource createdResource = null;
466 createdResource = bl.validateAndUpdateResourceFromCsar(resource, user, null, null, resource.getUniqueId());
467 assertThat(resource.getUniqueId()).isEqualTo(createdResource.getUniqueId());
468 } catch (ComponentException e) {
469 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
474 /* CREATE validations - start ***********************/
475 // Resource name - start
478 public void testFailedResourceValidations() {
479 testResourceNameExist();
480 testResourceNameEmpty();
481 // testResourceNameExceedsLimit();
482 testResourceNameWrongFormat();
483 testResourceDescExceedsLimitCreate();
484 testResourceDescNotEnglish();
485 testResourceDescriptionEmpty();
486 testResourceDescriptionMissing();
487 testResourceIconMissing();
488 testResourceIconInvalid();
489 testResourceIconExceedsLimit();
490 testResourceTagNotExist();
491 testResourceTagEmpty();
492 testTagsExceedsLimitCreate();
493 testTagsNoServiceName();
496 testContactIdTooLong();
497 testContactIdWrongFormatCreate();
498 testResourceContactIdEmpty();
499 testResourceContactIdMissing();
500 testVendorNameExceedsLimit();
501 testVendorNameWrongFormatCreate();
502 testVendorReleaseWrongFormat();
503 testVendorReleaseExceedsLimitCreate();
504 testResourceVendorModelNumberExceedsLimit();
505 testResourceVendorNameMissing();
506 testResourceVendorReleaseMissing();
507 testResourceCategoryExist();
508 testResourceBadCategoryCreate();
509 testHappyScenarioCostLicenseType();
510 testCostWrongFormatCreate();
511 testLicenseTypeWrongFormatCreate();
512 testResourceTemplateNotExist();
513 testResourceTemplateEmpty();
514 testResourceTemplateInvalid();
517 private void testResourceNameExist() {
518 String resourceName = "alreadyExists";
519 Resource resourceExist = createResourceObject(false);
520 resourceExist.setName(resourceName);
521 resourceExist.getTags()
523 validateUserRoles(Role.ADMIN, Role.DESIGNER);
525 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
526 } catch (ComponentException e) {
527 assertComponentException(e, ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
528 ComponentTypeEnum.RESOURCE.getValue(), resourceName);
532 private void testResourceNameEmpty() {
533 Resource resourceExist = createResourceObject(false);
534 resourceExist.setName(null);
537 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
538 } catch (ComponentException e) {
539 assertComponentException(e, ActionStatus.MISSING_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
543 private void testResourceNameExceedsLimit() {
544 Resource resourceExccedsNameLimit = createResourceObject(false);
545 // 51 chars, the limit is 50
546 String tooLongResourceName = "zCRCAWjqte0DtgcAAMmcJcXeNubeX1p1vOZNTShAHOYNAHvV3iK";
547 resourceExccedsNameLimit.setName(tooLongResourceName);
550 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
551 } catch (ComponentException e) {
552 assertComponentException(e, ActionStatus.COMPONENT_NAME_EXCEEDS_LIMIT,
553 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_NAME_MAX_LENGTH);
557 private void testResourceNameWrongFormat() {
558 Resource resource = createResourceObject(false);
560 String nameWrongFormat = "ljg?fd";
561 resource.setName(nameWrongFormat);
564 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
565 } catch (ComponentException e) {
566 assertComponentException(e, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
570 // Resource name - end
571 // Resource description - start
572 private void testResourceDescExceedsLimitCreate() {
573 Resource resourceExccedsDescLimit = createResourceObject(false);
574 // 1025 chars, the limit is 1024
575 String tooLongResourceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
576 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
577 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
578 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
579 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
580 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
581 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
582 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
584 resourceExccedsDescLimit.setDescription(tooLongResourceDesc);
586 bl.createResource(resourceExccedsDescLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
587 } catch (ComponentException e) {
588 assertComponentException(e, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT,
589 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
593 private void testResourceDescNotEnglish() {
594 Resource notEnglish = createResourceObject(false);
596 String notEnglishDesc = "\uC2B5";
597 notEnglish.setDescription(notEnglishDesc);
600 bl.createResource(notEnglish, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
601 } catch (ComponentException e) {
602 assertComponentException(e, ActionStatus.COMPONENT_INVALID_DESCRIPTION,
603 ComponentTypeEnum.RESOURCE.getValue());
607 private void testResourceDescriptionEmpty() {
608 Resource resourceExist = createResourceObject(false);
609 resourceExist.setDescription("");
612 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
613 } catch (ComponentException e) {
614 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION,
615 ComponentTypeEnum.RESOURCE.getValue());
619 private void testResourceDescriptionMissing() {
620 Resource resourceExist = createResourceObject(false);
621 resourceExist.setDescription(null);
624 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
625 } catch (ComponentException e) {
626 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION,
627 ComponentTypeEnum.RESOURCE.getValue());
630 // Resource description - end
631 // Resource icon start
633 private void testResourceIconMissing() {
634 Resource resourceExist = createResourceObject(false);
635 resourceExist.setIcon(null);
638 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
639 } catch (ComponentException e) {
640 assertComponentException(e, ActionStatus.COMPONENT_MISSING_ICON, ComponentTypeEnum.RESOURCE.getValue());
644 private void testResourceIconInvalid() {
645 Resource resourceExist = createResourceObject(false);
646 resourceExist.setIcon("kjk3453^&");
649 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
650 } catch (ComponentException e) {
651 assertComponentException(e, ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.RESOURCE.getValue());
655 private void testResourceIconExceedsLimit() {
656 Resource resourceExist = createResourceObject(false);
657 resourceExist.setIcon("dsjfhskdfhskjdhfskjdhkjdhfkshdfksjsdkfhsdfsdfsdfsfsdfsf");
659 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
660 } catch (ComponentException e) {
661 assertComponentException(e, ActionStatus.COMPONENT_ICON_EXCEEDS_LIMIT,
662 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.ICON_MAX_LENGTH);
667 // Resource tags - start
668 private void testResourceTagNotExist() {
669 Resource resourceExist = createResourceObject(false);
670 resourceExist.setTags(null);
672 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
673 } catch (ComponentException e) {
674 assertComponentException(e, ActionStatus.COMPONENT_MISSING_TAGS);
678 private void testResourceTagEmpty() {
679 Resource resourceExist = createResourceObject(false);
680 resourceExist.setTags(new ArrayList<>());
682 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
683 } catch (ComponentException e) {
684 assertComponentException(e, ActionStatus.COMPONENT_MISSING_TAGS);
688 private void testTagsExceedsLimitCreate() {
689 Resource resourceExccedsNameLimit = createResourceObject(false);
690 String tag1 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjQ";
691 String tag2 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjW";
692 String tag3 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjE";
693 String tag4 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjb";
694 String tag5 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjr";
695 String tag6 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
696 String tag7 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
697 String tag8 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjd";
698 String tag9 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
699 String tag10 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
700 String tag11 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjh";
701 String tag12 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjj";
702 String tag13 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjk";
703 String tag14 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjs";
704 String tag15 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjz";
705 String tag16 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjx";
706 String tag17 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj2";
707 String tag18 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj3";
708 String tag19 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj4";
709 String tag20 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj5";
710 String tag21 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj0";
712 List<String> tagsList = new ArrayList<>();
734 tagsList.add(resourceExccedsNameLimit.getName());
736 resourceExccedsNameLimit.setTags(tagsList);
738 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
739 } catch (ComponentException e) {
740 assertComponentException(e, ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT,
741 "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
745 private void testTagsSingleExceedsLimit() {
746 Resource resourceExccedsNameLimit = createResourceObject(false);
747 String tag1 = "afzs2qLBb5X6tZhiunkcEwiFX1qRQY8YZl3y3Du5M5xeQY5Nq9afcFHDZ9HaURw43gH27nAUWM36bMbMylwTFSzzNV8NO4v4ripe6Q15Vc2nPOFI";
748 String tag2 = resourceExccedsNameLimit.getName();
749 List<String> tagsList = new ArrayList<>();
753 resourceExccedsNameLimit.setTags(tagsList);
755 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
756 } catch (ComponentException e) {
757 assertComponentException(e, ActionStatus.COMPONENT_SINGLE_TAG_EXCEED_LIMIT,
758 "" + ValidationUtils.TAG_MAX_LENGTH);
762 private void testTagsNoServiceName() {
763 Resource serviceExccedsNameLimit = createResourceObject(false);
764 String tag1 = "afzs2qLBb";
765 List<String> tagsList = new ArrayList<>();
767 serviceExccedsNameLimit.setTags(tagsList);
769 bl.createResource(serviceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
770 } catch (ComponentException e) {
771 assertComponentException(e, ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
775 private void testInvalidTag() {
776 Resource serviceExccedsNameLimit = createResourceObject(false);
777 String tag1 = "afzs2qLBb%#%";
778 List<String> tagsList = new ArrayList<>();
780 serviceExccedsNameLimit.setTags(tagsList);
782 bl.createResource(serviceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
783 } catch (ComponentException e) {
784 assertComponentException(e, ActionStatus.INVALID_FIELD_FORMAT, new String[] { "Resource", "tag" });
788 // Resource tags - stop
789 // Resource contact start
791 private void testContactIdTooLong() {
792 Resource resourceContactId = createResourceObject(false);
793 // 59 chars instead of 50
794 String contactIdTooLong = "thisNameIsVeryLongAndExeccedsTheNormalLengthForContactId";
795 resourceContactId.setContactId(contactIdTooLong);
798 bl.createResource(resourceContactId, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
799 } catch (ComponentException e) {
800 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
804 private void testContactIdWrongFormatCreate() {
805 Resource resourceContactId = createResourceObject(false);
806 // 3 letters and 3 digits and special characters
807 String contactIdFormatWrong = "yrt134!!!";
808 resourceContactId.setContactId(contactIdFormatWrong);
810 bl.createResource(resourceContactId, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
811 } catch (ComponentException e) {
812 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
816 private void testResourceContactIdEmpty() {
817 Resource resourceExist = createResourceObject(false);
818 resourceExist.setContactId("");
820 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
821 } catch (ComponentException e) {
822 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
826 private void testResourceContactIdMissing() {
827 Resource resourceExist = createResourceObject(false);
828 resourceExist.setContactId(null);
830 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
831 } catch (ComponentException e) {
832 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
836 private void testVendorNameExceedsLimit() {
837 Resource resourceExccedsVendorNameLimit = createResourceObject(false);
838 String tooLongVendorName = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
839 resourceExccedsVendorNameLimit.setVendorName(tooLongVendorName);
841 bl.createResource(resourceExccedsVendorNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
842 } catch (ComponentException e) {
843 assertComponentException(e, ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT,
844 "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
848 private void testResourceVendorModelNumberExceedsLimit() {
849 Resource resourceExccedsVendorModelNumberLimit = createResourceObject(false);
850 String tooLongVendorModelNumber = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
851 resourceExccedsVendorModelNumberLimit.setResourceVendorModelNumber(tooLongVendorModelNumber);
853 bl.createResource(resourceExccedsVendorModelNumberLimit, AuditingActionEnum.CREATE_RESOURCE, user, null,
855 } catch (ComponentException e) {
856 assertComponentException(e, ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
857 "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
861 private void testVendorNameWrongFormatCreate() {
862 Resource resource = createResourceObject(false);
864 String nameWrongFormat = "ljg*fd";
865 resource.setVendorName(nameWrongFormat);
867 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
868 } catch (ComponentException e) {
869 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
873 private void testVendorReleaseWrongFormat() {
874 Resource resource = createResourceObject(false);
876 String vendorReleaseWrongFormat = "1>2";
877 resource.setVendorRelease(vendorReleaseWrongFormat);
879 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
880 } catch (ComponentException e) {
881 assertComponentException(e, ActionStatus.INVALID_VENDOR_RELEASE, vendorReleaseWrongFormat);
885 private void testVendorReleaseExceedsLimitCreate() {
886 Resource resourceExccedsNameLimit = createResourceObject(false);
887 String tooLongVendorRelease = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
888 resourceExccedsNameLimit.setVendorRelease(tooLongVendorRelease);
890 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
891 } catch (ComponentException e) {
892 assertComponentException(e, ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT,
893 "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
897 private void testResourceVendorNameMissing() {
898 Resource resourceExist = createResourceObject(false);
899 resourceExist.setVendorName(null);
901 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
902 } catch (ComponentException e) {
903 assertComponentException(e, ActionStatus.MISSING_VENDOR_NAME);
907 private void testResourceVendorReleaseMissing() {
908 Resource resourceExist = createResourceObject(false);
909 resourceExist.setVendorRelease(null);
911 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
912 } catch (ComponentException e) {
913 assertComponentException(e, ActionStatus.MISSING_VENDOR_RELEASE);
917 // Resource vendor name/release stop
919 private void testResourceCategoryExist() {
920 Resource resourceExist = createResourceObject(false);
921 resourceExist.setCategories(null);
923 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
924 } catch (ComponentException e) {
925 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
929 private void testResourceBadCategoryCreate() {
931 Resource resourceExist = createResourceObject(false);
932 resourceExist.setCategories(null);
933 resourceExist.addCategory("koko", "koko");
935 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
936 } catch (ComponentException e) {
937 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
943 private void testHappyScenarioCostLicenseType() {
944 Resource createResourceObject = createResourceObject(false);
945 Resource createResourceObjectAfterCreate = createResourceObject(true);
946 // Adding cost and licenseType to basic mock
947 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(createResourceObjectAfterCreate);
948 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
950 String cost = "123.456";
951 String licenseType = "User";
952 createResourceObject.setCost(cost);
953 createResourceObject.setLicenseType(licenseType);
954 Resource createdResource;
956 createdResource = bl.createResource(createResourceObject, AuditingActionEnum.CREATE_RESOURCE, user, null,
958 createResourceObjectAfterCreate.setCost(cost);
959 createResourceObjectAfterCreate.setLicenseType(licenseType);
960 assertThat(createResourceObjectAfterCreate).isEqualTo(createdResource);
961 } catch (ComponentException e) {
962 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
967 private void testCostWrongFormatCreate() {
968 Resource resourceCost = createResourceObject(false);
969 // Comma instead of fullstop
970 String cost = "12356,464";
971 resourceCost.setCost(cost);
973 bl.createResource(resourceCost, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
974 } catch (ComponentException e) {
975 assertComponentException(e, ActionStatus.INVALID_CONTENT);
980 // License type start
981 private void testLicenseTypeWrongFormatCreate() {
982 Resource resourceLicenseType = createResourceObject(false);
984 String licenseType = "cpu";
985 resourceLicenseType.setLicenseType(licenseType);
987 bl.createResource(resourceLicenseType, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
988 } catch (ComponentException e) {
989 assertComponentException(e, ActionStatus.INVALID_CONTENT);
994 // Derived from start
995 private void testResourceTemplateNotExist() {
996 Resource resourceExist = createResourceObject(false);
997 List<String> list = null;
998 resourceExist.setDerivedFrom(list);
1000 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1001 } catch (ComponentException e) {
1002 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1006 private void testResourceTemplateEmpty() {
1007 Resource resourceExist = createResourceObject(false);
1008 resourceExist.setDerivedFrom(new ArrayList<>());
1010 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1011 } catch (ComponentException e) {
1012 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1016 private void testResourceTemplateInvalid() {
1017 Resource resourceExist = createResourceObject(false);
1018 ArrayList<String> derivedFrom = new ArrayList<>();
1019 derivedFrom.add("kuku");
1020 resourceExist.setDerivedFrom(derivedFrom);
1022 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1023 } catch (ComponentException e) {
1024 assertComponentException(e, ActionStatus.PARENT_RESOURCE_NOT_FOUND);
1028 // Derived from stop
1029 private void assertComponentException(ComponentException e, ActionStatus expectedStatus, String... variables) {
1030 ResponseFormat actualResponse = e.getResponseFormat() != null ? e.getResponseFormat()
1031 : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
1032 assertResponse(actualResponse, expectedStatus, variables);
1035 private void assertResponse(ResponseFormat actualResponse, ActionStatus expectedStatus, String... variables) {
1036 ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables);
1037 assertThat(expectedResponse.getStatus()).isEqualTo(actualResponse.getStatus());
1038 assertThat(expectedResponse.getFormattedMessage()).isEqualTo(actualResponse.getFormattedMessage());
1041 private void assertResponse(Either<Resource, ResponseFormat> createResponse, ActionStatus expectedStatus,
1042 String... variables) {
1043 assertResponse(createResponse.right()
1044 .value(), expectedStatus, variables);
1047 // UPDATE tests - start
1050 public void testResourceNameWrongFormat_UPDATE() {
1051 Resource resource = createResourceObject(true);
1052 Resource updatedResource = createResourceObject(true);
1054 // this is in order to prevent failing with 403 earlier
1055 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1056 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1058 String nameWrongFormat = "ljg*fd";
1059 updatedResource.setName(nameWrongFormat);
1061 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1062 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1064 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1065 } catch (ComponentException e) {
1066 assertComponentException(e, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
1071 public void testResourceNameAfterCertify_UPDATE() {
1072 Resource resource = createResourceObject(true);
1073 Resource updatedResource = createResourceObject(true);
1075 // this is in order to prevent failing with 403 earlier
1076 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1077 // when(resourceOperation.getResource_tx(resource.getUniqueId(),false)).thenReturn(eitherUpdate);
1078 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1080 String name = "ljg";
1081 updatedResource.setName(name);
1082 resource.setVersion("1.0");
1084 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1085 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1087 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1088 } catch (ComponentException e) {
1089 assertComponentException(e, ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED);
1094 public void testResourceNameAlreadyExist_UPDATE() {
1095 Resource resource = createResourceObject(true);
1096 Resource updatedResource = createResourceObject(true);
1098 // this is in order to prevent failing with 403 earlier
1099 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1100 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1102 String resourceName = "alreadyExists";
1103 updatedResource.setName(resourceName);
1104 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(updatedResource);
1105 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1107 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1108 } catch (ComponentException e) {
1109 assertComponentException(e, ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
1110 ComponentTypeEnum.RESOURCE.getValue(), resourceName);
1117 public void testResourceDescExceedsLimit_UPDATE() {
1118 Resource resource = createResourceObject(true);
1119 Resource updatedResource = createResourceObject(true);
1121 // this is in order to prevent failing with 403 earlier
1122 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1123 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1125 // 1025 chars, the limit is 1024
1126 String tooLongResourceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
1127 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
1128 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
1129 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
1130 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
1131 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
1132 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
1133 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
1134 updatedResource.setDescription(tooLongResourceDesc);
1135 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1136 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1138 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1139 } catch (ComponentException e) {
1140 assertComponentException(e, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT,
1141 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
1146 public void testIconWrongFormat_UPDATE() {
1147 Resource resource = createResourceObject(true);
1148 Resource updatedResource = createResourceObject(true);
1150 // this is in order to prevent failing with 403 earlier
1151 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1152 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1155 String icon = "icon.jpg";
1156 updatedResource.setIcon(icon);
1157 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1158 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1160 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1161 } catch (ComponentException e) {
1162 assertComponentException(e, ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.RESOURCE.getValue());
1167 public void testIconAfterCertify_UPDATE() {
1168 Resource resource = createResourceObject(true);
1169 Resource updatedResource = createResourceObject(true);
1171 // this is in order to prevent failing with 403 earlier
1172 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1173 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1176 String icon = "icon";
1177 updatedResource.setIcon(icon);
1179 resource.setVersion("1.0");
1181 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1182 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1184 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1185 } catch (ComponentException e) {
1186 assertComponentException(e, ActionStatus.RESOURCE_ICON_CANNOT_BE_CHANGED);
1191 public void testTagsExceedsLimit_UPDATE() {
1192 Resource resource = createResourceObject(true);
1193 Resource updatedResource = createResourceObject(true);
1195 // this is in order to prevent failing with 403 earlier
1196 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1197 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1199 String tag1 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjQ";
1200 String tag2 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjW";
1201 String tag3 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjE";
1202 String tag4 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjb";
1203 String tag5 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjr";
1204 String tag6 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
1205 String tag7 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
1206 String tag8 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjd";
1207 String tag9 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
1208 String tag10 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
1209 String tag11 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjh";
1210 String tag12 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjj";
1211 String tag13 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjk";
1212 String tag14 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjs";
1213 String tag15 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjz";
1214 String tag16 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjx";
1215 String tag17 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj2";
1216 String tag18 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj3";
1217 String tag19 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj4";
1218 String tag20 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj5";
1219 String tag21 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj0";
1221 List<String> tagsList = new ArrayList<>();
1231 tagsList.add(tag10);
1232 tagsList.add(tag11);
1233 tagsList.add(tag12);
1234 tagsList.add(tag13);
1235 tagsList.add(tag14);
1236 tagsList.add(tag15);
1237 tagsList.add(tag16);
1238 tagsList.add(tag17);
1239 tagsList.add(tag18);
1240 tagsList.add(tag19);
1241 tagsList.add(tag20);
1242 tagsList.add(tag21);
1243 tagsList.add(resource.getName());
1245 updatedResource.setTags(tagsList);
1246 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1247 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1249 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1250 } catch (ComponentException e) {
1251 assertComponentException(e, ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT,
1252 "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
1257 public void testVendorNameWrongFormat_UPDATE() {
1258 Resource resource = createResourceObject(true);
1259 Resource updatedResource = createResourceObject(true);
1261 // this is in order to prevent failing with 403 earlier
1262 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1263 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1266 String nameWrongFormat = "ljg*fd";
1267 updatedResource.setVendorName(nameWrongFormat);
1268 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1269 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1271 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1272 } catch (ComponentException e) {
1273 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
1278 public void testVendorNameWrongFormat() {
1279 Resource resource = createResourceObject(true);
1280 Resource updatedResource = createResourceObject(true);
1282 // this is in order to prevent failing with 403 earlier
1283 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1284 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1287 String nameWrongFormat = "ljg*fd";
1288 updatedResource.setVendorName(nameWrongFormat);
1289 resource.setVersion("1.0");
1291 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1292 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1294 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1295 } catch (ComponentException e) {
1296 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
1301 public void testVendorReleaseExceedsLimit_UPDATE() {
1302 Resource resource = createResourceObject(true);
1303 Resource updatedResource = createResourceObject(true);
1305 // this is in order to prevent failing with 403 earlier
1306 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1307 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1308 // 129 chars, the limit is 128
1309 String tooLongVendorRelease = "h1KSyJh9EspI8SPwAGu4VETfqWejeanuB1PCJBxJmJncYnrW0lnsEFFVRIukRJkwlOVnZCy8p38tjhANeZq3BGMHIawWR6ICl8Wi9mikRYALWgvJug00JrlQ0iPVKPLxy";
1310 updatedResource.setVendorRelease(tooLongVendorRelease);
1311 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1312 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1314 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1315 } catch (ComponentException e) {
1316 assertComponentException(e, ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT,
1317 "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
1322 public void testResourceBadCategory_UPDATE() {
1323 Resource resource = createResourceObject(true);
1324 Resource updatedResource = createResourceObject(true);
1326 // this is in order to prevent failing with 403 earlier
1327 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1328 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1330 String resourceId = resource.getUniqueId();
1331 String badCategory = "ddfds";
1332 updatedResource.setCategories(null);
1333 updatedResource.addCategory(badCategory, "fikt");
1334 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1335 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1337 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1338 } catch (ComponentException e) {
1339 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
1344 public void testResourceCategoryAfterCertify_UPDATE() {
1345 Resource resource = createResourceObject(true);
1346 Resource updatedResource = createResourceObject(true);
1348 // this is in order to prevent failing with 403 earlier
1349 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1350 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1352 String resourceId = resource.getUniqueId();
1353 updatedResource.setCategories(null);
1354 updatedResource.addCategory(RESOURCE_CATEGORY1, UPDATED_SUBCATEGORY);
1355 resource.setVersion("1.0");
1357 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1358 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1360 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1361 } catch (ComponentException e) {
1362 assertComponentException(e, ActionStatus.RESOURCE_CATEGORY_CANNOT_BE_CHANGED);
1366 // Derived from start
1368 public void testResourceTemplateNotExist_UPDATE() {
1369 Resource resource = createResourceObject(true);
1370 Resource updatedResource = createResourceObject(true);
1372 // this is in order to prevent failing with 403 earlier
1373 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1374 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1375 String resourceId = resource.getUniqueId();
1377 List<String> list = null;
1378 updatedResource.setDerivedFrom(list);
1379 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1380 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1382 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1383 } catch (ComponentException e) {
1384 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1389 public void testResourceTemplateEmpty_UPDATE() {
1390 Resource resource = createResourceObject(true);
1391 Resource updatedResource = createResourceObject(true);
1392 String resourceId = resource.getUniqueId();
1394 // this is in order to prevent failing with 403 earlier
1395 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1396 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1398 updatedResource.setDerivedFrom(new ArrayList<>());
1399 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1400 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1402 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1403 } catch (ComponentException e) {
1404 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1409 public void testResourceTemplateInvalid_UPDATE() {
1410 Resource resource = createResourceObject(true);
1411 Resource updatedResource = createResourceObject(true);
1412 String resourceId = resource.getUniqueId();
1414 // this is in order to prevent failing with 403 earlier
1415 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1416 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1418 ArrayList<String> derivedFrom = new ArrayList<>();
1419 derivedFrom.add("kuku");
1420 updatedResource.setDerivedFrom(derivedFrom);
1421 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1422 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1424 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1425 } catch (ComponentException e) {
1426 assertComponentException(e, ActionStatus.PARENT_RESOURCE_NOT_FOUND);
1431 public void testResourceTemplateCertify_UPDATE_HAPPY() {
1432 Resource resource = createResourceObject(true);
1433 Resource updatedResource = createResourceObject(true);
1434 String resourceId = resource.getUniqueId();
1436 // this is in order to prevent failing with 403 earlier
1437 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1438 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1440 Either<Boolean, StorageOperationStatus> isToscaNameExtending = Either.left(true);
1441 when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString()))
1442 .thenReturn(isToscaNameExtending);
1444 Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = Either
1445 .left(new HashMap<>());
1446 when(propertyOperation.deleteAllPropertiesAssociatedToNode(any(NodeTypeEnum.class), anyString()))
1447 .thenReturn(findPropertiesOfNode);
1449 resource.setVersion("1.0");
1451 ArrayList<String> derivedFrom = new ArrayList<>();
1452 derivedFrom.add("tosca.nodes.Root");
1453 updatedResource.setDerivedFrom(derivedFrom);
1454 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(updatedResource);
1455 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1456 Resource createdResource = bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1457 assertThat(createdResource).isNotNull();
1461 public void testResourceTemplateCertify_UPDATE_SAD() {
1462 Resource resource = createResourceObject(true);
1463 Resource updatedResource = createResourceObject(true);
1464 String resourceId = resource.getUniqueId();
1466 // this is in order to prevent failing with 403 earlier
1467 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1468 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1470 Either<Boolean, StorageOperationStatus> isToscaNameExtending = Either.left(false);
1471 when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString()))
1472 .thenReturn(isToscaNameExtending);
1474 resource.setVersion("1.0");
1476 ArrayList<String> derivedFrom = new ArrayList<>();
1477 derivedFrom.add("tosca.nodes.Root");
1478 updatedResource.setDerivedFrom(derivedFrom);
1479 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1480 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1481 Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = Either
1482 .left(new HashMap<>());
1483 when(propertyOperation.deleteAllPropertiesAssociatedToNode(any(NodeTypeEnum.class), anyString()))
1484 .thenReturn(findPropertiesOfNode);
1487 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1488 } catch (ComponentException e) {
1489 assertComponentException(e, ActionStatus.PARENT_RESOURCE_DOES_NOT_EXTEND);
1492 // Derived from stop
1495 public void createOrUpdateResourceAlreadyCheckout() {
1497 Resource resourceExist = createResourceObject(false);
1498 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1499 Resource createdResource = bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null,
1501 createdResource.setLastUpdaterUserId(user.getUserId());
1502 assertThat(createdResource).isNotNull();
1503 Either<Resource, StorageOperationStatus> getLatestResult = Either.left(createdResource);
1504 Either<Component, StorageOperationStatus> getCompLatestResult = Either.left(createdResource);
1505 when(toscaOperationFacade.getLatestByToscaResourceName(resourceExist.getToscaResourceName()))
1506 .thenReturn(getCompLatestResult);
1507 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1508 .thenReturn(getLatestResult);
1510 Resource resourceToUpdtae = createResourceObject(false);
1512 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1513 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1514 assertNotNull(createOrUpdateResource);
1516 Mockito.verify(toscaOperationFacade, Mockito.times(1))
1517 .overrideComponent(any(Resource.class), any(Resource.class));
1518 Mockito.verify(lifecycleBl, Mockito.times(0))
1519 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1520 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1525 public void createOrUpdateResourceCertified() {
1527 Resource resourceExist = createResourceObject(false);
1528 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1529 Resource createdResource = bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null,
1532 assertThat(createdResource).isNotNull();
1533 createdResource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1534 createdResource.setVersion("1.0");
1536 Either<Resource, StorageOperationStatus> getLatestResult = Either.left(createdResource);
1537 Either<Component, StorageOperationStatus> getCompLatestResult = Either.left(createdResource);
1538 when(toscaOperationFacade.getLatestByToscaResourceName(resourceExist.getToscaResourceName()))
1539 .thenReturn(getCompLatestResult);
1540 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1541 .thenReturn(getLatestResult);
1543 when(lifecycleBl.changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1544 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean()))
1545 .thenReturn(Either.left(createdResource));
1547 Resource resourceToUpdtae = createResourceObject(false);
1549 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1550 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1551 assertNotNull(createOrUpdateResource);
1553 Mockito.verify(toscaOperationFacade, Mockito.times(1))
1554 .overrideComponent(any(Resource.class), any(Resource.class));
1555 Mockito.verify(lifecycleBl, Mockito.times(1))
1556 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1557 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1562 public void createOrUpdateResourceNotExist() {
1563 Resource resourceToUpdtae = createResourceObject(false);
1565 Either<Component, StorageOperationStatus> getLatestResult = Either.right(StorageOperationStatus.NOT_FOUND);
1566 when(toscaOperationFacade.getLatestByName(resourceToUpdtae.getName())).thenReturn(getLatestResult);
1568 Either<Component, StorageOperationStatus> getLatestToscaNameResult = Either
1569 .right(StorageOperationStatus.NOT_FOUND);
1570 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdtae.getToscaResourceName()))
1571 .thenReturn(getLatestToscaNameResult);
1573 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1574 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1575 assertThat(createOrUpdateResource).isNotNull();
1577 Mockito.verify(toscaOperationFacade, times(1))
1578 .createToscaComponent(eq(resourceToUpdtae));
1579 Mockito.verify(toscaOperationFacade, Mockito.times(0))
1580 .overrideComponent(any(Resource.class), any(Resource.class));
1581 Mockito.verify(lifecycleBl, Mockito.times(0))
1582 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1583 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1588 public void updateNestedResource_typeIsNew() throws IOException {
1589 Resource resourceToUpdate = createResourceObject(false);
1590 String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + "." + "abc";
1591 String jsonContent = ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml");
1592 CsarInfo csarInfo = new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", jsonContent,
1594 String nestedResourceName = bl.buildNestedToscaResourceName(resourceToUpdate.getResourceType()
1595 .name(), csarInfo.getVfResourceName(), nodeName)
1597 when(toscaOperationFacade.getLatestByName(resourceToUpdate.getName()))
1598 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1599 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdate.getToscaResourceName()))
1600 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1601 when(toscaOperationFacade.getLatestByToscaResourceName(nestedResourceName))
1602 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1604 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1605 .createOrUpdateResourceByImport(resourceToUpdate, user, false, false, false, csarInfo, nodeName, false);
1606 assertThat(createOrUpdateResource).isNotNull();
1608 Mockito.verify(toscaOperationFacade, times(1))
1609 .createToscaComponent(eq(resourceToUpdate));
1610 Mockito.verify(toscaOperationFacade, times(0))
1611 .overrideComponent(any(Resource.class), any(Resource.class));
1612 Mockito.verify(lifecycleBl, times(0))
1613 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1614 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1618 public void updateNestedResource_typeExists() throws IOException {
1620 Resource resourceToUpdate = createResourceObject(false);
1621 setCanWorkOnResource(resourceResponse);
1622 String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + "." + "abc";
1623 String jsonContent = ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml");
1624 CsarInfo csarInfo = new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", jsonContent,
1626 String nestedResourceName = bl.buildNestedToscaResourceName(resourceToUpdate.getResourceType()
1627 .name(), csarInfo.getVfResourceName(), nodeName)
1629 when(toscaOperationFacade.getLatestByName(resourceToUpdate.getName()))
1630 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1631 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdate.getToscaResourceName()))
1632 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1633 when(toscaOperationFacade.getLatestByToscaResourceName(nestedResourceName))
1634 .thenReturn(Either.left(resourceResponse));
1635 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1636 .thenReturn(Either.left(resourceResponse));
1638 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1639 .createOrUpdateResourceByImport(resourceToUpdate, user, false, false, false, csarInfo, nodeName, false);
1640 assertThat(createOrUpdateResource).isNotNull();
1641 Mockito.verify(toscaOperationFacade, times(1))
1642 .overrideComponent(any(Resource.class), any(Resource.class));
1643 Mockito.verify(lifecycleBl, times(0))
1644 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1645 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1649 public void testValidatePropertiesDefaultValues_SuccessfullWithoutProperties() {
1650 Resource basic = createResourceObject(true);
1652 Boolean validatePropertiesDefaultValues = bl.validatePropertiesDefaultValues(basic);
1653 assertTrue(validatePropertiesDefaultValues);
1657 public void testValidatePropertiesDefaultValues_SuccessfullWithProperties() {
1658 Resource basic = createResourceObject(true);
1659 PropertyDefinition property = new PropertyDefinition();
1660 property.setName("myProperty");
1661 property.setType(ToscaPropertyType.INTEGER.getType());
1662 property.setDefaultValue("1");
1663 List<PropertyDefinition> properties = new ArrayList<>();
1664 properties.add(property);
1665 basic.setProperties(properties);
1666 when(propertyOperation.isPropertyTypeValid(property)).thenReturn(true);
1667 when(propertyOperation.isPropertyDefaultValueValid(property, emptyDataTypes)).thenReturn(true);
1668 Boolean validatePropertiesDefaultValues = bl.validatePropertiesDefaultValues(basic);
1669 assertTrue(validatePropertiesDefaultValues);
1672 @Test(expected = ComponentException.class)
1673 public void testValidatePropertiesDefaultValues_FailedWithProperties() {
1674 Resource basic = createResourceObject(true);
1675 PropertyDefinition property = new PropertyDefinition();
1676 property.setName("myProperty");
1677 property.setType(ToscaPropertyType.INTEGER.getType());
1678 property.setDefaultValue("1.5");
1679 List<PropertyDefinition> properties = new ArrayList<>();
1680 properties.add(property);
1681 basic.setProperties(properties);
1683 when(propertyOperation.isPropertyDefaultValueValid(property, emptyDataTypes)).thenReturn(false);
1684 bl.validatePropertiesDefaultValues(basic);
1688 // public void testDeleteMarkedResourcesNoResources() {
1689 // List<GraphVertex> ids = new ArrayList<>();
1690 // Either<List<GraphVertex>, StorageOperationStatus> eitherNoResources =
1691 // Either.left(ids);
1692 // when(topologyTemplateOperation.getAllComponentsMarkedForDeletion(ComponentTypeEnum.RESOURCE)).thenReturn(eitherNoResources);
1694 // Either<List<String>, ResponseFormat> deleteMarkedResources =
1695 // bl.deleteMarkedComponents();
1696 // assertTrue(deleteMarkedResources.isLeft());
1697 // assertTrue(deleteMarkedResources.left().value().isEmpty());
1699 // Mockito.verify(artifactManager,
1700 // Mockito.times(0)).deleteAllComponentArtifactsIfNotOnGraph(Mockito.anyList());
1705 // public void testDeleteMarkedResources() {
1706 // List<String> ids = new ArrayList<String>();
1707 // String resourceInUse = "123";
1708 // ids.add(resourceInUse);
1709 // String resourceFree = "456";
1710 // ids.add(resourceFree);
1711 // Either<List<String>, StorageOperationStatus> eitherNoResources =
1712 // Either.left(ids);
1713 // when(toscaOperationFacade.getAllComponentsMarkedForDeletion()).thenReturn(eitherNoResources);
1715 // Either<Boolean, StorageOperationStatus> resourceInUseResponse =
1716 // Either.left(true);
1717 // Either<Boolean, StorageOperationStatus> resourceFreeResponse =
1718 // Either.left(false);
1720 // List<ArtifactDefinition> artifacts = new ArrayList<ArtifactDefinition>();
1721 // Either<List<ArtifactDefinition>, StorageOperationStatus>
1722 // getArtifactsResponse = Either.left(artifacts);
1723 // when(toscaOperationFacade.getComponentArtifactsForDelete(resourceFree,
1724 // NodeTypeEnum.Resource, true)).thenReturn(getArtifactsResponse);
1726 // when(toscaOperationFacade.isComponentInUse(resourceFree)).thenReturn(resourceFreeResponse);
1727 // when(toscaOperationFacade.isComponentInUse(resourceInUse)).thenReturn(resourceInUseResponse);
1729 // Either<Component, StorageOperationStatus> eitherDelete = Either.left(new
1731 // when(toscaOperationFacade.deleteToscaComponent(resourceFree)).thenReturn(eitherDelete);
1733 // when(artifactManager.deleteAllComponentArtifactsIfNotOnGraph(artifacts)).thenReturn(StorageOperationStatus.OK);
1734 // List<String> deletedComponents = new ArrayList<>();
1735 // deletedComponents.add(resourceFree);
1736 // when(toscaOperationFacade.deleteMarkedElements(ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(deletedComponents));
1738 // Either<List<String>, ResponseFormat> deleteMarkedResources =
1739 // bl.deleteMarkedComponents();
1740 // assertTrue(deleteMarkedResources.isLeft());
1741 // List<String> resourceIdList = deleteMarkedResources.left().value();
1742 // assertFalse(resourceIdList.isEmpty());
1743 // assertTrue(resourceIdList.contains(resourceFree));
1744 // assertFalse(resourceIdList.contains(resourceInUse));
1746 // Mockito.verify(artifactManager,
1747 // Mockito.times(1)).deleteAllComponentArtifactsIfNotOnGraph(artifacts);
1750 @SuppressWarnings("unchecked")
1752 public void testFindVfCsarArtifactsToHandle() {
1754 Class<ResourceBusinessLogic> targetClass = ResourceBusinessLogic.class;
1755 String methodName = "findVfCsarArtifactsToHandle";
1756 Resource resource = new Resource();
1757 String deploymentArtifactToUpdateFileName = "deploymentArtifactToUpdate.yaml";
1758 String deploymentArtifactToDeleteFileName = "deploymentArtifactToDelete.yaml";
1759 String deploymentArtifactToCreateFileName = "deploymentArtifactToCreate.yaml";
1761 String artifactInfoToUpdateFileName = "infoArtifactToUpdate.yaml";
1762 String artifactInfoToDeleteFileName = "infoArtifactToDelete.yaml";
1763 String artifactInfoToNotDeleteFileName = "infoArtifactNotToDelete.yaml";
1764 String artifactInfoToCreateFileName = "infoArtifactToCreate.yaml";
1766 byte[] oldPayloadData = "oldPayloadData".getBytes();
1767 byte[] newPayloadData = "newPayloadData".getBytes();
1768 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
1770 ArtifactDefinition deploymentArtifactToUpdate = new ArtifactDefinition();
1771 deploymentArtifactToUpdate.setMandatory(false);
1772 deploymentArtifactToUpdate.setArtifactName(deploymentArtifactToUpdateFileName);
1773 deploymentArtifactToUpdate.setArtifactType("SNMP_POLL");
1774 deploymentArtifactToUpdate.setPayload(oldPayloadData);
1775 deploymentArtifactToUpdate
1776 .setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1778 ArtifactDefinition deploymentArtifactToDelete = new ArtifactDefinition();
1779 deploymentArtifactToDelete.setMandatory(false);
1780 deploymentArtifactToDelete.setArtifactName(deploymentArtifactToDeleteFileName);
1781 deploymentArtifactToDelete.setArtifactType("SNMP_TRAP");
1782 deploymentArtifactToDelete.setPayload(oldPayloadData);
1783 deploymentArtifactToDelete
1784 .setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1786 ArtifactDefinition deploymentArtifactToIgnore = new ArtifactDefinition();
1788 deploymentArtifacts.put(ValidationUtils.normalizeArtifactLabel(deploymentArtifactToUpdate.getArtifactName()),
1789 deploymentArtifactToUpdate);
1790 deploymentArtifacts.put(ValidationUtils.normalizeArtifactLabel(deploymentArtifactToDelete.getArtifactName()),
1791 deploymentArtifactToDelete);
1792 deploymentArtifacts.put("ignore", deploymentArtifactToIgnore);
1794 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
1796 ArtifactDefinition artifactToUpdate = new ArtifactDefinition();
1797 artifactToUpdate.setMandatory(false);
1798 artifactToUpdate.setArtifactName(artifactInfoToUpdateFileName);
1799 artifactToUpdate.setArtifactType("SNMP_POLL");
1800 artifactToUpdate.setPayload(oldPayloadData);
1801 artifactToUpdate.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1803 ArtifactDefinition artifactToDelete = new ArtifactDefinition();
1804 artifactToDelete.setMandatory(false);
1805 artifactToDelete.setArtifactName(artifactInfoToDeleteFileName);
1806 artifactToDelete.setArtifactType("SNMP_TRAP");
1807 artifactToDelete.setPayload(oldPayloadData);
1808 artifactToDelete.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1809 artifactToDelete.setIsFromCsar(true);
1811 ArtifactDefinition artifactToNotDelete = new ArtifactDefinition();
1812 artifactToNotDelete.setMandatory(false);
1813 artifactToNotDelete.setArtifactName(artifactInfoToNotDeleteFileName);
1814 artifactToNotDelete.setArtifactType("SNMP_TRAP");
1815 artifactToNotDelete.setPayload(oldPayloadData);
1816 artifactToNotDelete.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
1817 artifactToNotDelete.setIsFromCsar(false);
1819 ArtifactDefinition artifactToIgnore = new ArtifactDefinition();
1821 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToUpdate.getArtifactName()), artifactToUpdate);
1822 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToDelete.getArtifactName()), artifactToDelete);
1823 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToNotDelete.getArtifactName()),
1824 artifactToNotDelete);
1825 artifacts.put("ignore", artifactToIgnore);
1827 resource.setDeploymentArtifacts(deploymentArtifacts);
1828 resource.setArtifacts(artifacts);
1830 List<NonMetaArtifactInfo> artifactPathAndNameList = new ArrayList<>();
1831 NonMetaArtifactInfo deploymentArtifactInfoToUpdate = new NonMetaArtifactInfo(
1832 deploymentArtifactToUpdate.getArtifactName(), null,
1833 ArtifactTypeEnum.findType(deploymentArtifactToUpdate.getArtifactType()),
1834 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, deploymentArtifactToUpdate.getArtifactName(), false);
1836 NonMetaArtifactInfo informationalArtifactInfoToUpdate = new NonMetaArtifactInfo(
1837 artifactToUpdate.getArtifactName(), null, ArtifactTypeEnum.findType(artifactToUpdate.getArtifactType()),
1838 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, artifactToUpdate.getArtifactName(), false);
1840 NonMetaArtifactInfo informationalArtifactInfoToUpdateFromCsar = new NonMetaArtifactInfo(
1841 artifactToUpdate.getArtifactName(), null, ArtifactTypeEnum.findType(artifactToUpdate.getArtifactType()),
1842 ArtifactGroupTypeEnum.INFORMATIONAL, newPayloadData, artifactToUpdate.getArtifactName(), true);
1844 NonMetaArtifactInfo deploymentArtifactInfoToUpdateFromCsar = new NonMetaArtifactInfo(
1845 artifactToUpdate.getArtifactName(), null, ArtifactTypeEnum.findType(artifactToUpdate.getArtifactType()),
1846 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, artifactToUpdate.getArtifactName(), true);
1848 NonMetaArtifactInfo deploymentArtifactInfoToCreate = new NonMetaArtifactInfo(deploymentArtifactToCreateFileName,
1849 null, ArtifactTypeEnum.OTHER, ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData,
1850 deploymentArtifactToCreateFileName, false);
1852 NonMetaArtifactInfo informationalArtifactInfoToCreate = new NonMetaArtifactInfo(artifactInfoToCreateFileName,
1853 null, ArtifactTypeEnum.OTHER, ArtifactGroupTypeEnum.INFORMATIONAL, newPayloadData,
1854 artifactInfoToCreateFileName, false);
1856 artifactPathAndNameList.add(deploymentArtifactInfoToUpdate);
1857 artifactPathAndNameList.add(informationalArtifactInfoToUpdate);
1858 artifactPathAndNameList.add(deploymentArtifactInfoToCreate);
1859 artifactPathAndNameList.add(informationalArtifactInfoToCreate);
1860 artifactPathAndNameList.add(informationalArtifactInfoToUpdateFromCsar);
1861 artifactPathAndNameList.add(deploymentArtifactInfoToUpdateFromCsar);
1863 Object[] argObjects = { resource, artifactPathAndNameList, user };
1864 Class[] argClasses = { Resource.class, List.class, User.class };
1866 Method method = targetClass.getDeclaredMethod(methodName, argClasses);
1867 method.setAccessible(true);
1868 Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = (Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat>) method
1869 .invoke(bl, argObjects);
1870 assertTrue(findVfCsarArtifactsToHandleRes.isLeft());
1871 EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> foundVfArtifacts = findVfCsarArtifactsToHandleRes
1874 assertEquals(4, foundVfArtifacts.get(ArtifactOperationEnum.CREATE)
1876 assertEquals(4, foundVfArtifacts.get(ArtifactOperationEnum.UPDATE)
1878 assertEquals(1, foundVfArtifacts.get(ArtifactOperationEnum.DELETE)
1881 } catch (Exception e) {
1882 e.printStackTrace();
1887 public void testVFGeneratedInputs() {
1888 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1889 Resource resource = createVF();
1890 List<InputDefinition> inputs = resource.getInputs();
1891 assertEquals(6, inputs.size());
1892 for (InputDefinition input : inputs) {
1893 assertThat(input.getOwnerId()).isNotNull();
1895 assertEquals(resource.getDerivedFromGenericType(), genericVF.getToscaResourceName());
1896 assertEquals(resource.getDerivedFromGenericVersion(), genericVF.getVersion());
1900 public void testCRGeneratedInputs() {
1901 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1902 Resource resource = createCR();
1903 List<InputDefinition> inputs = resource.getInputs();
1904 assertEquals(3, inputs.size());
1905 for (InputDefinition input : inputs) {
1906 assertThat(input.getOwnerId()).isNotNull();
1908 assertEquals(resource.getDerivedFromGenericType(), genericCR.getToscaResourceName());
1909 assertEquals(resource.getDerivedFromGenericVersion(), genericCR.getVersion());
1913 public void testVFUpdateGenericInputsToLatestOnCheckout() {
1914 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1915 // create a VF that is derived from generic version 1.0
1916 Resource resource = createVF();
1917 // create a new generic version without properties
1918 genericVF.setVersion("2.0");
1919 genericVF.setProperties(null);
1920 String currentDerivedFromVersion = resource.getDerivedFromGenericVersion();
1921 List<InputDefinition> currentInputs = resource.getInputs();
1922 // verify previous inputs ownerId fields exist - user may not delete
1924 assertEquals(6, currentInputs.stream()
1925 .filter(p -> null != p.getOwnerId())
1926 .collect(Collectors.toList())
1928 Either<Boolean, ResponseFormat> upgradeToLatestGeneric = bl.shouldUpgradeToLatestGeneric(resource);
1930 assertTrue(upgradeToLatestGeneric.isLeft());
1931 // verify update required and valid
1932 assertTrue(upgradeToLatestGeneric.left()
1934 // verify version was upgraded
1935 assertNotEquals(resource.getDerivedFromGenericVersion(), currentDerivedFromVersion);
1936 // verify inputs were not deleted
1937 assertEquals(6, resource.getInputs()
1939 // verify inputs ownerId fields were removed - user may delete/edit
1941 assertEquals(6, resource.getInputs()
1943 .filter(p -> null == p.getOwnerId())
1944 .collect(Collectors.toList())
1949 public void testVFUpdateGenericInputsToLatestOnCheckoutNotPerformed() {
1951 // create a VF that is derived from generic version 1.0
1952 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1953 Resource resource = createVF();
1955 // add an input to the VF
1956 PropertyDefinition newProp = new PropertyDefinition();
1957 newProp.setType("integer");
1958 newProp.setName("newProp");
1959 resource.getInputs()
1960 .add(new InputDefinition(newProp));
1962 // create a new generic version with a new property which has the same
1963 // name as a user defined input on the VF with a different type
1964 genericVF.setVersion("2.0");
1965 newProp.setType("string");
1966 genericVF.setProperties(new ArrayList<>());
1967 genericVF.getProperties()
1969 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericVF));
1970 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericVF.getProperties(),
1971 genericVF.getUniqueId())).thenCallRealMethod();
1972 String currentDerivedFromVersion = resource.getDerivedFromGenericVersion();
1973 assertEquals(6, resource.getInputs()
1975 .filter(p -> null != p.getOwnerId())
1976 .collect(Collectors.toList())
1978 Either<Boolean, ResponseFormat> upgradeToLatestGeneric = bl.shouldUpgradeToLatestGeneric(resource);
1980 assertTrue(upgradeToLatestGeneric.isLeft());
1981 // verify update is invalid an void
1982 assertFalse(upgradeToLatestGeneric.left()
1984 // verify version was not upgraded
1985 assertEquals(resource.getDerivedFromGenericVersion(), currentDerivedFromVersion);
1986 // verify inputs were not removed
1987 assertEquals(7, resource.getInputs()
1989 // verify user defined input exists
1990 assertEquals(1, resource.getInputs()
1992 .filter(p -> null == p.getOwnerId())
1993 .collect(Collectors.toList())
1995 assertEquals("integer", resource.getInputs()
1997 .filter(p -> null == p.getOwnerId())
2004 public void testPNFGeneratedInputsNoGeneratedInformationalArtifacts() {
2005 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2006 Resource resource = createPNF();
2007 List<InputDefinition> inputs = resource.getInputs();
2008 assertEquals(3, inputs.size());
2009 for (InputDefinition input : inputs) {
2010 assertThat(input.getOwnerId()).isNotNull();
2012 assertEquals(resource.getDerivedFromGenericType(), genericPNF.getToscaResourceName());
2013 assertEquals(resource.getDerivedFromGenericVersion(), genericPNF.getVersion());
2014 assertEquals(0, resource.getArtifacts()
2018 private Resource createVF() {
2020 genericVF = setupGenericTypeMock(GENERIC_VF_NAME);
2021 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_VF_NAME))
2022 .thenReturn(Either.left(genericVF));
2023 Resource resource = createResourceObject(true);
2024 resource.setDerivedFrom(null);
2025 resource.setResourceType(ResourceTypeEnum.VF);
2026 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2027 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericVF));
2028 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericVF)).thenCallRealMethod();
2029 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericVF.getProperties(),
2030 resource.getUniqueId())).thenCallRealMethod();
2031 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2032 assertThat(createdResource).isNotNull();
2033 return createdResource;
2036 private Resource createRoot() {
2037 rootType = setupGenericTypeMock(GENERIC_ROOT_NAME);
2038 when(toscaOperationFacade.getLatestByToscaResourceName(GENERIC_ROOT_NAME))
2039 .thenReturn(Either.left(rootType));
2043 private Resource createCR() {
2045 genericCR = setupGenericTypeMock(GENERIC_CR_NAME);
2046 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_CR_NAME))
2047 .thenReturn(Either.left(genericCR));
2048 Resource resource = createResourceObject(true);
2049 resource.setDerivedFrom(null);
2050 resource.setResourceType(ResourceTypeEnum.CR);
2051 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2052 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericCR));
2053 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericCR)).thenCallRealMethod();
2054 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericCR.getProperties(),
2055 resource.getUniqueId())).thenCallRealMethod();
2056 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2057 assertThat(createdResource).isNotNull();
2058 return createdResource;
2061 private Resource createPNF() {
2063 genericPNF = setupGenericTypeMock(GENERIC_PNF_NAME);
2064 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_PNF_NAME))
2065 .thenReturn(Either.left(genericPNF));
2066 Resource resource = createResourceObject(true);
2067 resource.setDerivedFrom(null);
2068 resource.setResourceType(ResourceTypeEnum.PNF);
2069 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2070 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericPNF));
2071 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericPNF)).thenCallRealMethod();
2072 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericPNF.getProperties(),
2073 resource.getUniqueId())).thenCallRealMethod();
2074 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2075 assertThat(createdResource).isNotNull();
2076 return createdResource;
2079 private Map<String, String> getGenericPropertiesByToscaName(String toscaName) {
2080 HashMap<String, String> PNFProps = new HashMap<String, String>() {
2082 put("nf_function", "string");
2083 put("nf_role", "string");
2084 put("nf_type", "string");
2088 HashMap<String, String> CRProps = new HashMap<String, String>() {
2090 put("cr_function", "string");
2091 put("cr_role", "string");
2092 put("cr_type", "string");
2096 HashMap<String, String> VFProps = new HashMap<String, String>() {
2099 put("availability_zone_max_count", "integer");
2100 put("min_instances", "integer");
2101 put("max_instances", "integer");
2105 if (toscaName.contains("PNF"))
2107 if (toscaName.contains("CR"))
2109 if (toscaName.contains("VF"))
2112 return new HashMap<>();
2115 private Resource setupGenericTypeMock(String toscaName) {
2117 Resource genericType = createResourceObject(true);
2118 genericType.setVersion("1.0");
2119 genericType.setToscaResourceName(toscaName);
2120 genericType.setAbstract(true);
2121 List<PropertyDefinition> genericProps = new ArrayList<>();
2122 Map<String, String> genericPropsMap = getGenericPropertiesByToscaName(toscaName);
2123 genericPropsMap.forEach((name, type) -> {
2124 PropertyDefinition prop = new PropertyDefinition();
2127 genericProps.add(prop);
2130 genericType.setProperties(genericProps);
2134 private void validateUserRoles(Role... roles) {
2135 List<Role> listOfRoles = Stream.of(roles)
2136 .collect(Collectors.toList());
2140 public void testUpdateVolumeGroup() {
2141 Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
2142 bl.updateVolumeGroup(resource);
2143 assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(true));
2147 public void testUpdateVolumeGroupNull() {
2148 Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
2149 resource.setGroups(null);
2150 bl.updateVolumeGroup(resource);
2154 public void testUpdateVolumeGroupFail() {
2155 Resource resource = getResourceWithType("NON_EXIST_HEAT", "org.openecomp.groups.VfModule");
2156 bl.updateVolumeGroup(resource);
2157 assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(false));
2160 private Resource getResourceWithType(String artifactType, String groupDefinitionType) {
2161 ArtifactDefinition artifactToUpdate = new ArtifactDefinition();
2162 List<GroupDefinition> groups = new ArrayList<>();
2163 GroupDefinition gd = new GroupDefinition();
2164 List<PropertyDataDefinition> properties = new ArrayList<>();
2165 PropertyDataDefinition pdd = new PropertyDataDefinition();
2166 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
2167 List<String> artifactsList = new ArrayList<>();
2169 artifactToUpdate.setArtifactType(artifactType);
2170 artifactToUpdate.setArtifactName(artifactType);
2171 artifactToUpdate.setUniqueId(artifactType);
2172 Resource resource = createResourceObjectCsar(true);
2173 artifactsList.add(artifactToUpdate.getArtifactName());
2176 pdd.setName("volume_group");
2177 pdd.setValue("true");
2178 pdd.setType(ToscaPropertyType.BOOLEAN.getType());
2180 artifacts.put(artifactToUpdate.getArtifactName(), artifactToUpdate);
2182 properties.add(pdd);
2183 gd.setType(groupDefinitionType);
2184 gd.setProperties(properties);
2185 gd.setArtifacts(artifactsList);
2188 resource.setGroups(groups);
2189 resource.setDeploymentArtifacts(artifacts);
2195 public void testgetAllCertifiedResources() throws Exception {
2196 List<Resource> list = bl.getAllCertifiedResources(true, HighestFilterEnum.HIGHEST_ONLY, "USER");
2197 Assert.assertEquals(reslist,list);
2200 @Test(expected = StorageException.class)
2201 public void testgetAllCertifiedResources_exception() throws Exception {
2202 List<Resource> list = bl.getAllCertifiedResources(false, HighestFilterEnum.NON_HIGHEST_ONLY, "USER");
2203 Assert.assertEquals(reslist,list);
2207 public void testvalidateResourceNameExists() throws Exception {
2208 Either<Map<String, Boolean>, ResponseFormat> res = bl.validateResourceNameExists("Resource", ResourceTypeEnum.CR, "jh0003");
2209 Assert.assertEquals(true,res.isLeft());