1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service.impl;
17
18 import org.junit.Assert;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoaderTestUtils;
22 import org.kuali.rice.kew.api.KewApiServiceLocator;
23 import org.kuali.rice.kew.api.document.Document;
24 import org.kuali.rice.kew.api.document.WorkflowDocumentService;
25 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
26 import org.kuali.rice.krad.data.provider.annotation.SerializationContext;
27 import org.kuali.rice.krad.data.provider.annotation.Serialized;
28 import org.kuali.rice.krad.maintenance.MaintainableImpl;
29 import org.kuali.rice.krad.maintenance.MaintenanceDocumentBase;
30 import org.kuali.rice.krad.service.DocumentDictionaryService;
31 import org.kuali.rice.krad.service.KRADServiceLocator;
32 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
33 import org.kuali.rice.krad.service.LegacyDataAdapter;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.stubbing.Answer;
36
37 import javax.persistence.Transient;
38 import java.io.Serializable;
39
40 import static org.mockito.Mockito.*;
41
42
43
44
45
46
47
48 public class MaintenanceDocumentSerializationTest {
49
50
51
52 static final TestKradChildOjb child = new TestKradChildOjb("child", "content1", "content2", "content3", "content4", "content5", "content6");
53 static final TestKradDataObj dataObject = new TestKradDataObj("dataObject", child, child, child, child, child, child);
54 static final XmlObjectSerializerServiceImpl xmlObjectSerializerServiceImpl = new XmlObjectSerializerServiceImpl();
55 static LegacyDataAdapter mockLegacyDataAdapter;
56 static WorkflowDocumentService mockWorkflowDocumentService;
57 static DocumentDictionaryService mockDocumentDictionaryService;
58
59
60
61
62 @BeforeClass
63 public static void setupServices() {
64
65 mockLegacyDataAdapter = mock(LegacyDataAdapter.class);
66 mockWorkflowDocumentService = mock(WorkflowDocumentService.class);
67 mockDocumentDictionaryService = mock(DocumentDictionaryService.class);
68 when(mockLegacyDataAdapter.areNotesSupported(any(Class.class))).thenReturn(Boolean.FALSE);
69 when(mockLegacyDataAdapter.isProxied(anyObject())).thenReturn(Boolean.FALSE);
70
71 xmlObjectSerializerServiceImpl.setLegacyDataAdapter(mockLegacyDataAdapter);
72
73
74 DataObjectSerializerServiceImpl dataObjectSerializerService = new DataObjectSerializerServiceImpl();
75 dataObjectSerializerService.setLegacyDataAdapter(mockLegacyDataAdapter);
76
77
78 GlobalResourceLoaderTestUtils.addMockService(KRADServiceLocatorWeb.LEGACY_DATA_ADAPTER, mockLegacyDataAdapter);
79 GlobalResourceLoaderTestUtils.addMockService(KRADServiceLocator.KRAD_SERIALIZER_SERVICE, dataObjectSerializerService);
80 GlobalResourceLoaderTestUtils.addMockService(KRADServiceLocator.XML_OBJECT_SERIALIZER_SERVICE,
81 xmlObjectSerializerServiceImpl);
82 GlobalResourceLoaderTestUtils.addMockService(KewApiServiceLocator.WORKFLOW_DOCUMENT_SERVICE,
83 mockWorkflowDocumentService);
84 GlobalResourceLoaderTestUtils.addMockService(KRADServiceLocatorWeb.DOCUMENT_DICTIONARY_SERVICE, mockDocumentDictionaryService);
85 when(mockWorkflowDocumentService.getDocument(anyString())).thenReturn(Document.Builder.create("1","1","1","1").build());
86 when(mockDocumentDictionaryService.getMaintainableClass(anyString())).thenAnswer(new Answer<Object>() {
87 @Override
88 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
89 return MaintainableImpl.class;
90 }
91 });
92 }
93
94
95
96
97
98 @Test
99 public void kradSerializationAnnotationTest() {
100
101 MaintainableImpl maintainable = new MaintainableImpl();
102 maintainable.setLegacyDataAdapter(mockLegacyDataAdapter);
103 maintainable.setDataObject(dataObject);
104 MaintenanceDocumentBase kradMaintenanceDoc = new MaintenanceDocumentBase();
105
106 kradMaintenanceDoc.setNewMaintainableObject(maintainable);
107 kradMaintenanceDoc.populateXmlDocumentContentsFromMaintainables();
108 kradMaintenanceDoc.populateMaintainablesFromXmlDocumentContents();
109 TestKradDataObj reconstitutedDataObject =
110 (TestKradDataObj) kradMaintenanceDoc.getNewMaintainableObject().getDataObject();
111
112
113
114 Assert.assertNull("annotated to disable serialization, should be null", reconstitutedDataObject.getChild2());
115 Assert.assertNull("annotated as transient with nothing to override, should be null", reconstitutedDataObject.getChild4());
116 Assert.assertNull("annotated as transient and to disable serialization, should be null", reconstitutedDataObject.getChild5());
117
118 Assert.assertEquals("This field annotated to disable serialization for a different SerializationType must survive",
119 dataObject.getName(), reconstitutedDataObject.getName());
120
121 Assert.assertNotNull("Child object with no field annotations, it must survive",
122 reconstitutedDataObject.getChild1());
123 Assert.assertNotNull("Annotated to be serialized, it must survive", reconstitutedDataObject.getChild3());
124 Assert.assertNotNull("Transient, but annotated to be serialized, it must survive", reconstitutedDataObject.getChild6());
125
126
127
128 Assert.assertNull("annotated to disable serialization, should be null", reconstitutedDataObject.getChild1().getContent2());
129 Assert.assertNull("annotated as transient with nothing to override, should be null", reconstitutedDataObject.getChild1().getContent4());
130 Assert.assertNull("annotated as transient and to disable serialization, should be null", reconstitutedDataObject.getChild1().getContent5());
131
132 Assert.assertEquals("with no field annotations, it must survive", child.getContent1(), reconstitutedDataObject.getChild1().getContent1());
133 Assert.assertEquals("Annotated to be serialized, it must survive", child.getContent3(), reconstitutedDataObject.getChild1().getContent3());
134 Assert.assertEquals("Transient, but annotated to be serialized, it must survive", child.getContent6(),
135 reconstitutedDataObject.getChild1().getContent6());
136 }
137 }
138
139
140
141
142
143 class TestKradDataObj extends PersistableBusinessObjectBase implements Serializable {
144
145
146 @Serialized(enabled = false, forContexts = { })
147 private String name;
148
149
150 private TestKradChildOjb child1;
151
152
153 @Serialized(enabled = false, forContexts = { SerializationContext.MAINTENANCE })
154 private TestKradChildOjb child2;
155
156
157
158 @Serialized(enabled = true)
159 private TestKradChildOjb child3;
160
161
162 @Transient
163 private TestKradChildOjb child4;
164
165
166 @Serialized(enabled = false)
167 @Transient
168 private TestKradChildOjb child5;
169
170
171
172 @Serialized(enabled = true, forContexts = { SerializationContext.ALL })
173 @Transient
174 private TestKradChildOjb child6;
175
176 TestKradDataObj(String name, TestKradChildOjb child1, TestKradChildOjb child2, TestKradChildOjb child3,
177 TestKradChildOjb child4, TestKradChildOjb child5, TestKradChildOjb child6) {
178 this.name = name;
179 this.child1 = child1;
180 this.child2 = child2;
181 this.child3 = child3;
182 this.child4 = child4;
183 this.child5 = child5;
184 this.child6 = child6;
185 }
186
187 public String getName() {
188 return name;
189 }
190
191 public TestKradChildOjb getChild1() {
192 return child1;
193 }
194
195 public TestKradChildOjb getChild2() {
196 return child2;
197 }
198
199 public TestKradChildOjb getChild3() {
200 return child3;
201 }
202
203 public TestKradChildOjb getChild4() {
204 return child4;
205 }
206
207 public TestKradChildOjb getChild5() {
208 return child5;
209 }
210
211 public TestKradChildOjb getChild6() {
212 return child6;
213 }
214 }
215
216
217
218
219
220 class TestKradChildOjb implements Serializable {
221
222 private String name;
223
224 private String content1;
225
226 @Serialized(enabled = false)
227 private String content2;
228
229 @Serialized(enabled = true)
230 private String content3;
231
232 @Transient
233 private String content4;
234
235 @Serialized(enabled = false)
236 @Transient
237 private String content5;
238
239 @Serialized(enabled = true)
240 @Transient
241 private String content6;
242
243 TestKradChildOjb(String name, String content1, String content2, String content3, String content4, String content5,
244 String content6) {
245 this.name = name;
246 this.content1 = content1;
247 this.content2 = content2;
248 this.content3 = content3;
249 this.content4 = content4;
250 this.content5 = content5;
251 this.content6 = content6;
252 }
253
254 public String getName() {
255 return name;
256 }
257
258 public String getContent1() {
259 return content1;
260 }
261
262 public String getContent2() {
263 return content2;
264 }
265
266 public String getContent3() {
267 return content3;
268 }
269
270 public String getContent4() {
271 return content4;
272 }
273
274 public String getContent5() {
275 return content5;
276 }
277
278 public String getContent6() {
279 return content6;
280 }
281 }
282
283