CorrelationId cleanup
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / bpmn / infrastructure / pnf / delegate / CheckAaiForPnfCorrelationIdDelegateTest.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.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.onap.so.bpmn.infrastructure.pnf.delegate.PnfManagementTestImpl.ID_WITHOUT_ENTRY;
29 import static org.onap.so.bpmn.infrastructure.pnf.delegate.PnfManagementTestImpl.ID_WITH_ENTRY;
30 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.AAI_CONTAINS_INFO_ABOUT_PNF;
31 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
32
33 import org.camunda.bpm.engine.delegate.BpmnError;
34 import org.camunda.bpm.engine.delegate.DelegateExecution;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.experimental.runners.Enclosed;
39 import org.junit.rules.ExpectedException;
40 import org.junit.runner.RunWith;
41 import org.onap.so.bpmn.core.WorkflowException;
42
43 @RunWith(Enclosed.class)
44 public class CheckAaiForPnfCorrelationIdDelegateTest {
45
46     public static class ConnectionOkTests {
47
48         private CheckAaiForPnfCorrelationIdDelegate delegate;
49         
50         @Rule
51         public ExpectedException expectedException = ExpectedException.none();
52
53         @Before
54         public void setUp() {
55             delegate = new CheckAaiForPnfCorrelationIdDelegate();
56             delegate.setPnfManagement(new PnfManagementTestImpl());
57         }
58
59         @Test
60         public void shouldThrowExceptionWhenPnfCorrelationIdIsNotSet() throws Exception {
61             // given
62             DelegateExecution execution = mock(DelegateExecution.class);
63             when(execution.getVariable(PNF_CORRELATION_ID)).thenReturn(null);
64             when(execution.getVariable("testProcessKey")).thenReturn("testProcessKeyValue");
65             // when, then
66             expectedException.expect(BpmnError.class);
67             delegate.execute(execution);
68             verify(execution).setVariable(eq("WorkflowException"), any(WorkflowException.class));
69         }
70
71         @Test
72         public void shouldSetCorrectVariablesWhenAaiDoesNotContainInfoAboutPnf() throws Exception {
73             // given
74             DelegateExecution execution = mock(DelegateExecution.class);
75             when(execution.getVariable(PNF_CORRELATION_ID)).thenReturn(ID_WITHOUT_ENTRY);
76             // when
77             delegate.execute(execution);
78             // then
79             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, false);
80         }
81
82         @Test
83         public void shouldSetCorrectVariablesWhenAaiContainsInfoAboutPnfWithoutIp() throws Exception {
84             // given
85             DelegateExecution execution = mock(DelegateExecution.class);
86             when(execution.getVariable(PNF_CORRELATION_ID)).thenReturn(ID_WITH_ENTRY);
87             // when
88             delegate.execute(execution);
89             // then
90             verify(execution).setVariableLocal(AAI_CONTAINS_INFO_ABOUT_PNF, true);
91         }
92     }
93
94     public static class NoConnectionTests {
95
96         private CheckAaiForPnfCorrelationIdDelegate delegate;
97         
98         @Rule
99         public ExpectedException expectedException = ExpectedException.none();
100
101         @Before
102         public void setUp() {
103             delegate = new CheckAaiForPnfCorrelationIdDelegate();
104             delegate.setPnfManagement(new PnfManagementThrowingException());
105         }
106
107         @Test
108         public void shouldThrowExceptionWhenIoExceptionOnConnectionToAai() throws Exception {
109             // given
110             DelegateExecution execution = mock(DelegateExecution.class);
111             when(execution.getVariable(PNF_CORRELATION_ID)).thenReturn(ID_WITH_ENTRY);
112             when(execution.getVariable("testProcessKey")).thenReturn("testProcessKey");
113             // when, then
114             expectedException.expect(BpmnError.class);
115             delegate.execute(execution);
116             verify(execution).setVariable(eq("WorkflowException"), any(WorkflowException.class));
117         }
118     }
119 }