2c0377d11ce5c828ea0ebd53576eb8cb62787be3
[so.git] /
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.extension.mockito.delegate.DelegateExecutionFake;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.junit.runner.RunWith;
35 import org.onap.so.bpmn.common.BuildingBlockExecution;
36 import org.onap.so.bpmn.common.DelegateExecutionImpl;
37 import org.onap.so.bpmn.common.listener.validation.BuildingBlockValidatorRunner;
38 import org.onap.so.bpmn.common.listener.validation.FlowValidator;
39 import org.onap.so.bpmn.common.listener.validation.MyPreValidatorOne;
40 import org.onap.so.bpmn.common.listener.validation.MyPreValidatorThree;
41 import org.onap.so.bpmn.common.listener.validation.MyPreValidatorTwo;
42 import org.onap.so.bpmn.common.listener.validation.ValidationConfig;
43 import org.onap.so.bpmn.core.WorkflowException;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ContextConfiguration(classes = {ValidationConfig.class})
50 public class BuildingBlockValidatorRunnerTest {
51
52     @Rule
53     public ExpectedException thrown = ExpectedException.none();
54
55     @Autowired
56     private BuildingBlockValidatorRunner runner;
57
58     @Test
59     public void filterValidatorTest() {
60
61         MyPreValidatorOne one = new MyPreValidatorOne();
62         MyPreValidatorTwo two = new MyPreValidatorTwo();
63         MyPreValidatorThree three = new MyPreValidatorThree();
64         List<FlowValidator> validators = Arrays.asList(one, two, three);
65
66         List<FlowValidator> result = runner.filterListeners(validators, (item -> item.shouldRunFor("test")));
67
68         List<FlowValidator> expected = Arrays.asList(two, one);
69
70         assertEquals(expected, result);
71     }
72
73     @Test
74     public void testValidate() {
75
76         BuildingBlockExecution execution = new DelegateExecutionImpl(new DelegateExecutionFake());
77         execution.setVariable("testProcessKey", "1234");
78         try {
79             runner.preValidate("test", execution);
80             fail("exception not thrown");
81         } catch (BpmnError e) {
82             WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
83             assertEquals(
84                     "Failed Validations:\norg.onap.so.bpmn.common.listener.validation.MyPreValidatorTwo: my-error-two\norg.onap.so.bpmn.common.listener.validation.MyPreValidatorOne: my-error-one",
85                     workflowException.getErrorMessage());
86         }
87         runner.preValidate("test2", mock(BuildingBlockExecution.class));
88     }
89
90     @Test
91     public void testEmptyList() {
92         boolean result = runner.preValidate("test3", mock(BuildingBlockExecution.class));
93
94         assertTrue(result);
95     }
96 }