Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / resource / ResourceDataMergeBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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  */
20
21 package org.openecomp.sdc.be.components.merge.resource;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.openecomp.sdc.be.components.merge.ComponentsMergeCommand;
31 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.model.Resource;
34
35 import static java.util.Arrays.asList;
36 import static org.junit.Assert.assertEquals;
37 import static org.mockito.Mockito.*;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class ResourceDataMergeBusinessLogicTest {
41
42     @InjectMocks
43     private ResourceDataMergeBusinessLogic testInstance;
44
45     @Mock
46     private MergeCommandsFactory mergeCommandsFactory;
47
48     @Mock
49     private ComponentsMergeCommand commandA;
50
51     @Mock
52     private ComponentsMergeCommand commandB;
53
54     @Mock
55     private ComponentsMergeCommand commandC;
56
57     private Resource oldResource, newResource;
58
59     @Before
60     public void setUp() throws Exception {
61         oldResource = ObjectGenerator.buildBasicResource();
62         newResource = ObjectGenerator.buildBasicResource();
63         when(mergeCommandsFactory.getMergeCommands(oldResource, newResource)).thenReturn(Either.left(asList(commandA, commandB, commandC)));
64     }
65
66     @Test
67     public void whenCommandsFactoryFails_propagateTheFailure() {
68         when(mergeCommandsFactory.getMergeCommands(oldResource, newResource)).thenReturn(Either.right(ActionStatus.GENERAL_ERROR));
69         ActionStatus actionStatus = testInstance.mergeResourceEntities(oldResource, newResource);
70         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
71         verifyZeroInteractions(commandA, commandB, commandC);
72     }
73
74     @Test
75     public void mergeResources_allMergeClassesAreCalled() {
76         when(commandA.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.OK);
77         when(commandB.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.OK);
78         when(commandC.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.OK);
79         ActionStatus actionStatus = testInstance.mergeResourceEntities(oldResource, newResource);
80         assertEquals(ActionStatus.OK, actionStatus);
81     }
82
83     @Test
84     public void mergeResources_mergeCommandFailed_dontCallOtherMergeMethods() {
85         when(commandA.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.GENERAL_ERROR);
86         ActionStatus actionStatus = testInstance.mergeResourceEntities(oldResource, newResource);
87         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
88         verify(commandA).description();
89         verifyZeroInteractions(commandB, commandC);
90     }
91
92 }