re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / instance / ComponentInstanceChangeOperationOrchestratorTest.java
1 package org.openecomp.sdc.be.components.impl.instance;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.junit.runner.RunWith;
6 import org.mockito.Mock;
7 import org.mockito.junit.MockitoJUnitRunner;
8 import org.openecomp.sdc.be.dao.api.ActionStatus;
9 import org.openecomp.sdc.be.model.ComponentInstance;
10 import org.openecomp.sdc.be.model.Resource;
11
12 import static java.util.Arrays.asList;
13 import static org.assertj.core.api.Assertions.assertThat;
14 import static org.mockito.Mockito.verifyZeroInteractions;
15 import static org.mockito.Mockito.when;
16
17 @RunWith(MockitoJUnitRunner.class)
18 public class ComponentInstanceChangeOperationOrchestratorTest {
19
20     private static final Resource CONTAINER = new Resource();
21     private static final ComponentInstance NEW_VERSION = new ComponentInstance();
22     private static final ComponentInstance PREV_VERSION = new ComponentInstance();
23     private static final String DELETED_INS_ID = "id";
24     private ComponentInstanceChangeOperationOrchestrator testInstance;
25     @Mock
26     private OnComponentInstanceChangeOperation componentInstanceChangeOperation1;
27     @Mock
28     private OnComponentInstanceChangeOperation componentInstanceChangeOperation2;
29     @Mock
30     private OnComponentInstanceChangeOperation componentInstanceChangeOperation3;
31
32     @Before
33     public void setUp() throws Exception {
34         testInstance = new ComponentInstanceChangeOperationOrchestrator(asList(componentInstanceChangeOperation1, componentInstanceChangeOperation2, componentInstanceChangeOperation3));
35     }
36
37     @Test
38     public void doPostChangeVersionOperations_whenFirstPostOperationFails_doNotRunFollowingOperations() {
39         when(componentInstanceChangeOperation1.onChangeVersion(CONTAINER, PREV_VERSION, NEW_VERSION)).thenReturn(ActionStatus.GENERAL_ERROR);
40         ActionStatus actionStatus = testInstance.doPostChangeVersionOperations(CONTAINER, PREV_VERSION, NEW_VERSION);
41         assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
42         verifyZeroInteractions(componentInstanceChangeOperation2, componentInstanceChangeOperation3);
43     }
44
45     @Test
46     public void doPostChangeVersionOperations_whenAnyPostOperationFails_doNotRunFollowingOperations() {
47         when(componentInstanceChangeOperation1.onChangeVersion(CONTAINER, PREV_VERSION, NEW_VERSION)).thenReturn(ActionStatus.OK);
48         when(componentInstanceChangeOperation2.onChangeVersion(CONTAINER, PREV_VERSION, NEW_VERSION)).thenReturn(ActionStatus.GENERAL_ERROR);
49         ActionStatus actionStatus = testInstance.doPostChangeVersionOperations(CONTAINER, PREV_VERSION, NEW_VERSION);
50         assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
51         verifyZeroInteractions(componentInstanceChangeOperation3);
52     }
53
54     @Test
55     public void doPostChangeVersionOperations_whenLastPostOperationFails_returnTheFailureResult() {
56         when(componentInstanceChangeOperation1.onChangeVersion(CONTAINER, PREV_VERSION, NEW_VERSION)).thenReturn(ActionStatus.OK);
57         when(componentInstanceChangeOperation2.onChangeVersion(CONTAINER, PREV_VERSION, NEW_VERSION)).thenReturn(ActionStatus.OK);
58         when(componentInstanceChangeOperation3.onChangeVersion(CONTAINER, PREV_VERSION, NEW_VERSION)).thenReturn(ActionStatus.GENERAL_ERROR);
59         ActionStatus actionStatus = testInstance.doPostChangeVersionOperations(CONTAINER, PREV_VERSION, NEW_VERSION);
60         assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
61     }
62
63     @Test
64     public void doPostChangeVersionOperations_whenAllOperationsSucceeds_returnOk() {
65         when(componentInstanceChangeOperation1.onChangeVersion(CONTAINER, PREV_VERSION, NEW_VERSION)).thenReturn(ActionStatus.OK);
66         when(componentInstanceChangeOperation2.onChangeVersion(CONTAINER, PREV_VERSION, NEW_VERSION)).thenReturn(ActionStatus.OK);
67         when(componentInstanceChangeOperation3.onChangeVersion(CONTAINER, PREV_VERSION, NEW_VERSION)).thenReturn(ActionStatus.OK);
68         ActionStatus actionStatus = testInstance.doPostChangeVersionOperations(CONTAINER, PREV_VERSION, NEW_VERSION);
69         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
70     }
71
72     @Test
73     public void doOnDeleteInstanceOperations_whenFirstPostOperationFails_doNotRunFollowingOperations() {
74         when(componentInstanceChangeOperation1.onDelete(CONTAINER, DELETED_INS_ID)).thenReturn(ActionStatus.GENERAL_ERROR);
75         ActionStatus actionStatus = testInstance.doOnDeleteInstanceOperations(CONTAINER, DELETED_INS_ID);
76         assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
77         verifyZeroInteractions(componentInstanceChangeOperation2, componentInstanceChangeOperation3);
78     }
79
80     @Test
81     public void doOnDeleteInstanceOperations_whenAnyPostOperationFails_doNotRunFollowingOperations() {
82         when(componentInstanceChangeOperation1.onDelete(CONTAINER, DELETED_INS_ID)).thenReturn(ActionStatus.OK);
83         when(componentInstanceChangeOperation2.onDelete(CONTAINER, DELETED_INS_ID)).thenReturn(ActionStatus.GENERAL_ERROR);
84         ActionStatus actionStatus = testInstance.doOnDeleteInstanceOperations(CONTAINER, DELETED_INS_ID);
85         assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
86         verifyZeroInteractions(componentInstanceChangeOperation3);
87     }
88
89     @Test
90     public void doOnDeleteInstanceOperations_whenLastPostOperationFails_returnTheFailureResult() {
91         when(componentInstanceChangeOperation1.onDelete(CONTAINER, DELETED_INS_ID)).thenReturn(ActionStatus.OK);
92         when(componentInstanceChangeOperation2.onDelete(CONTAINER, DELETED_INS_ID)).thenReturn(ActionStatus.OK);
93         when(componentInstanceChangeOperation3.onDelete(CONTAINER, DELETED_INS_ID)).thenReturn(ActionStatus.GENERAL_ERROR);
94         ActionStatus actionStatus = testInstance.doOnDeleteInstanceOperations(CONTAINER, DELETED_INS_ID);
95         assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
96     }
97
98     @Test
99     public void doOnDeleteInstanceOperations_whenAllOperationsSucceeds_returnOk() {
100         when(componentInstanceChangeOperation1.onDelete(CONTAINER, DELETED_INS_ID)).thenReturn(ActionStatus.OK);
101         when(componentInstanceChangeOperation2.onDelete(CONTAINER, DELETED_INS_ID)).thenReturn(ActionStatus.OK);
102         when(componentInstanceChangeOperation3.onDelete(CONTAINER, DELETED_INS_ID)).thenReturn(ActionStatus.OK);
103         ActionStatus actionStatus = testInstance.doOnDeleteInstanceOperations(CONTAINER, DELETED_INS_ID);
104         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
105     }
106 }