Merge "Updated as per review comments"
[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.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.CORRELATION_ID;
26 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_UUID;
27 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_INSTANCE_ID;
28 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.TIMEOUT_FOR_NOTIFICATION;
29
30 import java.util.UUID;
31 import org.camunda.bpm.engine.delegate.BpmnError;
32 import org.camunda.bpm.engine.delegate.DelegateExecution;
33 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 public class PnfCheckInputsTest {
38
39     private static final String DEFAULT_TIMEOUT = "P1D";
40     private static final String VALID_UUID = UUID.nameUUIDFromBytes("testUuid".getBytes()).toString();
41     private static final String RESERVED_UUID = new UUID(0, 0).toString();
42     private static final String DEFAULT_SERVICE_INSTANCE_ID = "da7d07d9-b71c-4128-809d-2ec01c807169";
43     private static final String DEFAULT_CORRELATION_ID = "testCorrelationId";
44
45     private DelegateExecutionBuilder delegateExecutionBuilder;
46
47     @Before
48     public void setUp() {
49         delegateExecutionBuilder = new DelegateExecutionBuilder();
50     }
51
52     @Test
53     public void shouldThrowException_whenCorrelationIdNotSet() {
54         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
55         DelegateExecution execution = delegateExecutionBuilder.setCorrelationId(null).setPnfUuid(VALID_UUID).build();
56         assertThatThrownBy(() -> testedObject.execute(execution)).isInstanceOf(BpmnError.class);
57     }
58
59     @Test
60     public void shouldThrowException_whenTimeoutIsEmptyStringAndDefaultIsNotDefined() {
61         PnfCheckInputs testedObject = new PnfCheckInputs(null);
62         DelegateExecution execution = delegateExecutionBuilder.setTimeoutForNotification("").build();
63         assertThatThrownBy(() -> testedObject.execute(execution)).isInstanceOf(BpmnError.class);
64     }
65
66     @Test
67     public void shouldSetDefaultTimeout_whenTimeoutIsNotSet() {
68         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
69         DelegateExecution execution = delegateExecutionBuilder.setTimeoutForNotification(null).build();
70         testedObject.execute(execution);
71         assertThat(execution.getVariable(TIMEOUT_FOR_NOTIFICATION)).isEqualTo(DEFAULT_TIMEOUT);
72     }
73
74     @Test
75     public void shouldThrowException_whenPnfUuidIsNotSet() {
76         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
77         DelegateExecution execution = delegateExecutionBuilder.setPnfUuid(null).build();
78         assertThatThrownBy(() -> testedObject.execute(execution)).isInstanceOf(BpmnError.class);
79     }
80
81     @Test
82     public void shouldThrowException_whenPnfUuidIsEmptyString() {
83         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
84         DelegateExecution execution = delegateExecutionBuilder.setPnfUuid("").build();
85         assertThatThrownBy(() -> testedObject.execute(execution)).isInstanceOf(BpmnError.class);
86     }
87
88     @Test
89     public void shouldThrowException_whenPnfUuidIsReservedUuid() {
90         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
91         DelegateExecution execution = delegateExecutionBuilder.setPnfUuid(RESERVED_UUID).build();
92         assertThatThrownBy(() -> testedObject.execute(execution)).isInstanceOf(BpmnError.class);
93     }
94
95     @Test
96     public void shouldThrowException_whenServiceInstanceIdIsNotSet() {
97         PnfCheckInputs testedObject = new PnfCheckInputs(DEFAULT_TIMEOUT);
98         DelegateExecution execution = delegateExecutionBuilder.setServiceInstanceId(null).build();
99         assertThatThrownBy(() -> testedObject.execute(execution)).isInstanceOf(BpmnError.class);
100     }
101
102     private static class DelegateExecutionBuilder {
103         private String correlationId = DEFAULT_CORRELATION_ID;
104         private String pnfUuid = VALID_UUID;
105         private String serviceInstanceId = DEFAULT_SERVICE_INSTANCE_ID;
106         private String timeoutForNotification;
107
108         public DelegateExecutionBuilder setCorrelationId(String correlationId) {
109             this.correlationId = correlationId;
110             return this;
111         }
112
113         public DelegateExecutionBuilder setPnfUuid(String pnfUuid) {
114             this.pnfUuid = pnfUuid;
115             return this;
116         }
117
118         public DelegateExecutionBuilder setServiceInstanceId(String serviceInstanceId) {
119             this.serviceInstanceId = serviceInstanceId;
120             return this;
121         }
122
123         public DelegateExecutionBuilder setTimeoutForNotification(String timeoutForNotification) {
124             this.timeoutForNotification = timeoutForNotification;
125             return this;
126         }
127
128         public DelegateExecution build() {
129             DelegateExecution execution = new DelegateExecutionFake();
130             execution.setVariable("testProcessKey", "testProcessKeyValue");
131             execution.setVariable(CORRELATION_ID, this.correlationId);
132             execution.setVariable(PNF_UUID, this.pnfUuid);
133             execution.setVariable(SERVICE_INSTANCE_ID, this.serviceInstanceId);
134             execution.setVariable(TIMEOUT_FOR_NOTIFICATION, this.timeoutForNotification);
135             return execution;
136         }
137     }
138 }