2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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.servicedecomposition;
23 import static org.junit.Assert.assertEquals;
24 import java.io.ByteArrayOutputStream;
25 import java.io.Externalizable;
27 import java.io.IOException;
28 import java.io.ObjectOutputStream;
29 import java.io.OutputStream;
30 import java.io.Serializable;
31 import java.util.HashMap;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.engine.impl.pvm.runtime.ExecutionImpl;
35 import org.junit.Test;
36 import org.onap.so.SerializableChecker;
37 import org.onap.so.SerializableChecker.SerializationFailure;
38 import org.onap.so.bpmn.common.BuildingBlockExecution;
39 import org.onap.so.bpmn.common.DelegateExecutionImpl;
40 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
41 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
42 import com.fasterxml.jackson.databind.ObjectMapper;
44 public class SerializationTest {
46 private static final String RESOURCE_PATH = "src/test/resources/__files/ExecuteBuildingBlock/";
47 private static final String FLOW_VAR_NAME = "flowToBeCalled";
48 private static final String LOOKUP_KEY_MAP_VAR_NAME = "lookupKeyMap";
49 private static final String GBB_INPUT_VAR_NAME = "gBBInput";
50 protected ObjectMapper mapper = new ObjectMapper();
53 public void testSerializationOfAllPojos() throws IOException {
54 GeneralBuildingBlock gbb = mapper.readValue(new File(RESOURCE_PATH + "GeneralBuildingBlockExpected.json"),
55 GeneralBuildingBlock.class);
56 Map<ResourceKey, String> lookupKeyMap = new HashMap<>();
57 DelegateExecution execution = new ExecutionImpl();
58 execution.setVariable(FLOW_VAR_NAME, "AssignServiceInstanceBB");
59 execution.setVariable(GBB_INPUT_VAR_NAME, gbb);
60 execution.setVariable(LOOKUP_KEY_MAP_VAR_NAME, lookupKeyMap);
61 System.out.println(execution.getVariables());
62 BuildingBlockExecution gBuildingBlockExecution = new DelegateExecutionImpl(execution);
63 boolean isSerializable = SerializationTest.isSerializable(gBuildingBlockExecution);
64 assertEquals(true, isSerializable);
67 public static boolean isSerializable(final Object o) {
70 if (implementsInterface(o)) {
71 retVal = attemptToSerialize(o);
79 private static boolean implementsInterface(final Object o) {
82 retVal = ((o instanceof Serializable) || (o instanceof Externalizable));
87 private static boolean attemptToSerialize(final Object o) {
88 final OutputStream sink;
89 ObjectOutputStream stream;
94 sink = new ByteArrayOutputStream();
95 stream = new ObjectOutputStream(sink);
96 stream.writeObject(o);
97 // could also re-serilalize at this point too
98 } catch (final IOException ex) {
101 if (stream != null) {
104 } catch (final IOException ex) {
105 // should not be able to happen