Merge "add junit coverage for SvnfmService"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / common / listener / WorkflowValidatorRunnerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.common.listener;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.mock;
27 import java.util.Arrays;
28 import java.util.List;
29 import org.camunda.bpm.engine.delegate.BpmnError;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.junit.runner.RunWith;
36 import org.onap.so.bpmn.common.listener.validation.FlowValidator;
37 import org.onap.so.bpmn.common.listener.validation.ValidationConfig;
38 import org.onap.so.bpmn.common.listener.validation.WorkflowPreValidatorOne;
39 import org.onap.so.bpmn.common.listener.validation.WorkflowPreValidatorTwo;
40 import org.onap.so.bpmn.common.listener.validation.WorkflowValidatorRunner;
41 import org.onap.so.bpmn.core.WorkflowException;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
45
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ContextConfiguration(classes = {ValidationConfig.class})
48 public class WorkflowValidatorRunnerTest extends WorkflowValidatorRunner {
49
50     @Rule
51     public ExpectedException thrown = ExpectedException.none();
52
53     @Autowired
54     private WorkflowValidatorRunner runner;
55
56     @Test
57     public void filterValidatorTest() {
58
59         WorkflowPreValidatorOne one = new WorkflowPreValidatorOne();
60         WorkflowPreValidatorTwo two = new WorkflowPreValidatorTwo();
61         List<FlowValidator> validators = Arrays.asList(one, two);
62
63         List<FlowValidator> result = runner.filterListeners(validators, (item -> item.shouldRunFor("test")));
64
65         List<FlowValidator> expected = Arrays.asList(two, one);
66
67         assertEquals(expected, result);
68     }
69
70     @Test
71     public void testValidate() {
72
73         DelegateExecution execution = new DelegateExecutionFake();
74         execution.setVariable("testProcessKey", "1234");
75         try {
76             runner.preValidate("test", execution);
77             fail("exception not thrown");
78         } catch (BpmnError e) {
79             WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
80             assertEquals(
81                     "Failed Validations:\norg.onap.so.bpmn.common.listener.validation.WorkflowPreValidatorTwo: my-error-two\norg.onap.so.bpmn.common.listener.validation.WorkflowPreValidatorOne: my-error-one",
82                     workflowException.getErrorMessage());
83         }
84         runner.preValidate("test2", mock(DelegateExecution.class));
85     }
86
87     @Test
88     public void testEmptyList() {
89         boolean result = runner.preValidate("test3", mock(DelegateExecution.class));
90
91         assertTrue(result);
92     }
93 }