Merge "Miscellaneous fixes"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / common / validation / 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.validation;
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
28 import java.util.Arrays;
29 import java.util.List;
30
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.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.junit.runner.RunWith;
38 import org.onap.so.bpmn.common.BuildingBlockExecution;
39 import org.onap.so.bpmn.common.DelegateExecutionImpl;
40 import org.onap.so.bpmn.core.WorkflowException;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44
45 @RunWith(SpringJUnit4ClassRunner.class)
46 @ContextConfiguration(classes = {ValidationConfig.class})
47 public class WorkflowValidatorRunnerTest {
48
49         @Rule
50     public ExpectedException thrown= ExpectedException.none();
51         
52         @Autowired
53         private WorkflowValidatorRunner runner;
54         
55         @Test
56         public void filterValidatorTest() {
57                 
58                 WorkflowPreValidatorOne one = new WorkflowPreValidatorOne();
59                 WorkflowPreValidatorTwo two = new WorkflowPreValidatorTwo();            
60                 List<WorkflowValidator> validators = Arrays.asList(one, two);
61
62                 List<WorkflowValidator> result = runner.filterValidators(validators, "test");
63
64                 List<WorkflowValidator> expected = Arrays.asList(two, one);
65                 
66                 assertEquals(expected, result);
67         }
68         
69         @Test
70         public void testValidate() {
71                 
72                 DelegateExecution execution = new DelegateExecutionFake();
73                 execution.setVariable("testProcessKey", "1234");
74                 try {
75                         runner.preValidate("test", execution);
76                         fail("exception not thrown");
77                 } catch (BpmnError e) {
78                         WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
79                         assertEquals("Failed Validations:\norg.onap.so.bpmn.common.validation.WorkflowPreValidatorTwo\norg.onap.so.bpmn.common.validation.WorkflowPreValidatorOne", workflowException.getErrorMessage());
80                 }
81                 runner.preValidate("test2", mock(DelegateExecution.class));
82         }
83         
84         @Test
85         public void testEmptyList() {
86                 boolean result = runner.preValidate("test3", mock(DelegateExecution.class));
87                 
88                 assertTrue(result);
89         }
90 }