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