cfafe1622b6cf5c3fe0c736bbf76e308f2864dd8
[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
25 import java.io.ByteArrayOutputStream;
26 import java.io.Externalizable;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.ObjectOutputStream;
30 import java.io.OutputStream;
31 import java.io.Serializable;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.camunda.bpm.engine.delegate.DelegateExecution;
36 import org.camunda.bpm.engine.impl.pvm.runtime.ExecutionImpl;
37 import org.junit.Test;
38 import org.onap.so.SerializableChecker;
39 import org.onap.so.SerializableChecker.SerializationFailure;
40 import org.onap.so.bpmn.common.BuildingBlockExecution;
41 import org.onap.so.bpmn.common.DelegateExecutionImpl;
42 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
43 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
44
45 import com.fasterxml.jackson.databind.ObjectMapper;
46
47 public class SerializationTest {
48         
49         private static final String RESOURCE_PATH = "src/test/resources/__files/ExecuteBuildingBlock/";
50         private static final String FLOW_VAR_NAME = "flowToBeCalled";
51         private static final String LOOKUP_KEY_MAP_VAR_NAME = "lookupKeyMap";
52         private static final String GBB_INPUT_VAR_NAME = "gBBInput";
53         protected ObjectMapper mapper = new ObjectMapper();
54         
55         @Test
56         public void testSerializationOfAllPojos() throws IOException {
57                 GeneralBuildingBlock gbb = mapper.readValue(new File(RESOURCE_PATH + "GeneralBuildingBlockExpected.json"),
58                                 GeneralBuildingBlock.class);
59                 Map<ResourceKey, String> lookupKeyMap = new HashMap<>();
60                 DelegateExecution execution = new ExecutionImpl();
61                 execution.setVariable(FLOW_VAR_NAME, "AssignServiceInstanceBB");
62                 execution.setVariable(GBB_INPUT_VAR_NAME, gbb);
63                 execution.setVariable(LOOKUP_KEY_MAP_VAR_NAME, lookupKeyMap);
64                 System.out.println(execution.getVariables());
65                 BuildingBlockExecution gBuildingBlockExecution = new DelegateExecutionImpl(execution);
66                 boolean isSerializable = SerializationTest.isSerializable(gBuildingBlockExecution);
67                 assertEquals(true, isSerializable);
68         }
69         
70         public static boolean isSerializable(final Object o)
71     {
72         final boolean retVal;
73
74         if(implementsInterface(o))
75         {
76             retVal = attemptToSerialize(o);
77         }
78         else
79         {
80             retVal = false;
81         }
82
83         return (retVal);
84     }
85
86     private static boolean implementsInterface(final Object o)
87     {
88         final boolean retVal;
89
90         retVal = ((o instanceof Serializable) || (o instanceof Externalizable));
91
92         return (retVal);
93     }
94
95     private static boolean attemptToSerialize(final Object o)
96     {
97         final OutputStream sink;
98         ObjectOutputStream stream;
99
100         stream = null;
101
102         try
103         {
104             sink   = new ByteArrayOutputStream();
105             stream = new ObjectOutputStream(sink);
106             stream.writeObject(o);
107             // could also re-serilalize at this point too
108         }
109         catch(final IOException ex)
110         {
111             return (false);
112         }
113         finally
114         {
115             if(stream != null)
116             {
117                 try
118                 {
119                     stream.close();
120                 }
121                 catch(final IOException ex)
122                 {
123                     // should not be able to happen
124                 }
125             }
126         }
127
128         return (true);
129     }
130 }