Catalog alignment
[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.verify;
38 import static org.mockito.Mockito.verifyZeroInteractions;
39 import static org.mockito.Mockito.when;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class ResourceDataMergeBusinessLogicTest {
43
44     @InjectMocks
45     private ResourceDataMergeBusinessLogic testInstance;
46
47     @Mock
48     private MergeCommandsFactory mergeCommandsFactory;
49
50     @Mock
51     private ComponentsMergeCommand commandA;
52
53     @Mock
54     private ComponentsMergeCommand commandB;
55
56     @Mock
57     private ComponentsMergeCommand commandC;
58
59     private Resource oldResource, newResource;
60
61     @Before
62     public void setUp() throws Exception {
63         oldResource = ObjectGenerator.buildBasicResource();
64         newResource = ObjectGenerator.buildBasicResource();
65         when(mergeCommandsFactory.getMergeCommands(oldResource, newResource)).thenReturn(Either.left(asList(commandA, commandB, commandC)));
66     }
67
68     @Test
69     public void whenCommandsFactoryFails_propagateTheFailure() {
70         when(mergeCommandsFactory.getMergeCommands(oldResource, newResource)).thenReturn(Either.right(ActionStatus.GENERAL_ERROR));
71         ActionStatus actionStatus = testInstance.mergeResourceEntities(oldResource, newResource);
72         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
73         verifyZeroInteractions(commandA, commandB, commandC);
74     }
75
76     @Test
77     public void mergeResources_allMergeClassesAreCalled() {
78         when(commandA.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.OK);
79         when(commandB.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.OK);
80         when(commandC.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.OK);
81         ActionStatus actionStatus = testInstance.mergeResourceEntities(oldResource, newResource);
82         assertEquals(ActionStatus.OK, actionStatus);
83     }
84
85     @Test
86     public void mergeResources_mergeCommandFailed_dontCallOtherMergeMethods() {
87         when(commandA.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.GENERAL_ERROR);
88         ActionStatus actionStatus = testInstance.mergeResourceEntities(oldResource, newResource);
89         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
90         verify(commandA).description();
91         verifyZeroInteractions(commandB, commandC);
92     }
93
94 }