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