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.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;
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;
37 import java.util.Map.Entry;
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;
49 public class DelegateExecutionImplTest {
52 public ExpectedException thrown= ExpectedException.none();
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);
63 assertEquals("value1", impl.getVariable("var1"));
64 assertEquals("value2", impl.getRequiredVariable("var2"));
65 assertThat(impl.getVariable("list1"), IsIterableContainingInOrder.contains("value1", "value2"));
71 public void getRequiredVariableNotFound() throws RequiredExecutionVariableExeception {
72 DelegateExecutionImpl impl = create();
74 thrown.expect(RequiredExecutionVariableExeception.class);
75 impl.getRequiredVariable("var1");
80 public void setVariable() {
81 DelegateExecutionImpl impl = create();
82 impl.setVariable("var1", "value1");
84 assertEquals("value1", impl.get("var1"));
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);
94 assertEquals(gBB, impl.getGeneralBuildingBlock());
98 public void getGeneralBuildingBlockNotFound() {
100 DelegateExecutionImpl impl = create();
102 thrown.expect(MissingBuildingBlockInputException.class);
103 impl.getGeneralBuildingBlock();
107 public void getGeneralBuildingBlockCastException() {
108 Map<String, Serializable> map = new HashMap<>();
109 map.put("gBBInput", new DelegateExecutionFake());
110 DelegateExecutionImpl impl = create(map);
112 thrown.expect(MalformedBuildingBlockInputException.class);
113 impl.getGeneralBuildingBlock();
117 public void getDelegateExecution() {
118 DelegateExecutionImpl impl = create();
120 assertNotNull(impl.getDelegateExecution());
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);
130 assertEquals(lookup, impl.getLookupMap());
133 private DelegateExecutionImpl create() {
134 return create(new HashMap<String, Serializable>());
137 private DelegateExecutionImpl create(Map<String, Serializable> map) {
138 DelegateExecutionFake fake = new DelegateExecutionFake();
140 for (Entry<String, Serializable> entry : map.entrySet()) {
141 fake.setVariable(entry.getKey(), entry.getValue());
143 return new DelegateExecutionImpl(fake);