From befc44d1b17d92b5417aa121714152a898236a7b Mon Sep 17 00:00:00 2001 From: Andrei Barcovschi Date: Tue, 26 Mar 2019 15:25:44 +0000 Subject: Fix serialize DelegateExecutionImpl object bug Change-Id: I140796717f9e8a365650355d92df60b85bc7a8e2 Issue-ID: SO-1690 Signed-off-by: Andrei Barcovschi --- .../onap/so/bpmn/common/DelegateExecutionImpl.java | 219 +++++++++++---------- .../so/bpmn/common/DelegateExecutionImplTest.java | 208 +++++++++---------- 2 files changed, 226 insertions(+), 201 deletions(-) (limited to 'bpmn/MSOCommonBPMN/src') diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/common/DelegateExecutionImpl.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/common/DelegateExecutionImpl.java index 1c1d6b73fd..39a209efa8 100644 --- a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/common/DelegateExecutionImpl.java +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/common/DelegateExecutionImpl.java @@ -1,22 +1,22 @@ /*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * +* ============LICENSE_START======================================================= +* ONAP - SO +* ================================================================================ +* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +* ================================================================================ +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* * http://www.apache.org/licenses/LICENSE-2.0 - * +* * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ package org.onap.so.bpmn.common; @@ -31,92 +31,107 @@ import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception; import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock; import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + public class DelegateExecutionImpl implements BuildingBlockExecution, Serializable { - private final Map seedMap; - private transient DelegateExecution execution; - private static final String INVALID_INPUT_MISSING = "Expected variable of \"%s\" not found in execution"; - private static final String INVALID_INPUT_CLASS_CAST = "Expected variable of \"%s\" was the wrong object type in the execution"; - - private static final String MISSING_MSG = "Execution variable \"gBBInput\" cannot be null when executing building blocks"; - private static final String MALFORMED_MSG = "Execution variable \"gBBInput\" must contain an element of type GeneralBuildingBlock"; - - public DelegateExecutionImpl(Map seedMap) { - this.seedMap = seedMap; - } - - public DelegateExecutionImpl(DelegateExecution execution) { - this.seedMap = new HashMap<>(); - execution.getVariables().forEach((key, value) -> { - if (value instanceof Serializable) { - seedMap.put(key, (Serializable)value); - } - }); - /* must occur for junit tests to work */ - this.execution = execution; - } - @Override - public GeneralBuildingBlock getGeneralBuildingBlock() { - try { - GeneralBuildingBlock generalBuildingBlock = (GeneralBuildingBlock) execution.getVariable("gBBInput"); - - if (generalBuildingBlock == null) { - throw new MissingBuildingBlockInputException(MISSING_MSG); - } - - return generalBuildingBlock; - } catch (ClassCastException e) { - throw new MalformedBuildingBlockInputException(MALFORMED_MSG, e); - } - } - - @Override - public T getVariable(String key) { - return this.get(key); - } - - @Override - public T getRequiredVariable(String key) throws RequiredExecutionVariableExeception { - final T result; - - result = this.get(key); - if (result == null) { - throw new RequiredExecutionVariableExeception(String.format(INVALID_INPUT_MISSING, key)); - - } - return result; - } - - @Override - public void setVariable(String key, Serializable value) { - this.execution.setVariable(key, value); - } - - @Override - public Map getLookupMap() { - return this.get("lookupKeyMap"); - } - - @Override - public String getFlowToBeCalled() { - return this.get("flowToBeCalled"); - } - public DelegateExecution getDelegateExecution() { - return this.execution; - } - - public void setDelegateExecution(DelegateExecution execution) { - this.execution = execution; - this.seedMap.forEach((key, value) -> { - if (!execution.hasVariable(key)) { - execution.setVariable(key, value); - } - }); - } - - protected T get(String key) { - final Object value = this.execution.getVariable(key); - - return (T)value; - } + private static final long serialVersionUID = 5559067662634919395L; + + @JsonProperty + private final Map seedMap; + + private transient DelegateExecution execution; + private static final String INVALID_INPUT_MISSING = "Expected variable of \"%s\" not found in execution"; + + private static final String MISSING_MSG = + "Execution variable \"gBBInput\" cannot be null when executing building blocks"; + private static final String MALFORMED_MSG = + "Execution variable \"gBBInput\" must contain an element of type GeneralBuildingBlock"; + + public DelegateExecutionImpl(final Map seedMap) { + this.seedMap = seedMap; + } + + public DelegateExecutionImpl(final DelegateExecution execution) { + this.seedMap = new HashMap<>(); + execution.getVariables().forEach((key, value) -> { + if (value instanceof Serializable) { + seedMap.put(key, (Serializable) value); + } + }); + /* must occur for junit tests to work */ + this.execution = execution; + } + + @JsonIgnore + @Override + public GeneralBuildingBlock getGeneralBuildingBlock() { + try { + final GeneralBuildingBlock generalBuildingBlock = (GeneralBuildingBlock) execution.getVariable("gBBInput"); + + if (generalBuildingBlock == null) { + throw new MissingBuildingBlockInputException(MISSING_MSG); + } + + return generalBuildingBlock; + } catch (final ClassCastException e) { + throw new MalformedBuildingBlockInputException(MALFORMED_MSG, e); + } + } + + @Override + public T getVariable(final String key) { + return this.get(key); + } + + @Override + public T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception { + final T result; + + result = this.get(key); + if (result == null) { + throw new RequiredExecutionVariableExeception(String.format(INVALID_INPUT_MISSING, key)); + + } + return result; + } + + @Override + public void setVariable(final String key, final Serializable value) { + this.execution.setVariable(key, value); + } + + @JsonIgnore + @Override + public Map getLookupMap() { + return this.get("lookupKeyMap"); + } + + @JsonIgnore + @Override + public String getFlowToBeCalled() { + return this.get("flowToBeCalled"); + } + + @JsonIgnore + public DelegateExecution getDelegateExecution() { + return this.execution; + } + + public void setDelegateExecution(final DelegateExecution execution) { + this.execution = execution; + this.seedMap.forEach((key, value) -> { + if (!execution.hasVariable(key)) { + execution.setVariable(key, value); + } + }); + } + + @SuppressWarnings("unchecked") + protected T get(final String key) { + final Object value = this.execution.getVariable(key); + return (T) value; + } + } diff --git a/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/common/DelegateExecutionImplTest.java b/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/common/DelegateExecutionImplTest.java index 32a18d5a7a..a30aadc1f8 100644 --- a/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/common/DelegateExecutionImplTest.java +++ b/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/common/DelegateExecutionImplTest.java @@ -20,19 +20,15 @@ package org.onap.so.bpmn.common; -import static org.hamcrest.CoreMatchers.hasItems; -import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; -import static org.mockito.ArgumentMatchers.contains; +import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import java.io.Serializable; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -46,101 +42,115 @@ import org.onap.so.bpmn.common.exceptions.MissingBuildingBlockInputException; import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception; import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + public class DelegateExecutionImplTest { - @Rule - public ExpectedException thrown= ExpectedException.none(); - - - @Test - public void getVariable() throws RequiredExecutionVariableExeception { - Map map = new HashMap<>(); - map.put("var1", "value1"); - map.put("var2", "value2"); - map.put("list1", (Serializable)Arrays.asList("value1", "value2")); - DelegateExecutionImpl impl = create(map); - - assertEquals("value1", impl.getVariable("var1")); - assertEquals("value2", impl.getRequiredVariable("var2")); - assertThat(impl.getVariable("list1"), IsIterableContainingInOrder.contains("value1", "value2")); - - } - - - @Test - public void getRequiredVariableNotFound() throws RequiredExecutionVariableExeception { - DelegateExecutionImpl impl = create(); - - thrown.expect(RequiredExecutionVariableExeception.class); - impl.getRequiredVariable("var1"); - } - - - @Test - public void setVariable() { - DelegateExecutionImpl impl = create(); - impl.setVariable("var1", "value1"); - - assertEquals("value1", impl.get("var1")); - } - - @Test - public void getGeneralBuildingBlock() { - GeneralBuildingBlock gBB = mock(GeneralBuildingBlock.class); - Map map = new HashMap<>(); - map.put("gBBInput", gBB); - DelegateExecutionImpl impl = create(map); - - assertEquals(gBB, impl.getGeneralBuildingBlock()); - } - - @Test - public void getGeneralBuildingBlockNotFound() { - - DelegateExecutionImpl impl = create(); - - thrown.expect(MissingBuildingBlockInputException.class); - impl.getGeneralBuildingBlock(); - } - - @Test - public void getGeneralBuildingBlockCastException() { - Map map = new HashMap<>(); - map.put("gBBInput", new DelegateExecutionFake()); - DelegateExecutionImpl impl = create(map); - - thrown.expect(MalformedBuildingBlockInputException.class); - impl.getGeneralBuildingBlock(); - } - - @Test - public void getDelegateExecution() { - DelegateExecutionImpl impl = create(); - - assertNotNull(impl.getDelegateExecution()); - } - - @Test - public void getLookupMap() { - Map lookup = new HashMap<>(); - Map map = new HashMap<>(); - map.put("lookupKeyMap", (Serializable) lookup); - DelegateExecutionImpl impl = create(map); - - assertEquals(lookup, impl.getLookupMap()); - } - - private DelegateExecutionImpl create() { - return create(new HashMap()); - } - - private DelegateExecutionImpl create(Map map) { - DelegateExecutionFake fake = new DelegateExecutionFake(); - - for (Entry entry : map.entrySet()) { - fake.setVariable(entry.getKey(), entry.getValue()); - } - return new DelegateExecutionImpl(fake); - } + @Rule + public ExpectedException thrown = ExpectedException.none(); + + + @Test + public void getVariable() throws RequiredExecutionVariableExeception { + final Map map = new HashMap<>(); + map.put("var1", "value1"); + map.put("var2", "value2"); + map.put("list1", (Serializable) Arrays.asList("value1", "value2")); + final DelegateExecutionImpl impl = create(map); + + assertEquals("value1", impl.getVariable("var1")); + assertEquals("value2", impl.getRequiredVariable("var2")); + assertThat(impl.getVariable("list1"), IsIterableContainingInOrder.contains("value1", "value2")); + + } + + + @Test + public void getRequiredVariableNotFound() throws RequiredExecutionVariableExeception { + final DelegateExecutionImpl impl = create(); + + thrown.expect(RequiredExecutionVariableExeception.class); + impl.getRequiredVariable("var1"); + } + + + @Test + public void setVariable() { + final DelegateExecutionImpl impl = create(); + impl.setVariable("var1", "value1"); + + assertEquals("value1", impl.get("var1")); + } + + @Test + public void getGeneralBuildingBlock() { + final GeneralBuildingBlock gBB = mock(GeneralBuildingBlock.class); + final Map map = new HashMap<>(); + map.put("gBBInput", gBB); + final DelegateExecutionImpl impl = create(map); + + assertEquals(gBB, impl.getGeneralBuildingBlock()); + } + + @Test + public void getGeneralBuildingBlockNotFound() { + final DelegateExecutionImpl impl = create(); + thrown.expect(MissingBuildingBlockInputException.class); + impl.getGeneralBuildingBlock(); + } + + @Test + public void getGeneralBuildingBlockCastException() { + final Map map = new HashMap<>(); + map.put("gBBInput", new DelegateExecutionFake()); + final DelegateExecutionImpl impl = create(map); + + thrown.expect(MalformedBuildingBlockInputException.class); + impl.getGeneralBuildingBlock(); + } + + @Test + public void getDelegateExecution() { + final DelegateExecutionImpl impl = create(); + + assertNotNull(impl.getDelegateExecution()); + } + + @Test + public void getLookupMap() { + final Map lookup = new HashMap<>(); + final Map map = new HashMap<>(); + map.put("lookupKeyMap", (Serializable) lookup); + final DelegateExecutionImpl impl = create(map); + + assertEquals(lookup, impl.getLookupMap()); + } + + @Test + public void testDelegateExecutionImpl_serializeDelegateExecutionImplObject_shouldNotThrowAnyExceptionWhenSerializing() { + final DelegateExecutionImpl objectUnderTest = create(); + + try { + final ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.writeValueAsString(objectUnderTest); + } catch (final JsonProcessingException e) { + fail("Should be possible to serialize DelegateExecutionImpl object"); + } + + } + + private DelegateExecutionImpl create() { + return create(new HashMap()); + } + + private DelegateExecutionImpl create(final Map map) { + final DelegateExecutionFake fake = new DelegateExecutionFake(); + + for (final Entry entry : map.entrySet()) { + fake.setVariable(entry.getKey(), entry.getValue()); + } + return new DelegateExecutionImpl(fake); + } } -- cgit 1.2.3-korg