Containerization feature of SO
[so.git] / bpmn / mso-infrastructure-bpmn / src / test / java / org / onap / so / bpmn / infrastructure / pnf / delegate / CheckAaiForCorrelationIdDelegateTest.java
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.Test;
40 import org.junit.experimental.runners.Enclosed;
41 import org.junit.runner.RunWith;
42 import org.onap.so.bpmn.core.WorkflowException;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.test.context.junit4.SpringRunner;
46
47 @RunWith(Enclosed.class)
48 public class CheckAaiForCorrelationIdDelegateTest {
49
50     @RunWith(SpringRunner.class)
51     @SpringBootTest(classes = {CheckAaiForCorrelationIdDelegate.class, AaiConnectionTestImpl.class})
52     public static class ConnectionOkTests {
53
54         @Autowired
55         private CheckAaiForCorrelationIdDelegate delegate;
56
57         @Test
58         public void shouldThrowExceptionWhenCorrelationIdIsNotSet() throws Exception {
59             // given
60             DelegateExecution execution = mock(DelegateExecution.class);
61             when(execution.getVariable(CORRELATION_ID)).thenReturn(null);
62             when(execution.getVariable("testProcessKey")).thenReturn("testProcessKeyValue");
63             // when, then
64             assertThatThrownBy(() -> delegate.execute(execution)).isInstanceOf(BpmnError.class);
65             verify(execution).setVariable(eq("WorkflowException"), any(WorkflowException.class));
66         }
67
68         @Test
69         public void shouldSetCorrectVariablesWhenAaiDoesNotContainInfoAboutPnf() throws Exception {
70             // given
71             DelegateExecution execution = mock(DelegateExecution.class);
72             when(execution.getVariable(CORRELATION_ID)).thenReturn(ID_WITHOUT_ENTRY);
73             // when
74             delegate.execute(execution);
75             // then
76             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, false);
77         }
78
79         @Test
80         public void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnf() throws Exception {
81             shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnf(ID_WITH_ENTRY_AND_IP);
82         }
83
84         @Test
85         public void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnfWithIpV6() throws Exception {
86             shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnf(ID_WITH_IP_V6);
87         }
88
89         private void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnf(String id) throws Exception {
90             // given
91             DelegateExecution execution = mock(DelegateExecution.class);
92             when(execution.getVariable(CORRELATION_ID)).thenReturn(id);
93             // when
94             delegate.execute(execution);
95             // then
96             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, true);
97             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_IP, true);
98         }
99
100         @Test
101         public void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnfWithoutIp() throws Exception {
102             // given
103             DelegateExecution execution = mock(DelegateExecution.class);
104             when(execution.getVariable(CORRELATION_ID)).thenReturn(ID_WITH_ENTRY_NO_IP);
105             // when
106             delegate.execute(execution);
107             // then
108             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, true);
109             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_IP, false);
110         }
111     }
112
113
114     @RunWith(SpringRunner.class)
115     @SpringBootTest(classes = {CheckAaiForCorrelationIdDelegate.class, AaiConnectionThrowingException.class})
116     public static class NoConnectionTests {
117
118         @Autowired
119         private CheckAaiForCorrelationIdDelegate delegate;
120
121         @Test
122         public void shouldThrowExceptionWhenIoExceptionOnConnectionToAai() throws Exception {
123             // given
124             DelegateExecution execution = mock(DelegateExecution.class);
125             when(execution.getVariable(CORRELATION_ID)).thenReturn(ID_WITH_ENTRY_NO_IP);
126             when(execution.getVariable("testProcessKey")).thenReturn("testProcessKey");
127             // when, then
128             assertThatThrownBy(() -> delegate.execute(execution)).isInstanceOf(BpmnError.class);
129             verify(execution).setVariable(eq("WorkflowException"), any(WorkflowException.class));
130         }
131     }
132 }