Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / PropertyBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.be.components;
24
25 import fj.data.Either;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 import org.openecomp.sdc.be.components.impl.BaseBusinessLogicMock;
33 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
34 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
35 import org.openecomp.sdc.be.components.validation.UserValidations;
36 import org.openecomp.sdc.be.config.ConfigurationManager;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
41 import org.openecomp.sdc.be.model.Component;
42 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
43 import org.openecomp.sdc.be.model.InterfaceDefinition;
44 import org.openecomp.sdc.be.model.LifecycleStateEnum;
45 import org.openecomp.sdc.be.model.PropertyConstraint;
46 import org.openecomp.sdc.be.model.PropertyDefinition;
47 import org.openecomp.sdc.be.model.Resource;
48 import org.openecomp.sdc.be.model.Service;
49 import org.openecomp.sdc.be.model.User;
50 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ToscaOperationException;
52 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
53 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
54 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
55 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
56 import org.openecomp.sdc.be.user.Role;
57 import org.openecomp.sdc.be.user.UserBusinessLogic;
58 import org.openecomp.sdc.common.api.ConfigurationSource;
59 import org.openecomp.sdc.common.api.Constants;
60 import org.openecomp.sdc.common.impl.ExternalConfiguration;
61 import org.openecomp.sdc.common.impl.FSConfigurationSource;
62 import org.openecomp.sdc.exception.ResponseFormat;
63 import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils;
64 import org.springframework.web.context.WebApplicationContext;
65
66 import javax.servlet.ServletContext;
67 import java.lang.reflect.Field;
68 import java.util.ArrayList;
69 import java.util.Arrays;
70 import java.util.HashMap;
71 import java.util.List;
72 import java.util.Map;
73
74 import static org.hamcrest.Matchers.equalTo;
75 import static org.hamcrest.Matchers.hasItems;
76 import static org.hamcrest.Matchers.hasSize;
77 import static org.hamcrest.Matchers.instanceOf;
78 import static org.hamcrest.Matchers.is;
79 import static org.hamcrest.Matchers.notNullValue;
80 import static org.hamcrest.Matchers.nullValue;
81 import static org.junit.Assert.assertEquals;
82 import static org.junit.Assert.assertFalse;
83 import static org.junit.Assert.assertThat;
84 import static org.junit.Assert.assertTrue;
85 import static org.mockito.ArgumentMatchers.any;
86 import static org.mockito.ArgumentMatchers.anyObject;
87 import static org.mockito.ArgumentMatchers.anyString;
88 import static org.mockito.ArgumentMatchers.eq;
89 import static org.mockito.Mockito.when;
90
91 public class PropertyBusinessLogicTest extends BaseBusinessLogicMock {
92
93     @Mock
94     private ServletContext servletContext;
95     @Mock
96     private IPropertyOperation propertyOperation;
97     @Mock
98     private WebAppContextWrapper webAppContextWrapper;
99     @Mock
100     private UserBusinessLogic mockUserAdmin;
101     @Mock
102     private WebApplicationContext webAppContext;
103     @Mock
104     private ComponentsUtils componentsUtils;
105     @Mock
106     private ToscaOperationFacade toscaOperationFacade;
107     @Mock
108     private UserValidations userValidations;
109     @Mock
110     IGraphLockOperation graphLockOperation;
111     @Mock
112     JanusGraphDao janusGraphDao;
113
114     @InjectMocks
115     private PropertyBusinessLogic propertyBusinessLogic = new PropertyBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
116         groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
117     private User user = null;
118     private String resourceId = "resourceforproperty.0.1";
119     private String serviceId = "serviceForProperty.0.1";
120     private static final String interfaceType = "interfaceType";
121     private static final String operationType = "operationType";
122     private static final String operationId = "operationId";
123
124     @Before
125     public void setup() {
126         MockitoAnnotations.initMocks(this);
127         ExternalConfiguration.setAppName("catalog-be");
128
129         // init Configuration
130         String appConfigDir = "src/test/resources/config/catalog-be";
131         ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
132         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
133
134         // User data and management
135         user = new User();
136         user.setUserId("jh003");
137         user.setFirstName("Jimmi");
138         user.setLastName("Hendrix");
139         user.setRole(Role.ADMIN.name());
140
141         when(mockUserAdmin.getUser("jh003", false)).thenReturn(user);
142         when(userValidations.validateUserExists(eq("jh003"))).thenReturn(user);
143
144         // Servlet Context attributes
145         when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
146         when(servletContext.getAttribute(Constants.PROPERTY_OPERATION_MANAGER)).thenReturn(propertyOperation);
147         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
148 //        when(servletContext.getAttribute(Constants.RESOURCE_OPERATION_MANAGER)).thenReturn(resourceOperation);
149         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
150     }
151
152     @Test
153     public void getProperty_propertyNotFound() throws Exception {
154         Resource resource = new Resource();
155         PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
156         PropertyDefinition property2 = createPropertyObject("someProperty2", "myResource");
157         resource.setProperties(Arrays.asList(property1, property2));
158         String resourceId = "myResource";
159         resource.setUniqueId(resourceId);
160
161         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
162         Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> nonExistingProperty = propertyBusinessLogic
163             .getComponentProperty(resourceId, "NonExistingProperty", user.getUserId());
164         assertTrue(nonExistingProperty.isRight());
165         Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
166     }
167
168     @Test
169     public void getProperty_propertyNotBelongsToResource() throws Exception {
170         Resource resource = new Resource();
171         PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
172         resource.setProperties(Arrays.asList(property1));
173         String resourceId = "myResource";
174         resource.setUniqueId(resourceId);
175
176         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
177         Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> notFoundProperty = propertyBusinessLogic
178             .getComponentProperty(resourceId, "invalidId", user.getUserId());
179         assertTrue(notFoundProperty.isRight());
180         Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
181     }
182
183     @Test
184     public void getProperty() throws Exception {
185         Resource resource = new Resource();
186         resource.setUniqueId(resourceId);
187         PropertyDefinition property1 = createPropertyObject("someProperty", null);
188         resource.setProperties(Arrays.asList(property1));
189
190         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
191         Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> foundProperty = propertyBusinessLogic
192             .getComponentProperty(resourceId, property1.getUniqueId(), user.getUserId());
193         assertTrue(foundProperty.isLeft());
194         assertEquals(foundProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
195     }
196
197     @Test
198     public void testGetPropertyFromService() {
199         Service service = new Service();
200         service.setUniqueId(serviceId);
201
202         PropertyDefinition property1 = createPropertyObject("someProperty", null);
203         service.setProperties(Arrays.asList(property1));
204
205         Mockito.when(toscaOperationFacade.getToscaElement(serviceId)).thenReturn(Either.left(service));
206         Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> serviceProperty =
207             propertyBusinessLogic.getComponentProperty(serviceId, property1.getUniqueId(), user.getUserId());
208
209         assertTrue(serviceProperty.isLeft());
210         assertEquals(serviceProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
211     }
212
213     @Test
214     public void testPropertyNotFoundOnService() {
215         Service service = new Service();
216         service.setUniqueId(serviceId);
217
218         PropertyDefinition property1 = createPropertyObject("someProperty", null);
219         service.setProperties(Arrays.asList(property1));
220
221         Mockito.when(toscaOperationFacade.getToscaElement(serviceId)).thenReturn(Either.left(service));
222         Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> serviceProperty =
223             propertyBusinessLogic.getComponentProperty(serviceId, "notExistingPropId", user.getUserId());
224
225         assertTrue(serviceProperty.isRight());
226     }
227
228     @Test
229     public void isPropertyUsedByComponentInterface(){
230         Service service = new Service();
231         service.setUniqueId(serviceId);
232         service.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType));
233
234         PropertyDefinition propDef1 = new PropertyDefinition();
235         propDef1.setUniqueId("ComponentInput1_uniqueId");
236         assertTrue(propertyBusinessLogic.isPropertyUsedByOperation(service, propDef1));
237
238         PropertyDefinition propDef2 = new PropertyDefinition();
239         propDef1.setUniqueId("inputId2");
240         Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>()));
241         assertFalse(propertyBusinessLogic.isPropertyUsedByOperation(service, propDef2));
242     }
243
244     @Test
245     public void isPropertyUsedByComponentInstanceInterface(){
246         Map<String, InterfaceDefinition> newInterfaceDefinition = InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType);
247         ComponentInstanceInterface componentInstanceInterface = new ComponentInstanceInterface(interfaceType, newInterfaceDefinition.get(interfaceType));
248
249         Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
250         componentInstanceInterfaces.put("Test", Arrays.asList(componentInstanceInterface));
251
252         Service service = new Service();
253         service.setUniqueId(serviceId);
254         service.setComponentInstancesInterfaces(componentInstanceInterfaces);
255
256         PropertyDefinition propDef1 = new PropertyDefinition();
257         propDef1.setUniqueId("ComponentInput1_uniqueId");
258         assertTrue(propertyBusinessLogic.isPropertyUsedByOperation(service, propDef1));
259
260         PropertyDefinition propDef2 = new PropertyDefinition();
261         propDef1.setUniqueId("inputId2");
262         Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>()));
263         assertFalse(propertyBusinessLogic.isPropertyUsedByOperation(service, propDef2));
264     }
265
266     @Test
267     public void isPropertyUsedByComponentParentComponentInstanceInterface(){
268         Map<String, InterfaceDefinition> newInterfaceDefinition = InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType);
269         ComponentInstanceInterface componentInstanceInterface = new ComponentInstanceInterface(interfaceType, newInterfaceDefinition.get(interfaceType));
270
271         Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
272         componentInstanceInterfaces.put("Test", Arrays.asList(componentInstanceInterface));
273
274         Service parentService = new Service();
275         parentService.setComponentInstancesInterfaces(componentInstanceInterfaces);
276         Service childService = new Service();
277         childService.setUniqueId(serviceId);
278
279         PropertyDefinition propDef1 = new PropertyDefinition();
280         propDef1.setUniqueId("ComponentInput1_uniqueId");
281         Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(Arrays.asList(parentService)));
282         assertTrue(propertyBusinessLogic.isPropertyUsedByOperation(childService, propDef1));
283
284         PropertyDefinition propDef2 = new PropertyDefinition();
285         propDef1.setUniqueId("inputId2");
286         Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>()));
287         assertFalse(propertyBusinessLogic.isPropertyUsedByOperation(childService, propDef2));
288     }
289
290     private PropertyDefinition createPropertyObject(final String propertyName, final String resourceId) {
291         final PropertyDefinition pd = new PropertyDefinition();
292         List<PropertyConstraint> constraints = new ArrayList<>();
293         pd.setConstraints(null);
294         pd.setDefaultValue("100");
295         pd.setDescription("Size of thasdasdasdasde local disk, in Gigabytes (GB), available to applications running on the Compute node");
296         pd.setPassword(false);
297         pd.setRequired(true);
298         pd.setType("Integer");
299         pd.setOwnerId(resourceId);
300         pd.setName(propertyName);
301         pd.setUniqueId(resourceId + "." + propertyName);
302         return pd;
303     }
304
305     @Test
306     public void deleteProperty_CONNECTION_FAILURE() {
307         StorageOperationStatus lockResult = StorageOperationStatus.CONNECTION_FAILURE;
308         when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
309         when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(Either.left(new Resource()));
310         assertTrue(propertyBusinessLogic.deletePropertyFromComponent("resourceforproperty.0.1", "someProperty","i726").isRight());
311     }
312
313     @Test
314     public void deleteProperty_RESOURCE_NOT_FOUND() throws Exception {
315
316         Resource resource = new Resource();
317         PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
318
319         resource.setProperties(Arrays.asList(property1));
320         String resourceId = "myResource";
321         resource.setUniqueId(resourceId);
322
323         Field baseBusinessLogic3;
324         baseBusinessLogic3 = propertyBusinessLogic.getClass().getSuperclass().getDeclaredField("janusGraphDao");
325         baseBusinessLogic3.setAccessible(true);
326         baseBusinessLogic3.set(propertyBusinessLogic, janusGraphDao);
327
328         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
329
330         StorageOperationStatus lockResult = StorageOperationStatus.OK;
331         when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
332
333         Component resourcereturn= new Resource();
334         resourcereturn.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
335         resourcereturn.setIsDeleted(false);
336         resourcereturn.setLastUpdaterUserId("USR01");
337
338         Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
339         when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
340
341         assertTrue(propertyBusinessLogic.deletePropertyFromComponent("RES01", "someProperty","i726").isRight());
342     }
343
344     @Test
345     public void deleteProperty_RESTRICTED_OPERATION() throws Exception {
346         Resource resource = new Resource();
347         PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
348
349         resource.setProperties(Arrays.asList(property1));
350         String resourceId = "myResource";
351         resource.setUniqueId(resourceId);
352
353         Field baseBusinessLogic3;
354         baseBusinessLogic3 = propertyBusinessLogic.getClass().getSuperclass().getDeclaredField("janusGraphDao");
355         baseBusinessLogic3.setAccessible(true);
356         baseBusinessLogic3.set(propertyBusinessLogic, janusGraphDao);
357
358         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
359
360         StorageOperationStatus lockResult = StorageOperationStatus.OK;
361         when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
362
363         resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
364         resource.setIsDeleted(false);
365         resource.setLastUpdaterUserId("USR01");
366
367         Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
368         when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
369
370
371         assertTrue(propertyBusinessLogic.deletePropertyFromComponent("RES01", "someProperty","i726").isRight());
372     }
373
374     @Test
375     public void deleteProperty_RESTRICTED_() throws Exception {
376         final PropertyDefinition property1 = createPropertyObject("PROP", "RES01");
377         final Resource resource = new Resource();
378         final String resourceId = "myResource";
379         resource.setUniqueId(resourceId);
380         resource.setProperties(Arrays.asList(property1));
381
382         final Field baseBusinessLogic3 =
383             propertyBusinessLogic.getClass().getSuperclass().getDeclaredField("janusGraphDao");
384         baseBusinessLogic3.setAccessible(true);
385         baseBusinessLogic3.set(propertyBusinessLogic, janusGraphDao);
386
387         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
388
389         when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
390
391         resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
392         resource.setIsDeleted(false);
393         resource.setLastUpdaterUserId("USR01");
394
395         when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(Either.left(resource));
396         when(toscaOperationFacade.deletePropertyOfComponent(anyObject(), anyString())).thenReturn(StorageOperationStatus.OK);
397         when(toscaOperationFacade.getParentComponents(anyString())).thenReturn(Either.left(new ArrayList<>()));
398
399         assertTrue(propertyBusinessLogic.deletePropertyFromComponent("RES01", "PROP","USR01").isRight());
400     }
401
402     @Test
403     public void findComponentByIdTest() throws BusinessLogicException {
404         //give
405         final Resource resource = new Resource();
406         resource.setUniqueId(resourceId);
407         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
408         //when
409         final Component actualResource = propertyBusinessLogic.findComponentById(resourceId).orElse(null);
410         //then
411         assertThat("Actual resource should not be null", actualResource, is(notNullValue()));
412         assertThat("Actual resource must have the expected id",
413             actualResource.getUniqueId(), is(equalTo(resource.getUniqueId())));
414     }
415
416     @Test(expected = BusinessLogicException.class)
417     public void findComponentById_resourceNotFoundTest() throws BusinessLogicException {
418         //given
419         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.right(null));
420         Mockito.when(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, "")).thenReturn(new ResponseFormat());
421         //when
422         propertyBusinessLogic.findComponentById(resourceId);
423     }
424
425     @Test
426     public void updateComponentPropertyTest() throws BusinessLogicException {
427         //given
428         final Resource resource = new Resource();
429         resource.setUniqueId(resourceId);
430         final PropertyDefinition propertyDefinition = createPropertyObject("testProperty", resourceId);
431         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
432         when(toscaOperationFacade.updatePropertyOfComponent(resource, propertyDefinition)).thenReturn(Either.left(propertyDefinition));
433         //when
434         final PropertyDefinition actualPropertyDefinition = propertyBusinessLogic
435             .updateComponentProperty(resourceId, propertyDefinition);
436         //then
437         assertThat("Actual property definition should not be null", actualPropertyDefinition, is(notNullValue()));
438         assertThat("Actual property definition must have the expected id",
439             actualPropertyDefinition.getOwnerId(), is(equalTo(resource.getUniqueId())));
440         assertThat("Actual property definition must have the expected id",
441             actualPropertyDefinition.getName(), is(equalTo(propertyDefinition.getName())));
442     }
443
444     @Test(expected = BusinessLogicException.class)
445     public void updateComponentProperty_updateFailedTest() throws BusinessLogicException {
446         //given
447         final Resource resource = new Resource();
448         resource.setUniqueId(resourceId);
449         final PropertyDefinition propertyDefinition = createPropertyObject("testProperty", resourceId);
450         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
451         when(toscaOperationFacade.updatePropertyOfComponent(resource, propertyDefinition)).thenReturn(Either.right(null));
452         when(componentsUtils.getResponseFormatByResource(Mockito.any(), Mockito.anyString())).thenReturn(new ResponseFormat());
453         when(componentsUtils.convertFromStorageResponse(Mockito.any())).thenReturn(null);
454         //when
455         propertyBusinessLogic.updateComponentProperty(resourceId, propertyDefinition);
456     }
457
458     @Test
459     public void copyPropertyToComponentTest() throws ToscaOperationException {
460         //given
461         final Resource expectedResource = new Resource();
462         expectedResource.setUniqueId(resourceId);
463         final List<PropertyDefinition> propertiesToCopyList = new ArrayList<>();
464         final PropertyDefinition property1 = createPropertyObject("property1", resourceId);
465         propertiesToCopyList.add(property1);
466         final PropertyDefinition property2 = createPropertyObject("property2", resourceId);
467         propertiesToCopyList.add(property2);
468
469         final PropertyDefinition copiedProperty1 = new PropertyDefinition(property1);
470         copiedProperty1.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(resourceId, copiedProperty1.getName()));
471         expectedResource.addProperty(copiedProperty1);
472         final PropertyDefinition copiedProperty2 = new PropertyDefinition(property2);
473         copiedProperty2.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(resourceId, copiedProperty2.getName()));
474         expectedResource.addProperty(copiedProperty2);
475
476         Mockito.when(toscaOperationFacade
477             .addPropertyToComponent(eq(property1.getName()), Mockito.any(PropertyDefinition.class), eq(expectedResource)))
478             .thenReturn(Either.left(copiedProperty1));
479         Mockito.when(toscaOperationFacade
480             .addPropertyToComponent(eq(property2.getName()), Mockito.any(PropertyDefinition.class), eq(expectedResource)))
481             .thenReturn(Either.left(copiedProperty2));
482         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(expectedResource));
483         //when
484         final Component actualComponent = propertyBusinessLogic.copyPropertyToComponent(expectedResource, propertiesToCopyList, true);
485         //then
486         assertThat("Actual component should not be null", actualComponent, is(notNullValue()));
487         assertThat("Actual component should be an instance of Resource", actualComponent, is(instanceOf(Resource.class)));
488         assertThat("Actual component should have the expected id", actualComponent.getUniqueId(), is(equalTo(expectedResource.getUniqueId())));
489         assertThat("Actual component should have 2 properties", actualComponent.getProperties(), hasSize(2));
490         assertThat("Actual component should have the expected properties", actualComponent.getProperties(), hasItems(copiedProperty1, copiedProperty2));
491     }
492
493     @Test
494     public void copyPropertyToComponent1() throws ToscaOperationException {
495         //given
496         final Resource expectedResource = new Resource();
497         expectedResource.setUniqueId(resourceId);
498         //when
499         final Component actualComponent = propertyBusinessLogic.copyPropertyToComponent(expectedResource, null);
500         //then
501         assertThat("Actual component should not be null", actualComponent, is(notNullValue()));
502         assertThat("Actual component should be an instance of Resource", actualComponent, is(instanceOf(Resource.class)));
503         assertThat("Actual component should have the expected id", actualComponent.getUniqueId(), is(equalTo(expectedResource.getUniqueId())));
504         assertThat("Actual component should have no properties", actualComponent.getProperties(), is(nullValue()));
505     }
506
507     @Test(expected = ToscaOperationException.class)
508     public void copyPropertyToComponent_copyFailed() throws ToscaOperationException {
509         //given
510         final Resource expectedResource = new Resource();
511         expectedResource.setUniqueId(resourceId);
512         final List<PropertyDefinition> propertiesToCopyList = new ArrayList<>();
513         final PropertyDefinition property1 = createPropertyObject("property1", resourceId);
514         propertiesToCopyList.add(property1);
515         Mockito.when(toscaOperationFacade
516             .addPropertyToComponent(eq(property1.getName()), Mockito.any(PropertyDefinition.class), eq(expectedResource)))
517             .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
518         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(expectedResource));
519         //when
520         propertyBusinessLogic.copyPropertyToComponent(expectedResource, propertiesToCopyList, true);
521     }
522 }