Fix serialize DelegateExecutionImpl object bug 67/83367/1
authorAndrei Barcovschi <andrei.barcovschi@ericsson.com>
Tue, 26 Mar 2019 15:25:44 +0000 (15:25 +0000)
committerAndrei Barcovschi <andrei.barcovschi@ericsson.com>
Tue, 26 Mar 2019 15:25:55 +0000 (15:25 +0000)
Change-Id: I140796717f9e8a365650355d92df60b85bc7a8e2
Issue-ID: SO-1690
Signed-off-by: Andrei Barcovschi <andrei.barcovschi@ericsson.com>
bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/common/DelegateExecutionImpl.java
bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/common/DelegateExecutionImplTest.java

index 1c1d6b7..39a209e 100644 (file)
@@ -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<String, Serializable> 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<String, Serializable> 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> T getVariable(String key) {
-               return this.get(key);
-       }
-
-       @Override
-       public <T> 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<ResourceKey, String> 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> T get(String key) {
-               final Object value = this.execution.getVariable(key);
-       
-               return (T)value;
-       }
+    private static final long serialVersionUID = 5559067662634919395L;
+
+    @JsonProperty
+    private final Map<String, Serializable> 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<String, Serializable> 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> T getVariable(final String key) {
+        return this.get(key);
+    }
+
+    @Override
+    public <T> 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<ResourceKey, String> 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> T get(final String key) {
+        final Object value = this.execution.getVariable(key);
+        return (T) value;
+    }
+
 }
index 32a18d5..a30aadc 100644 (file)
 
 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<String, Serializable> 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<String, Serializable> 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<String, Serializable> 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<String, Serializable> lookup = new HashMap<>();
-               Map<String, Serializable> map = new HashMap<>();
-               map.put("lookupKeyMap", (Serializable) lookup);
-               DelegateExecutionImpl impl = create(map);
-               
-               assertEquals(lookup, impl.getLookupMap());
-       }
-       
-       private DelegateExecutionImpl create() {
-               return create(new HashMap<String, Serializable>());
-       }
-       
-       private DelegateExecutionImpl create(Map<String, Serializable> map) {
-               DelegateExecutionFake fake = new DelegateExecutionFake();
-               
-               for (Entry<String, Serializable> 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<String, Serializable> 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<String, Serializable> 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<String, Serializable> 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<String, Serializable> lookup = new HashMap<>();
+        final Map<String, Serializable> 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<String, Serializable>());
+    }
+
+    private DelegateExecutionImpl create(final Map<String, Serializable> map) {
+        final DelegateExecutionFake fake = new DelegateExecutionFake();
+
+        for (final Entry<String, Serializable> entry : map.entrySet()) {
+            fake.setVariable(entry.getKey(), entry.getValue());
+        }
+        return new DelegateExecutionImpl(fake);
+    }
 
 }