2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import static org.assertj.core.api.Java6Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.anyList;
31 import static org.mockito.ArgumentMatchers.anyString;
32 import static org.mockito.ArgumentMatchers.eq;
33 import static org.mockito.Mockito.doAnswer;
34 import static org.mockito.Mockito.times;
35 import static org.mockito.Mockito.when;
37 import fj.data.Either;
39 import java.io.IOException;
40 import java.lang.reflect.Method;
41 import java.net.URISyntaxException;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collections;
45 import java.util.EnumMap;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.Map.Entry;
50 import java.util.Optional;
51 import java.util.stream.Collectors;
52 import java.util.stream.Stream;
53 import javax.servlet.ServletContext;
54 import org.apache.commons.lang3.tuple.ImmutablePair;
55 import org.junit.Assert;
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.mockito.InjectMocks;
59 import org.mockito.Mockito;
60 import org.mockito.MockitoAnnotations;
61 import org.openecomp.sdc.ElementOperationMock;
62 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
63 import org.openecomp.sdc.be.components.ArtifactsResolver;
64 import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
65 import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
66 import org.openecomp.sdc.be.components.csar.CsarInfo;
67 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
68 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
69 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
70 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
71 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
72 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
73 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
74 import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
75 import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
76 import org.openecomp.sdc.be.components.validation.UserValidations;
77 import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
78 import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
79 import org.openecomp.sdc.be.components.validation.component.ComponentFieldValidator;
80 import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator;
81 import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
82 import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator;
83 import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator;
84 import org.openecomp.sdc.be.components.validation.component.ComponentValidator;
85 import org.openecomp.sdc.be.config.ConfigurationManager;
86 import org.openecomp.sdc.be.dao.api.ActionStatus;
87 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
88 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
89 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
90 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
91 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
92 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
93 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
94 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
95 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
96 import org.openecomp.sdc.be.facade.operations.CatalogOperation;
97 import org.openecomp.sdc.be.impl.ComponentsUtils;
98 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
99 import org.openecomp.sdc.be.model.ArtifactDefinition;
100 import org.openecomp.sdc.be.model.Component;
101 import org.openecomp.sdc.be.model.ComponentInstance;
102 import org.openecomp.sdc.be.model.ComponentParametersView;
103 import org.openecomp.sdc.be.model.DataTypeDefinition;
104 import org.openecomp.sdc.be.model.GroupDefinition;
105 import org.openecomp.sdc.be.model.InputDefinition;
106 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
107 import org.openecomp.sdc.be.model.LifecycleStateEnum;
108 import org.openecomp.sdc.be.model.NodeTypeInfo;
109 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
110 import org.openecomp.sdc.be.model.PropertyDefinition;
111 import org.openecomp.sdc.be.model.RequirementDefinition;
112 import org.openecomp.sdc.be.model.Resource;
113 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
114 import org.openecomp.sdc.be.model.User;
115 import org.openecomp.sdc.be.model.VendorSoftwareProduct;
116 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
117 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
118 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
119 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
120 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation;
121 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
122 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
123 import org.openecomp.sdc.be.model.operations.StorageException;
124 import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation;
125 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
126 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
127 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
128 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
129 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
130 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
131 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
132 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
133 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
134 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
135 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
136 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
137 import org.openecomp.sdc.be.tosca.CsarUtils;
138 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
139 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
140 import org.openecomp.sdc.be.user.Role;
141 import org.openecomp.sdc.be.user.UserBusinessLogic;
142 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
143 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
144 import org.openecomp.sdc.common.api.Constants;
145 import org.openecomp.sdc.common.impl.ExternalConfiguration;
146 import org.openecomp.sdc.common.impl.FSConfigurationSource;
147 import org.openecomp.sdc.common.util.GeneralUtility;
148 import org.openecomp.sdc.common.util.ValidationUtils;
149 import org.openecomp.sdc.common.zip.ZipUtils;
150 import org.openecomp.sdc.common.zip.exception.ZipException;
151 import org.openecomp.sdc.exception.ResponseFormat;
152 import org.springframework.web.context.WebApplicationContext;
154 public class ResourceBusinessLogicTest {
156 private final ConfigurationManager configurationManager = new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
157 private static final String RESOURCE_CATEGORY1 = "Network Layer 2-3";
158 private static final String RESOURCE_SUBCATEGORY = "Router";
160 private static final String UPDATED_SUBCATEGORY = "Gateway";
162 private static final String RESOURCE_NAME = "My-Resource_Name with space";
163 private static final String RESOURCE_TOSCA_NAME = "My-Resource_Tosca_Name";
164 private static final String GENERIC_ROOT_NAME = "tosca.nodes.Root";
165 private static final String GENERIC_VF_NAME = "org.openecomp.resource.abstract.nodes.VF";
166 private static final String GENERIC_CR_NAME = "org.openecomp.resource.abstract.nodes.CR";
167 private static final String GENERIC_PNF_NAME = "org.openecomp.resource.abstract.nodes.PNF";
169 private final ServletContext servletContext = Mockito.mock(ServletContext.class);
170 IElementOperation mockElementDao;
171 private final JanusGraphDao mockJanusGraphDao = Mockito.mock(JanusGraphDao.class);
172 private final UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
173 private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
174 private final NodeTypeOperation nodeTypeOperation = Mockito.mock(NodeTypeOperation.class);
175 private final NodeTemplateOperation nodeTemplateOperation = Mockito.mock(NodeTemplateOperation.class);
176 private final TopologyTemplateOperation topologyTemplateOperation = Mockito.mock(TopologyTemplateOperation.class);
177 private final LifecycleBusinessLogic lifecycleBl = Mockito.mock(LifecycleBusinessLogic.class);
178 private final CatalogOperation catalogOperation = Mockito.mock(CatalogOperation.class);
179 private final ICapabilityTypeOperation capabilityTypeOperation = Mockito.mock(ICapabilityTypeOperation.class);
180 private final PropertyOperation propertyOperation = Mockito.mock(PropertyOperation.class);
181 private final ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
182 private final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
183 private final UserValidations userValidations = Mockito.mock(UserValidations.class);
184 private final WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
185 private final IInterfaceLifecycleOperation interfaceTypeOperation = Mockito.mock(IInterfaceLifecycleOperation.class);
186 private final ArtifactCassandraDao artifactCassandraDao = Mockito.mock(ArtifactCassandraDao.class);
187 private final IElementOperation elementDao = new ElementOperationMock();
189 private final CsarUtils csarUtils = Mockito.mock(CsarUtils.class);
190 private final UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class);
191 private final IGroupOperation groupOperation = Mockito.mock(IGroupOperation.class);
192 private final IGroupInstanceOperation groupInstanceOperation = Mockito.mock(IGroupInstanceOperation.class);
193 private final IGroupTypeOperation groupTypeOperation = Mockito.mock(IGroupTypeOperation.class);
194 private final GroupBusinessLogic groupBusinessLogic = Mockito.mock(GroupBusinessLogic.class);
195 private final ModelBusinessLogic modelBusinessLogic = Mockito.mock(ModelBusinessLogic.class);
196 private final InterfaceOperation interfaceOperation = Mockito.mock(InterfaceOperation.class);
197 private final ArtifactsOperations artifactToscaOperation = Mockito.mock(ArtifactsOperations.class);
198 private final PropertyBusinessLogic propertyBusinessLogic = Mockito.mock(PropertyBusinessLogic.class);
199 private final ArtifactsResolver artifactsResolver = Mockito.mock(ArtifactsResolver.class);
200 private final InterfaceLifecycleOperation interfaceLifecycleTypeOperation = Mockito.mock(InterfaceLifecycleOperation.class);
201 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
202 private final ResourceImportManager resourceImportManager = Mockito.mock(ResourceImportManager.class);
203 private final InputsBusinessLogic inputsBusinessLogic = Mockito.mock(InputsBusinessLogic.class);
204 private final OutputsBusinessLogic outputsBusinessLogic = Mockito.mock(OutputsBusinessLogic.class);
205 private final CompositionBusinessLogic compositionBusinessLogic = Mockito.mock(CompositionBusinessLogic.class);
206 private final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic = Mockito.mock(ResourceDataMergeBusinessLogic.class);
207 private final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic = Mockito.mock(CsarArtifactsAndGroupsBusinessLogic.class);
208 private final MergeInstanceUtils mergeInstanceUtils = Mockito.mock(MergeInstanceUtils.class);
209 private final UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class);
210 private final ToscaExportHandler toscaExportHandler = Mockito.mock(ToscaExportHandler.class);
211 private final PolicyBusinessLogic policyBusinessLogic = Mockito.mock(PolicyBusinessLogic.class);
213 private YamlTemplateParsingHandler yamlTemplateParsingHandler = Mockito.mock(YamlTemplateParsingHandler.class);
215 ResponseFormatManager responseManager = null;
216 private final GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
218 Resource resourceResponse = null;
219 Resource genericVF = null;
220 Resource genericCR = null;
221 Resource genericVFC = null;
222 Resource genericPNF = null;
223 Resource rootType = null;
224 ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
225 ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic(artifactCassandraDao, toscaExportHandler, csarUtils, lifecycleBl,
226 userBusinessLogic, artifactsResolver, elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
227 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
228 CsarOperation csarOperation = Mockito.mock(CsarOperation.class);
230 CsarBusinessLogic csarBusinessLogic ;
231 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<>();
232 List<Resource> reslist;
233 private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
234 protected ComponentDescriptionValidator componentDescriptionValidator = new ComponentDescriptionValidator(componentsUtils);
235 protected ComponentProjectCodeValidator componentProjectCodeValidator = new ComponentProjectCodeValidator(componentsUtils);
236 protected ComponentIconValidator componentIconValidator = new ComponentIconValidator(componentsUtils);
237 protected ComponentContactIdValidator componentContactIdValidator = new ComponentContactIdValidator(componentsUtils);
238 protected ComponentTagsValidator componentTagsValidator = new ComponentTagsValidator(componentsUtils);
239 protected ComponentNameValidator componentNameValidator = new ComponentNameValidator(componentsUtils, toscaOperationFacade);
240 private ComponentValidator componentValidator = createComponentValidator();
241 private SoftwareInformationBusinessLogic softwareInformationBusinessLogic = Mockito.mock(SoftwareInformationBusinessLogic.class);
243 private ComponentValidator createComponentValidator() {
244 List<ComponentFieldValidator> componentFieldValidators = Arrays.asList(componentNameValidator,
245 componentDescriptionValidator, componentProjectCodeValidator,
246 componentIconValidator, componentContactIdValidator,
247 componentTagsValidator);
248 return new ComponentValidator(componentsUtils,componentFieldValidators);
251 ResourceBusinessLogic bl;
254 public void setup() {
255 MockitoAnnotations.initMocks(this);
256 Mockito.reset(propertyOperation);
259 mockElementDao = new ElementOperationMock();
262 // User data and management
264 user.setUserId("jh0003");
265 user.setFirstName("Jimmi");
266 user.setLastName("Hendrix");
267 user.setRole(Role.ADMIN.name());
269 when(mockUserAdmin.getUser("jh0003", false)).thenReturn(user);
270 when(userValidations.validateUserExists(user.getUserId())).thenReturn(user);
271 when(userValidations.validateUserNotEmpty(eq(user), anyString())).thenReturn(user);
272 // Servlet Context attributes
273 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
274 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))
275 .thenReturn(webAppContextWrapper);
276 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
277 when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
279 Either<Boolean, StorageOperationStatus> eitherFalse = Either.left(true);
280 when(toscaOperationFacade.validateComponentNameExists("tosca.nodes.Root", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
281 .thenReturn(eitherFalse);
284 Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
285 when(toscaOperationFacade.validateComponentNameExists("alreadyExists", ResourceTypeEnum.VFC,
286 ComponentTypeEnum.RESOURCE)).thenReturn(eitherCountExist);
288 Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
289 when(toscaOperationFacade.validateComponentNameExists(eq(RESOURCE_NAME), any(ResourceTypeEnum.class),
290 eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherCount);
291 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
292 when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(validateDerivedExists);
294 Either<Boolean, StorageOperationStatus> validateDerivedNotExists = Either.left(false);
295 when(toscaOperationFacade.validateToscaResourceNameExists("kuku")).thenReturn(validateDerivedNotExists);
296 when(graphLockOperation.lockComponent(anyString(), eq(NodeTypeEnum.Resource)))
297 .thenReturn(StorageOperationStatus.OK);
298 when(graphLockOperation.lockComponentByName(anyString(), eq(NodeTypeEnum.Resource)))
299 .thenReturn(StorageOperationStatus.OK);
302 resourceResponse = createResourceObject(true);
303 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(resourceResponse);
304 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
305 when(catalogOperation.updateCatalog(Mockito.any(), Mockito.any())).thenReturn(ActionStatus.OK);
306 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<>();
307 when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(emptyDataTypes));
308 when(mockJanusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
311 artifactManager.setNodeTemplateOperation(nodeTemplateOperation);
312 bl = new ResourceBusinessLogic(mockElementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic,
313 interfaceOperation, interfaceLifecycleTypeOperation, artifactManager, componentInstanceBusinessLogic,
314 resourceImportManager, inputsBusinessLogic, outputsBusinessLogic, compositionBusinessLogic, resourceDataMergeBusinessLogic,
315 csarArtifactsAndGroupsBusinessLogic, mergeInstanceUtils, uiComponentDataConverter, csarBusinessLogic,
316 artifactToscaOperation, propertyBusinessLogic, componentContactIdValidator, componentNameValidator,
317 componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator,
318 componentDescriptionValidator, policyBusinessLogic, modelBusinessLogic);
319 bl.setElementDao(mockElementDao);
320 bl.setUserAdmin(mockUserAdmin);
321 bl.setCapabilityTypeOperation(capabilityTypeOperation);
322 bl.setComponentsUtils(componentsUtils);
323 bl.setLifecycleManager(lifecycleBl);
324 bl.setArtifactsManager(artifactManager);
325 bl.setGraphLockOperation(graphLockOperation);
326 bl.setPropertyOperation(propertyOperation);
327 bl.setJanusGraphDao(mockJanusGraphDao);
328 bl.setApplicationDataTypeCache(applicationDataTypeCache);
329 bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
330 bl.setCatalogOperations(catalogOperation);
331 toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
332 csarBusinessLogic.setComponentsUtils(componentsUtils);
333 toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
334 bl.setToscaOperationFacade(toscaOperationFacade);
335 bl.setUserValidations(userValidations);
336 bl.setInterfaceTypeOperation(interfaceTypeOperation);
338 csarBusinessLogic.setCsarOperation(csarOperation);
339 Resource resourceCsar = createResourceObjectCsar(true);
340 setCanWorkOnResource(resourceCsar);
341 Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
342 when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
343 responseManager = ResponseFormatManager.getInstance();
344 bl.setComponentIconValidator(componentIconValidator);
345 bl.setComponentNameValidator(componentNameValidator);
346 bl.setComponentDescriptionValidator(componentDescriptionValidator);
347 bl.setComponentTagsValidator(componentTagsValidator);
348 bl.setComponentContactIdValidator(componentContactIdValidator);
349 bl.setComponentProjectCodeValidator(componentProjectCodeValidator);
350 bl.setComponentValidator(componentValidator);
351 reslist = new ArrayList<>();
352 reslist.add(resourceResponse);
353 reslist.add(genericVF);
354 reslist.add(genericCR);
355 reslist.add(genericVFC);
356 reslist.add(genericPNF);
357 Either<List<Resource>, StorageOperationStatus> returneval= Either.left(reslist);
358 when(toscaOperationFacade.getAllCertifiedResources(true, true)).thenReturn(returneval);
359 when(toscaOperationFacade.validateComponentNameUniqueness("Resource", ResourceTypeEnum.CR, ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(true));
360 Either<List<Resource>, StorageOperationStatus> returnevalexception= Either.right(StorageOperationStatus.BAD_REQUEST);
361 when(toscaOperationFacade.getAllCertifiedResources(false, false)).thenReturn(returnevalexception);
364 @Test(expected = ComponentException.class)
365 public void createResourcesFromYamlNodeTypesList() throws IOException {
366 Map<String, Object> mappedToscaTemplate = new HashMap<>();
367 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>();
368 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
369 Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>();
370 bl.createResourcesFromYamlNodeTypesList(
375 nodeTypesArtifactsToHandle,
376 nodeTypesNewCreatedArtifacts,
378 new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml"),true), "");
382 public void handleNodeTypeArtifactsTest() {
383 Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = new HashMap<>();
384 List<ArtifactDefinition> defs = new ArrayList<>();
385 defs.add(new ArtifactDefinition());
386 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, defs);
387 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, defs);
388 assertTrue(bl.handleNodeTypeArtifacts(resourceResponse, nodeTypeArtifactsToHandle, new ArrayList<>(), user, true, true).isRight());
392 public void getUiComponentDataTransferByComponentIdTest() {
393 when(toscaOperationFacade.getToscaElement(eq(""), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(genericCR));
394 assertTrue(bl.getUiComponentDataTransferByComponentId("", new ArrayList<>()).isLeft());
395 when(toscaOperationFacade.getToscaElement(eq(""), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.OK));
396 assertTrue(bl.getUiComponentDataTransferByComponentId("", new ArrayList<>()).isRight());
400 public void shouldUpgradeToLatestDerivedTest() {
403 when(toscaOperationFacade.shouldUpgradeToLatestDerived(genericCR)).thenReturn(Either.left(genericCR));
404 when(toscaOperationFacade.shouldUpgradeToLatestDerived(genericVFC)).thenReturn(Either.right(StorageOperationStatus.OK));
405 assertTrue(bl.shouldUpgradeToLatestDerived(genericVF).isLeft());
406 assertTrue(bl.shouldUpgradeToLatestDerived(genericCR).isLeft());
409 private Resource createResourceObject(boolean afterCreate) {
410 Resource resource = new Resource();
411 resource.setName(RESOURCE_NAME);
412 resource.setToscaResourceName(RESOURCE_TOSCA_NAME);
413 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
414 resource.setDescription("My short description");
415 List<String> tgs = new ArrayList<>();
417 tgs.add(resource.getName());
418 resource.setTags(tgs);
419 List<String> template = new ArrayList<>();
420 template.add("tosca.nodes.Root");
421 resource.setDerivedFrom(template);
422 resource.setVendorName("Motorola");
423 resource.setVendorRelease("1.0.0");
424 resource.setContactId("ya5467");
425 resource.setIcon("defaulticon");
428 resource.setName(resource.getName());
429 resource.setVersion("0.1");
430 resource.setUniqueId(resource.getName()
431 .toLowerCase() + ":" + resource.getVersion());
432 resource.setCreatorUserId(user.getUserId());
433 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
434 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
439 private Resource createResourceObjectWithModel(boolean afterCreate) {
440 Resource resource = new Resource();
441 resource.setName(RESOURCE_NAME);
442 resource.setToscaResourceName(RESOURCE_TOSCA_NAME);
443 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
444 resource.setDescription("My short description");
445 List<String> tgs = new ArrayList<>();
447 tgs.add(resource.getName());
448 resource.setTags(tgs);
449 List<String> template = new ArrayList<>();
450 template.add("tosca.nodes.Root");
451 resource.setDerivedFrom(template);
452 resource.setVendorName("Motorola");
453 resource.setVendorRelease("1.0.0");
454 resource.setContactId("ya5467");
455 resource.setIcon("defaulticon");
456 resource.setModel("Test Model");
459 resource.setName(resource.getName());
460 resource.setVersion("0.1");
461 resource.setUniqueId(resource.getName()
462 .toLowerCase() + ":" + resource.getVersion());
463 resource.setCreatorUserId(user.getUserId());
464 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
465 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
470 private Resource createResourceObjectCsar(boolean afterCreate) {
471 Resource resource = new Resource();
472 resource.setName(RESOURCE_NAME);
473 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
474 resource.setDescription("My short description");
475 List<String> tgs = new ArrayList<>();
477 tgs.add(resource.getName());
478 resource.setTags(tgs);
479 List<String> template = new ArrayList<>();
480 template.add("tosca.nodes.Root");
481 resource.setDerivedFrom(template);
482 resource.setVendorName("Motorola");
483 resource.setVendorRelease("1.0.0");
484 resource.setResourceVendorModelNumber("");
485 resource.setContactId("ya5467");
486 resource.setIcon("MyIcon");
487 resource.setCsarUUID("valid_vf.csar");
488 resource.setCsarVersion("1");
491 resource.setName(resource.getName());
492 resource.setVersion("0.1");
494 resource.setUniqueId(resource.getName()
495 .toLowerCase() + ":" + resource.getVersion());
496 resource.setCreatorUserId(user.getUserId());
497 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
498 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
503 private Resource setCanWorkOnResource(Resource resource) {
504 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
505 resource.setLastUpdaterUserId(user.getUserId());
510 public void testHappyScenario() {
511 validateUserRoles(Role.ADMIN, Role.DESIGNER);
512 Resource resource = createResourceObject(false);
513 Resource createdResource = null;
515 when(toscaOperationFacade
516 .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
517 .thenReturn(Either.left(false));
518 createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
519 assertThat(createResourceObject(true)).isEqualTo(createdResource);
520 } catch (ComponentException e) {
521 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
527 public void testUpdateHappyScenario() {
528 Resource resource = createResourceObjectCsar(true);
529 setCanWorkOnResource(resource);
530 validateUserRoles(Role.ADMIN, Role.DESIGNER);
531 Either<Resource, StorageOperationStatus> resourceLinkedToCsarRes = Either.left(resource);
532 when(toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, resource.getCsarUUID(), resource.getSystemName())).thenReturn(resourceLinkedToCsarRes);
533 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
534 when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(validateDerivedExists);
535 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
536 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
537 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
538 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
540 assertThat(resource.getUniqueId()).isEqualTo(bl.validateAndUpdateResourceFromCsar(resource, user, null, null, resource.getUniqueId()).getUniqueId());
541 } catch (ComponentException e) {
542 assertThat(e.getResponseFormat().getStatus()).isEqualTo(200);
547 public void testUpdateUnhappyScenario() {
548 Resource resource = createResourceObjectCsar(true);
549 final var csarVersionId = "csarVersionId";
550 resource.setCsarVersionId(csarVersionId);
552 final var vendorSoftwareProduct = new VendorSoftwareProduct();
553 vendorSoftwareProduct.setFileMap(new HashMap<>());
554 vendorSoftwareProduct.setModelList(Collections.emptyList());
555 setCanWorkOnResource(resource);
556 validateUserRoles(Role.ADMIN, Role.DESIGNER);
558 when(toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, resource.getCsarUUID(), resource.getSystemName())).thenReturn(Either.left(resource));
559 when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(Either.left(true));
560 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(Either.left(setCanWorkOnResource(resource)));
561 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(Either.left(resource));
562 when(csarOperation.findVsp("valid_vf.csar", csarVersionId, user)).thenReturn(Optional.of(vendorSoftwareProduct));
565 Resource createdResource = bl.validateAndUpdateResourceFromCsar(resource, user, null, "", resource.getUniqueId());
566 assertThat(resource.getUniqueId()).isEqualTo(createdResource.getUniqueId());
567 } catch (ComponentException e) {
568 assertThat(e.getResponseFormat().getStatus()).isEqualTo(400);
571 resource.setCsarVersion("2");
572 when(toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, resource.getCsarUUID(), resource.getSystemName())).thenReturn(Either.left(resource));
573 bl.validateAndUpdateResourceFromCsar(resource, user, null, null, resource.getUniqueId());
574 } catch (ComponentException e) {
575 assertThat(e.getResponseFormat().getStatus()).isEqualTo(400);
578 /* CREATE validations - start ***********************/
579 // Resource name - start
582 public void testFailedResourceValidations() {
583 testResourceNameExist();
584 testResourceNameEmpty();
585 // testResourceNameExceedsLimit();
586 testResourceNameWrongFormat();
587 testResourceDescExceedsLimitCreate();
588 testResourceDescNotEnglish();
589 testResourceDescriptionEmpty();
590 testResourceDescriptionMissing();
591 testResourceIconMissing();
592 testResourceIconInvalid();
593 testResourceIconExceedsLimit();
594 testResourceTagNotExist();
595 testResourceTagEmpty();
596 testTagsExceedsLimitCreate();
597 testTagsNoServiceName();
600 testContactIdTooLong();
601 testContactIdWrongFormatCreate();
602 testResourceContactIdEmpty();
603 testResourceContactIdMissing();
604 testVendorNameExceedsLimit();
605 testVendorNameWrongFormatCreate();
606 testVendorReleaseWrongFormat();
607 testVendorReleaseExceedsLimitCreate();
608 testResourceVendorModelNumberExceedsLimit();
609 testResourceVendorNameMissing();
610 testResourceVendorReleaseMissing();
611 testResourceCategoryExist();
612 testResourceBadCategoryCreate();
613 testHappyScenarioCostLicenseType();
614 testCostWrongFormatCreate();
615 testLicenseTypeWrongFormatCreate();
616 testResourceTemplateNotExist();
617 testResourceTemplateEmpty();
618 testResourceTemplateInvalid();
621 private void testResourceNameExist() {
622 String resourceName = "alreadyExists";
623 Resource resourceExist = createResourceObject(false);
624 resourceExist.setName(resourceName);
625 resourceExist.getTags()
627 validateUserRoles(Role.ADMIN, Role.DESIGNER);
628 when(toscaOperationFacade
629 .validateComponentNameAndModelExists(resourceName, null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
630 .thenReturn(Either.left(true));
632 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
633 } catch (ComponentException e) {
634 assertComponentException(e, ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
635 ComponentTypeEnum.RESOURCE.getValue(), resourceName);
639 private void testResourceNameEmpty() {
640 Resource resourceExist = createResourceObject(false);
641 resourceExist.setName(null);
644 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
645 } catch (ComponentException e) {
646 assertComponentException(e, ActionStatus.MISSING_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
650 private void testResourceNameExceedsLimit() {
651 Resource resourceExccedsNameLimit = createResourceObject(false);
652 // 51 chars, the limit is 50
653 String tooLongResourceName = "zCRCAWjqte0DtgcAAMmcJcXeNubeX1p1vOZNTShAHOYNAHvV3iK";
654 resourceExccedsNameLimit.setName(tooLongResourceName);
657 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
658 } catch (ComponentException e) {
659 assertComponentException(e, ActionStatus.COMPONENT_NAME_EXCEEDS_LIMIT,
660 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_NAME_MAX_LENGTH);
664 private void testResourceNameWrongFormat() {
665 Resource resource = createResourceObject(false);
667 String nameWrongFormat = "ljg?fd";
668 resource.setName(nameWrongFormat);
671 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
672 } catch (ComponentException e) {
673 assertComponentException(e, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
677 // Resource name - end
678 // Resource description - start
679 private void testResourceDescExceedsLimitCreate() {
680 Resource resourceExccedsDescLimit = createResourceObject(false);
681 // 1025 chars, the limit is 1024
682 String tooLongResourceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
683 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
684 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
685 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
686 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
687 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
688 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
689 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
691 resourceExccedsDescLimit.setDescription(tooLongResourceDesc);
693 bl.createResource(resourceExccedsDescLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
694 } catch (ComponentException e) {
695 assertComponentException(e, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT,
696 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
700 private void testResourceDescNotEnglish() {
701 Resource notEnglish = createResourceObject(false);
703 String notEnglishDesc = "\uC2B5";
704 notEnglish.setDescription(notEnglishDesc);
707 bl.createResource(notEnglish, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
708 } catch (ComponentException e) {
709 assertComponentException(e, ActionStatus.COMPONENT_INVALID_DESCRIPTION,
710 ComponentTypeEnum.RESOURCE.getValue());
714 private void testResourceDescriptionEmpty() {
715 Resource resourceExist = createResourceObject(false);
716 resourceExist.setDescription("");
719 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
720 } catch (ComponentException e) {
721 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION,
722 ComponentTypeEnum.RESOURCE.getValue());
726 private void testResourceDescriptionMissing() {
727 Resource resourceExist = createResourceObject(false);
728 resourceExist.setDescription(null);
731 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
732 } catch (ComponentException e) {
733 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION,
734 ComponentTypeEnum.RESOURCE.getValue());
737 // Resource description - end
738 // Resource icon start
740 private void testResourceIconMissing() {
741 Resource resourceExist = createResourceObject(false);
742 resourceExist.setIcon(null);
743 when(toscaOperationFacade
744 .validateComponentNameAndModelExists(resourceExist.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
745 .thenReturn(Either.left(false));
747 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
748 } catch (ComponentException e) {
749 assertComponentException(e, ActionStatus.COMPONENT_MISSING_ICON, ComponentTypeEnum.RESOURCE.getValue());
753 private void testResourceIconInvalid() {
754 Resource resourceExist = createResourceObject(false);
755 resourceExist.setIcon("kjk3453^&");
758 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
759 } catch (ComponentException e) {
760 assertComponentException(e, ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.RESOURCE.getValue());
764 private void testResourceIconExceedsLimit() {
765 Resource resourceExist = createResourceObject(false);
766 resourceExist.setIcon("dsjfhskdfhskjdhfskjdhkjdhfkshdfksjsdkfhsdfsdfsdfsfsdfsf");
768 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
769 } catch (ComponentException e) {
770 assertComponentException(e, ActionStatus.COMPONENT_ICON_EXCEEDS_LIMIT,
771 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.ICON_MAX_LENGTH);
776 // Resource tags - start
777 private void testResourceTagNotExist() {
778 Resource resourceExist = createResourceObject(false);
779 resourceExist.setTags(null);
781 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
782 } catch (ComponentException e) {
783 assertComponentException(e, ActionStatus.COMPONENT_MISSING_TAGS);
787 private void testResourceTagEmpty() {
788 Resource resourceExist = createResourceObject(false);
789 resourceExist.setTags(new ArrayList<>());
791 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
792 } catch (ComponentException e) {
793 assertComponentException(e, ActionStatus.COMPONENT_MISSING_TAGS);
797 private void testTagsExceedsLimitCreate() {
798 Resource resourceExccedsNameLimit = createResourceObject(false);
799 String tag1 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjQ";
800 String tag2 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjW";
801 String tag3 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjE";
802 String tag4 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjb";
803 String tag5 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjr";
804 String tag6 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
805 String tag7 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
806 String tag8 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjd";
807 String tag9 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
808 String tag10 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
809 String tag11 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjh";
810 String tag12 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjj";
811 String tag13 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjk";
812 String tag14 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjs";
813 String tag15 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjz";
814 String tag16 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjx";
815 String tag17 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj2";
816 String tag18 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj3";
817 String tag19 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj4";
818 String tag20 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj5";
819 String tag21 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj0";
821 List<String> tagsList = new ArrayList<>();
843 tagsList.add(resourceExccedsNameLimit.getName());
845 resourceExccedsNameLimit.setTags(tagsList);
847 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
848 } catch (ComponentException e) {
849 assertComponentException(e, ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT,
850 "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
854 private void testTagsSingleExceedsLimit() {
855 Resource resourceExccedsNameLimit = createResourceObject(false);
856 String tag1 = "afzs2qLBb5X6tZhiunkcEwiFX1qRQY8YZl3y3Du5M5xeQY5Nq9afcFHDZ9HaURw43gH27nAUWM36bMbMylwTFSzzNV8NO4v4ripe6Q15Vc2nPOFI";
857 String tag2 = resourceExccedsNameLimit.getName();
858 List<String> tagsList = new ArrayList<>();
862 resourceExccedsNameLimit.setTags(tagsList);
864 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
865 } catch (ComponentException e) {
866 assertComponentException(e, ActionStatus.COMPONENT_SINGLE_TAG_EXCEED_LIMIT,
867 "" + ValidationUtils.TAG_MAX_LENGTH);
871 private void testTagsNoServiceName() {
872 Resource serviceExccedsNameLimit = createResourceObject(false);
873 String tag1 = "afzs2qLBb";
874 List<String> tagsList = new ArrayList<>();
876 serviceExccedsNameLimit.setTags(tagsList);
878 bl.createResource(serviceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
879 } catch (ComponentException e) {
880 assertComponentException(e, ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
884 private void testInvalidTag() {
885 Resource serviceExccedsNameLimit = createResourceObject(false);
886 String tag1 = "afzs2qLBb%#%";
887 List<String> tagsList = new ArrayList<>();
889 serviceExccedsNameLimit.setTags(tagsList);
891 bl.createResource(serviceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
892 } catch (ComponentException e) {
893 assertComponentException(e, ActionStatus.INVALID_FIELD_FORMAT, new String[] { "Resource", "tag" });
897 // Resource tags - stop
898 // Resource contact start
900 private void testContactIdTooLong() {
901 Resource resourceContactId = createResourceObject(false);
902 // 59 chars instead of 50
903 String contactIdTooLong = "thisNameIsVeryLongAndExeccedsTheNormalLengthForContactId";
904 resourceContactId.setContactId(contactIdTooLong);
907 bl.createResource(resourceContactId, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
908 } catch (ComponentException e) {
909 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
913 private void testContactIdWrongFormatCreate() {
914 Resource resourceContactId = createResourceObject(false);
915 // 3 letters and 3 digits and special characters
916 String contactIdFormatWrong = "yrt134!!!";
917 resourceContactId.setContactId(contactIdFormatWrong);
919 bl.createResource(resourceContactId, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
920 } catch (ComponentException e) {
921 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
925 private void testResourceContactIdEmpty() {
926 Resource resourceExist = createResourceObject(false);
927 resourceExist.setContactId("");
929 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
930 } catch (ComponentException e) {
931 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
935 private void testResourceContactIdMissing() {
936 Resource resourceExist = createResourceObject(false);
937 resourceExist.setContactId(null);
939 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
940 } catch (ComponentException e) {
941 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
945 private void testVendorNameExceedsLimit() {
946 Resource resourceExccedsVendorNameLimit = createResourceObject(false);
947 String tooLongVendorName = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
948 resourceExccedsVendorNameLimit.setVendorName(tooLongVendorName);
950 bl.createResource(resourceExccedsVendorNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
951 } catch (ComponentException e) {
952 assertComponentException(e, ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT,
953 "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
957 private void testResourceVendorModelNumberExceedsLimit() {
958 Resource resourceExccedsVendorModelNumberLimit = createResourceObject(false);
959 String tooLongVendorModelNumber = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
960 resourceExccedsVendorModelNumberLimit.setResourceVendorModelNumber(tooLongVendorModelNumber);
962 bl.createResource(resourceExccedsVendorModelNumberLimit, AuditingActionEnum.CREATE_RESOURCE, user, null,
964 } catch (ComponentException e) {
965 assertComponentException(e, ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
966 "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
970 private void testVendorNameWrongFormatCreate() {
971 Resource resource = createResourceObject(false);
973 String nameWrongFormat = "ljg*fd";
974 resource.setVendorName(nameWrongFormat);
976 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
977 } catch (ComponentException e) {
978 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
982 private void testVendorReleaseWrongFormat() {
983 Resource resource = createResourceObject(false);
985 String vendorReleaseWrongFormat = "1>2";
986 resource.setVendorRelease(vendorReleaseWrongFormat);
988 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
989 } catch (ComponentException e) {
990 assertComponentException(e, ActionStatus.INVALID_VENDOR_RELEASE, vendorReleaseWrongFormat);
994 private void testVendorReleaseExceedsLimitCreate() {
995 Resource resourceExccedsNameLimit = createResourceObject(false);
996 String tooLongVendorRelease = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
997 resourceExccedsNameLimit.setVendorRelease(tooLongVendorRelease);
999 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1000 } catch (ComponentException e) {
1001 assertComponentException(e, ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT,
1002 "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
1006 private void testResourceVendorNameMissing() {
1007 Resource resourceExist = createResourceObject(false);
1008 resourceExist.setVendorName(null);
1010 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1011 } catch (ComponentException e) {
1012 assertComponentException(e, ActionStatus.MISSING_VENDOR_NAME);
1016 private void testResourceVendorReleaseMissing() {
1017 Resource resourceExist = createResourceObject(false);
1018 resourceExist.setVendorRelease(null);
1020 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1021 } catch (ComponentException e) {
1022 assertComponentException(e, ActionStatus.MISSING_VENDOR_RELEASE);
1026 // Resource vendor name/release stop
1028 private void testResourceCategoryExist() {
1029 Resource resourceExist = createResourceObject(false);
1030 resourceExist.setCategories(null);
1032 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1033 } catch (ComponentException e) {
1034 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
1038 private void testResourceBadCategoryCreate() {
1040 Resource resourceExist = createResourceObject(false);
1041 resourceExist.setCategories(null);
1042 resourceExist.addCategory("koko", "koko");
1044 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1045 } catch (ComponentException e) {
1046 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
1052 private void testHappyScenarioCostLicenseType() {
1053 Resource createResourceObject = createResourceObject(false);
1054 Resource createResourceObjectAfterCreate = createResourceObject(true);
1055 // Adding cost and licenseType to basic mock
1056 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(createResourceObjectAfterCreate);
1057 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
1059 String cost = "123.456";
1060 String licenseType = "User";
1061 createResourceObject.setCost(cost);
1062 createResourceObject.setLicenseType(licenseType);
1063 Resource createdResource;
1065 createdResource = bl.createResource(createResourceObject, AuditingActionEnum.CREATE_RESOURCE, user, null,
1067 createResourceObjectAfterCreate.setCost(cost);
1068 createResourceObjectAfterCreate.setLicenseType(licenseType);
1069 assertThat(createResourceObjectAfterCreate).isEqualTo(createdResource);
1070 } catch (ComponentException e) {
1071 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
1076 private void testCostWrongFormatCreate() {
1077 Resource resourceCost = createResourceObject(false);
1078 // Comma instead of fullstop
1079 String cost = "12356,464";
1080 resourceCost.setCost(cost);
1082 bl.createResource(resourceCost, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1083 } catch (ComponentException e) {
1084 assertComponentException(e, ActionStatus.INVALID_CONTENT);
1089 // License type start
1090 private void testLicenseTypeWrongFormatCreate() {
1091 Resource resourceLicenseType = createResourceObject(false);
1093 String licenseType = "cpu";
1094 resourceLicenseType.setLicenseType(licenseType);
1096 bl.createResource(resourceLicenseType, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1097 } catch (ComponentException e) {
1098 assertComponentException(e, ActionStatus.INVALID_CONTENT);
1102 // License type stop
1103 // Derived from start
1104 private void testResourceTemplateNotExist() {
1105 Resource resourceExist = createResourceObject(false);
1106 List<String> list = null;
1107 resourceExist.setDerivedFrom(list);
1109 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1110 } catch (ComponentException e) {
1111 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1115 private void testResourceTemplateEmpty() {
1116 Resource resourceExist = createResourceObject(false);
1117 resourceExist.setDerivedFrom(new ArrayList<>());
1119 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1120 } catch (ComponentException e) {
1121 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1125 private void testResourceTemplateInvalid() {
1126 Resource resourceExist = createResourceObject(false);
1127 ArrayList<String> derivedFrom = new ArrayList<>();
1128 derivedFrom.add("kuku");
1129 resourceExist.setDerivedFrom(derivedFrom);
1131 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1132 } catch (ComponentException e) {
1133 assertComponentException(e, ActionStatus.PARENT_RESOURCE_NOT_FOUND);
1137 // Derived from stop
1138 private void assertComponentException(ComponentException e, ActionStatus expectedStatus, String... variables) {
1139 ResponseFormat actualResponse = e.getResponseFormat() != null ? e.getResponseFormat()
1140 : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
1141 assertResponse(actualResponse, expectedStatus, variables);
1144 private void assertResponse(ResponseFormat actualResponse, ActionStatus expectedStatus, String... variables) {
1145 ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables);
1146 assertThat(expectedResponse.getStatus()).isEqualTo(actualResponse.getStatus());
1147 assertThat(expectedResponse.getFormattedMessage()).isEqualTo(actualResponse.getFormattedMessage());
1150 private void assertResponse(Either<Resource, ResponseFormat> createResponse, ActionStatus expectedStatus,
1151 String... variables) {
1152 assertResponse(createResponse.right()
1153 .value(), expectedStatus, variables);
1156 // UPDATE tests - start
1159 public void testResourceNameWrongFormat_UPDATE() {
1160 Resource resource = createResourceObject(true);
1161 Resource updatedResource = createResourceObject(true);
1163 // this is in order to prevent failing with 403 earlier
1164 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1165 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1167 String nameWrongFormat = "ljg*fd";
1168 updatedResource.setName(nameWrongFormat);
1170 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1171 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1173 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1174 } catch (ComponentException e) {
1175 assertComponentException(e, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
1180 public void testResourceNameAfterCertify_UPDATE() {
1181 Resource resource = createResourceObject(true);
1182 Resource updatedResource = createResourceObject(true);
1184 // this is in order to prevent failing with 403 earlier
1185 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1186 // when(resourceOperation.getResource_tx(resource.getUniqueId(),false)).thenReturn(eitherUpdate);
1187 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1189 String name = "ljg";
1190 updatedResource.setName(name);
1191 resource.setVersion("1.0");
1193 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1194 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1196 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1197 } catch (ComponentException e) {
1198 assertComponentException(e, ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED);
1203 public void testResourceNameAlreadyExist_UPDATE() {
1204 Resource resource = createResourceObject(true);
1205 Resource updatedResource = createResourceObject(true);
1207 // this is in order to prevent failing with 403 earlier
1208 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1209 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1211 String resourceName = "alreadyExists";
1212 updatedResource.setName(resourceName);
1213 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(updatedResource);
1214 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1216 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1217 } catch (ComponentException e) {
1218 assertComponentException(e, ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
1219 ComponentTypeEnum.RESOURCE.getValue(), resourceName);
1226 public void testResourceDescExceedsLimit_UPDATE() {
1227 Resource resource = createResourceObject(true);
1228 Resource updatedResource = createResourceObject(true);
1230 // this is in order to prevent failing with 403 earlier
1231 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1232 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1234 // 1025 chars, the limit is 1024
1235 String tooLongResourceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
1236 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
1237 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
1238 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
1239 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
1240 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
1241 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
1242 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
1243 updatedResource.setDescription(tooLongResourceDesc);
1244 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1245 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1247 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1248 } catch (ComponentException e) {
1249 assertComponentException(e, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT,
1250 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
1255 public void testIconWrongFormat_UPDATE() {
1256 Resource resource = createResourceObject(true);
1257 Resource updatedResource = createResourceObject(true);
1259 // this is in order to prevent failing with 403 earlier
1260 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1261 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1264 String icon = "icon.jpg";
1265 updatedResource.setIcon(icon);
1266 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1267 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1269 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1270 } catch (ComponentException e) {
1271 assertComponentException(e, ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.RESOURCE.getValue());
1276 public void testIconAfterCertify_UPDATE() {
1277 Resource resource = createResourceObject(true);
1278 Resource updatedResource = createResourceObject(true);
1280 // this is in order to prevent failing with 403 earlier
1281 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1282 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1285 String icon = "icon";
1286 updatedResource.setIcon(icon);
1288 resource.setVersion("1.0");
1290 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1291 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1293 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1294 } catch (ComponentException e) {
1295 assertComponentException(e, ActionStatus.RESOURCE_ICON_CANNOT_BE_CHANGED);
1300 public void testTagsExceedsLimit_UPDATE() {
1301 Resource resource = createResourceObject(true);
1302 Resource updatedResource = createResourceObject(true);
1304 // this is in order to prevent failing with 403 earlier
1305 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1306 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1308 String tag1 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjQ";
1309 String tag2 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjW";
1310 String tag3 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjE";
1311 String tag4 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjb";
1312 String tag5 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjr";
1313 String tag6 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
1314 String tag7 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
1315 String tag8 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjd";
1316 String tag9 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
1317 String tag10 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
1318 String tag11 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjh";
1319 String tag12 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjj";
1320 String tag13 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjk";
1321 String tag14 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjs";
1322 String tag15 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjz";
1323 String tag16 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjx";
1324 String tag17 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj2";
1325 String tag18 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj3";
1326 String tag19 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj4";
1327 String tag20 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj5";
1328 String tag21 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj0";
1330 List<String> tagsList = new ArrayList<>();
1340 tagsList.add(tag10);
1341 tagsList.add(tag11);
1342 tagsList.add(tag12);
1343 tagsList.add(tag13);
1344 tagsList.add(tag14);
1345 tagsList.add(tag15);
1346 tagsList.add(tag16);
1347 tagsList.add(tag17);
1348 tagsList.add(tag18);
1349 tagsList.add(tag19);
1350 tagsList.add(tag20);
1351 tagsList.add(tag21);
1352 tagsList.add(resource.getName());
1354 updatedResource.setTags(tagsList);
1355 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1356 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1358 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1359 } catch (ComponentException e) {
1360 assertComponentException(e, ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT,
1361 "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
1366 public void testVendorNameWrongFormat_UPDATE() {
1367 Resource resource = createResourceObject(true);
1368 Resource updatedResource = createResourceObject(true);
1370 // this is in order to prevent failing with 403 earlier
1371 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1372 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1375 String nameWrongFormat = "ljg*fd";
1376 updatedResource.setVendorName(nameWrongFormat);
1377 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1378 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1380 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1381 } catch (ComponentException e) {
1382 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
1387 public void testVendorNameWrongFormat() {
1388 Resource resource = createResourceObject(true);
1389 Resource updatedResource = createResourceObject(true);
1391 // this is in order to prevent failing with 403 earlier
1392 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1393 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1396 String nameWrongFormat = "ljg*fd";
1397 updatedResource.setVendorName(nameWrongFormat);
1398 resource.setVersion("1.0");
1400 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1401 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1403 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1404 } catch (ComponentException e) {
1405 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
1410 public void testVendorReleaseExceedsLimit_UPDATE() {
1411 Resource resource = createResourceObject(true);
1412 Resource updatedResource = createResourceObject(true);
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);
1417 // 129 chars, the limit is 128
1418 String tooLongVendorRelease = "h1KSyJh9EspI8SPwAGu4VETfqWejeanuB1PCJBxJmJncYnrW0lnsEFFVRIukRJkwlOVnZCy8p38tjhANeZq3BGMHIawWR6ICl8Wi9mikRYALWgvJug00JrlQ0iPVKPLxy";
1419 updatedResource.setVendorRelease(tooLongVendorRelease);
1420 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1421 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1423 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1424 } catch (ComponentException e) {
1425 assertComponentException(e, ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT,
1426 "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
1431 public void testResourceBadCategory_UPDATE() {
1432 Resource resource = createResourceObject(true);
1433 Resource updatedResource = createResourceObject(true);
1435 // this is in order to prevent failing with 403 earlier
1436 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1437 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1439 String resourceId = resource.getUniqueId();
1440 String badCategory = "ddfds";
1441 updatedResource.setCategories(null);
1442 updatedResource.addCategory(badCategory, "fikt");
1443 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1444 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1446 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1447 } catch (ComponentException e) {
1448 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
1452 @Test(expected = ComponentException.class)
1453 public void createResourceFromCsarTest() {
1454 bl.createResourceFromCsar(resourceResponse, user, new HashMap<>(), "");
1458 public void testCreateResourceFromCsarWithModel() throws URISyntaxException, ZipException {
1460 final File csarFile = new File(
1461 ResourceBusinessLogicTest.class.getClassLoader().getResource("csars/nonOnapCsar.csar").toURI());
1462 final Map<String, byte[]> csar = ZipUtils.readZip(csarFile, false);
1464 String resourceYml = new String(csar.get("Definitions/my_vnf.yaml"));
1466 YamlTemplateParsingHandler yamlTemplateParser = new YamlTemplateParsingHandler(mockJanusGraphDao, null, Mockito.mock(AnnotationBusinessLogic.class), null);
1467 final ParsedToscaYamlInfo parsedToscaYamlInfo = yamlTemplateParser.parseResourceInfoFromYAML("Definitions/my_vnf.yml", resourceYml, Collections.EMPTY_MAP, Collections.EMPTY_MAP, "myVnf", resourceResponse);
1469 when(propertyOperation.getDataTypeByName("tosca.datatypes.testDataType.FromMainTemplate", "testModel")).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1471 when(toscaOperationFacade.getLatestByToscaResourceName(anyString(), any())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1472 Resource vduCp = new Resource();
1473 vduCp.setToscaResourceName("tosca.nodes.nfv.VduCp");
1474 vduCp.setState(LifecycleStateEnum.CERTIFIED);
1475 vduCp.setUniqueId("tosca.nodes.nfv.VduCp");
1476 vduCp.setProperties(new ArrayList<>());
1477 Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
1478 RequirementDefinition req = new RequirementDefinition();
1479 req.setName("virtual_link");
1480 List<RequirementDefinition> listReqs = new ArrayList<>();
1482 requirements.put("tosca.nodes.nfv.VduCp", listReqs);
1483 vduCp.setRequirements(requirements);
1484 PropertyDefinition bitrateProp = new PropertyDefinition();
1485 bitrateProp.setName("bitrate_requirement");
1486 bitrateProp.setType("integer");
1487 vduCp.getProperties().add(bitrateProp);
1488 PropertyDefinition virtNiProp = new PropertyDefinition();
1489 virtNiProp.setName("virtual_network_interface_requirements");
1490 virtNiProp.setType("list");
1491 vduCp.getProperties().add(virtNiProp);
1492 PropertyDefinition descriptionProp = new PropertyDefinition();
1493 descriptionProp.setName("description");
1494 descriptionProp.setType("string");
1495 vduCp.getProperties().add(descriptionProp);
1496 PropertyDefinition roleProp = new PropertyDefinition();
1497 roleProp.setName("role");
1498 roleProp.setType("string");
1499 vduCp.getProperties().add(roleProp);
1500 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel("tosca.nodes.nfv.VduCp", "testModel_myVnf1.0")).thenReturn(Either.left(vduCp));
1503 when(yamlTemplateParsingHandler.parseResourceInfoFromYAML(any(), any(), any(), any(), any(), any())).thenReturn(parsedToscaYamlInfo);
1505 UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo();
1506 uploadComponentInstanceInfo.setType("myType");
1507 resourceResponse.setUniqueId("myVnf");
1508 resourceResponse.setName("myVnf");
1509 resourceResponse.setSystemName("myVnf");
1510 resourceResponse.setModel("testModel");
1511 resourceResponse.setResourceType(ResourceTypeEnum.VF);
1512 resourceResponse.setProperties(new ArrayList<>());
1513 resourceResponse.setCsarVersion("1.0");
1515 Resource derivedFrom = new Resource();
1516 List<PropertyDefinition> properties = new ArrayList<>();
1517 PropertyDefinition baseTypeProp = new PropertyDefinition();
1518 baseTypeProp.setName("propInBase");
1519 baseTypeProp.setType("string");
1520 properties.add(baseTypeProp);
1521 derivedFrom.setProperties(properties );
1522 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(any(), eq("tosca.nodes.nfv.VNF"))).thenReturn(Either.left(derivedFrom));
1524 when(toscaOperationFacade
1525 .validateComponentNameAndModelExists("myVnf", "testModel_myVnf1.0", ResourceTypeEnum.VF, ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(false));
1527 when(toscaOperationFacade.addPropertyToComponent(any(), any(), any())).thenReturn(Either.left(new PropertyDefinition()));
1528 when(toscaOperationFacade.associateComponentInstancePropertiesToComponent(any(), any())).thenReturn(Either.left(Collections.emptyMap()));
1529 when(toscaOperationFacade.associateArtifactsToInstances(any(), any())).thenReturn(StorageOperationStatus.OK);
1530 when(toscaOperationFacade.associateDeploymentArtifactsToInstances(any(), any(), any())).thenReturn(StorageOperationStatus.OK);
1531 when(toscaOperationFacade.associateInstAttributeToComponentToInstances(any(), any())).thenReturn(StorageOperationStatus.OK);
1532 when(toscaOperationFacade.associateResourceInstances(any(Component.class), anyString(), anyList())).thenReturn(Either.left(Collections.EMPTY_LIST));
1533 when(applicationDataTypeCache.getAll("testModel_myVnf1.0")).thenReturn(Either.left(emptyDataTypes));
1535 doAnswer(invocation -> {
1536 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReqs = invocation.getArgument(1);
1537 for (final Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> m: instReqs.entrySet()) {
1538 m.getKey().setRequirements(m.getValue());
1540 return StorageOperationStatus.OK;
1542 when(toscaOperationFacade).associateOrAddCalculatedCapReq(any(), any(), any());
1544 when(toscaOperationFacade.updateCalculatedCapabilitiesRequirements(any(), any(), any())).thenReturn(StorageOperationStatus.OK);
1545 when(groupBusinessLogic.validateUpdateVfGroupNames(any(), any())).thenReturn(Either.left(Collections.EMPTY_MAP));
1547 ComponentInstance ci = new ComponentInstance();
1548 List<ComponentInstance> cis = new ArrayList<>();
1550 doAnswer(invocation -> {
1551 List<ComponentInstance> componentInstances = new ArrayList<ComponentInstance>(((Map<ComponentInstance, Resource>)invocation.getArgument(1)).keySet());
1552 ((Resource)invocation.getArgument(0)).setComponentInstances(componentInstances);
1554 }).when(toscaOperationFacade).associateComponentInstancesToComponent(any(), any(), eq(false), eq(false));
1556 doAnswer(invocation -> {
1557 return Either.left(invocation.getArgument(0));
1558 }).when(csarArtifactsAndGroupsBusinessLogic).deleteVFModules(any(Resource.class), any(CsarInfo.class), eq(true), eq(false));
1560 doAnswer(invocation -> {
1561 return Either.left(resourceResponse);
1562 }).when(toscaOperationFacade).getToscaFullElement("myVnf");
1564 Resource result = bl.createResourceFromCsar(resourceResponse, user, csar, "1234");
1566 assertEquals("myDomain.myVnf", result.getToscaResourceName());
1567 List<String> propIds = result.getProperties().stream().map(prop -> prop.getUniqueId()).collect(Collectors.toList());
1568 assertTrue(propIds.contains("myVnf.propInBase"));
1569 assertTrue(propIds.contains("myVnf.descriptor_id"));
1570 assertTrue(propIds.contains("myVnf.descriptor_version"));
1571 assertTrue(propIds.contains("myVnf.flavour_description"));
1572 assertTrue(propIds.contains("myVnf.flavour_id"));
1573 assertTrue(propIds.contains("myVnf.product_name"));
1574 assertTrue(propIds.contains("myVnf.provider"));
1575 assertTrue(propIds.contains("myVnf.software_version"));
1576 assertTrue(propIds.contains("myVnf.vnfm_info"));
1578 final List<String> reqsName = new ArrayList<>();
1580 final List<ComponentInstance> cisWithExtReq = result.getComponentInstances().stream().filter(instance -> instance.getRequirements().get("tosca.nodes.nfv.VduCp").get(0).isExternal()).collect(Collectors.toList());
1581 cisWithExtReq.forEach(instance -> reqsName.add(instance.getRequirements().get("tosca.nodes.nfv.VduCp").get(0).getExternalName()));
1582 assertEquals(3, cisWithExtReq.size());
1586 public void testResourceCategoryAfterCertify_UPDATE() {
1587 Resource resource = createResourceObject(true);
1588 Resource updatedResource = createResourceObject(true);
1590 // this is in order to prevent failing with 403 earlier
1591 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1592 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1594 String resourceId = resource.getUniqueId();
1595 updatedResource.setCategories(null);
1596 updatedResource.addCategory(RESOURCE_CATEGORY1, UPDATED_SUBCATEGORY);
1597 resource.setVersion("1.0");
1599 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1600 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1602 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1603 } catch (ComponentException e) {
1604 assertComponentException(e, ActionStatus.RESOURCE_CATEGORY_CANNOT_BE_CHANGED);
1608 // Derived from start
1610 public void testResourceTemplateNotExist_UPDATE() {
1611 Resource resource = createResourceObject(true);
1612 Resource updatedResource = createResourceObject(true);
1614 // this is in order to prevent failing with 403 earlier
1615 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1616 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1617 String resourceId = resource.getUniqueId();
1619 List<String> list = null;
1620 updatedResource.setDerivedFrom(list);
1621 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1622 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1624 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1625 } catch (ComponentException e) {
1626 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1631 public void testResourceTemplateEmpty_UPDATE() {
1632 Resource resource = createResourceObject(true);
1633 Resource updatedResource = createResourceObject(true);
1634 String resourceId = resource.getUniqueId();
1636 // this is in order to prevent failing with 403 earlier
1637 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1638 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1640 updatedResource.setDerivedFrom(new ArrayList<>());
1641 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1642 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1644 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1645 } catch (ComponentException e) {
1646 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1651 public void testResourceTemplateInvalid_UPDATE() {
1652 Resource resource = createResourceObject(true);
1653 Resource updatedResource = createResourceObject(true);
1654 String resourceId = resource.getUniqueId();
1656 // this is in order to prevent failing with 403 earlier
1657 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1658 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1660 ArrayList<String> derivedFrom = new ArrayList<>();
1661 derivedFrom.add("kuku");
1662 updatedResource.setDerivedFrom(derivedFrom);
1663 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1664 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1666 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1667 } catch (ComponentException e) {
1668 assertComponentException(e, ActionStatus.PARENT_RESOURCE_NOT_FOUND);
1673 public void testResourceTemplateCertify_UPDATE_HAPPY() {
1674 Resource resource = createResourceObject(true);
1675 Resource updatedResource = createResourceObject(true);
1676 String resourceId = resource.getUniqueId();
1678 // this is in order to prevent failing with 403 earlier
1679 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1680 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1682 Either<Boolean, StorageOperationStatus> isToscaNameExtending = Either.left(true);
1683 when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString(), anyString()))
1684 .thenReturn(isToscaNameExtending);
1686 Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = Either
1687 .left(new HashMap<>());
1688 when(propertyOperation.deleteAllPropertiesAssociatedToNode(any(NodeTypeEnum.class), anyString()))
1689 .thenReturn(findPropertiesOfNode);
1691 resource.setVersion("1.0");
1693 ArrayList<String> derivedFrom = new ArrayList<>();
1694 derivedFrom.add("tosca.nodes.Root");
1695 updatedResource.setDerivedFrom(derivedFrom);
1696 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(updatedResource);
1697 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1698 Resource createdResource = bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1699 assertThat(createdResource).isNotNull();
1703 public void testResourceTemplateCertify_UPDATE_SAD() {
1704 Resource resource = createResourceObject(true);
1705 Resource updatedResource = createResourceObject(true);
1706 String resourceId = resource.getUniqueId();
1708 // this is in order to prevent failing with 403 earlier
1709 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1710 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1712 Either<Boolean, StorageOperationStatus> isToscaNameExtending = Either.left(false);
1713 when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString(), anyString()))
1714 .thenReturn(isToscaNameExtending);
1716 resource.setVersion("1.0");
1718 ArrayList<String> derivedFrom = new ArrayList<>();
1719 derivedFrom.add("tosca.nodes.Root");
1720 updatedResource.setDerivedFrom(derivedFrom);
1721 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1722 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1723 Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = Either
1724 .left(new HashMap<>());
1725 when(propertyOperation.deleteAllPropertiesAssociatedToNode(any(NodeTypeEnum.class), anyString()))
1726 .thenReturn(findPropertiesOfNode);
1729 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1730 } catch (ComponentException e) {
1731 assertComponentException(e, ActionStatus.PARENT_RESOURCE_DOES_NOT_EXTEND);
1734 // Derived from stop
1737 public void createOrUpdateResourceAlreadyCheckout() {
1739 Resource resourceExist = createResourceObject(false);
1740 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1741 when(toscaOperationFacade
1742 .validateComponentNameAndModelExists(resourceExist.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
1743 .thenReturn(Either.left(false));
1744 Resource createdResource = bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null,
1746 createdResource.setLastUpdaterUserId(user.getUserId());
1747 assertThat(createdResource).isNotNull();
1748 Either<Resource, StorageOperationStatus> getLatestResult = Either.left(createdResource);
1749 Either<Component, StorageOperationStatus> getCompLatestResult = Either.left(createdResource);
1750 when(toscaOperationFacade.getLatestByToscaResourceName(resourceExist.getToscaResourceName(), null))
1751 .thenReturn(getCompLatestResult);
1752 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceExist.getToscaResourceName(), null))
1753 .thenReturn(getCompLatestResult);
1754 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1755 .thenReturn(getLatestResult);
1757 Resource resourceToUpdtae = createResourceObject(false);
1759 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1760 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1761 assertNotNull(createOrUpdateResource);
1763 Mockito.verify(toscaOperationFacade, Mockito.times(1))
1764 .overrideComponent(any(Resource.class), any(Resource.class));
1765 Mockito.verify(lifecycleBl, Mockito.times(0))
1766 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1767 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1772 public void createOrUpdateResourceCertified() {
1774 Resource resourceExist = createResourceObject(false);
1775 when(toscaOperationFacade
1776 .validateComponentNameAndModelExists(resourceExist.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
1777 .thenReturn(Either.left(false));
1778 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1779 Resource createdResource = bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null,
1782 assertThat(createdResource).isNotNull();
1783 createdResource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1784 createdResource.setVersion("1.0");
1786 Either<Resource, StorageOperationStatus> getLatestResult = Either.left(createdResource);
1787 Either<Component, StorageOperationStatus> getCompLatestResult = Either.left(createdResource);
1788 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceExist.getToscaResourceName(), null))
1789 .thenReturn(getCompLatestResult);
1790 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1791 .thenReturn(getLatestResult);
1793 when(lifecycleBl.changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1794 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean()))
1795 .thenReturn(Either.left(createdResource));
1797 Resource resourceToUpdtae = createResourceObject(false);
1799 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1800 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1801 assertNotNull(createOrUpdateResource);
1803 Mockito.verify(toscaOperationFacade, Mockito.times(1))
1804 .overrideComponent(any(Resource.class), any(Resource.class));
1805 Mockito.verify(lifecycleBl, Mockito.times(1))
1806 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1807 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1812 public void createOrUpdateResourceNotExist() {
1813 Resource resourceToUpdtae = createResourceObject(false);
1815 Either<Component, StorageOperationStatus> getLatestResult = Either.right(StorageOperationStatus.NOT_FOUND);
1816 when(toscaOperationFacade.getLatestByName(resourceToUpdtae.getName(), null)).thenReturn(getLatestResult);
1817 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceToUpdtae.getToscaResourceName(), null))
1818 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1819 when(toscaOperationFacade
1820 .validateComponentNameAndModelExists(resourceToUpdtae.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
1821 .thenReturn(Either.left(false));
1823 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1824 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1825 assertThat(createOrUpdateResource).isNotNull();
1827 Mockito.verify(toscaOperationFacade, times(1))
1828 .createToscaComponent(resourceToUpdtae);
1829 Mockito.verify(toscaOperationFacade, Mockito.times(0))
1830 .overrideComponent(any(Resource.class), any(Resource.class));
1831 Mockito.verify(lifecycleBl, Mockito.times(0))
1832 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1833 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1838 public void testIfNodeTypeNameHasValidPrefix() {
1839 final List<String> definedNodeTypeNamespaceList = ConfigurationManager.getConfigurationManager()
1840 .getConfiguration().getDefinedResourceNamespace();
1842 definedNodeTypeNamespaceList.parallelStream().forEach(validNodeTypePrefix -> {
1843 final String nodeName = validNodeTypePrefix + "." + "abc";
1844 final Optional<String> result = bl.validateNodeTypeNamePrefix(nodeName, definedNodeTypeNamespaceList);
1845 assertTrue(result.isPresent());
1850 public void updateNestedResource_typeIsNew() throws IOException {
1851 Resource resourceToUpdate = createResourceObject(false);
1852 String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + "." + "abc";
1853 String jsonContent = ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml");
1854 CsarInfo csarInfo = new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", jsonContent,
1856 String nestedResourceName = bl.buildNestedToscaResourceName(resourceToUpdate.getResourceType()
1857 .name(), csarInfo.getVfResourceName(), nodeName)
1859 when(toscaOperationFacade.getLatestByName(resourceToUpdate.getName(), null))
1860 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1861 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdate.getToscaResourceName(), null))
1862 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1863 when(toscaOperationFacade.getLatestByToscaResourceName(nestedResourceName, null))
1864 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1866 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceToUpdate.getToscaResourceName(), null))
1867 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1868 when(toscaOperationFacade
1869 .validateComponentNameAndModelExists(resourceToUpdate.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
1870 .thenReturn(Either.left(false));
1872 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1873 .createOrUpdateResourceByImport(resourceToUpdate, user, false, false, false, csarInfo, nodeName, false);
1874 assertThat(createOrUpdateResource).isNotNull();
1876 Mockito.verify(toscaOperationFacade, times(1))
1877 .createToscaComponent(resourceToUpdate);
1878 Mockito.verify(toscaOperationFacade, times(0))
1879 .overrideComponent(any(Resource.class), any(Resource.class));
1880 Mockito.verify(lifecycleBl, times(0))
1881 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1882 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1886 public void updateNestedResource_typeExists() throws IOException {
1888 Resource resourceToUpdate = createResourceObject(false);
1889 setCanWorkOnResource(resourceResponse);
1890 String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + "." + "abc";
1891 String jsonContent = ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml");
1892 CsarInfo csarInfo = new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", jsonContent,
1894 String nestedResourceName = bl.buildNestedToscaResourceName(resourceToUpdate.getResourceType()
1895 .name(), csarInfo.getVfResourceName(), nodeName)
1897 when(toscaOperationFacade.getLatestByName(resourceToUpdate.getName(), null))
1898 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1899 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdate.getToscaResourceName(), null))
1900 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1901 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceToUpdate.getToscaResourceName(), null))
1902 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1903 when(toscaOperationFacade.getLatestByToscaResourceName(nestedResourceName, null))
1904 .thenReturn(Either.left(resourceResponse));
1905 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1906 .thenReturn(Either.left(resourceResponse));
1908 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1909 .createOrUpdateResourceByImport(resourceToUpdate, user, false, false, false, csarInfo, nodeName, false);
1910 assertThat(createOrUpdateResource).isNotNull();
1911 Mockito.verify(toscaOperationFacade, times(1))
1912 .overrideComponent(any(Resource.class), any(Resource.class));
1913 Mockito.verify(lifecycleBl, times(0))
1914 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1915 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1919 public void testValidatePropertiesDefaultValues_SuccessfullWithoutProperties() {
1920 Resource basic = createResourceObject(true);
1922 Boolean validatePropertiesDefaultValues = bl.validatePropertiesDefaultValues(basic);
1923 assertTrue(validatePropertiesDefaultValues);
1927 public void testValidatePropertiesDefaultValues_SuccessfullWithProperties() {
1928 Resource basic = createResourceObject(true);
1929 PropertyDefinition property = new PropertyDefinition();
1930 property.setName("myProperty");
1931 property.setType(ToscaPropertyType.INTEGER.getType());
1932 property.setDefaultValue("1");
1933 List<PropertyDefinition> properties = new ArrayList<>();
1934 properties.add(property);
1935 basic.setProperties(properties);
1936 when(propertyOperation.isPropertyTypeValid(property, null)).thenReturn(true);
1937 when(propertyOperation.isPropertyDefaultValueValid(property, emptyDataTypes)).thenReturn(true);
1938 Boolean validatePropertiesDefaultValues = bl.validatePropertiesDefaultValues(basic);
1939 assertTrue(validatePropertiesDefaultValues);
1942 @Test(expected = ComponentException.class)
1943 public void testValidatePropertiesDefaultValues_FailedWithProperties() {
1944 Resource basic = createResourceObject(true);
1945 PropertyDefinition property = new PropertyDefinition();
1946 property.setName("myProperty");
1947 property.setType(ToscaPropertyType.INTEGER.getType());
1948 property.setDefaultValue("1.5");
1949 List<PropertyDefinition> properties = new ArrayList<>();
1950 properties.add(property);
1951 basic.setProperties(properties);
1953 when(propertyOperation.isPropertyDefaultValueValid(property, emptyDataTypes)).thenReturn(false);
1954 bl.validatePropertiesDefaultValues(basic);
1958 public void testDeleteMarkedResourcesNoResources() {
1959 Either<List<String>, StorageOperationStatus> eitherNoResources = Either.left(new ArrayList<>());
1961 when(toscaOperationFacade.deleteMarkedElements(ComponentTypeEnum.RESOURCE)).thenReturn(eitherNoResources);
1963 Either<List<String>, ResponseFormat> deleteMarkedResources = bl.deleteMarkedComponents();
1964 assertTrue(deleteMarkedResources.isLeft());
1965 assertTrue(deleteMarkedResources.left().value().isEmpty());
1969 public void testDeleteMarkedResources() {
1970 List<String> ids = new ArrayList<>();
1971 String resourceInUse = "123";
1972 String resourceFree = "456";
1973 ids.add(resourceInUse);
1974 ids.add(resourceFree);
1975 Either<List<String>, StorageOperationStatus> eitherNoResources = Either.left(ids);
1976 when(toscaOperationFacade.getAllComponentsMarkedForDeletion(ComponentTypeEnum.RESOURCE)).thenReturn(eitherNoResources);
1978 Either<Boolean, StorageOperationStatus> resourceInUseResponse = Either.left(true);
1979 Either<Boolean, StorageOperationStatus> resourceFreeResponse = Either.left(false);
1981 List<ArtifactDefinition> artifacts = new ArrayList<>();
1983 when(toscaOperationFacade.isComponentInUse(resourceFree)).thenReturn(resourceFreeResponse);
1984 when(toscaOperationFacade.isComponentInUse(resourceInUse)).thenReturn(resourceInUseResponse);
1986 Either<Component, StorageOperationStatus> eitherDelete = Either.left(new
1988 when(toscaOperationFacade.deleteToscaComponent(resourceFree)).thenReturn(eitherDelete);
1990 List<String> deletedComponents = new ArrayList<>();
1991 deletedComponents.add(resourceFree);
1992 when(toscaOperationFacade.deleteMarkedElements(ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(deletedComponents));
1994 Either<List<String>, ResponseFormat> deleteMarkedResources = bl.deleteMarkedComponents();
1995 assertTrue(deleteMarkedResources.isLeft());
1996 List<String> resourceIdList = deleteMarkedResources.left().value();
1997 assertFalse(resourceIdList.isEmpty());
1998 assertTrue(resourceIdList.contains(resourceFree));
1999 assertFalse(resourceIdList.contains(resourceInUse));
2002 @SuppressWarnings("unchecked")
2004 public void testFindVfCsarArtifactsToHandle() {
2006 Class<ResourceBusinessLogic> targetClass = ResourceBusinessLogic.class;
2007 String methodName = "findVfCsarArtifactsToHandle";
2008 Resource resource = new Resource();
2009 String deploymentArtifactToUpdateFileName = "deploymentArtifactToUpdate.yaml";
2010 String deploymentArtifactToDeleteFileName = "deploymentArtifactToDelete.yaml";
2011 String deploymentArtifactToCreateFileName = "deploymentArtifactToCreate.yaml";
2013 String artifactInfoToUpdateFileName = "infoArtifactToUpdate.yaml";
2014 String artifactInfoToDeleteFileName = "infoArtifactToDelete.yaml";
2015 String artifactInfoToNotDeleteFileName = "infoArtifactNotToDelete.yaml";
2016 String artifactInfoToCreateFileName = "infoArtifactToCreate.yaml";
2018 byte[] oldPayloadData = "oldPayloadData".getBytes();
2019 byte[] newPayloadData = "newPayloadData".getBytes();
2020 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
2022 ArtifactDefinition deploymentArtifactToUpdate = new ArtifactDefinition();
2023 deploymentArtifactToUpdate.setMandatory(false);
2024 deploymentArtifactToUpdate.setArtifactName(deploymentArtifactToUpdateFileName);
2025 deploymentArtifactToUpdate.setArtifactType("SNMP_POLL");
2026 deploymentArtifactToUpdate.setPayload(oldPayloadData);
2027 deploymentArtifactToUpdate
2028 .setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2030 ArtifactDefinition deploymentArtifactToDelete = new ArtifactDefinition();
2031 deploymentArtifactToDelete.setMandatory(false);
2032 deploymentArtifactToDelete.setArtifactName(deploymentArtifactToDeleteFileName);
2033 deploymentArtifactToDelete.setArtifactType("SNMP_TRAP");
2034 deploymentArtifactToDelete.setPayload(oldPayloadData);
2035 deploymentArtifactToDelete
2036 .setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2038 ArtifactDefinition deploymentArtifactToIgnore = new ArtifactDefinition();
2040 deploymentArtifacts.put(ValidationUtils.normalizeArtifactLabel(deploymentArtifactToUpdate.getArtifactName()),
2041 deploymentArtifactToUpdate);
2042 deploymentArtifacts.put(ValidationUtils.normalizeArtifactLabel(deploymentArtifactToDelete.getArtifactName()),
2043 deploymentArtifactToDelete);
2044 deploymentArtifacts.put("ignore", deploymentArtifactToIgnore);
2046 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
2048 ArtifactDefinition artifactToUpdate = new ArtifactDefinition();
2049 artifactToUpdate.setMandatory(false);
2050 artifactToUpdate.setArtifactName(artifactInfoToUpdateFileName);
2051 artifactToUpdate.setArtifactType("SNMP_POLL");
2052 artifactToUpdate.setPayload(oldPayloadData);
2053 artifactToUpdate.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2055 ArtifactDefinition artifactToDelete = new ArtifactDefinition();
2056 artifactToDelete.setMandatory(false);
2057 artifactToDelete.setArtifactName(artifactInfoToDeleteFileName);
2058 artifactToDelete.setArtifactType("SNMP_TRAP");
2059 artifactToDelete.setPayload(oldPayloadData);
2060 artifactToDelete.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2061 artifactToDelete.setIsFromCsar(true);
2063 ArtifactDefinition artifactToNotDelete = new ArtifactDefinition();
2064 artifactToNotDelete.setMandatory(false);
2065 artifactToNotDelete.setArtifactName(artifactInfoToNotDeleteFileName);
2066 artifactToNotDelete.setArtifactType("SNMP_TRAP");
2067 artifactToNotDelete.setPayload(oldPayloadData);
2068 artifactToNotDelete.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2069 artifactToNotDelete.setIsFromCsar(false);
2071 ArtifactDefinition artifactToIgnore = new ArtifactDefinition();
2073 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToUpdate.getArtifactName()), artifactToUpdate);
2074 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToDelete.getArtifactName()), artifactToDelete);
2075 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToNotDelete.getArtifactName()),
2076 artifactToNotDelete);
2077 artifacts.put("ignore", artifactToIgnore);
2079 resource.setDeploymentArtifacts(deploymentArtifacts);
2080 resource.setArtifacts(artifacts);
2082 List<NonMetaArtifactInfo> artifactPathAndNameList = new ArrayList<>();
2083 NonMetaArtifactInfo deploymentArtifactInfoToUpdate = new NonMetaArtifactInfo(
2084 deploymentArtifactToUpdate.getArtifactName(), null,
2085 deploymentArtifactToUpdate.getArtifactType(),
2086 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, deploymentArtifactToUpdate.getArtifactName(), false);
2088 NonMetaArtifactInfo informationalArtifactInfoToUpdate = new NonMetaArtifactInfo(
2089 artifactToUpdate.getArtifactName(), null, artifactToUpdate.getArtifactType(),
2090 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, artifactToUpdate.getArtifactName(), false);
2092 NonMetaArtifactInfo informationalArtifactInfoToUpdateFromCsar = new NonMetaArtifactInfo(
2093 artifactToUpdate.getArtifactName(), null, artifactToUpdate.getArtifactType(),
2094 ArtifactGroupTypeEnum.INFORMATIONAL, newPayloadData, artifactToUpdate.getArtifactName(), true);
2096 NonMetaArtifactInfo deploymentArtifactInfoToUpdateFromCsar = new NonMetaArtifactInfo(
2097 artifactToUpdate.getArtifactName(), null, artifactToUpdate.getArtifactType(),
2098 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, artifactToUpdate.getArtifactName(), true);
2100 NonMetaArtifactInfo deploymentArtifactInfoToCreate = new NonMetaArtifactInfo(deploymentArtifactToCreateFileName,
2101 null, ArtifactTypeEnum.OTHER.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData,
2102 deploymentArtifactToCreateFileName, false);
2104 NonMetaArtifactInfo informationalArtifactInfoToCreate = new NonMetaArtifactInfo(artifactInfoToCreateFileName,
2105 null, ArtifactTypeEnum.OTHER.getType(), ArtifactGroupTypeEnum.INFORMATIONAL, newPayloadData,
2106 artifactInfoToCreateFileName, false);
2108 artifactPathAndNameList.add(deploymentArtifactInfoToUpdate);
2109 artifactPathAndNameList.add(informationalArtifactInfoToUpdate);
2110 artifactPathAndNameList.add(deploymentArtifactInfoToCreate);
2111 artifactPathAndNameList.add(informationalArtifactInfoToCreate);
2112 artifactPathAndNameList.add(informationalArtifactInfoToUpdateFromCsar);
2113 artifactPathAndNameList.add(deploymentArtifactInfoToUpdateFromCsar);
2115 Object[] argObjects = { resource, artifactPathAndNameList, user };
2116 Class[] argClasses = { Resource.class, List.class, User.class };
2118 Method method = targetClass.getDeclaredMethod(methodName, argClasses);
2119 method.setAccessible(true);
2120 Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = (Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat>) method
2121 .invoke(bl, argObjects);
2122 assertTrue(findVfCsarArtifactsToHandleRes.isLeft());
2123 EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> foundVfArtifacts = findVfCsarArtifactsToHandleRes
2126 assertEquals(4, foundVfArtifacts.get(ArtifactOperationEnum.CREATE)
2128 assertEquals(4, foundVfArtifacts.get(ArtifactOperationEnum.UPDATE)
2130 assertEquals(1, foundVfArtifacts.get(ArtifactOperationEnum.DELETE)
2133 } catch (Exception e) {
2134 e.printStackTrace();
2139 public void testVFGeneratedInputs() {
2140 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2141 Resource resource = createVF();
2142 List<InputDefinition> inputs = resource.getInputs();
2143 assertEquals(6, inputs.size());
2144 for (InputDefinition input : inputs) {
2145 assertThat(input.getOwnerId()).isNotNull();
2147 assertEquals(resource.getDerivedFromGenericType(), genericVF.getToscaResourceName());
2148 assertEquals(resource.getDerivedFromGenericVersion(), genericVF.getVersion());
2152 public void testCRGeneratedInputs() {
2153 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2154 Resource resource = createCR();
2155 List<InputDefinition> inputs = resource.getInputs();
2156 assertEquals(3, inputs.size());
2157 for (InputDefinition input : inputs) {
2158 assertThat(input.getOwnerId()).isNotNull();
2160 assertEquals(resource.getDerivedFromGenericType(), genericCR.getToscaResourceName());
2161 assertEquals(resource.getDerivedFromGenericVersion(), genericCR.getVersion());
2165 public void testVFUpdateGenericInputsToLatestOnCheckout() {
2166 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2167 // create a VF that is derived from generic version 1.0
2168 Resource resource = createVF();
2169 // create a new generic version without properties
2170 genericVF.setVersion("2.0");
2171 genericVF.setProperties(null);
2172 String currentDerivedFromVersion = resource.getDerivedFromGenericVersion();
2173 List<InputDefinition> currentInputs = resource.getInputs();
2174 // verify previous inputs ownerId fields exist - user may not delete
2176 assertEquals(6, currentInputs.stream()
2177 .filter(p -> null != p.getOwnerId())
2178 .collect(Collectors.toList())
2180 Either<Boolean, ResponseFormat> upgradeToLatestGeneric = bl.shouldUpgradeToLatestGeneric(resource);
2182 assertTrue(upgradeToLatestGeneric.isLeft());
2183 // verify update required and valid
2184 assertTrue(upgradeToLatestGeneric.left()
2186 // verify version was upgraded
2187 assertNotEquals(resource.getDerivedFromGenericVersion(), currentDerivedFromVersion);
2188 // verify inputs were not deleted
2189 assertEquals(6, resource.getInputs()
2191 // verify inputs ownerId fields were removed - user may delete/edit
2193 assertEquals(6, resource.getInputs()
2195 .filter(p -> null == p.getOwnerId())
2196 .collect(Collectors.toList())
2201 public void testVFUpdateGenericInputsToLatestOnCheckoutNotPerformed() {
2203 // create a VF that is derived from generic version 1.0
2204 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2205 Resource resource = createVF();
2207 // add an input to the VF
2208 PropertyDefinition newProp = new PropertyDefinition();
2209 newProp.setType("integer");
2210 newProp.setName("newProp");
2211 resource.getInputs()
2212 .add(new InputDefinition(newProp));
2214 // create a new generic version with a new property which has the same
2215 // name as a user defined input on the VF with a different type
2216 genericVF.setVersion("2.0");
2217 newProp.setType("string");
2218 genericVF.setProperties(new ArrayList<>());
2219 genericVF.getProperties()
2221 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericVF));
2222 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericVF.getProperties(),
2223 genericVF.getUniqueId())).thenCallRealMethod();
2224 String currentDerivedFromVersion = resource.getDerivedFromGenericVersion();
2225 assertEquals(6, resource.getInputs()
2227 .filter(p -> null != p.getOwnerId())
2228 .collect(Collectors.toList())
2230 Either<Boolean, ResponseFormat> upgradeToLatestGeneric = bl.shouldUpgradeToLatestGeneric(resource);
2232 assertTrue(upgradeToLatestGeneric.isLeft());
2233 // verify update is invalid an void
2234 assertFalse(upgradeToLatestGeneric.left()
2236 // verify version was not upgraded
2237 assertEquals(resource.getDerivedFromGenericVersion(), currentDerivedFromVersion);
2238 // verify inputs were not removed
2239 assertEquals(7, resource.getInputs()
2241 // verify user defined input exists
2242 assertEquals(1, resource.getInputs()
2244 .filter(p -> null == p.getOwnerId())
2245 .collect(Collectors.toList())
2247 assertEquals("integer", resource.getInputs()
2249 .filter(p -> null == p.getOwnerId())
2256 public void testPNFGeneratedInputsNoGeneratedInformationalArtifacts() {
2257 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2258 Resource resource = createPNF();
2259 List<InputDefinition> inputs = resource.getInputs();
2260 assertEquals(3, inputs.size());
2261 for (InputDefinition input : inputs) {
2262 assertThat(input.getOwnerId()).isNotNull();
2264 assertEquals(resource.getDerivedFromGenericType(), genericPNF.getToscaResourceName());
2265 assertEquals(resource.getDerivedFromGenericVersion(), genericPNF.getVersion());
2266 assertEquals(0, resource.getArtifacts()
2270 private Resource createVF() {
2272 genericVF = setupGenericTypeMock(GENERIC_VF_NAME);
2273 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_VF_NAME))
2274 .thenReturn(Either.left(genericVF));
2275 Resource resource = createResourceObject(true);
2276 resource.setDerivedFrom(null);
2277 resource.setResourceType(ResourceTypeEnum.VF);
2278 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2279 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource, null)).thenReturn(Either.left(genericVF));
2280 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericVF)).thenCallRealMethod();
2281 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericVF.getProperties(),
2282 resource.getUniqueId())).thenCallRealMethod();
2283 when(toscaOperationFacade
2284 .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.VF, ComponentTypeEnum.RESOURCE))
2285 .thenReturn(Either.left(false));
2286 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2287 assertThat(createdResource).isNotNull();
2288 return createdResource;
2291 private Resource createRoot() {
2292 rootType = setupGenericTypeMock(GENERIC_ROOT_NAME);
2293 when(toscaOperationFacade.getLatestByToscaResourceName(GENERIC_ROOT_NAME, null))
2294 .thenReturn(Either.left(rootType));
2298 private Resource createCR() {
2300 genericCR = setupGenericTypeMock(GENERIC_CR_NAME);
2301 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_CR_NAME))
2302 .thenReturn(Either.left(genericCR));
2303 Resource resource = createResourceObject(true);
2304 resource.setDerivedFrom(null);
2305 resource.setResourceType(ResourceTypeEnum.CR);
2306 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2307 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource, null)).thenReturn(Either.left(genericCR));
2308 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericCR)).thenCallRealMethod();
2309 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericCR.getProperties(),
2310 resource.getUniqueId())).thenCallRealMethod();
2311 when(toscaOperationFacade
2312 .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.CR, ComponentTypeEnum.RESOURCE))
2313 .thenReturn(Either.left(false));
2314 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2315 assertThat(createdResource).isNotNull();
2316 return createdResource;
2319 private Resource createPNF() {
2321 genericPNF = setupGenericTypeMock(GENERIC_PNF_NAME);
2322 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_PNF_NAME))
2323 .thenReturn(Either.left(genericPNF));
2324 Resource resource = createResourceObject(true);
2325 resource.setDerivedFrom(null);
2326 resource.setResourceType(ResourceTypeEnum.PNF);
2327 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2328 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource, null)).thenReturn(Either.left(genericPNF));
2329 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericPNF)).thenCallRealMethod();
2330 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericPNF.getProperties(),
2331 resource.getUniqueId())).thenCallRealMethod();
2332 when(toscaOperationFacade
2333 .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.PNF, ComponentTypeEnum.RESOURCE))
2334 .thenReturn(Either.left(false));
2335 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2336 assertThat(createdResource).isNotNull();
2337 return createdResource;
2340 private Map<String, String> getGenericPropertiesByToscaName(String toscaName) {
2341 HashMap<String, String> PNFProps = new HashMap<String, String>() {
2343 put("nf_function", "string");
2344 put("nf_role", "string");
2345 put("nf_type", "string");
2349 HashMap<String, String> CRProps = new HashMap<String, String>() {
2351 put("cr_function", "string");
2352 put("cr_role", "string");
2353 put("cr_type", "string");
2357 HashMap<String, String> VFProps = new HashMap<String, String>() {
2360 put("availability_zone_max_count", "integer");
2361 put("min_instances", "integer");
2362 put("max_instances", "integer");
2366 if (toscaName.contains("PNF"))
2368 if (toscaName.contains("CR"))
2370 if (toscaName.contains("VF"))
2373 return new HashMap<>();
2376 private Resource setupGenericTypeMock(String toscaName) {
2378 Resource genericType = createResourceObject(true);
2379 genericType.setVersion("1.0");
2380 genericType.setToscaResourceName(toscaName);
2381 genericType.setAbstract(true);
2382 List<PropertyDefinition> genericProps = new ArrayList<>();
2383 Map<String, String> genericPropsMap = getGenericPropertiesByToscaName(toscaName);
2384 genericPropsMap.forEach((name, type) -> {
2385 PropertyDefinition prop = new PropertyDefinition();
2388 genericProps.add(prop);
2391 genericType.setProperties(genericProps);
2395 private void validateUserRoles(Role... roles) {
2396 List<Role> listOfRoles = Stream.of(roles)
2397 .collect(Collectors.toList());
2401 public void testUpdateVolumeGroup() {
2402 Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
2403 bl.updateVolumeGroup(resource);
2404 assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(true));
2408 public void testUpdateVolumeGroupNull() {
2409 Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
2410 resource.setGroups(null);
2411 bl.updateVolumeGroup(resource);
2412 assertThat(resource.getGroups()).isNull();
2416 public void testUpdateVolumeGroupFail() {
2417 Resource resource = getResourceWithType("NON_EXIST_HEAT", "org.openecomp.groups.VfModule");
2418 bl.updateVolumeGroup(resource);
2419 assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(false));
2422 private Resource getResourceWithType(String artifactType, String groupDefinitionType) {
2423 ArtifactDefinition artifactToUpdate = new ArtifactDefinition();
2424 List<GroupDefinition> groups = new ArrayList<>();
2425 GroupDefinition gd = new GroupDefinition();
2426 List<PropertyDataDefinition> properties = new ArrayList<>();
2427 PropertyDataDefinition pdd = new PropertyDataDefinition();
2428 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
2429 List<String> artifactsList = new ArrayList<>();
2431 artifactToUpdate.setArtifactType(artifactType);
2432 artifactToUpdate.setArtifactName(artifactType);
2433 artifactToUpdate.setUniqueId(artifactType);
2434 Resource resource = createResourceObjectCsar(true);
2435 artifactsList.add(artifactToUpdate.getArtifactName());
2438 pdd.setName("volume_group");
2439 pdd.setValue("true");
2440 pdd.setType(ToscaPropertyType.BOOLEAN.getType());
2442 artifacts.put(artifactToUpdate.getArtifactName(), artifactToUpdate);
2444 properties.add(pdd);
2445 gd.setType(groupDefinitionType);
2446 gd.setProperties(properties);
2447 gd.setArtifacts(artifactsList);
2450 resource.setGroups(groups);
2451 resource.setDeploymentArtifacts(artifacts);
2457 public void testgetAllCertifiedResources() throws Exception {
2458 List<Resource> list = bl.getAllCertifiedResources(true, HighestFilterEnum.HIGHEST_ONLY, "USER");
2459 Assert.assertEquals(reslist,list);
2462 @Test(expected = StorageException.class)
2463 public void testgetAllCertifiedResources_exception() throws Exception {
2464 List<Resource> list = bl.getAllCertifiedResources(false, HighestFilterEnum.NON_HIGHEST_ONLY, "USER");
2465 Assert.assertEquals(reslist,list);
2469 public void testvalidateResourceNameExists() throws Exception {
2470 Either<Map<String, Boolean>, ResponseFormat> res = bl.validateResourceNameExists("Resource", ResourceTypeEnum.CR, "jh0003");
2471 Assert.assertEquals(true,res.isLeft());
2475 public void rollbackWithEitherAlwaysReturnARuntimeException() {
2476 JanusGraphDao janusGraphDao = mockJanusGraphDao;
2477 ActionStatus actionStatus = ActionStatus.INPUTS_NOT_FOUND;
2478 String params = "testName";
2480 Either<Object, RuntimeException> result =
2481 ResourceBusinessLogic.rollbackWithEither(janusGraphDao, actionStatus, params);
2483 assertTrue(result.isRight());
2484 assertTrue(result.right().value() instanceof ByActionStatusComponentException);
2488 public void rollbackWithEitherWorksWithNullJanusGraphDao() {
2489 JanusGraphDao janusGraphDao = null;
2490 ActionStatus actionStatus = ActionStatus.INPUTS_NOT_FOUND;
2491 String params = "testName";
2493 Either<Object, RuntimeException> result =
2494 ResourceBusinessLogic.rollbackWithEither(janusGraphDao, actionStatus, params);
2496 assertTrue(result.isRight());
2497 assertTrue(result.right().value() instanceof ByActionStatusComponentException);