Springboot 2.0 upgrade
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / bpmn / infrastructure / pnf / delegate / PnfCheckInputsTest.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.eq;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.CORRELATION_ID;
28 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.TIMEOUT_FOR_NOTIFICATION;
29
30 import org.camunda.bpm.engine.delegate.BpmnError;
31 import org.camunda.bpm.engine.delegate.DelegateExecution;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35
36 public class PnfCheckInputsTest {
37
38     private static final String DEFAULT_TIMEOUT = "P1D";
39
40     @Rule
41         public ExpectedException expectedException = ExpectedException.none();
42     
43     private DelegateExecution mockDelegateExecution() {
44         new PnfCheckInputs(DEFAULT_TIMEOUT);
45         DelegateExecution delegateExecution = mock(DelegateExecution.class);
46         when(delegateExecution.getVariable("testProcessKey")).thenReturn("testProcessKeyValue");
47         return delegateExecution;
48     }
49
50     @Test
51     public void shouldThrowException_whenPnfIdNotSet() {
52         // given
53         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
54         DelegateExecution delegateExecution = mockDelegateExecution();
55         // when, then
56         expectedException.expect(BpmnError.class);
57         testedObject.execute(delegateExecution);
58     }
59
60     @Test
61     public void shouldThrowException_whenPnfIdIsEmptyString() throws Exception {
62         // given
63         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
64         DelegateExecution delegateExecution = mockDelegateExecution();
65         when(delegateExecution.getVariable(CORRELATION_ID)).thenReturn("");
66         // when, then
67         expectedException.expect(BpmnError.class);
68         testedObject.execute(delegateExecution);
69     }
70
71     private DelegateExecution mockDelegateExecutionWithCorrelationId() {
72         new PnfCheckInputs(DEFAULT_TIMEOUT);
73         DelegateExecution delegateExecution = mockDelegateExecution();
74         when(delegateExecution.getVariable(CORRELATION_ID)).thenReturn("testCorrelationId");
75         return delegateExecution;
76     }
77
78     @Test
79     public void shouldThrowException_whenTimeoutIsNotSetAndDefaultIsNotDefined() {
80         // given
81         PnfCheckInputs testedObject = new PnfCheckInputs(null);
82         DelegateExecution delegateExecution = mockDelegateExecutionWithCorrelationId();
83         // when, then
84         expectedException.expect(BpmnError.class);
85         testedObject.execute(delegateExecution);
86     }
87
88     @Test
89     public void shouldThrowException_whenTimeoutIsEmptyStringAndDefaultIsNotDefined() throws Exception {
90         // given
91         PnfCheckInputs testedObject = new PnfCheckInputs(null);
92         DelegateExecution delegateExecution = mockDelegateExecutionWithCorrelationId();
93         when(delegateExecution.getVariable(TIMEOUT_FOR_NOTIFICATION)).thenReturn("");
94         // when, then
95         expectedException.expect(BpmnError.class);
96         testedObject.execute(delegateExecution);
97     }
98
99     @Test
100     public void shouldSetDefaultTimeout_whenTimeoutIsNotSet() {
101         // given
102         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
103         DelegateExecution delegateExecution = mockDelegateExecutionWithCorrelationId();
104         // when
105         testedObject.execute(delegateExecution);
106         // then
107         verify(delegateExecution).setVariable(eq(TIMEOUT_FOR_NOTIFICATION), eq(DEFAULT_TIMEOUT));
108     }
109 }