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;
38 import java.io.IOException;
39 import java.lang.reflect.Method;
40 import java.net.URISyntaxException;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.EnumMap;
45 import java.util.HashMap;
46 import java.util.List;
48 import java.util.Map.Entry;
49 import java.util.Optional;
50 import java.util.stream.Collectors;
51 import java.util.stream.Stream;
53 import javax.servlet.ServletContext;
55 import org.apache.commons.lang3.tuple.ImmutablePair;
56 import org.junit.Assert;
57 import org.junit.Before;
58 import org.junit.Test;
59 import org.mockito.InjectMocks;
60 import org.mockito.Mockito;
61 import org.mockito.MockitoAnnotations;
62 import org.openecomp.sdc.ElementOperationMock;
63 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
64 import org.openecomp.sdc.be.components.ArtifactsResolver;
65 import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
66 import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
67 import org.openecomp.sdc.be.components.csar.CsarInfo;
68 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
69 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
70 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
71 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
72 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
73 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
74 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
75 import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
76 import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
77 import org.openecomp.sdc.be.components.validation.UserValidations;
78 import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
79 import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
80 import org.openecomp.sdc.be.components.validation.component.ComponentFieldValidator;
81 import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator;
82 import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
83 import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator;
84 import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator;
85 import org.openecomp.sdc.be.components.validation.component.ComponentValidator;
86 import org.openecomp.sdc.be.config.ConfigurationManager;
87 import org.openecomp.sdc.be.dao.api.ActionStatus;
88 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
89 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
90 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
91 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
92 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
93 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
94 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
95 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
96 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
97 import org.openecomp.sdc.be.facade.operations.CatalogOperation;
98 import org.openecomp.sdc.be.impl.ComponentsUtils;
99 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
100 import org.openecomp.sdc.be.model.ArtifactDefinition;
101 import org.openecomp.sdc.be.model.Component;
102 import org.openecomp.sdc.be.model.ComponentInstance;
103 import org.openecomp.sdc.be.model.ComponentParametersView;
104 import org.openecomp.sdc.be.model.DataTypeDefinition;
105 import org.openecomp.sdc.be.model.GroupDefinition;
106 import org.openecomp.sdc.be.model.InputDefinition;
107 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
108 import org.openecomp.sdc.be.model.LifecycleStateEnum;
109 import org.openecomp.sdc.be.model.NodeTypeInfo;
110 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
111 import org.openecomp.sdc.be.model.PropertyDefinition;
112 import org.openecomp.sdc.be.model.RequirementDefinition;
113 import org.openecomp.sdc.be.model.Resource;
114 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
115 import org.openecomp.sdc.be.model.User;
116 import org.openecomp.sdc.be.model.VendorSoftwareProduct;
117 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
118 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
119 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
120 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
121 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation;
122 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
123 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
124 import org.openecomp.sdc.be.model.operations.StorageException;
125 import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation;
126 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
127 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
128 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
129 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
130 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
131 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
132 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
133 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
134 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
135 import org.openecomp.sdc.be.model.operations.impl.PolicyTypeOperation;
136 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
137 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
138 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
139 import org.openecomp.sdc.be.tosca.CsarUtils;
140 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
141 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
142 import org.openecomp.sdc.be.user.Role;
143 import org.openecomp.sdc.be.user.UserBusinessLogic;
144 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
145 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
146 import org.openecomp.sdc.common.api.Constants;
147 import org.openecomp.sdc.common.impl.ExternalConfiguration;
148 import org.openecomp.sdc.common.impl.FSConfigurationSource;
149 import org.openecomp.sdc.common.util.GeneralUtility;
150 import org.openecomp.sdc.common.util.ValidationUtils;
151 import org.openecomp.sdc.common.zip.ZipUtils;
152 import org.openecomp.sdc.common.zip.exception.ZipException;
153 import org.openecomp.sdc.exception.ResponseFormat;
154 import org.springframework.web.context.WebApplicationContext;
156 import fj.data.Either;
158 public class ResourceBusinessLogicTest {
160 private final ConfigurationManager configurationManager = new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
161 private static final String RESOURCE_CATEGORY1 = "Network Layer 2-3";
162 private static final String RESOURCE_SUBCATEGORY = "Router";
164 private static final String UPDATED_SUBCATEGORY = "Gateway";
166 private static final String RESOURCE_NAME = "My-Resource_Name with space";
167 private static final String RESOURCE_TOSCA_NAME = "My-Resource_Tosca_Name";
168 private static final String GENERIC_ROOT_NAME = "tosca.nodes.Root";
169 private static final String GENERIC_VF_NAME = "org.openecomp.resource.abstract.nodes.VF";
170 private static final String GENERIC_CR_NAME = "org.openecomp.resource.abstract.nodes.CR";
171 private static final String GENERIC_PNF_NAME = "org.openecomp.resource.abstract.nodes.PNF";
173 private final ServletContext servletContext = Mockito.mock(ServletContext.class);
174 IElementOperation mockElementDao;
175 private final JanusGraphDao mockJanusGraphDao = Mockito.mock(JanusGraphDao.class);
176 private final UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
177 private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
178 private final NodeTypeOperation nodeTypeOperation = Mockito.mock(NodeTypeOperation.class);
179 private final NodeTemplateOperation nodeTemplateOperation = Mockito.mock(NodeTemplateOperation.class);
180 private final TopologyTemplateOperation topologyTemplateOperation = Mockito.mock(TopologyTemplateOperation.class);
181 private final LifecycleBusinessLogic lifecycleBl = Mockito.mock(LifecycleBusinessLogic.class);
182 private final CatalogOperation catalogOperation = Mockito.mock(CatalogOperation.class);
183 private final ICapabilityTypeOperation capabilityTypeOperation = Mockito.mock(ICapabilityTypeOperation.class);
184 private final PropertyOperation propertyOperation = Mockito.mock(PropertyOperation.class);
185 private final ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
186 private final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
187 private final UserValidations userValidations = Mockito.mock(UserValidations.class);
188 private final WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
189 private final IInterfaceLifecycleOperation interfaceTypeOperation = Mockito.mock(IInterfaceLifecycleOperation.class);
190 private final ArtifactCassandraDao artifactCassandraDao = Mockito.mock(ArtifactCassandraDao.class);
191 private final IElementOperation elementDao = new ElementOperationMock();
193 private final CsarUtils csarUtils = Mockito.mock(CsarUtils.class);
194 private final UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class);
195 private final IGroupOperation groupOperation = Mockito.mock(IGroupOperation.class);
196 private final IGroupInstanceOperation groupInstanceOperation = Mockito.mock(IGroupInstanceOperation.class);
197 private final IGroupTypeOperation groupTypeOperation = Mockito.mock(IGroupTypeOperation.class);
198 private final GroupBusinessLogic groupBusinessLogic = Mockito.mock(GroupBusinessLogic.class);
199 private final ModelBusinessLogic modelBusinessLogic = Mockito.mock(ModelBusinessLogic.class);
200 private final InterfaceOperation interfaceOperation = Mockito.mock(InterfaceOperation.class);
201 private final ArtifactsOperations artifactToscaOperation = Mockito.mock(ArtifactsOperations.class);
202 private final PropertyBusinessLogic propertyBusinessLogic = Mockito.mock(PropertyBusinessLogic.class);
203 private final ArtifactsResolver artifactsResolver = Mockito.mock(ArtifactsResolver.class);
204 private final InterfaceLifecycleOperation interfaceLifecycleTypeOperation = Mockito.mock(InterfaceLifecycleOperation.class);
205 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
206 private final ResourceImportManager resourceImportManager = Mockito.mock(ResourceImportManager.class);
207 private final InputsBusinessLogic inputsBusinessLogic = Mockito.mock(InputsBusinessLogic.class);
208 private final OutputsBusinessLogic outputsBusinessLogic = Mockito.mock(OutputsBusinessLogic.class);
209 private final CompositionBusinessLogic compositionBusinessLogic = Mockito.mock(CompositionBusinessLogic.class);
210 private final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic = Mockito.mock(ResourceDataMergeBusinessLogic.class);
211 private final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic = Mockito.mock(CsarArtifactsAndGroupsBusinessLogic.class);
212 private final MergeInstanceUtils mergeInstanceUtils = Mockito.mock(MergeInstanceUtils.class);
213 private final UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class);
214 private final ToscaExportHandler toscaExportHandler = Mockito.mock(ToscaExportHandler.class);
215 private final PolicyTypeOperation policyTypeOperation = Mockito.mock(PolicyTypeOperation.class);
216 private final PolicyBusinessLogic policyBusinessLogic = Mockito.mock(PolicyBusinessLogic.class);
217 private final DataTypeBusinessLogic dataTypeBusinessLogic = Mockito.mock(DataTypeBusinessLogic.class);
218 private final PolicyTypeBusinessLogic policyTypeBusinessLogic = Mockito.mock(PolicyTypeBusinessLogic.class);
220 private YamlTemplateParsingHandler yamlTemplateParsingHandler = Mockito.mock(YamlTemplateParsingHandler.class);
222 ResponseFormatManager responseManager = null;
223 private final GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
225 Resource resourceResponse = null;
226 Resource genericVF = null;
227 Resource genericCR = null;
228 Resource genericVFC = null;
229 Resource genericPNF = null;
230 Resource rootType = null;
231 ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
232 ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic(artifactCassandraDao, toscaExportHandler, csarUtils, lifecycleBl,
233 userBusinessLogic, artifactsResolver, elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
234 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
235 CsarOperation csarOperation = Mockito.mock(CsarOperation.class);
237 CsarBusinessLogic csarBusinessLogic ;
238 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<>();
239 List<Resource> reslist;
240 private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
241 protected ComponentDescriptionValidator componentDescriptionValidator = new ComponentDescriptionValidator(componentsUtils);
242 protected ComponentProjectCodeValidator componentProjectCodeValidator = new ComponentProjectCodeValidator(componentsUtils);
243 protected ComponentIconValidator componentIconValidator = new ComponentIconValidator(componentsUtils);
244 protected ComponentContactIdValidator componentContactIdValidator = new ComponentContactIdValidator(componentsUtils);
245 protected ComponentTagsValidator componentTagsValidator = new ComponentTagsValidator(componentsUtils);
246 protected ComponentNameValidator componentNameValidator = new ComponentNameValidator(componentsUtils, toscaOperationFacade);
247 private ComponentValidator componentValidator = createComponentValidator();
248 private SoftwareInformationBusinessLogic softwareInformationBusinessLogic = Mockito.mock(SoftwareInformationBusinessLogic.class);
250 private ComponentValidator createComponentValidator() {
251 List<ComponentFieldValidator> componentFieldValidators = Arrays.asList(componentNameValidator,
252 componentDescriptionValidator, componentProjectCodeValidator,
253 componentIconValidator, componentContactIdValidator,
254 componentTagsValidator);
255 return new ComponentValidator(componentsUtils,componentFieldValidators);
258 ResourceBusinessLogic bl;
261 public void setup() {
262 MockitoAnnotations.initMocks(this);
263 Mockito.reset(propertyOperation);
266 mockElementDao = new ElementOperationMock();
269 // User data and management
271 user.setUserId("jh0003");
272 user.setFirstName("Jimmi");
273 user.setLastName("Hendrix");
274 user.setRole(Role.ADMIN.name());
276 when(mockUserAdmin.getUser("jh0003", false)).thenReturn(user);
277 when(userValidations.validateUserExists(user.getUserId())).thenReturn(user);
278 when(userValidations.validateUserNotEmpty(eq(user), anyString())).thenReturn(user);
279 // Servlet Context attributes
280 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
281 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))
282 .thenReturn(webAppContextWrapper);
283 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
284 when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
286 Either<Boolean, StorageOperationStatus> eitherFalse = Either.left(true);
287 when(toscaOperationFacade.validateComponentNameExists("tosca.nodes.Root", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
288 .thenReturn(eitherFalse);
291 Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
292 when(toscaOperationFacade.validateComponentNameExists("alreadyExists", ResourceTypeEnum.VFC,
293 ComponentTypeEnum.RESOURCE)).thenReturn(eitherCountExist);
295 Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
296 when(toscaOperationFacade.validateComponentNameExists(eq(RESOURCE_NAME), any(ResourceTypeEnum.class),
297 eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherCount);
298 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
299 when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(validateDerivedExists);
301 Either<Boolean, StorageOperationStatus> validateDerivedNotExists = Either.left(false);
302 when(toscaOperationFacade.validateToscaResourceNameExists("kuku")).thenReturn(validateDerivedNotExists);
303 when(graphLockOperation.lockComponent(anyString(), eq(NodeTypeEnum.Resource)))
304 .thenReturn(StorageOperationStatus.OK);
305 when(graphLockOperation.lockComponentByName(anyString(), eq(NodeTypeEnum.Resource)))
306 .thenReturn(StorageOperationStatus.OK);
309 resourceResponse = createResourceObject(true);
310 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(resourceResponse);
311 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
312 when(catalogOperation.updateCatalog(Mockito.any(), Mockito.any())).thenReturn(ActionStatus.OK);
313 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<>();
314 when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(emptyDataTypes));
315 when(mockJanusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
316 when(policyTypeOperation.getLatestPolicyTypeByType(any(String.class), any(String.class)))
317 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
319 artifactManager.setNodeTemplateOperation(nodeTemplateOperation);
320 bl = new ResourceBusinessLogic(mockElementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic,
321 interfaceOperation, interfaceLifecycleTypeOperation, artifactManager, componentInstanceBusinessLogic,
322 resourceImportManager, inputsBusinessLogic, outputsBusinessLogic, compositionBusinessLogic, resourceDataMergeBusinessLogic,
323 csarArtifactsAndGroupsBusinessLogic, mergeInstanceUtils, uiComponentDataConverter, csarBusinessLogic,
324 artifactToscaOperation, propertyBusinessLogic, componentContactIdValidator, componentNameValidator,
325 componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator,
326 componentDescriptionValidator, policyBusinessLogic, modelBusinessLogic, dataTypeBusinessLogic, policyTypeBusinessLogic);
327 bl.setElementDao(mockElementDao);
328 bl.setUserAdmin(mockUserAdmin);
329 bl.setCapabilityTypeOperation(capabilityTypeOperation);
330 bl.setComponentsUtils(componentsUtils);
331 bl.setLifecycleManager(lifecycleBl);
332 bl.setArtifactsManager(artifactManager);
333 bl.setGraphLockOperation(graphLockOperation);
334 bl.setPropertyOperation(propertyOperation);
335 bl.setJanusGraphDao(mockJanusGraphDao);
336 bl.setApplicationDataTypeCache(applicationDataTypeCache);
337 bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
338 bl.setCatalogOperations(catalogOperation);
339 toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
340 csarBusinessLogic.setComponentsUtils(componentsUtils);
341 toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
342 bl.setToscaOperationFacade(toscaOperationFacade);
343 bl.setUserValidations(userValidations);
344 bl.setInterfaceTypeOperation(interfaceTypeOperation);
345 bl.setPolicyTypeOperation(policyTypeOperation);
347 csarBusinessLogic.setCsarOperation(csarOperation);
348 Resource resourceCsar = createResourceObjectCsar(true);
349 setCanWorkOnResource(resourceCsar);
350 Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
351 when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
352 responseManager = ResponseFormatManager.getInstance();
353 bl.setComponentIconValidator(componentIconValidator);
354 bl.setComponentNameValidator(componentNameValidator);
355 bl.setComponentDescriptionValidator(componentDescriptionValidator);
356 bl.setComponentTagsValidator(componentTagsValidator);
357 bl.setComponentContactIdValidator(componentContactIdValidator);
358 bl.setComponentProjectCodeValidator(componentProjectCodeValidator);
359 bl.setComponentValidator(componentValidator);
360 reslist = new ArrayList<>();
361 reslist.add(resourceResponse);
362 reslist.add(genericVF);
363 reslist.add(genericCR);
364 reslist.add(genericVFC);
365 reslist.add(genericPNF);
366 Either<List<Resource>, StorageOperationStatus> returneval= Either.left(reslist);
367 when(toscaOperationFacade.getAllCertifiedResources(true, true)).thenReturn(returneval);
368 when(toscaOperationFacade.validateComponentNameUniqueness("Resource", ResourceTypeEnum.CR, ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(true));
369 Either<List<Resource>, StorageOperationStatus> returnevalexception= Either.right(StorageOperationStatus.BAD_REQUEST);
370 when(toscaOperationFacade.getAllCertifiedResources(false, false)).thenReturn(returnevalexception);
373 @Test(expected = ComponentException.class)
374 public void createResourcesFromYamlNodeTypesList() throws IOException {
375 Map<String, Object> mappedToscaTemplate = new HashMap<>();
376 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>();
377 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
378 Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>();
379 bl.createResourcesFromYamlNodeTypesList(
384 nodeTypesArtifactsToHandle,
385 nodeTypesNewCreatedArtifacts,
387 new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml"),true), "");
391 public void handleNodeTypeArtifactsTest() {
392 Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = new HashMap<>();
393 List<ArtifactDefinition> defs = new ArrayList<>();
394 defs.add(new ArtifactDefinition());
395 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, defs);
396 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, defs);
397 assertTrue(bl.handleNodeTypeArtifacts(resourceResponse, nodeTypeArtifactsToHandle, new ArrayList<>(), user, true, true).isRight());
401 public void getUiComponentDataTransferByComponentIdTest() {
402 when(toscaOperationFacade.getToscaElement(eq(""), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(genericCR));
403 assertTrue(bl.getUiComponentDataTransferByComponentId("", new ArrayList<>()).isLeft());
404 when(toscaOperationFacade.getToscaElement(eq(""), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.OK));
405 assertTrue(bl.getUiComponentDataTransferByComponentId("", new ArrayList<>()).isRight());
409 public void shouldUpgradeToLatestDerivedTest() {
412 when(toscaOperationFacade.shouldUpgradeToLatestDerived(genericCR)).thenReturn(Either.left(genericCR));
413 when(toscaOperationFacade.shouldUpgradeToLatestDerived(genericVFC)).thenReturn(Either.right(StorageOperationStatus.OK));
414 assertTrue(bl.shouldUpgradeToLatestDerived(genericVF).isLeft());
415 assertTrue(bl.shouldUpgradeToLatestDerived(genericCR).isLeft());
418 private Resource createResourceObject(boolean afterCreate) {
419 Resource resource = new Resource();
420 resource.setName(RESOURCE_NAME);
421 resource.setToscaResourceName(RESOURCE_TOSCA_NAME);
422 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
423 resource.setDescription("My short description");
424 List<String> tgs = new ArrayList<>();
426 tgs.add(resource.getName());
427 resource.setTags(tgs);
428 List<String> template = new ArrayList<>();
429 template.add("tosca.nodes.Root");
430 resource.setDerivedFrom(template);
431 resource.setVendorName("Motorola");
432 resource.setVendorRelease("1.0.0");
433 resource.setContactId("ya5467");
434 resource.setIcon("defaulticon");
437 resource.setName(resource.getName());
438 resource.setVersion("0.1");
439 resource.setUniqueId(resource.getName()
440 .toLowerCase() + ":" + resource.getVersion());
441 resource.setCreatorUserId(user.getUserId());
442 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
443 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
448 private Resource createResourceObjectWithModel(boolean afterCreate) {
449 Resource resource = new Resource();
450 resource.setName(RESOURCE_NAME);
451 resource.setToscaResourceName(RESOURCE_TOSCA_NAME);
452 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
453 resource.setDescription("My short description");
454 List<String> tgs = new ArrayList<>();
456 tgs.add(resource.getName());
457 resource.setTags(tgs);
458 List<String> template = new ArrayList<>();
459 template.add("tosca.nodes.Root");
460 resource.setDerivedFrom(template);
461 resource.setVendorName("Motorola");
462 resource.setVendorRelease("1.0.0");
463 resource.setContactId("ya5467");
464 resource.setIcon("defaulticon");
465 resource.setModel("Test Model");
468 resource.setName(resource.getName());
469 resource.setVersion("0.1");
470 resource.setUniqueId(resource.getName()
471 .toLowerCase() + ":" + resource.getVersion());
472 resource.setCreatorUserId(user.getUserId());
473 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
474 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
479 private Resource createResourceObjectCsar(boolean afterCreate) {
480 Resource resource = new Resource();
481 resource.setName(RESOURCE_NAME);
482 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
483 resource.setDescription("My short description");
484 List<String> tgs = new ArrayList<>();
486 tgs.add(resource.getName());
487 resource.setTags(tgs);
488 List<String> template = new ArrayList<>();
489 template.add("tosca.nodes.Root");
490 resource.setDerivedFrom(template);
491 resource.setVendorName("Motorola");
492 resource.setVendorRelease("1.0.0");
493 resource.setResourceVendorModelNumber("");
494 resource.setContactId("ya5467");
495 resource.setIcon("MyIcon");
496 resource.setCsarUUID("valid_vf.csar");
497 resource.setCsarVersion("1");
500 resource.setName(resource.getName());
501 resource.setVersion("0.1");
503 resource.setUniqueId(resource.getName()
504 .toLowerCase() + ":" + resource.getVersion());
505 resource.setCreatorUserId(user.getUserId());
506 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
507 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
512 private Resource setCanWorkOnResource(Resource resource) {
513 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
514 resource.setLastUpdaterUserId(user.getUserId());
519 public void testHappyScenario() {
520 validateUserRoles(Role.ADMIN, Role.DESIGNER);
521 Resource resource = createResourceObject(false);
522 Resource createdResource = null;
524 when(toscaOperationFacade
525 .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
526 .thenReturn(Either.left(false));
527 createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
528 assertThat(createResourceObject(true)).isEqualTo(createdResource);
529 } catch (ComponentException e) {
530 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
536 public void testUpdateHappyScenario() {
537 Resource resource = createResourceObjectCsar(true);
538 setCanWorkOnResource(resource);
539 validateUserRoles(Role.ADMIN, Role.DESIGNER);
540 Either<Resource, StorageOperationStatus> resourceLinkedToCsarRes = Either.left(resource);
541 when(toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, resource.getCsarUUID(), resource.getSystemName())).thenReturn(resourceLinkedToCsarRes);
542 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
543 when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(validateDerivedExists);
544 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
545 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
546 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
547 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
549 assertThat(resource.getUniqueId()).isEqualTo(bl.validateAndUpdateResourceFromCsar(resource, user, null, null, resource.getUniqueId()).getUniqueId());
550 } catch (ComponentException e) {
551 assertThat(e.getResponseFormat().getStatus()).isEqualTo(200);
556 public void testUpdateUnhappyScenario() {
557 Resource resource = createResourceObjectCsar(true);
558 final var csarVersionId = "csarVersionId";
559 resource.setCsarVersionId(csarVersionId);
561 final var vendorSoftwareProduct = new VendorSoftwareProduct();
562 vendorSoftwareProduct.setFileMap(new HashMap<>());
563 vendorSoftwareProduct.setModelList(Collections.emptyList());
564 setCanWorkOnResource(resource);
565 validateUserRoles(Role.ADMIN, Role.DESIGNER);
567 when(toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, resource.getCsarUUID(), resource.getSystemName())).thenReturn(Either.left(resource));
568 when(toscaOperationFacade.validateToscaResourceNameExists("tosca.nodes.Root")).thenReturn(Either.left(true));
569 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(Either.left(setCanWorkOnResource(resource)));
570 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(Either.left(resource));
571 when(csarOperation.findVsp("valid_vf.csar", csarVersionId, user)).thenReturn(Optional.of(vendorSoftwareProduct));
574 Resource createdResource = bl.validateAndUpdateResourceFromCsar(resource, user, null, "", resource.getUniqueId());
575 assertThat(resource.getUniqueId()).isEqualTo(createdResource.getUniqueId());
576 } catch (ComponentException e) {
577 assertThat(e.getResponseFormat().getStatus()).isEqualTo(400);
580 resource.setCsarVersion("2");
581 when(toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, resource.getCsarUUID(), resource.getSystemName())).thenReturn(Either.left(resource));
582 bl.validateAndUpdateResourceFromCsar(resource, user, null, null, resource.getUniqueId());
583 } catch (ComponentException e) {
584 assertThat(e.getResponseFormat().getStatus()).isEqualTo(400);
587 /* CREATE validations - start ***********************/
588 // Resource name - start
591 public void testFailedResourceValidations() {
592 testResourceNameExist();
593 testResourceNameEmpty();
594 // testResourceNameExceedsLimit();
595 testResourceNameWrongFormat();
596 testResourceDescExceedsLimitCreate();
597 testResourceDescNotEnglish();
598 testResourceDescriptionEmpty();
599 testResourceDescriptionMissing();
600 testResourceIconMissing();
601 testResourceIconInvalid();
602 testResourceIconExceedsLimit();
603 testResourceTagNotExist();
604 testResourceTagEmpty();
605 testTagsExceedsLimitCreate();
606 testTagsNoServiceName();
609 testContactIdTooLong();
610 testContactIdWrongFormatCreate();
611 testResourceContactIdEmpty();
612 testResourceContactIdMissing();
613 testVendorNameExceedsLimit();
614 testVendorNameWrongFormatCreate();
615 testVendorReleaseWrongFormat();
616 testVendorReleaseExceedsLimitCreate();
617 testResourceVendorModelNumberExceedsLimit();
618 testResourceVendorNameMissing();
619 testResourceVendorReleaseMissing();
620 testResourceCategoryExist();
621 testResourceBadCategoryCreate();
622 testHappyScenarioCostLicenseType();
623 testCostWrongFormatCreate();
624 testLicenseTypeWrongFormatCreate();
625 testResourceTemplateNotExist();
626 testResourceTemplateEmpty();
627 testResourceTemplateInvalid();
630 private void testResourceNameExist() {
631 String resourceName = "alreadyExists";
632 Resource resourceExist = createResourceObject(false);
633 resourceExist.setName(resourceName);
634 resourceExist.getTags()
636 validateUserRoles(Role.ADMIN, Role.DESIGNER);
637 when(toscaOperationFacade
638 .validateComponentNameAndModelExists(resourceName, null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
639 .thenReturn(Either.left(true));
641 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
642 } catch (ComponentException e) {
643 assertComponentException(e, ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
644 ComponentTypeEnum.RESOURCE.getValue(), resourceName);
648 private void testResourceNameEmpty() {
649 Resource resourceExist = createResourceObject(false);
650 resourceExist.setName(null);
653 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
654 } catch (ComponentException e) {
655 assertComponentException(e, ActionStatus.MISSING_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
659 private void testResourceNameExceedsLimit() {
660 Resource resourceExccedsNameLimit = createResourceObject(false);
661 // 51 chars, the limit is 50
662 String tooLongResourceName = "zCRCAWjqte0DtgcAAMmcJcXeNubeX1p1vOZNTShAHOYNAHvV3iK";
663 resourceExccedsNameLimit.setName(tooLongResourceName);
666 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
667 } catch (ComponentException e) {
668 assertComponentException(e, ActionStatus.COMPONENT_NAME_EXCEEDS_LIMIT,
669 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_NAME_MAX_LENGTH);
673 private void testResourceNameWrongFormat() {
674 Resource resource = createResourceObject(false);
676 String nameWrongFormat = "ljg?fd";
677 resource.setName(nameWrongFormat);
680 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
681 } catch (ComponentException e) {
682 assertComponentException(e, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
686 // Resource name - end
687 // Resource description - start
688 private void testResourceDescExceedsLimitCreate() {
689 Resource resourceExccedsDescLimit = createResourceObject(false);
690 // 1025 chars, the limit is 1024
691 String tooLongResourceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
692 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
693 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
694 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
695 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
696 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
697 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
698 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
700 resourceExccedsDescLimit.setDescription(tooLongResourceDesc);
702 bl.createResource(resourceExccedsDescLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
703 } catch (ComponentException e) {
704 assertComponentException(e, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT,
705 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
709 private void testResourceDescNotEnglish() {
710 Resource notEnglish = createResourceObject(false);
712 String notEnglishDesc = "\uC2B5";
713 notEnglish.setDescription(notEnglishDesc);
716 bl.createResource(notEnglish, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
717 } catch (ComponentException e) {
718 assertComponentException(e, ActionStatus.COMPONENT_INVALID_DESCRIPTION,
719 ComponentTypeEnum.RESOURCE.getValue());
723 private void testResourceDescriptionEmpty() {
724 Resource resourceExist = createResourceObject(false);
725 resourceExist.setDescription("");
728 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
729 } catch (ComponentException e) {
730 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION,
731 ComponentTypeEnum.RESOURCE.getValue());
735 private void testResourceDescriptionMissing() {
736 Resource resourceExist = createResourceObject(false);
737 resourceExist.setDescription(null);
740 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
741 } catch (ComponentException e) {
742 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION,
743 ComponentTypeEnum.RESOURCE.getValue());
746 // Resource description - end
747 // Resource icon start
749 private void testResourceIconMissing() {
750 Resource resourceExist = createResourceObject(false);
751 resourceExist.setIcon(null);
752 when(toscaOperationFacade
753 .validateComponentNameAndModelExists(resourceExist.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
754 .thenReturn(Either.left(false));
756 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
757 } catch (ComponentException e) {
758 assertComponentException(e, ActionStatus.COMPONENT_MISSING_ICON, ComponentTypeEnum.RESOURCE.getValue());
762 private void testResourceIconInvalid() {
763 Resource resourceExist = createResourceObject(false);
764 resourceExist.setIcon("kjk3453^&");
767 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
768 } catch (ComponentException e) {
769 assertComponentException(e, ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.RESOURCE.getValue());
773 private void testResourceIconExceedsLimit() {
774 Resource resourceExist = createResourceObject(false);
775 resourceExist.setIcon("dsjfhskdfhskjdhfskjdhkjdhfkshdfksjsdkfhsdfsdfsdfsfsdfsf");
777 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
778 } catch (ComponentException e) {
779 assertComponentException(e, ActionStatus.COMPONENT_ICON_EXCEEDS_LIMIT,
780 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.ICON_MAX_LENGTH);
785 // Resource tags - start
786 private void testResourceTagNotExist() {
787 Resource resourceExist = createResourceObject(false);
788 resourceExist.setTags(null);
790 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
791 } catch (ComponentException e) {
792 assertComponentException(e, ActionStatus.COMPONENT_MISSING_TAGS);
796 private void testResourceTagEmpty() {
797 Resource resourceExist = createResourceObject(false);
798 resourceExist.setTags(new ArrayList<>());
800 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
801 } catch (ComponentException e) {
802 assertComponentException(e, ActionStatus.COMPONENT_MISSING_TAGS);
806 private void testTagsExceedsLimitCreate() {
807 Resource resourceExccedsNameLimit = createResourceObject(false);
808 String tag1 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjQ";
809 String tag2 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjW";
810 String tag3 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjE";
811 String tag4 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjb";
812 String tag5 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjr";
813 String tag6 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
814 String tag7 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
815 String tag8 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjd";
816 String tag9 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
817 String tag10 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
818 String tag11 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjh";
819 String tag12 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjj";
820 String tag13 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjk";
821 String tag14 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjs";
822 String tag15 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjz";
823 String tag16 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjx";
824 String tag17 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj2";
825 String tag18 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj3";
826 String tag19 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj4";
827 String tag20 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj5";
828 String tag21 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj0";
830 List<String> tagsList = new ArrayList<>();
852 tagsList.add(resourceExccedsNameLimit.getName());
854 resourceExccedsNameLimit.setTags(tagsList);
856 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
857 } catch (ComponentException e) {
858 assertComponentException(e, ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT,
859 "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
863 private void testTagsSingleExceedsLimit() {
864 Resource resourceExccedsNameLimit = createResourceObject(false);
865 String tag1 = "afzs2qLBb5X6tZhiunkcEwiFX1qRQY8YZl3y3Du5M5xeQY5Nq9afcFHDZ9HaURw43gH27nAUWM36bMbMylwTFSzzNV8NO4v4ripe6Q15Vc2nPOFI";
866 String tag2 = resourceExccedsNameLimit.getName();
867 List<String> tagsList = new ArrayList<>();
871 resourceExccedsNameLimit.setTags(tagsList);
873 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
874 } catch (ComponentException e) {
875 assertComponentException(e, ActionStatus.COMPONENT_SINGLE_TAG_EXCEED_LIMIT,
876 "" + ValidationUtils.TAG_MAX_LENGTH);
880 private void testTagsNoServiceName() {
881 Resource serviceExccedsNameLimit = createResourceObject(false);
882 String tag1 = "afzs2qLBb";
883 List<String> tagsList = new ArrayList<>();
885 serviceExccedsNameLimit.setTags(tagsList);
887 bl.createResource(serviceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
888 } catch (ComponentException e) {
889 assertComponentException(e, ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
893 private void testInvalidTag() {
894 Resource serviceExccedsNameLimit = createResourceObject(false);
895 String tag1 = "afzs2qLBb%#%";
896 List<String> tagsList = new ArrayList<>();
898 serviceExccedsNameLimit.setTags(tagsList);
900 bl.createResource(serviceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
901 } catch (ComponentException e) {
902 assertComponentException(e, ActionStatus.INVALID_FIELD_FORMAT, new String[] { "Resource", "tag" });
906 // Resource tags - stop
907 // Resource contact start
909 private void testContactIdTooLong() {
910 Resource resourceContactId = createResourceObject(false);
911 // 59 chars instead of 50
912 String contactIdTooLong = "thisNameIsVeryLongAndExeccedsTheNormalLengthForContactId";
913 resourceContactId.setContactId(contactIdTooLong);
916 bl.createResource(resourceContactId, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
917 } catch (ComponentException e) {
918 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
922 private void testContactIdWrongFormatCreate() {
923 Resource resourceContactId = createResourceObject(false);
924 // 3 letters and 3 digits and special characters
925 String contactIdFormatWrong = "yrt134!!!";
926 resourceContactId.setContactId(contactIdFormatWrong);
928 bl.createResource(resourceContactId, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
929 } catch (ComponentException e) {
930 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
934 private void testResourceContactIdEmpty() {
935 Resource resourceExist = createResourceObject(false);
936 resourceExist.setContactId("");
938 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
939 } catch (ComponentException e) {
940 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
944 private void testResourceContactIdMissing() {
945 Resource resourceExist = createResourceObject(false);
946 resourceExist.setContactId(null);
948 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
949 } catch (ComponentException e) {
950 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.RESOURCE.getValue());
954 private void testVendorNameExceedsLimit() {
955 Resource resourceExccedsVendorNameLimit = createResourceObject(false);
956 String tooLongVendorName = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
957 resourceExccedsVendorNameLimit.setVendorName(tooLongVendorName);
959 bl.createResource(resourceExccedsVendorNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
960 } catch (ComponentException e) {
961 assertComponentException(e, ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT,
962 "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
966 private void testResourceVendorModelNumberExceedsLimit() {
967 Resource resourceExccedsVendorModelNumberLimit = createResourceObject(false);
968 String tooLongVendorModelNumber = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
969 resourceExccedsVendorModelNumberLimit.setResourceVendorModelNumber(tooLongVendorModelNumber);
971 bl.createResource(resourceExccedsVendorModelNumberLimit, AuditingActionEnum.CREATE_RESOURCE, user, null,
973 } catch (ComponentException e) {
974 assertComponentException(e, ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
975 "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
979 private void testVendorNameWrongFormatCreate() {
980 Resource resource = createResourceObject(false);
982 String nameWrongFormat = "ljg*fd";
983 resource.setVendorName(nameWrongFormat);
985 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
986 } catch (ComponentException e) {
987 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
991 private void testVendorReleaseWrongFormat() {
992 Resource resource = createResourceObject(false);
994 String vendorReleaseWrongFormat = "1>2";
995 resource.setVendorRelease(vendorReleaseWrongFormat);
997 bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
998 } catch (ComponentException e) {
999 assertComponentException(e, ActionStatus.INVALID_VENDOR_RELEASE, vendorReleaseWrongFormat);
1003 private void testVendorReleaseExceedsLimitCreate() {
1004 Resource resourceExccedsNameLimit = createResourceObject(false);
1005 String tooLongVendorRelease = "h1KSyJh9Eh1KSyJh9Eh1KSyJh9Eh1KSyJh9E";
1006 resourceExccedsNameLimit.setVendorRelease(tooLongVendorRelease);
1008 bl.createResource(resourceExccedsNameLimit, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1009 } catch (ComponentException e) {
1010 assertComponentException(e, ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT,
1011 "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
1015 private void testResourceVendorNameMissing() {
1016 Resource resourceExist = createResourceObject(false);
1017 resourceExist.setVendorName(null);
1019 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1020 } catch (ComponentException e) {
1021 assertComponentException(e, ActionStatus.MISSING_VENDOR_NAME);
1025 private void testResourceVendorReleaseMissing() {
1026 Resource resourceExist = createResourceObject(false);
1027 resourceExist.setVendorRelease(null);
1029 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1030 } catch (ComponentException e) {
1031 assertComponentException(e, ActionStatus.MISSING_VENDOR_RELEASE);
1035 // Resource vendor name/release stop
1037 private void testResourceCategoryExist() {
1038 Resource resourceExist = createResourceObject(false);
1039 resourceExist.setCategories(null);
1041 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1042 } catch (ComponentException e) {
1043 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
1047 private void testResourceBadCategoryCreate() {
1049 Resource resourceExist = createResourceObject(false);
1050 resourceExist.setCategories(null);
1051 resourceExist.addCategory("koko", "koko");
1053 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1054 } catch (ComponentException e) {
1055 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
1061 private void testHappyScenarioCostLicenseType() {
1062 Resource createResourceObject = createResourceObject(false);
1063 Resource createResourceObjectAfterCreate = createResourceObject(true);
1064 // Adding cost and licenseType to basic mock
1065 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(createResourceObjectAfterCreate);
1066 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
1068 String cost = "123.456";
1069 String licenseType = "User";
1070 createResourceObject.setCost(cost);
1071 createResourceObject.setLicenseType(licenseType);
1072 Resource createdResource;
1074 createdResource = bl.createResource(createResourceObject, AuditingActionEnum.CREATE_RESOURCE, user, null,
1076 createResourceObjectAfterCreate.setCost(cost);
1077 createResourceObjectAfterCreate.setLicenseType(licenseType);
1078 assertThat(createResourceObjectAfterCreate).isEqualTo(createdResource);
1079 } catch (ComponentException e) {
1080 assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
1085 private void testCostWrongFormatCreate() {
1086 Resource resourceCost = createResourceObject(false);
1087 // Comma instead of fullstop
1088 String cost = "12356,464";
1089 resourceCost.setCost(cost);
1091 bl.createResource(resourceCost, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1092 } catch (ComponentException e) {
1093 assertComponentException(e, ActionStatus.INVALID_CONTENT);
1098 // License type start
1099 private void testLicenseTypeWrongFormatCreate() {
1100 Resource resourceLicenseType = createResourceObject(false);
1102 String licenseType = "cpu";
1103 resourceLicenseType.setLicenseType(licenseType);
1105 bl.createResource(resourceLicenseType, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1106 } catch (ComponentException e) {
1107 assertComponentException(e, ActionStatus.INVALID_CONTENT);
1111 // License type stop
1112 // Derived from start
1113 private void testResourceTemplateNotExist() {
1114 Resource resourceExist = createResourceObject(false);
1115 List<String> list = null;
1116 resourceExist.setDerivedFrom(list);
1118 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1119 } catch (ComponentException e) {
1120 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1124 private void testResourceTemplateEmpty() {
1125 Resource resourceExist = createResourceObject(false);
1126 resourceExist.setDerivedFrom(new ArrayList<>());
1128 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1129 } catch (ComponentException e) {
1130 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1134 private void testResourceTemplateInvalid() {
1135 Resource resourceExist = createResourceObject(false);
1136 ArrayList<String> derivedFrom = new ArrayList<>();
1137 derivedFrom.add("kuku");
1138 resourceExist.setDerivedFrom(derivedFrom);
1140 bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
1141 } catch (ComponentException e) {
1142 assertComponentException(e, ActionStatus.PARENT_RESOURCE_NOT_FOUND);
1146 // Derived from stop
1147 private void assertComponentException(ComponentException e, ActionStatus expectedStatus, String... variables) {
1148 ResponseFormat actualResponse = e.getResponseFormat() != null ? e.getResponseFormat()
1149 : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
1150 assertResponse(actualResponse, expectedStatus, variables);
1153 private void assertResponse(ResponseFormat actualResponse, ActionStatus expectedStatus, String... variables) {
1154 ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables);
1155 assertThat(expectedResponse.getStatus()).isEqualTo(actualResponse.getStatus());
1156 assertThat(expectedResponse.getFormattedMessage()).isEqualTo(actualResponse.getFormattedMessage());
1159 private void assertResponse(Either<Resource, ResponseFormat> createResponse, ActionStatus expectedStatus,
1160 String... variables) {
1161 assertResponse(createResponse.right()
1162 .value(), expectedStatus, variables);
1165 // UPDATE tests - start
1168 public void testResourceNameWrongFormat_UPDATE() {
1169 Resource resource = createResourceObject(true);
1170 Resource updatedResource = createResourceObject(true);
1172 // this is in order to prevent failing with 403 earlier
1173 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1174 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1176 String nameWrongFormat = "ljg*fd";
1177 updatedResource.setName(nameWrongFormat);
1179 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1180 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1182 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1183 } catch (ComponentException e) {
1184 assertComponentException(e, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue());
1189 public void testResourceNameAfterCertify_UPDATE() {
1190 Resource resource = createResourceObject(true);
1191 Resource updatedResource = createResourceObject(true);
1193 // this is in order to prevent failing with 403 earlier
1194 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1195 // when(resourceOperation.getResource_tx(resource.getUniqueId(),false)).thenReturn(eitherUpdate);
1196 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1198 String name = "ljg";
1199 updatedResource.setName(name);
1200 resource.setVersion("1.0");
1202 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1203 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1205 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1206 } catch (ComponentException e) {
1207 assertComponentException(e, ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED);
1212 public void testResourceNameAlreadyExist_UPDATE() {
1213 Resource resource = createResourceObject(true);
1214 Resource updatedResource = createResourceObject(true);
1216 // this is in order to prevent failing with 403 earlier
1217 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1218 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1220 String resourceName = "alreadyExists";
1221 updatedResource.setName(resourceName);
1222 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(updatedResource);
1223 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1225 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1226 } catch (ComponentException e) {
1227 assertComponentException(e, ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
1228 ComponentTypeEnum.RESOURCE.getValue(), resourceName);
1235 public void testResourceDescExceedsLimit_UPDATE() {
1236 Resource resource = createResourceObject(true);
1237 Resource updatedResource = createResourceObject(true);
1239 // this is in order to prevent failing with 403 earlier
1240 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1241 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1243 // 1025 chars, the limit is 1024
1244 String tooLongResourceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
1245 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
1246 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
1247 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
1248 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
1249 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
1250 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
1251 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
1252 updatedResource.setDescription(tooLongResourceDesc);
1253 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1254 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1256 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1257 } catch (ComponentException e) {
1258 assertComponentException(e, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT,
1259 ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
1264 public void testIconWrongFormat_UPDATE() {
1265 Resource resource = createResourceObject(true);
1266 Resource updatedResource = createResourceObject(true);
1268 // this is in order to prevent failing with 403 earlier
1269 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1270 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1273 String icon = "icon.jpg";
1274 updatedResource.setIcon(icon);
1275 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1276 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1278 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1279 } catch (ComponentException e) {
1280 assertComponentException(e, ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.RESOURCE.getValue());
1285 public void testIconAfterCertify_UPDATE() {
1286 Resource resource = createResourceObject(true);
1287 Resource updatedResource = createResourceObject(true);
1289 // this is in order to prevent failing with 403 earlier
1290 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1291 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1294 String icon = "icon";
1295 updatedResource.setIcon(icon);
1297 resource.setVersion("1.0");
1299 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1300 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1302 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1303 } catch (ComponentException e) {
1304 assertComponentException(e, ActionStatus.RESOURCE_ICON_CANNOT_BE_CHANGED);
1309 public void testTagsExceedsLimit_UPDATE() {
1310 Resource resource = createResourceObject(true);
1311 Resource updatedResource = createResourceObject(true);
1313 // this is in order to prevent failing with 403 earlier
1314 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1315 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1317 String tag1 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjQ";
1318 String tag2 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjW";
1319 String tag3 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjE";
1320 String tag4 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjb";
1321 String tag5 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjr";
1322 String tag6 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
1323 String tag7 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
1324 String tag8 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjd";
1325 String tag9 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
1326 String tag10 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
1327 String tag11 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjh";
1328 String tag12 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjj";
1329 String tag13 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjk";
1330 String tag14 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjs";
1331 String tag15 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjz";
1332 String tag16 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjx";
1333 String tag17 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj2";
1334 String tag18 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj3";
1335 String tag19 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj4";
1336 String tag20 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj5";
1337 String tag21 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj0";
1339 List<String> tagsList = new ArrayList<>();
1349 tagsList.add(tag10);
1350 tagsList.add(tag11);
1351 tagsList.add(tag12);
1352 tagsList.add(tag13);
1353 tagsList.add(tag14);
1354 tagsList.add(tag15);
1355 tagsList.add(tag16);
1356 tagsList.add(tag17);
1357 tagsList.add(tag18);
1358 tagsList.add(tag19);
1359 tagsList.add(tag20);
1360 tagsList.add(tag21);
1361 tagsList.add(resource.getName());
1363 updatedResource.setTags(tagsList);
1364 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1365 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1367 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1368 } catch (ComponentException e) {
1369 assertComponentException(e, ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT,
1370 "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
1375 public void testVendorNameWrongFormat_UPDATE() {
1376 Resource resource = createResourceObject(true);
1377 Resource updatedResource = createResourceObject(true);
1379 // this is in order to prevent failing with 403 earlier
1380 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1381 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1384 String nameWrongFormat = "ljg*fd";
1385 updatedResource.setVendorName(nameWrongFormat);
1386 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1387 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1389 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1390 } catch (ComponentException e) {
1391 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
1396 public void testVendorNameWrongFormat() {
1397 Resource resource = createResourceObject(true);
1398 Resource updatedResource = createResourceObject(true);
1400 // this is in order to prevent failing with 403 earlier
1401 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1402 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1405 String nameWrongFormat = "ljg*fd";
1406 updatedResource.setVendorName(nameWrongFormat);
1407 resource.setVersion("1.0");
1409 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1410 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1412 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1413 } catch (ComponentException e) {
1414 assertComponentException(e, ActionStatus.INVALID_VENDOR_NAME, nameWrongFormat);
1419 public void testVendorReleaseExceedsLimit_UPDATE() {
1420 Resource resource = createResourceObject(true);
1421 Resource updatedResource = createResourceObject(true);
1423 // this is in order to prevent failing with 403 earlier
1424 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1425 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1426 // 129 chars, the limit is 128
1427 String tooLongVendorRelease = "h1KSyJh9EspI8SPwAGu4VETfqWejeanuB1PCJBxJmJncYnrW0lnsEFFVRIukRJkwlOVnZCy8p38tjhANeZq3BGMHIawWR6ICl8Wi9mikRYALWgvJug00JrlQ0iPVKPLxy";
1428 updatedResource.setVendorRelease(tooLongVendorRelease);
1429 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1430 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1432 bl.updateResourceMetadata(resource.getUniqueId(), updatedResource, null, user, false);
1433 } catch (ComponentException e) {
1434 assertComponentException(e, ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT,
1435 "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
1440 public void testResourceBadCategory_UPDATE() {
1441 Resource resource = createResourceObject(true);
1442 Resource updatedResource = createResourceObject(true);
1444 // this is in order to prevent failing with 403 earlier
1445 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1446 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1448 String resourceId = resource.getUniqueId();
1449 String badCategory = "ddfds";
1450 updatedResource.setCategories(null);
1451 updatedResource.addCategory(badCategory, "fikt");
1452 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1453 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1455 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1456 } catch (ComponentException e) {
1457 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
1461 @Test(expected = ComponentException.class)
1462 public void createResourceFromCsarTest() {
1463 bl.createResourceFromCsar(resourceResponse, user, new HashMap<>(), "");
1467 public void testCreateResourceFromCsarWithModel() throws URISyntaxException, ZipException {
1469 final File csarFile = new File(
1470 ResourceBusinessLogicTest.class.getClassLoader().getResource("csars/nonOnapCsar.csar").toURI());
1471 final Map<String, byte[]> csar = ZipUtils.readZip(csarFile, false);
1473 String resourceYml = new String(csar.get("Definitions/my_vnf.yaml"));
1475 YamlTemplateParsingHandler yamlTemplateParser = new YamlTemplateParsingHandler(mockJanusGraphDao, null, Mockito.mock(AnnotationBusinessLogic.class), null);
1476 final ParsedToscaYamlInfo parsedToscaYamlInfo = yamlTemplateParser.parseResourceInfoFromYAML("Definitions/my_vnf.yml", resourceYml, Collections.EMPTY_MAP, Collections.EMPTY_MAP, "myVnf", resourceResponse);
1478 when(propertyOperation.getDataTypeByName("tosca.datatypes.testDataType.FromMainTemplate", "testModel")).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1480 when(toscaOperationFacade.getLatestByToscaResourceName(anyString(), any())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1481 Resource vduCp = new Resource();
1482 vduCp.setToscaResourceName("tosca.nodes.nfv.VduCp");
1483 vduCp.setState(LifecycleStateEnum.CERTIFIED);
1484 vduCp.setUniqueId("tosca.nodes.nfv.VduCp");
1485 vduCp.setProperties(new ArrayList<>());
1486 Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
1487 RequirementDefinition req = new RequirementDefinition();
1488 req.setName("virtual_link");
1489 List<RequirementDefinition> listReqs = new ArrayList<>();
1491 requirements.put("tosca.nodes.nfv.VduCp", listReqs);
1492 vduCp.setRequirements(requirements);
1493 PropertyDefinition bitrateProp = new PropertyDefinition();
1494 bitrateProp.setName("bitrate_requirement");
1495 bitrateProp.setType("integer");
1496 vduCp.getProperties().add(bitrateProp);
1497 PropertyDefinition virtNiProp = new PropertyDefinition();
1498 virtNiProp.setName("virtual_network_interface_requirements");
1499 virtNiProp.setType("list");
1500 vduCp.getProperties().add(virtNiProp);
1501 PropertyDefinition descriptionProp = new PropertyDefinition();
1502 descriptionProp.setName("description");
1503 descriptionProp.setType("string");
1504 vduCp.getProperties().add(descriptionProp);
1505 PropertyDefinition roleProp = new PropertyDefinition();
1506 roleProp.setName("role");
1507 roleProp.setType("string");
1508 vduCp.getProperties().add(roleProp);
1509 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel("tosca.nodes.nfv.VduCp", "testModel_myVnf1.0")).thenReturn(Either.left(vduCp));
1511 when(yamlTemplateParsingHandler.parseResourceInfoFromYAML(any(), any(), any(), any(), any(), any())).thenReturn(parsedToscaYamlInfo);
1513 UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo();
1514 uploadComponentInstanceInfo.setType("myType");
1515 resourceResponse.setUniqueId("myVnf");
1516 resourceResponse.setName("myVnf");
1517 resourceResponse.setSystemName("myVnf");
1518 resourceResponse.setModel("testModel");
1519 resourceResponse.setResourceType(ResourceTypeEnum.VF);
1520 resourceResponse.setProperties(new ArrayList<>());
1521 resourceResponse.setCsarVersion("1.0");
1523 Resource derivedFrom = new Resource();
1524 List<PropertyDefinition> properties = new ArrayList<>();
1525 PropertyDefinition baseTypeProp = new PropertyDefinition();
1526 baseTypeProp.setName("propInBase");
1527 baseTypeProp.setType("string");
1528 properties.add(baseTypeProp);
1529 derivedFrom.setProperties(properties );
1530 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(any(), eq("tosca.nodes.nfv.VNF"))).thenReturn(Either.left(derivedFrom));
1532 when(toscaOperationFacade
1533 .validateComponentNameAndModelExists("myVnf", "testModel_myVnf1.0", ResourceTypeEnum.VF, ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(false));
1535 when(toscaOperationFacade.addPropertyToComponent(any(), any(), any())).thenReturn(Either.left(new PropertyDefinition()));
1536 when(toscaOperationFacade.associateComponentInstancePropertiesToComponent(any(), any())).thenReturn(Either.left(Collections.emptyMap()));
1537 when(toscaOperationFacade.associateArtifactsToInstances(any(), any())).thenReturn(StorageOperationStatus.OK);
1538 when(toscaOperationFacade.associateDeploymentArtifactsToInstances(any(), any(), any())).thenReturn(StorageOperationStatus.OK);
1539 when(toscaOperationFacade.associateInstAttributeToComponentToInstances(any(), any())).thenReturn(StorageOperationStatus.OK);
1540 when(toscaOperationFacade.associateResourceInstances(any(Component.class), anyString(), anyList())).thenReturn(Either.left(Collections.EMPTY_LIST));
1541 when(applicationDataTypeCache.getAll("testModel_myVnf1.0")).thenReturn(Either.left(emptyDataTypes));
1543 doAnswer(invocation -> {
1544 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReqs = invocation.getArgument(1);
1545 for (final Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> m: instReqs.entrySet()) {
1546 m.getKey().setRequirements(m.getValue());
1548 return StorageOperationStatus.OK;
1550 when(toscaOperationFacade).associateOrAddCalculatedCapReq(any(), any(), any());
1552 when(toscaOperationFacade.updateCalculatedCapabilitiesRequirements(any(), any(), any())).thenReturn(StorageOperationStatus.OK);
1553 when(groupBusinessLogic.validateUpdateVfGroupNames(any(), any())).thenReturn(Either.left(Collections.EMPTY_MAP));
1555 ComponentInstance ci = new ComponentInstance();
1556 List<ComponentInstance> cis = new ArrayList<>();
1558 doAnswer(invocation -> {
1559 List<ComponentInstance> componentInstances = new ArrayList<ComponentInstance>(((Map<ComponentInstance, Resource>)invocation.getArgument(1)).keySet());
1560 ((Resource)invocation.getArgument(0)).setComponentInstances(componentInstances);
1562 }).when(toscaOperationFacade).associateComponentInstancesToComponent(any(), any(), eq(false), eq(false));
1564 doAnswer(invocation -> {
1565 return Either.left(invocation.getArgument(0));
1566 }).when(csarArtifactsAndGroupsBusinessLogic).deleteVFModules(any(Resource.class), any(CsarInfo.class), eq(true), eq(false));
1568 doAnswer(invocation -> {
1569 return Either.left(resourceResponse);
1570 }).when(toscaOperationFacade).getToscaFullElement("myVnf");
1572 Resource result = bl.createResourceFromCsar(resourceResponse, user, csar, "1234");
1574 assertEquals("myDomain.myVnf", result.getToscaResourceName());
1575 List<String> propIds = result.getProperties().stream().map(prop -> prop.getUniqueId()).collect(Collectors.toList());
1576 assertTrue(propIds.contains("myVnf.propInBase"));
1577 assertTrue(propIds.contains("myVnf.descriptor_id"));
1578 assertTrue(propIds.contains("myVnf.descriptor_version"));
1579 assertTrue(propIds.contains("myVnf.flavour_description"));
1580 assertTrue(propIds.contains("myVnf.flavour_id"));
1581 assertTrue(propIds.contains("myVnf.product_name"));
1582 assertTrue(propIds.contains("myVnf.provider"));
1583 assertTrue(propIds.contains("myVnf.software_version"));
1584 assertTrue(propIds.contains("myVnf.vnfm_info"));
1586 final List<String> reqsName = new ArrayList<>();
1588 final List<ComponentInstance> cisWithExtReq = result.getComponentInstances().stream().filter(instance -> instance.getRequirements().get("tosca.nodes.nfv.VduCp").get(0).isExternal()).collect(Collectors.toList());
1589 cisWithExtReq.forEach(instance -> reqsName.add(instance.getRequirements().get("tosca.nodes.nfv.VduCp").get(0).getExternalName()));
1590 assertEquals(3, cisWithExtReq.size());
1594 public void testResourceCategoryAfterCertify_UPDATE() {
1595 Resource resource = createResourceObject(true);
1596 Resource updatedResource = createResourceObject(true);
1598 // this is in order to prevent failing with 403 earlier
1599 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1600 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1602 String resourceId = resource.getUniqueId();
1603 updatedResource.setCategories(null);
1604 updatedResource.addCategory(RESOURCE_CATEGORY1, UPDATED_SUBCATEGORY);
1605 resource.setVersion("1.0");
1607 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1608 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1610 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1611 } catch (ComponentException e) {
1612 assertComponentException(e, ActionStatus.RESOURCE_CATEGORY_CANNOT_BE_CHANGED);
1616 // Derived from start
1618 public void testResourceTemplateNotExist_UPDATE() {
1619 Resource resource = createResourceObject(true);
1620 Resource updatedResource = createResourceObject(true);
1622 // this is in order to prevent failing with 403 earlier
1623 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1624 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1625 String resourceId = resource.getUniqueId();
1627 List<String> list = null;
1628 updatedResource.setDerivedFrom(list);
1629 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1630 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1632 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1633 } catch (ComponentException e) {
1634 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1639 public void testResourceTemplateEmpty_UPDATE() {
1640 Resource resource = createResourceObject(true);
1641 Resource updatedResource = createResourceObject(true);
1642 String resourceId = resource.getUniqueId();
1644 // this is in order to prevent failing with 403 earlier
1645 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1646 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1648 updatedResource.setDerivedFrom(new ArrayList<>());
1649 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1650 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1652 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1653 } catch (ComponentException e) {
1654 assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
1659 public void testResourceTemplateInvalid_UPDATE() {
1660 Resource resource = createResourceObject(true);
1661 Resource updatedResource = createResourceObject(true);
1662 String resourceId = resource.getUniqueId();
1664 // this is in order to prevent failing with 403 earlier
1665 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1666 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1668 ArrayList<String> derivedFrom = new ArrayList<>();
1669 derivedFrom.add("kuku");
1670 updatedResource.setDerivedFrom(derivedFrom);
1671 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1672 when(toscaOperationFacade.updateToscaElement(resource)).thenReturn(dataModelResponse);
1674 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1675 } catch (ComponentException e) {
1676 assertComponentException(e, ActionStatus.PARENT_RESOURCE_NOT_FOUND);
1681 public void testResourceTemplateCertify_UPDATE_HAPPY() {
1682 Resource resource = createResourceObject(true);
1683 Resource updatedResource = createResourceObject(true);
1684 String resourceId = resource.getUniqueId();
1686 // this is in order to prevent failing with 403 earlier
1687 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1688 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1690 Either<Boolean, StorageOperationStatus> isToscaNameExtending = Either.left(true);
1691 when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString(), anyString()))
1692 .thenReturn(isToscaNameExtending);
1694 Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = Either
1695 .left(new HashMap<>());
1696 when(propertyOperation.deleteAllPropertiesAssociatedToNode(any(NodeTypeEnum.class), anyString()))
1697 .thenReturn(findPropertiesOfNode);
1699 resource.setVersion("1.0");
1701 ArrayList<String> derivedFrom = new ArrayList<>();
1702 derivedFrom.add("tosca.nodes.Root");
1703 updatedResource.setDerivedFrom(derivedFrom);
1704 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(updatedResource);
1705 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1706 Resource createdResource = bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1707 assertThat(createdResource).isNotNull();
1711 public void testResourceTemplateCertify_UPDATE_SAD() {
1712 Resource resource = createResourceObject(true);
1713 Resource updatedResource = createResourceObject(true);
1714 String resourceId = resource.getUniqueId();
1716 // this is in order to prevent failing with 403 earlier
1717 Either<Component, StorageOperationStatus> eitherUpdate = Either.left(setCanWorkOnResource(resource));
1718 when(toscaOperationFacade.getToscaElement(resource.getUniqueId())).thenReturn(eitherUpdate);
1720 Either<Boolean, StorageOperationStatus> isToscaNameExtending = Either.left(false);
1721 when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString(), anyString()))
1722 .thenReturn(isToscaNameExtending);
1724 resource.setVersion("1.0");
1726 ArrayList<String> derivedFrom = new ArrayList<>();
1727 derivedFrom.add("tosca.nodes.Root");
1728 updatedResource.setDerivedFrom(derivedFrom);
1729 Either<Resource, StorageOperationStatus> dataModelResponse = Either.left(resource);
1730 when(toscaOperationFacade.updateToscaElement(updatedResource)).thenReturn(dataModelResponse);
1731 Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = Either
1732 .left(new HashMap<>());
1733 when(propertyOperation.deleteAllPropertiesAssociatedToNode(any(NodeTypeEnum.class), anyString()))
1734 .thenReturn(findPropertiesOfNode);
1737 bl.updateResourceMetadata(resourceId, updatedResource, null, user, false);
1738 } catch (ComponentException e) {
1739 assertComponentException(e, ActionStatus.PARENT_RESOURCE_DOES_NOT_EXTEND);
1742 // Derived from stop
1745 public void createOrUpdateResourceAlreadyCheckout() {
1747 Resource resourceExist = createResourceObject(false);
1748 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1749 when(toscaOperationFacade
1750 .validateComponentNameAndModelExists(resourceExist.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
1751 .thenReturn(Either.left(false));
1752 Resource createdResource = bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null,
1754 createdResource.setLastUpdaterUserId(user.getUserId());
1755 assertThat(createdResource).isNotNull();
1756 Either<Resource, StorageOperationStatus> getLatestResult = Either.left(createdResource);
1757 Either<Component, StorageOperationStatus> getCompLatestResult = Either.left(createdResource);
1758 when(toscaOperationFacade.getLatestByToscaResourceName(resourceExist.getToscaResourceName(), null))
1759 .thenReturn(getCompLatestResult);
1760 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceExist.getToscaResourceName(), null))
1761 .thenReturn(getCompLatestResult);
1762 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1763 .thenReturn(getLatestResult);
1765 Resource resourceToUpdtae = createResourceObject(false);
1767 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1768 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1769 assertNotNull(createOrUpdateResource);
1771 Mockito.verify(toscaOperationFacade, Mockito.times(1))
1772 .overrideComponent(any(Resource.class), any(Resource.class));
1773 Mockito.verify(lifecycleBl, Mockito.times(0))
1774 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1775 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1780 public void createOrUpdateResourceCertified() {
1782 Resource resourceExist = createResourceObject(false);
1783 when(toscaOperationFacade
1784 .validateComponentNameAndModelExists(resourceExist.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
1785 .thenReturn(Either.left(false));
1786 validateUserRoles(Role.ADMIN, Role.DESIGNER);
1787 Resource createdResource = bl.createResource(resourceExist, AuditingActionEnum.CREATE_RESOURCE, user, null,
1790 assertThat(createdResource).isNotNull();
1791 createdResource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
1792 createdResource.setVersion("1.0");
1794 Either<Resource, StorageOperationStatus> getLatestResult = Either.left(createdResource);
1795 Either<Component, StorageOperationStatus> getCompLatestResult = Either.left(createdResource);
1796 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceExist.getToscaResourceName(), null))
1797 .thenReturn(getCompLatestResult);
1798 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1799 .thenReturn(getLatestResult);
1801 when(lifecycleBl.changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1802 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean()))
1803 .thenReturn(Either.left(createdResource));
1805 Resource resourceToUpdtae = createResourceObject(false);
1807 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1808 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1809 assertNotNull(createOrUpdateResource);
1811 Mockito.verify(toscaOperationFacade, Mockito.times(1))
1812 .overrideComponent(any(Resource.class), any(Resource.class));
1813 Mockito.verify(lifecycleBl, Mockito.times(1))
1814 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1815 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1820 public void createOrUpdateResourceNotExist() {
1821 Resource resourceToUpdtae = createResourceObject(false);
1823 Either<Component, StorageOperationStatus> getLatestResult = Either.right(StorageOperationStatus.NOT_FOUND);
1824 when(toscaOperationFacade.getLatestByName(resourceToUpdtae.getName(), null)).thenReturn(getLatestResult);
1825 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceToUpdtae.getToscaResourceName(), null))
1826 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1827 when(toscaOperationFacade
1828 .validateComponentNameAndModelExists(resourceToUpdtae.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
1829 .thenReturn(Either.left(false));
1831 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1832 .createOrUpdateResourceByImport(resourceToUpdtae, user, false, false, false, null, null, false);
1833 assertThat(createOrUpdateResource).isNotNull();
1835 Mockito.verify(toscaOperationFacade, times(1))
1836 .createToscaComponent(resourceToUpdtae);
1837 Mockito.verify(toscaOperationFacade, Mockito.times(0))
1838 .overrideComponent(any(Resource.class), any(Resource.class));
1839 Mockito.verify(lifecycleBl, Mockito.times(0))
1840 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1841 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1846 public void testIfNodeTypeNameHasValidPrefix() {
1847 final List<String> definedNodeTypeNamespaceList = ConfigurationManager.getConfigurationManager()
1848 .getConfiguration().getDefinedResourceNamespace();
1850 definedNodeTypeNamespaceList.parallelStream().forEach(validNodeTypePrefix -> {
1851 final String nodeName = validNodeTypePrefix + "." + "abc";
1852 final Optional<String> result = bl.validateNodeTypeNamePrefix(nodeName, definedNodeTypeNamespaceList);
1853 assertTrue(result.isPresent());
1858 public void updateNestedResource_typeIsNew() throws IOException {
1859 Resource resourceToUpdate = createResourceObject(false);
1860 String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + "." + "abc";
1861 String jsonContent = ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml");
1862 CsarInfo csarInfo = new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", jsonContent,
1864 String nestedResourceName = bl.buildNestedToscaResourceName(resourceToUpdate.getResourceType()
1865 .name(), csarInfo.getVfResourceName(), nodeName)
1867 when(toscaOperationFacade.getLatestByName(resourceToUpdate.getName(), null))
1868 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1869 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdate.getToscaResourceName(), null))
1870 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1871 when(toscaOperationFacade.getLatestByToscaResourceName(nestedResourceName, null))
1872 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1874 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceToUpdate.getToscaResourceName(), null))
1875 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1876 when(toscaOperationFacade
1877 .validateComponentNameAndModelExists(resourceToUpdate.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
1878 .thenReturn(Either.left(false));
1880 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1881 .createOrUpdateResourceByImport(resourceToUpdate, user, false, false, false, csarInfo, nodeName, false);
1882 assertThat(createOrUpdateResource).isNotNull();
1884 Mockito.verify(toscaOperationFacade, times(1))
1885 .createToscaComponent(resourceToUpdate);
1886 Mockito.verify(toscaOperationFacade, times(0))
1887 .overrideComponent(any(Resource.class), any(Resource.class));
1888 Mockito.verify(lifecycleBl, times(0))
1889 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1890 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1894 public void updateNestedResource_typeExists() throws IOException {
1896 Resource resourceToUpdate = createResourceObject(false);
1897 setCanWorkOnResource(resourceResponse);
1898 String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + "." + "abc";
1899 String jsonContent = ImportUtilsTest.loadFileNameToJsonString("normative-types-new-webServer.yml");
1900 CsarInfo csarInfo = new CsarInfo(user, "abcd1234", new HashMap<>(), RESOURCE_NAME, "template name", jsonContent,
1902 String nestedResourceName = bl.buildNestedToscaResourceName(resourceToUpdate.getResourceType()
1903 .name(), csarInfo.getVfResourceName(), nodeName)
1905 when(toscaOperationFacade.getLatestByName(resourceToUpdate.getName(), null))
1906 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1907 when(toscaOperationFacade.getLatestByToscaResourceName(resourceToUpdate.getToscaResourceName(), null))
1908 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1909 when(toscaOperationFacade.getLatestByToscaResourceNameAndModel(resourceToUpdate.getToscaResourceName(), null))
1910 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
1911 when(toscaOperationFacade.getLatestByToscaResourceName(nestedResourceName, null))
1912 .thenReturn(Either.left(resourceResponse));
1913 when(toscaOperationFacade.overrideComponent(any(Resource.class), any(Resource.class)))
1914 .thenReturn(Either.left(resourceResponse));
1916 ImmutablePair<Resource, ActionStatus> createOrUpdateResource = bl
1917 .createOrUpdateResourceByImport(resourceToUpdate, user, false, false, false, csarInfo, nodeName, false);
1918 assertThat(createOrUpdateResource).isNotNull();
1919 Mockito.verify(toscaOperationFacade, times(1))
1920 .overrideComponent(any(Resource.class), any(Resource.class));
1921 Mockito.verify(lifecycleBl, times(0))
1922 .changeState(anyString(), eq(user), eq(LifeCycleTransitionEnum.CHECKOUT),
1923 any(LifecycleChangeInfoWithAction.class), Mockito.anyBoolean(), Mockito.anyBoolean());
1927 public void testValidatePropertiesDefaultValues_SuccessfullWithoutProperties() {
1928 Resource basic = createResourceObject(true);
1930 Boolean validatePropertiesDefaultValues = bl.validatePropertiesDefaultValues(basic);
1931 assertTrue(validatePropertiesDefaultValues);
1935 public void testValidatePropertiesDefaultValues_SuccessfullWithProperties() {
1936 Resource basic = createResourceObject(true);
1937 PropertyDefinition property = new PropertyDefinition();
1938 property.setName("myProperty");
1939 property.setType(ToscaPropertyType.INTEGER.getType());
1940 property.setDefaultValue("1");
1941 List<PropertyDefinition> properties = new ArrayList<>();
1942 properties.add(property);
1943 basic.setProperties(properties);
1944 when(propertyOperation.isPropertyTypeValid(property, null)).thenReturn(true);
1945 when(propertyOperation.isPropertyDefaultValueValid(property, emptyDataTypes)).thenReturn(true);
1946 Boolean validatePropertiesDefaultValues = bl.validatePropertiesDefaultValues(basic);
1947 assertTrue(validatePropertiesDefaultValues);
1950 @Test(expected = ComponentException.class)
1951 public void testValidatePropertiesDefaultValues_FailedWithProperties() {
1952 Resource basic = createResourceObject(true);
1953 PropertyDefinition property = new PropertyDefinition();
1954 property.setName("myProperty");
1955 property.setType(ToscaPropertyType.INTEGER.getType());
1956 property.setDefaultValue("1.5");
1957 List<PropertyDefinition> properties = new ArrayList<>();
1958 properties.add(property);
1959 basic.setProperties(properties);
1961 when(propertyOperation.isPropertyDefaultValueValid(property, emptyDataTypes)).thenReturn(false);
1962 bl.validatePropertiesDefaultValues(basic);
1966 public void testDeleteMarkedResourcesNoResources() {
1967 Either<List<String>, StorageOperationStatus> eitherNoResources = Either.left(new ArrayList<>());
1969 when(toscaOperationFacade.deleteMarkedElements(ComponentTypeEnum.RESOURCE)).thenReturn(eitherNoResources);
1971 Either<List<String>, ResponseFormat> deleteMarkedResources = bl.deleteMarkedComponents();
1972 assertTrue(deleteMarkedResources.isLeft());
1973 assertTrue(deleteMarkedResources.left().value().isEmpty());
1977 public void testDeleteMarkedResources() {
1978 List<String> ids = new ArrayList<>();
1979 String resourceInUse = "123";
1980 String resourceFree = "456";
1981 ids.add(resourceInUse);
1982 ids.add(resourceFree);
1983 Either<List<String>, StorageOperationStatus> eitherNoResources = Either.left(ids);
1984 when(toscaOperationFacade.getAllComponentsMarkedForDeletion(ComponentTypeEnum.RESOURCE)).thenReturn(eitherNoResources);
1986 Either<Boolean, StorageOperationStatus> resourceInUseResponse = Either.left(true);
1987 Either<Boolean, StorageOperationStatus> resourceFreeResponse = Either.left(false);
1989 List<ArtifactDefinition> artifacts = new ArrayList<>();
1991 when(toscaOperationFacade.isComponentInUse(resourceFree)).thenReturn(resourceFreeResponse);
1992 when(toscaOperationFacade.isComponentInUse(resourceInUse)).thenReturn(resourceInUseResponse);
1994 Either<Component, StorageOperationStatus> eitherDelete = Either.left(new
1996 when(toscaOperationFacade.deleteToscaComponent(resourceFree)).thenReturn(eitherDelete);
1998 List<String> deletedComponents = new ArrayList<>();
1999 deletedComponents.add(resourceFree);
2000 when(toscaOperationFacade.deleteMarkedElements(ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(deletedComponents));
2002 Either<List<String>, ResponseFormat> deleteMarkedResources = bl.deleteMarkedComponents();
2003 assertTrue(deleteMarkedResources.isLeft());
2004 List<String> resourceIdList = deleteMarkedResources.left().value();
2005 assertFalse(resourceIdList.isEmpty());
2006 assertTrue(resourceIdList.contains(resourceFree));
2007 assertFalse(resourceIdList.contains(resourceInUse));
2010 @SuppressWarnings("unchecked")
2012 public void testFindVfCsarArtifactsToHandle() {
2014 Class<ResourceBusinessLogic> targetClass = ResourceBusinessLogic.class;
2015 String methodName = "findVfCsarArtifactsToHandle";
2016 Resource resource = new Resource();
2017 String deploymentArtifactToUpdateFileName = "deploymentArtifactToUpdate.yaml";
2018 String deploymentArtifactToDeleteFileName = "deploymentArtifactToDelete.yaml";
2019 String deploymentArtifactToCreateFileName = "deploymentArtifactToCreate.yaml";
2021 String artifactInfoToUpdateFileName = "infoArtifactToUpdate.yaml";
2022 String artifactInfoToDeleteFileName = "infoArtifactToDelete.yaml";
2023 String artifactInfoToNotDeleteFileName = "infoArtifactNotToDelete.yaml";
2024 String artifactInfoToCreateFileName = "infoArtifactToCreate.yaml";
2026 byte[] oldPayloadData = "oldPayloadData".getBytes();
2027 byte[] newPayloadData = "newPayloadData".getBytes();
2028 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
2030 ArtifactDefinition deploymentArtifactToUpdate = new ArtifactDefinition();
2031 deploymentArtifactToUpdate.setMandatory(false);
2032 deploymentArtifactToUpdate.setArtifactName(deploymentArtifactToUpdateFileName);
2033 deploymentArtifactToUpdate.setArtifactType("SNMP_POLL");
2034 deploymentArtifactToUpdate.setPayload(oldPayloadData);
2035 deploymentArtifactToUpdate
2036 .setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2038 ArtifactDefinition deploymentArtifactToDelete = new ArtifactDefinition();
2039 deploymentArtifactToDelete.setMandatory(false);
2040 deploymentArtifactToDelete.setArtifactName(deploymentArtifactToDeleteFileName);
2041 deploymentArtifactToDelete.setArtifactType("SNMP_TRAP");
2042 deploymentArtifactToDelete.setPayload(oldPayloadData);
2043 deploymentArtifactToDelete
2044 .setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2046 ArtifactDefinition deploymentArtifactToIgnore = new ArtifactDefinition();
2048 deploymentArtifacts.put(ValidationUtils.normalizeArtifactLabel(deploymentArtifactToUpdate.getArtifactName()),
2049 deploymentArtifactToUpdate);
2050 deploymentArtifacts.put(ValidationUtils.normalizeArtifactLabel(deploymentArtifactToDelete.getArtifactName()),
2051 deploymentArtifactToDelete);
2052 deploymentArtifacts.put("ignore", deploymentArtifactToIgnore);
2054 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
2056 ArtifactDefinition artifactToUpdate = new ArtifactDefinition();
2057 artifactToUpdate.setMandatory(false);
2058 artifactToUpdate.setArtifactName(artifactInfoToUpdateFileName);
2059 artifactToUpdate.setArtifactType("SNMP_POLL");
2060 artifactToUpdate.setPayload(oldPayloadData);
2061 artifactToUpdate.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2063 ArtifactDefinition artifactToDelete = new ArtifactDefinition();
2064 artifactToDelete.setMandatory(false);
2065 artifactToDelete.setArtifactName(artifactInfoToDeleteFileName);
2066 artifactToDelete.setArtifactType("SNMP_TRAP");
2067 artifactToDelete.setPayload(oldPayloadData);
2068 artifactToDelete.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2069 artifactToDelete.setIsFromCsar(true);
2071 ArtifactDefinition artifactToNotDelete = new ArtifactDefinition();
2072 artifactToNotDelete.setMandatory(false);
2073 artifactToNotDelete.setArtifactName(artifactInfoToNotDeleteFileName);
2074 artifactToNotDelete.setArtifactType("SNMP_TRAP");
2075 artifactToNotDelete.setPayload(oldPayloadData);
2076 artifactToNotDelete.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(oldPayloadData));
2077 artifactToNotDelete.setIsFromCsar(false);
2079 ArtifactDefinition artifactToIgnore = new ArtifactDefinition();
2081 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToUpdate.getArtifactName()), artifactToUpdate);
2082 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToDelete.getArtifactName()), artifactToDelete);
2083 artifacts.put(ValidationUtils.normalizeArtifactLabel(artifactToNotDelete.getArtifactName()),
2084 artifactToNotDelete);
2085 artifacts.put("ignore", artifactToIgnore);
2087 resource.setDeploymentArtifacts(deploymentArtifacts);
2088 resource.setArtifacts(artifacts);
2090 List<NonMetaArtifactInfo> artifactPathAndNameList = new ArrayList<>();
2091 NonMetaArtifactInfo deploymentArtifactInfoToUpdate = new NonMetaArtifactInfo(
2092 deploymentArtifactToUpdate.getArtifactName(), null,
2093 deploymentArtifactToUpdate.getArtifactType(),
2094 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, deploymentArtifactToUpdate.getArtifactName(), false);
2096 NonMetaArtifactInfo informationalArtifactInfoToUpdate = new NonMetaArtifactInfo(
2097 artifactToUpdate.getArtifactName(), null, artifactToUpdate.getArtifactType(),
2098 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, artifactToUpdate.getArtifactName(), false);
2100 NonMetaArtifactInfo informationalArtifactInfoToUpdateFromCsar = new NonMetaArtifactInfo(
2101 artifactToUpdate.getArtifactName(), null, artifactToUpdate.getArtifactType(),
2102 ArtifactGroupTypeEnum.INFORMATIONAL, newPayloadData, artifactToUpdate.getArtifactName(), true);
2104 NonMetaArtifactInfo deploymentArtifactInfoToUpdateFromCsar = new NonMetaArtifactInfo(
2105 artifactToUpdate.getArtifactName(), null, artifactToUpdate.getArtifactType(),
2106 ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData, artifactToUpdate.getArtifactName(), true);
2108 NonMetaArtifactInfo deploymentArtifactInfoToCreate = new NonMetaArtifactInfo(deploymentArtifactToCreateFileName,
2109 null, ArtifactTypeEnum.OTHER.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, newPayloadData,
2110 deploymentArtifactToCreateFileName, false);
2112 NonMetaArtifactInfo informationalArtifactInfoToCreate = new NonMetaArtifactInfo(artifactInfoToCreateFileName,
2113 null, ArtifactTypeEnum.OTHER.getType(), ArtifactGroupTypeEnum.INFORMATIONAL, newPayloadData,
2114 artifactInfoToCreateFileName, false);
2116 artifactPathAndNameList.add(deploymentArtifactInfoToUpdate);
2117 artifactPathAndNameList.add(informationalArtifactInfoToUpdate);
2118 artifactPathAndNameList.add(deploymentArtifactInfoToCreate);
2119 artifactPathAndNameList.add(informationalArtifactInfoToCreate);
2120 artifactPathAndNameList.add(informationalArtifactInfoToUpdateFromCsar);
2121 artifactPathAndNameList.add(deploymentArtifactInfoToUpdateFromCsar);
2123 Object[] argObjects = { resource, artifactPathAndNameList, user };
2124 Class[] argClasses = { Resource.class, List.class, User.class };
2126 Method method = targetClass.getDeclaredMethod(methodName, argClasses);
2127 method.setAccessible(true);
2128 Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = (Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat>) method
2129 .invoke(bl, argObjects);
2130 assertTrue(findVfCsarArtifactsToHandleRes.isLeft());
2131 EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> foundVfArtifacts = findVfCsarArtifactsToHandleRes
2134 assertEquals(4, foundVfArtifacts.get(ArtifactOperationEnum.CREATE)
2136 assertEquals(4, foundVfArtifacts.get(ArtifactOperationEnum.UPDATE)
2138 assertEquals(1, foundVfArtifacts.get(ArtifactOperationEnum.DELETE)
2141 } catch (Exception e) {
2142 e.printStackTrace();
2147 public void testVFGeneratedInputs() {
2148 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2149 Resource resource = createVF();
2150 List<InputDefinition> inputs = resource.getInputs();
2151 assertEquals(6, inputs.size());
2152 for (InputDefinition input : inputs) {
2153 assertThat(input.getOwnerId()).isNotNull();
2155 assertEquals(resource.getDerivedFromGenericType(), genericVF.getToscaResourceName());
2156 assertEquals(resource.getDerivedFromGenericVersion(), genericVF.getVersion());
2160 public void testCRGeneratedInputs() {
2161 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2162 Resource resource = createCR();
2163 List<InputDefinition> inputs = resource.getInputs();
2164 assertEquals(3, inputs.size());
2165 for (InputDefinition input : inputs) {
2166 assertThat(input.getOwnerId()).isNotNull();
2168 assertEquals(resource.getDerivedFromGenericType(), genericCR.getToscaResourceName());
2169 assertEquals(resource.getDerivedFromGenericVersion(), genericCR.getVersion());
2173 public void testVFUpdateGenericInputsToLatestOnCheckout() {
2174 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2175 // create a VF that is derived from generic version 1.0
2176 Resource resource = createVF();
2177 // create a new generic version without properties
2178 genericVF.setVersion("2.0");
2179 genericVF.setProperties(null);
2180 String currentDerivedFromVersion = resource.getDerivedFromGenericVersion();
2181 List<InputDefinition> currentInputs = resource.getInputs();
2182 // verify previous inputs ownerId fields exist - user may not delete
2184 assertEquals(6, currentInputs.stream()
2185 .filter(p -> null != p.getOwnerId())
2186 .collect(Collectors.toList())
2188 Either<Boolean, ResponseFormat> upgradeToLatestGeneric = bl.shouldUpgradeToLatestGeneric(resource);
2190 assertTrue(upgradeToLatestGeneric.isLeft());
2191 // verify update required and valid
2192 assertTrue(upgradeToLatestGeneric.left()
2194 // verify version was upgraded
2195 assertNotEquals(resource.getDerivedFromGenericVersion(), currentDerivedFromVersion);
2196 // verify inputs were not deleted
2197 assertEquals(6, resource.getInputs()
2199 // verify inputs ownerId fields were removed - user may delete/edit
2201 assertEquals(6, resource.getInputs()
2203 .filter(p -> null == p.getOwnerId())
2204 .collect(Collectors.toList())
2209 public void testVFUpdateGenericInputsToLatestOnCheckoutNotPerformed() {
2211 // create a VF that is derived from generic version 1.0
2212 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2213 Resource resource = createVF();
2215 // add an input to the VF
2216 PropertyDefinition newProp = new PropertyDefinition();
2217 newProp.setType("integer");
2218 newProp.setName("newProp");
2219 resource.getInputs()
2220 .add(new InputDefinition(newProp));
2222 // create a new generic version with a new property which has the same
2223 // name as a user defined input on the VF with a different type
2224 genericVF.setVersion("2.0");
2225 newProp.setType("string");
2226 genericVF.setProperties(new ArrayList<>());
2227 genericVF.getProperties()
2229 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource)).thenReturn(Either.left(genericVF));
2230 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericVF.getProperties(),
2231 genericVF.getUniqueId())).thenCallRealMethod();
2232 String currentDerivedFromVersion = resource.getDerivedFromGenericVersion();
2233 assertEquals(6, resource.getInputs()
2235 .filter(p -> null != p.getOwnerId())
2236 .collect(Collectors.toList())
2238 Either<Boolean, ResponseFormat> upgradeToLatestGeneric = bl.shouldUpgradeToLatestGeneric(resource);
2240 assertTrue(upgradeToLatestGeneric.isLeft());
2241 // verify update is invalid an void
2242 assertFalse(upgradeToLatestGeneric.left()
2244 // verify version was not upgraded
2245 assertEquals(resource.getDerivedFromGenericVersion(), currentDerivedFromVersion);
2246 // verify inputs were not removed
2247 assertEquals(7, resource.getInputs()
2249 // verify user defined input exists
2250 assertEquals(1, resource.getInputs()
2252 .filter(p -> null == p.getOwnerId())
2253 .collect(Collectors.toList())
2255 assertEquals("integer", resource.getInputs()
2257 .filter(p -> null == p.getOwnerId())
2264 public void testPNFGeneratedInputsNoGeneratedInformationalArtifacts() {
2265 validateUserRoles(Role.ADMIN, Role.DESIGNER);
2266 Resource resource = createPNF();
2267 List<InputDefinition> inputs = resource.getInputs();
2268 assertEquals(3, inputs.size());
2269 for (InputDefinition input : inputs) {
2270 assertThat(input.getOwnerId()).isNotNull();
2272 assertEquals(resource.getDerivedFromGenericType(), genericPNF.getToscaResourceName());
2273 assertEquals(resource.getDerivedFromGenericVersion(), genericPNF.getVersion());
2274 assertEquals(0, resource.getArtifacts()
2278 private Resource createVF() {
2280 genericVF = setupGenericTypeMock(GENERIC_VF_NAME);
2281 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_VF_NAME))
2282 .thenReturn(Either.left(genericVF));
2283 Resource resource = createResourceObject(true);
2284 resource.setDerivedFrom(null);
2285 resource.setResourceType(ResourceTypeEnum.VF);
2286 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2287 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource, null)).thenReturn(Either.left(genericVF));
2288 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericVF)).thenCallRealMethod();
2289 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericVF.getProperties(),
2290 resource.getUniqueId())).thenCallRealMethod();
2291 when(toscaOperationFacade
2292 .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.VF, ComponentTypeEnum.RESOURCE))
2293 .thenReturn(Either.left(false));
2294 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2295 assertThat(createdResource).isNotNull();
2296 return createdResource;
2299 private Resource createRoot() {
2300 rootType = setupGenericTypeMock(GENERIC_ROOT_NAME);
2301 when(toscaOperationFacade.getLatestByToscaResourceName(GENERIC_ROOT_NAME, null))
2302 .thenReturn(Either.left(rootType));
2306 private Resource createCR() {
2308 genericCR = setupGenericTypeMock(GENERIC_CR_NAME);
2309 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_CR_NAME))
2310 .thenReturn(Either.left(genericCR));
2311 Resource resource = createResourceObject(true);
2312 resource.setDerivedFrom(null);
2313 resource.setResourceType(ResourceTypeEnum.CR);
2314 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2315 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource, null)).thenReturn(Either.left(genericCR));
2316 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericCR)).thenCallRealMethod();
2317 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericCR.getProperties(),
2318 resource.getUniqueId())).thenCallRealMethod();
2319 when(toscaOperationFacade
2320 .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.CR, ComponentTypeEnum.RESOURCE))
2321 .thenReturn(Either.left(false));
2322 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2323 assertThat(createdResource).isNotNull();
2324 return createdResource;
2327 private Resource createPNF() {
2329 genericPNF = setupGenericTypeMock(GENERIC_PNF_NAME);
2330 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_PNF_NAME))
2331 .thenReturn(Either.left(genericPNF));
2332 Resource resource = createResourceObject(true);
2333 resource.setDerivedFrom(null);
2334 resource.setResourceType(ResourceTypeEnum.PNF);
2335 when(toscaOperationFacade.createToscaComponent(resource)).thenReturn(Either.left(resource));
2336 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(resource, null)).thenReturn(Either.left(genericPNF));
2337 when(genericTypeBusinessLogic.generateInputsFromGenericTypeProperties(genericPNF)).thenCallRealMethod();
2338 when(genericTypeBusinessLogic.convertGenericTypePropertiesToInputsDefintion(genericPNF.getProperties(),
2339 resource.getUniqueId())).thenCallRealMethod();
2340 when(toscaOperationFacade
2341 .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.PNF, ComponentTypeEnum.RESOURCE))
2342 .thenReturn(Either.left(false));
2343 Resource createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
2344 assertThat(createdResource).isNotNull();
2345 return createdResource;
2348 private Map<String, String> getGenericPropertiesByToscaName(String toscaName) {
2349 HashMap<String, String> PNFProps = new HashMap<String, String>() {
2351 put("nf_function", "string");
2352 put("nf_role", "string");
2353 put("nf_type", "string");
2357 HashMap<String, String> CRProps = new HashMap<String, String>() {
2359 put("cr_function", "string");
2360 put("cr_role", "string");
2361 put("cr_type", "string");
2365 HashMap<String, String> VFProps = new HashMap<String, String>() {
2368 put("availability_zone_max_count", "integer");
2369 put("min_instances", "integer");
2370 put("max_instances", "integer");
2374 if (toscaName.contains("PNF"))
2376 if (toscaName.contains("CR"))
2378 if (toscaName.contains("VF"))
2381 return new HashMap<>();
2384 private Resource setupGenericTypeMock(String toscaName) {
2386 Resource genericType = createResourceObject(true);
2387 genericType.setVersion("1.0");
2388 genericType.setToscaResourceName(toscaName);
2389 genericType.setAbstract(true);
2390 List<PropertyDefinition> genericProps = new ArrayList<>();
2391 Map<String, String> genericPropsMap = getGenericPropertiesByToscaName(toscaName);
2392 genericPropsMap.forEach((name, type) -> {
2393 PropertyDefinition prop = new PropertyDefinition();
2396 genericProps.add(prop);
2399 genericType.setProperties(genericProps);
2403 private void validateUserRoles(Role... roles) {
2404 List<Role> listOfRoles = Stream.of(roles)
2405 .collect(Collectors.toList());
2409 public void testUpdateVolumeGroup() {
2410 Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
2411 bl.updateVolumeGroup(resource);
2412 assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(true));
2416 public void testUpdateVolumeGroupNull() {
2417 Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
2418 resource.setGroups(null);
2419 bl.updateVolumeGroup(resource);
2420 assertThat(resource.getGroups()).isNull();
2424 public void testUpdateVolumeGroupFail() {
2425 Resource resource = getResourceWithType("NON_EXIST_HEAT", "org.openecomp.groups.VfModule");
2426 bl.updateVolumeGroup(resource);
2427 assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(false));
2430 private Resource getResourceWithType(String artifactType, String groupDefinitionType) {
2431 ArtifactDefinition artifactToUpdate = new ArtifactDefinition();
2432 List<GroupDefinition> groups = new ArrayList<>();
2433 GroupDefinition gd = new GroupDefinition();
2434 List<PropertyDataDefinition> properties = new ArrayList<>();
2435 PropertyDataDefinition pdd = new PropertyDataDefinition();
2436 Map<String, ArtifactDefinition> artifacts = new HashMap<>();
2437 List<String> artifactsList = new ArrayList<>();
2439 artifactToUpdate.setArtifactType(artifactType);
2440 artifactToUpdate.setArtifactName(artifactType);
2441 artifactToUpdate.setUniqueId(artifactType);
2442 Resource resource = createResourceObjectCsar(true);
2443 artifactsList.add(artifactToUpdate.getArtifactName());
2446 pdd.setName("volume_group");
2447 pdd.setValue("true");
2448 pdd.setType(ToscaPropertyType.BOOLEAN.getType());
2450 artifacts.put(artifactToUpdate.getArtifactName(), artifactToUpdate);
2452 properties.add(pdd);
2453 gd.setType(groupDefinitionType);
2454 gd.setProperties(properties);
2455 gd.setArtifacts(artifactsList);
2458 resource.setGroups(groups);
2459 resource.setDeploymentArtifacts(artifacts);
2465 public void testgetAllCertifiedResources() throws Exception {
2466 List<Resource> list = bl.getAllCertifiedResources(true, HighestFilterEnum.HIGHEST_ONLY, "USER");
2467 Assert.assertEquals(reslist,list);
2470 @Test(expected = StorageException.class)
2471 public void testgetAllCertifiedResources_exception() throws Exception {
2472 List<Resource> list = bl.getAllCertifiedResources(false, HighestFilterEnum.NON_HIGHEST_ONLY, "USER");
2473 Assert.assertEquals(reslist,list);
2477 public void testvalidateResourceNameExists() throws Exception {
2478 Either<Map<String, Boolean>, ResponseFormat> res = bl.validateResourceNameExists("Resource", ResourceTypeEnum.CR, "jh0003");
2479 Assert.assertEquals(true,res.isLeft());
2483 public void rollbackWithEitherAlwaysReturnARuntimeException() {
2484 JanusGraphDao janusGraphDao = mockJanusGraphDao;
2485 ActionStatus actionStatus = ActionStatus.INPUTS_NOT_FOUND;
2486 String params = "testName";
2488 Either<Object, RuntimeException> result =
2489 ResourceBusinessLogic.rollbackWithEither(janusGraphDao, actionStatus, params);
2491 assertTrue(result.isRight());
2492 assertTrue(result.right().value() instanceof ByActionStatusComponentException);
2496 public void rollbackWithEitherWorksWithNullJanusGraphDao() {
2497 JanusGraphDao janusGraphDao = null;
2498 ActionStatus actionStatus = ActionStatus.INPUTS_NOT_FOUND;
2499 String params = "testName";
2501 Either<Object, RuntimeException> result =
2502 ResourceBusinessLogic.rollbackWithEither(janusGraphDao, actionStatus, params);
2504 assertTrue(result.isRight());
2505 assertTrue(result.right().value() instanceof ByActionStatusComponentException);