Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / common / DelegateExecutionImplTest.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.common;
22
23 import static org.hamcrest.CoreMatchers.hasItems;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertThat;
28 import static org.mockito.Matchers.contains;
29 import static org.mockito.Mockito.mock;
30
31 import java.io.Serializable;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38
39 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
40 import org.hamcrest.collection.IsIterableContainingInOrder;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.rules.ExpectedException;
44 import org.onap.so.bpmn.common.exceptions.MalformedBuildingBlockInputException;
45 import org.onap.so.bpmn.common.exceptions.MissingBuildingBlockInputException;
46 import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception;
47 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
48
49 public class DelegateExecutionImplTest {
50
51         @Rule
52         public ExpectedException thrown= ExpectedException.none();
53
54         
55         @Test
56         public void getVariable() throws RequiredExecutionVariableExeception {
57                 Map<String, Serializable> map = new HashMap<>();
58                 map.put("var1", "value1");
59                 map.put("var2", "value2");
60                 map.put("list1", (Serializable)Arrays.asList("value1", "value2"));
61                 DelegateExecutionImpl impl = create(map);
62                 
63                 assertEquals("value1", impl.getVariable("var1"));
64                 assertEquals("value2", impl.getRequiredVariable("var2"));
65                 assertThat(impl.getVariable("list1"), IsIterableContainingInOrder.contains("value1", "value2"));
66
67         }
68         
69         
70         @Test
71         public void getRequiredVariableNotFound() throws RequiredExecutionVariableExeception {
72                 DelegateExecutionImpl impl = create();
73                 
74                 thrown.expect(RequiredExecutionVariableExeception.class);
75                 impl.getRequiredVariable("var1");
76         }
77         
78         
79         @Test
80         public void setVariable() {
81                 DelegateExecutionImpl impl = create();
82                 impl.setVariable("var1", "value1");
83                 
84                 assertEquals("value1", impl.get("var1"));
85         }
86         
87         @Test
88         public void getGeneralBuildingBlock() {
89                 GeneralBuildingBlock gBB = mock(GeneralBuildingBlock.class);
90                 Map<String, Serializable> map = new HashMap<>();
91                 map.put("gBBInput", gBB);
92                 DelegateExecutionImpl impl = create(map);
93                 
94                 assertEquals(gBB, impl.getGeneralBuildingBlock());
95         }
96         
97         @Test
98         public void getGeneralBuildingBlockNotFound() {
99
100                 DelegateExecutionImpl impl = create();
101                 
102                 thrown.expect(MissingBuildingBlockInputException.class);
103                 impl.getGeneralBuildingBlock();
104         }
105         
106         @Test
107         public void getGeneralBuildingBlockCastException() {
108                 Map<String, Serializable> map = new HashMap<>();
109                 map.put("gBBInput", new DelegateExecutionFake());
110                 DelegateExecutionImpl impl = create(map);
111                 
112                 thrown.expect(MalformedBuildingBlockInputException.class);
113                 impl.getGeneralBuildingBlock();
114         }
115         
116         @Test
117         public void getDelegateExecution() {
118                 DelegateExecutionImpl impl = create();
119
120                 assertNotNull(impl.getDelegateExecution());
121         }
122         
123         @Test
124         public void getLookupMap() {
125                 Map<String, Serializable> lookup = new HashMap<>();
126                 Map<String, Serializable> map = new HashMap<>();
127                 map.put("lookupKeyMap", (Serializable) lookup);
128                 DelegateExecutionImpl impl = create(map);
129                 
130                 assertEquals(lookup, impl.getLookupMap());
131         }
132         
133         private DelegateExecutionImpl create() {
134                 return create(new HashMap<String, Serializable>());
135         }
136         
137         private DelegateExecutionImpl create(Map<String, Serializable> map) {
138                 DelegateExecutionFake fake = new DelegateExecutionFake();
139                 
140                 for (Entry<String, Serializable> entry : map.entrySet()) {
141                         fake.setVariable(entry.getKey(), entry.getValue());
142                 }
143                 return new DelegateExecutionImpl(fake);
144         }
145
146 }