3eb3bd8796ad6ed0cebfe25009d4d76cded738c6
[so.git] / bpmn / so-bpmn-infrastructure-common / 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;
31 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.AAI_CONTAINS_INFO_ABOUT_PNF;
32 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.CORRELATION_ID;
33
34 import org.camunda.bpm.engine.delegate.BpmnError;
35 import org.camunda.bpm.engine.delegate.DelegateExecution;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.experimental.runners.Enclosed;
39 import org.junit.runner.RunWith;
40 import org.onap.so.bpmn.core.WorkflowException;
41
42 @RunWith(Enclosed.class)
43 public class CheckAaiForCorrelationIdDelegateTest {
44
45     public static class ConnectionOkTests {
46
47         private CheckAaiForCorrelationIdDelegate delegate;
48
49         @Before
50         public void setUp() {
51             delegate = new CheckAaiForCorrelationIdDelegate();
52             delegate.setAaiConnection(new AaiConnectionTestImpl());
53         }
54
55         @Test
56         public void shouldThrowExceptionWhenCorrelationIdIsNotSet() {
57             // given
58             DelegateExecution execution = mock(DelegateExecution.class);
59             when(execution.getVariable(CORRELATION_ID)).thenReturn(null);
60             when(execution.getVariable("testProcessKey")).thenReturn("testProcessKeyValue");
61             // when, then
62             assertThatThrownBy(() -> delegate.execute(execution)).isInstanceOf(BpmnError.class);
63             verify(execution).setVariable(eq("WorkflowException"), any(WorkflowException.class));
64         }
65
66         @Test
67         public void shouldSetCorrectVariablesWhenAaiDoesNotContainInfoAboutPnf() throws Exception {
68             // given
69             DelegateExecution execution = mock(DelegateExecution.class);
70             when(execution.getVariable(CORRELATION_ID)).thenReturn(ID_WITHOUT_ENTRY);
71             // when
72             delegate.execute(execution);
73             // then
74             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, false);
75         }
76
77         @Test
78         public void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnfWithoutIp() throws Exception {
79             // given
80             DelegateExecution execution = mock(DelegateExecution.class);
81             when(execution.getVariable(CORRELATION_ID)).thenReturn(ID_WITH_ENTRY);
82             // when
83             delegate.execute(execution);
84             // then
85             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, true);
86         }
87     }
88
89     public static class NoConnectionTests {
90
91         private CheckAaiForCorrelationIdDelegate delegate;
92
93         @Before
94         public void setUp() {
95             delegate = new CheckAaiForCorrelationIdDelegate();
96             delegate.setAaiConnection(new AaiConnectionThrowingException());
97         }
98
99         @Test
100         public void shouldThrowExceptionWhenIoExceptionOnConnectionToAai() {
101             // given
102             DelegateExecution execution = mock(DelegateExecution.class);
103             when(execution.getVariable(CORRELATION_ID)).thenReturn(ID_WITH_ENTRY);
104             when(execution.getVariable("testProcessKey")).thenReturn("testProcessKey");
105             // when, then
106             assertThatThrownBy(() -> delegate.execute(execution)).isInstanceOf(BpmnError.class);
107             verify(execution).setVariable(eq("WorkflowException"), any(WorkflowException.class));
108         }
109     }
110 }