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;
28 import java.io.Serializable;
29 import java.util.Arrays;
30 import java.util.HashMap;
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;
45 public class DelegateExecutionImplTest {
48 public ExpectedException thrown = ExpectedException.none();
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);
59 assertEquals("value1", impl.getVariable("var1"));
60 assertEquals("value2", impl.getRequiredVariable("var2"));
61 assertThat(impl.getVariable("list1"), IsIterableContainingInOrder.contains("value1", "value2"));
67 public void getRequiredVariableNotFound() throws RequiredExecutionVariableExeception {
68 final DelegateExecutionImpl impl = create();
70 thrown.expect(RequiredExecutionVariableExeception.class);
71 impl.getRequiredVariable("var1");
76 public void setVariable() {
77 final DelegateExecutionImpl impl = create();
78 impl.setVariable("var1", "value1");
80 assertEquals("value1", impl.get("var1"));
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);
90 assertEquals(gBB, impl.getGeneralBuildingBlock());
94 public void getGeneralBuildingBlockNotFound() {
95 final DelegateExecutionImpl impl = create();
96 thrown.expect(MissingBuildingBlockInputException.class);
97 impl.getGeneralBuildingBlock();
101 public void getGeneralBuildingBlockCastException() {
102 final Map<String, Serializable> map = new HashMap<>();
103 map.put("gBBInput", new DelegateExecutionFake());
104 final DelegateExecutionImpl impl = create(map);
106 thrown.expect(MalformedBuildingBlockInputException.class);
107 impl.getGeneralBuildingBlock();
111 public void getDelegateExecution() {
112 final DelegateExecutionImpl impl = create();
114 assertNotNull(impl.getDelegateExecution());
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);
124 assertEquals(lookup, impl.getLookupMap());
128 public void testDelegateExecutionImpl_serializeDelegateExecutionImplObject_shouldNotThrowAnyExceptionWhenSerializing() {
129 final DelegateExecutionImpl objectUnderTest = create();
132 final ObjectMapper objectMapper = new ObjectMapper();
133 objectMapper.writeValueAsString(objectUnderTest);
134 } catch (final JsonProcessingException e) {
135 fail("Should be possible to serialize DelegateExecutionImpl object");
140 private DelegateExecutionImpl create() {
141 return create(new HashMap<String, Serializable>());
144 private DelegateExecutionImpl create(final Map<String, Serializable> map) {
145 final DelegateExecutionFake fake = new DelegateExecutionFake();
147 for (final Entry<String, Serializable> entry : map.entrySet()) {
148 fake.setVariable(entry.getKey(), entry.getValue());
150 return new DelegateExecutionImpl(fake);