2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.common;
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;
29 import java.io.Serializable;
30 import java.util.Arrays;
31 import java.util.HashMap;
33 import java.util.Map.Entry;
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;
45 import com.fasterxml.jackson.core.JsonProcessingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
48 public class DelegateExecutionImplTest {
51 public ExpectedException thrown = ExpectedException.none();
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);
62 assertEquals("value1", impl.getVariable("var1"));
63 assertEquals("value2", impl.getRequiredVariable("var2"));
64 assertThat(impl.getVariable("list1"), IsIterableContainingInOrder.contains("value1", "value2"));
70 public void getRequiredVariableNotFound() throws RequiredExecutionVariableExeception {
71 final DelegateExecutionImpl impl = create();
73 thrown.expect(RequiredExecutionVariableExeception.class);
74 impl.getRequiredVariable("var1");
79 public void setVariable() {
80 final DelegateExecutionImpl impl = create();
81 impl.setVariable("var1", "value1");
83 assertEquals("value1", impl.get("var1"));
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);
93 assertEquals(gBB, impl.getGeneralBuildingBlock());
97 public void getGeneralBuildingBlockNotFound() {
98 final DelegateExecutionImpl impl = create();
99 thrown.expect(MissingBuildingBlockInputException.class);
100 impl.getGeneralBuildingBlock();
104 public void getGeneralBuildingBlockCastException() {
105 final Map<String, Serializable> map = new HashMap<>();
106 map.put("gBBInput", new DelegateExecutionFake());
107 final DelegateExecutionImpl impl = create(map);
109 thrown.expect(MalformedBuildingBlockInputException.class);
110 impl.getGeneralBuildingBlock();
114 public void getDelegateExecution() {
115 final DelegateExecutionImpl impl = create();
117 assertNotNull(impl.getDelegateExecution());
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);
127 assertEquals(lookup, impl.getLookupMap());
131 public void testDelegateExecutionImpl_serializeDelegateExecutionImplObject_shouldNotThrowAnyExceptionWhenSerializing() {
132 final DelegateExecutionImpl objectUnderTest = create();
135 final ObjectMapper objectMapper = new ObjectMapper();
136 objectMapper.writeValueAsString(objectUnderTest);
137 } catch (final JsonProcessingException e) {
138 fail("Should be possible to serialize DelegateExecutionImpl object");
143 private DelegateExecutionImpl create() {
144 return create(new HashMap<String, Serializable>());
147 private DelegateExecutionImpl create(final Map<String, Serializable> map) {
148 final DelegateExecutionFake fake = new DelegateExecutionFake();
150 for (final Entry<String, Serializable> entry : map.entrySet()) {
151 fake.setVariable(entry.getKey(), entry.getValue());
153 return new DelegateExecutionImpl(fake);