Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / servicedecomposition / SerializationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.servicedecomposition;
22
23 import static org.junit.Assert.assertEquals;
24 import java.io.ByteArrayOutputStream;
25 import java.io.Externalizable;
26 import java.io.File;
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;
32 import java.util.Map;
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;
43
44 public class SerializationTest {
45
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();
51
52     @Test
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);
65     }
66
67     public static boolean isSerializable(final Object o) {
68         final boolean retVal;
69
70         if (implementsInterface(o)) {
71             retVal = attemptToSerialize(o);
72         } else {
73             retVal = false;
74         }
75
76         return (retVal);
77     }
78
79     private static boolean implementsInterface(final Object o) {
80         final boolean retVal;
81
82         retVal = ((o instanceof Serializable) || (o instanceof Externalizable));
83
84         return (retVal);
85     }
86
87     private static boolean attemptToSerialize(final Object o) {
88         final OutputStream sink;
89         ObjectOutputStream stream;
90
91         stream = null;
92
93         try {
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) {
99             return (false);
100         } finally {
101             if (stream != null) {
102                 try {
103                     stream.close();
104                 } catch (final IOException ex) {
105                     // should not be able to happen
106                 }
107             }
108         }
109
110         return (true);
111     }
112 }