d670305a9a526f8d6f2ca86b7daf4bda07f3d03e
[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  * Modifications Copyright 2018 Nokia
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.pnf.delegate;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
27 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.CORRELATION_ID;
28 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_UUID;
29 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.TIMEOUT_FOR_NOTIFICATION;
30
31 import java.util.UUID;
32 import org.camunda.bpm.engine.delegate.BpmnError;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 public class PnfCheckInputsTest {
39
40     private static final String DEFAULT_TIMEOUT = "P1D";
41     private static final String VALID_UUID = UUID.nameUUIDFromBytes("testUuid".getBytes()).toString();
42     private static final String RESERVED_UUID = new UUID(0, 0).toString();
43
44     private DelegateExecution delegateExecution;
45
46     @Before
47     public void setUp() {
48         delegateExecution = new DelegateExecutionFake();
49         delegateExecution.setVariable("testProcessKey", "testProcessKeyValue");
50     }
51
52     @Test
53     public void shouldThrowException_whenCorrelationIdNotSet() {
54         PnfCheckInputs testedObject = prepareExecutionForCorrelationId(null);
55         assertThatThrownBy(() -> testedObject.execute(delegateExecution)).isInstanceOf(BpmnError.class);
56     }
57
58     @Test
59     public void shouldThrowException_whenCorrelationIdIsEmptyString() {
60         PnfCheckInputs testedObject = prepareExecutionForCorrelationId("");
61         assertThatThrownBy(() -> testedObject.execute(delegateExecution)).isInstanceOf(BpmnError.class);
62     }
63
64     @Test
65     public void shouldThrowException_whenTimeoutIsNotSetAndDefaultIsNotDefined() {
66         PnfCheckInputs testedObject = prepareExecutionForTimeout(null, null);
67         assertThatThrownBy(() -> testedObject.execute(delegateExecution)).isInstanceOf(BpmnError.class);
68     }
69
70     @Test
71     public void shouldThrowException_whenTimeoutIsEmptyStringAndDefaultIsNotDefined() {
72         PnfCheckInputs testedObject = prepareExecutionForTimeout(null, "");
73         assertThatThrownBy(() -> testedObject.execute(delegateExecution)).isInstanceOf(BpmnError.class);
74     }
75
76     @Test
77     public void shouldSetDefaultTimeout_whenTimeoutIsNotSet() {
78         PnfCheckInputs testedObject = prepareExecutionForTimeout(DEFAULT_TIMEOUT, null);
79         testedObject.execute(delegateExecution);
80         assertThat(delegateExecution.getVariable(TIMEOUT_FOR_NOTIFICATION)).isEqualTo(DEFAULT_TIMEOUT);
81     }
82
83     @Test
84     public void shouldThrowException_whenPnfUuidIsNotSet() {
85         PnfCheckInputs testedObject = prepareExecutionForUuid(null);
86         assertThatThrownBy(() -> testedObject.execute(delegateExecution)).isInstanceOf(BpmnError.class);
87     }
88
89     @Test
90     public void shouldThrowException_whenPnfUuidIsEmptyString() {
91         PnfCheckInputs testedObject = prepareExecutionForUuid("");
92         assertThatThrownBy(() -> testedObject.execute(delegateExecution)).isInstanceOf(BpmnError.class);
93     }
94
95     @Test
96     public void shouldThrowException_whenPnfUuidIsReservedUuid() {
97         PnfCheckInputs testedObject = prepareExecutionForUuid(RESERVED_UUID);
98         assertThatThrownBy(() -> testedObject.execute(delegateExecution)).isInstanceOf(BpmnError.class);
99     }
100
101     private PnfCheckInputs prepareExecutionForCorrelationId(String correlationId) {
102         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
103         delegateExecution.setVariable(CORRELATION_ID, correlationId);
104         delegateExecution.setVariable(PNF_UUID, VALID_UUID);
105         return testedObject;
106     }
107
108     private PnfCheckInputs prepareExecutionForTimeout(String defaultTimeout, String timeout) {
109         PnfCheckInputs testedObject = new PnfCheckInputs(defaultTimeout);
110         delegateExecution.setVariable(CORRELATION_ID, "testCorrelationId");
111         delegateExecution.setVariable(PNF_UUID, VALID_UUID);
112         delegateExecution.setVariable(TIMEOUT_FOR_NOTIFICATION, timeout);
113         return testedObject;
114     }
115
116     private PnfCheckInputs prepareExecutionForUuid(String uuid) {
117         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
118         delegateExecution.setVariable(CORRELATION_ID, "testCorrelationId");
119         delegateExecution.setVariable(PNF_UUID, uuid);
120         return testedObject;
121     }
122 }