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