3fa9fbf3b5b781c773e820ce1dffa3c8f28f3612
[so.git] /
1 package org.onap.so.bpmn.infrastructure.pnf.tasks;
2
3 import org.camunda.bpm.engine.delegate.BpmnError;
4 import org.junit.Before;
5 import org.junit.Rule;
6 import org.junit.Test;
7 import org.junit.experimental.runners.Enclosed;
8 import org.junit.rules.ExpectedException;
9 import org.junit.runner.RunWith;
10 import org.mockito.InjectMocks;
11 import org.mockito.Mock;
12 import org.mockito.junit.MockitoJUnitRunner;
13 import org.onap.so.bpmn.common.BuildingBlockExecution;
14 import org.onap.so.bpmn.infrastructure.pnf.delegate.PnfManagementTestImpl;
15 import org.onap.so.bpmn.infrastructure.pnf.delegate.PnfManagementThrowingException;
16 import org.onap.so.bpmn.infrastructure.pnf.management.PnfManagement;
17 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
18 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
19 import org.onap.so.client.exception.ExceptionBuilder;
20 import java.io.IOException;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.anyInt;
24 import static org.mockito.Mockito.anyString;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.AAI_CONTAINS_INFO_ABOUT_PNF;
30 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
31 import static org.onap.so.bpmn.infrastructure.pnf.delegate.PnfManagementTestImpl.ID_WITHOUT_ENTRY;
32 import static org.onap.so.bpmn.infrastructure.pnf.delegate.PnfManagementTestImpl.ID_WITH_ENTRY;
33 import static org.onap.so.bpmn.infrastructure.pnf.tasks.PnfTasksUtils.PNF_UUID;
34 import static org.onap.so.bpmn.infrastructure.pnf.tasks.PnfTasksUtils.preparePnf;
35
36 @RunWith(Enclosed.class)
37 public class CheckAaiForPnfCorrelationIdTest {
38
39     @RunWith(MockitoJUnitRunner.class)
40     public static class ConnectionOkTests {
41
42         @Mock
43         private ExtractPojosForBB extractPojosForBB;
44         @Mock
45         private ExceptionBuilder exceptionUtil;
46         @Rule
47         public ExpectedException expectedException = ExpectedException.none();
48
49         @InjectMocks
50         private CheckAaiForPnfCorrelationId task = new CheckAaiForPnfCorrelationId();
51         private PnfManagement pnfManagementTest = new PnfManagementTestImpl();
52
53         @Before
54         public void setUp() {
55             task.setPnfManagement(pnfManagementTest);
56         }
57
58         @Test
59         public void shouldThrowExceptionWhenPnfCorrelationIdIsNotSet() throws Exception {
60             // given
61             when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.PNF))).thenReturn(preparePnf(null, PNF_UUID));
62             BuildingBlockExecution execution = mock(BuildingBlockExecution.class);
63             doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(eq(execution),
64                     anyInt(), anyString());
65             // when, then
66             expectedException.expect(BpmnError.class);
67             task.execute(execution);
68             verify(exceptionUtil).buildAndThrowWorkflowException(eq(execution), anyInt(), anyString());
69         }
70
71         @Test
72         public void shouldSetCorrectVariablesWhenAaiDoesNotContainInfoAboutPnf() throws Exception {
73             // given
74             when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.PNF)))
75                     .thenReturn(preparePnf(ID_WITHOUT_ENTRY, PNF_UUID));
76             BuildingBlockExecution execution = mock(BuildingBlockExecution.class);
77             // when
78             task.execute(execution);
79             // then
80             verify(execution).setVariable(AAI_CONTAINS_INFO_ABOUT_PNF, false);
81         }
82
83         @Test
84         public void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnfWithoutIp() throws Exception {
85             // given
86             when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.PNF)))
87                     .thenReturn(preparePnf(ID_WITH_ENTRY, PNF_UUID));
88             BuildingBlockExecution execution = mock(BuildingBlockExecution.class);
89             // when
90             task.execute(execution);
91             // then
92             verify(execution).setVariable(AAI_CONTAINS_INFO_ABOUT_PNF, true);
93         }
94     }
95
96     @RunWith(MockitoJUnitRunner.class)
97     public static class NoConnectionTests {
98
99         @Mock
100         private ExtractPojosForBB extractPojosForBB;
101         @Mock
102         private ExceptionBuilder exceptionUtil;
103         @Rule
104         public ExpectedException expectedException = ExpectedException.none();
105
106         @InjectMocks
107         private CheckAaiForPnfCorrelationId task = new CheckAaiForPnfCorrelationId();
108         private PnfManagement pnfManagementTest = new PnfManagementThrowingException();
109
110         @Before
111         public void setUp() throws Exception {
112             task.setPnfManagement(pnfManagementTest);
113             when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.PNF)))
114                     .thenReturn(preparePnf(PNF_CORRELATION_ID, PNF_UUID));
115         }
116
117         @Test
118         public void shouldThrowExceptionWhenIoExceptionOnConnectionToAai() {
119             // given
120             BuildingBlockExecution execution = mock(BuildingBlockExecution.class);
121             doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(eq(execution),
122                     anyInt(), any(IOException.class));
123             // when, then
124             expectedException.expect(BpmnError.class);
125             task.execute(execution);
126             verify(exceptionUtil).buildAndThrowWorkflowException(eq(execution), anyInt(), any(IOException.class));
127         }
128     }
129 }