627e57bfb84c312177b4e8fb61acdf9b7f1e5cdc
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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  */
20
21 package org.onap.so.bpmn.infrastructure.pnf.delegate;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
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.AaiConnectionTestImpl.ID_WITHOUT_ENTRY;
30 import static org.onap.so.bpmn.infrastructure.pnf.delegate.AaiConnectionTestImpl.ID_WITH_ENTRY_AND_IP;
31 import static org.onap.so.bpmn.infrastructure.pnf.delegate.AaiConnectionTestImpl.ID_WITH_ENTRY_NO_IP;
32 import static org.onap.so.bpmn.infrastructure.pnf.delegate.AaiConnectionTestImpl.ID_WITH_IP_V6;
33 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.AAI_CONTAINS_INFO_ABOUT_IP;
34 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.AAI_CONTAINS_INFO_ABOUT_PNF;
35 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.CORRELATION_ID;
36
37 import org.camunda.bpm.engine.delegate.BpmnError;
38 import org.camunda.bpm.engine.delegate.DelegateExecution;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.experimental.runners.Enclosed;
42 import org.junit.runner.RunWith;
43 import org.onap.so.bpmn.core.WorkflowException;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.test.context.junit4.SpringRunner;
47
48 @RunWith(Enclosed.class)
49 public class CheckAaiForCorrelationIdDelegateTest {
50
51     public static class ConnectionOkTests {
52
53         private CheckAaiForCorrelationIdDelegate delegate;
54
55         @Before
56         public void setUp() {
57             delegate = new CheckAaiForCorrelationIdDelegate();
58             delegate.setAaiConnection(new AaiConnectionTestImpl());
59         }
60
61         @Test
62         public void shouldThrowExceptionWhenCorrelationIdIsNotSet() {
63             // given
64             DelegateExecution execution = mock(DelegateExecution.class);
65             when(execution.getVariable(CORRELATION_ID)).thenReturn(null);
66             when(execution.getVariable("testProcessKey")).thenReturn("testProcessKeyValue");
67             // when, then
68             assertThatThrownBy(() -> delegate.execute(execution)).isInstanceOf(BpmnError.class);
69             verify(execution).setVariable(eq("WorkflowException"), any(WorkflowException.class));
70         }
71
72         @Test
73         public void shouldSetCorrectVariablesWhenAaiDoesNotContainInfoAboutPnf() throws Exception {
74             // given
75             DelegateExecution execution = mock(DelegateExecution.class);
76             when(execution.getVariable(CORRELATION_ID)).thenReturn(ID_WITHOUT_ENTRY);
77             // when
78             delegate.execute(execution);
79             // then
80             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, false);
81         }
82
83         @Test
84         public void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnf() throws Exception {
85             shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnf(ID_WITH_ENTRY_AND_IP);
86         }
87
88         @Test
89         public void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnfWithIpV6() throws Exception {
90             shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnf(ID_WITH_IP_V6);
91         }
92
93         private void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnf(String id) throws Exception {
94             // given
95             DelegateExecution execution = mock(DelegateExecution.class);
96             when(execution.getVariable(CORRELATION_ID)).thenReturn(id);
97             // when
98             delegate.execute(execution);
99             // then
100             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, true);
101             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_IP, true);
102         }
103
104         @Test
105         public void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnfWithoutIp() throws Exception {
106             // given
107             DelegateExecution execution = mock(DelegateExecution.class);
108             when(execution.getVariable(CORRELATION_ID)).thenReturn(ID_WITH_ENTRY_NO_IP);
109             // when
110             delegate.execute(execution);
111             // then
112             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, true);
113             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_IP, false);
114         }
115     }
116
117     public static class NoConnectionTests {
118
119         private CheckAaiForCorrelationIdDelegate delegate;
120
121         @Before
122         public void setUp() {
123             delegate = new CheckAaiForCorrelationIdDelegate();
124             delegate.setAaiConnection(new AaiConnectionThrowingException());
125         }
126
127         @Test
128         public void shouldThrowExceptionWhenIoExceptionOnConnectionToAai() {
129             // given
130             DelegateExecution execution = mock(DelegateExecution.class);
131             when(execution.getVariable(CORRELATION_ID)).thenReturn(ID_WITH_ENTRY_NO_IP);
132             when(execution.getVariable("testProcessKey")).thenReturn("testProcessKey");
133             // when, then
134             assertThatThrownBy(() -> delegate.execute(execution)).isInstanceOf(BpmnError.class);
135             verify(execution).setVariable(eq("WorkflowException"), any(WorkflowException.class));
136         }
137     }
138 }