forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultSerializerTests.java
More file actions
166 lines (147 loc) · 8.19 KB
/
Copy pathDefaultSerializerTests.java
File metadata and controls
166 lines (147 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.microsoft.graph.serializer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.microsoft.graph.http.MockConnection;
import com.microsoft.graph.logger.DefaultLogger;
import com.microsoft.graph.models.extensions.Attachment;
import com.microsoft.graph.models.extensions.DateOnly;
import com.microsoft.graph.models.extensions.Drive;
import com.microsoft.graph.models.extensions.FileAttachment;
import com.microsoft.graph.models.extensions.RecurrenceRange;
import com.microsoft.graph.models.extensions.User;
import com.microsoft.graph.models.generated.RecurrenceRangeType;
import com.microsoft.graph.requests.extensions.DriveItemDeltaCollectionResponse;
import org.junit.Test;
public class DefaultSerializerTests {
/**
* Make sure that deserializing a Drive also returns members from BaseDrive
*
* @throws Exception If there is an exception during the test
*/
@Test
public void testDriveDeserialization() throws Exception {
final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#drives/$entity\",\"id\":\"8bf6ae90006c4a4c\",\"driveType\":\"personal\",\"owner\":{\"user\":{\"displayName\":\"Peter\",\"id\":\"8bf6ae90006c4a4c\"}},\"quota\":{\"deleted\":1485718314,\"remaining\":983887466461,\"state\":\"normal\",\"total\":1142461300736,\"used\":158573834275}}";
Drive result = serializer.deserializeObject(source, Drive.class);
assertNotNull(result);
assertEquals("personal", result.driveType);
assertEquals(Long.valueOf(983887466461L), result.quota.remaining);
assertEquals("8bf6ae90006c4a4c", result.id);
}
/**
* Make sure that deserializing a DriveItems also deserializes child additionalData
*
* @throws Exception If there is an exception during the test
*/
@Test
public void testDriveItemChildAdditionalDataDeserialization() throws Exception {
final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
String source = "{\n"
+ " \"@odata.context\": \"https://graph.microsoft.com/v1.0/$metadata#users('02008492-3fec-4ce4-bb54-980ad856856f')/drive/root/children\",\n"
+ " \"value\": [\n"
+ " {\n"
+ " \"createdBy\": {\n"
+ " \"user\": {\n"
+ " \"email\": \"the@email.com\",\n"
+ " \"id\": \"02008492-3fec-4ce4-bb54-980ad856856f\",\n"
+ " \"displayName\": \"John Doe\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ "}";
DriveItemDeltaCollectionResponse result = serializer
.deserializeObject(source, DriveItemDeltaCollectionResponse.class);
assertNotNull(result);
assertNotNull(result.value);
assertEquals(1, result.value.size());
assertNotNull(result.value.get(0));
assertNotNull(result.value.get(0).createdBy);
assertNotNull(result.value.get(0).createdBy.user);
assertNotNull(result.value.get(0).createdBy.user.additionalDataManager());
assertNotNull(result.value.get(0).createdBy.user.additionalDataManager().get("email"));
assertEquals("the@email.com",
result.value.get(0).createdBy.user.additionalDataManager().get("email").getAsString());
}
@Test
public void testRecurrenceRangeDeserialization() throws Exception {
final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
String source = "{\n" +
" \"type\": \"noEnd\",\n" +
" \"startDate\": \"2016-04-27\",\n" +
" \"endDate\": \"0001-01-01\",\n" +
" \"recurrenceTimeZone\": \"China Standard Time\",\n" +
" \"numberOfOccurrences\": 0\n" +
"}";
RecurrenceRange baseRecurrenceRange = serializer.deserializeObject(source, RecurrenceRange.class);
assertNotNull(source);
assertEquals(RecurrenceRangeType.NO_END, baseRecurrenceRange.type);
assertEquals("2016-04-27", baseRecurrenceRange.startDate.toString());
assertEquals("0001-01-01", baseRecurrenceRange.endDate.toString());
assertEquals("China Standard Time", baseRecurrenceRange.recurrenceTimeZone);
assertEquals(Integer.valueOf(0), baseRecurrenceRange.numberOfOccurrences);
}
@Test
public void testRecurrenceRangeSerialization() throws Exception {
final String expected = "{\"endDate\":\"2016-05-25\",\"numberOfOccurrences\":4,\"recurrenceTimeZone\":\"PST\",\"startDate\":\"2016-04-25\",\"type\":\"endDate\"}";
final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
RecurrenceRange brr = new RecurrenceRange();
brr.type = RecurrenceRangeType.END_DATE;
brr.startDate = new DateOnly(2016, 4, 25);
brr.endDate = new DateOnly(2016, 5, 25);
brr.recurrenceTimeZone = "PST";
brr.numberOfOccurrences = 4;
String jsonOut = serializer.serializeObject(brr);
assertNotNull(jsonOut);
assertEquals(expected, jsonOut);
}
@Test
public void testResponseHeaders() throws Exception {
MockConnection connection = new MockConnection(null);
final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
User user = serializer.deserializeObject("{\"id\":\"1\"}", User.class, connection.getResponseHeaders());
JsonElement responseHeaders = user.additionalDataManager().get("graphResponseHeaders");
assertNotNull(responseHeaders);
JsonElement responseHeader = responseHeaders.getAsJsonObject().get("header1");
assertNotNull(responseHeader);
assertEquals("value1", responseHeader.getAsJsonArray().get(0).getAsString());
}
@Test
public void testDeserializeDerivedType() throws Exception {
final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
String source = "{\"@odata.context\": \"/attachments/$entity\",\"@odata.type\": \"#microsoft.graph.fileAttachment\",\"id\": \"AAMkAGQ0MjBmNWVkLTYxZjUtNDRmYi05Y2NiLTBlYjIwNzJjNmM1NgBGAAAAAAC6ff7latYeQqu_gLrhSAIhBwCF7iGjpaOmRqVwbZc-xXzwAAAAAAEMAACF7iGjpaOmRqVwbZc-xXzwAABQStA0AAABEgAQAFbGmeisbjtLnQdp7kC_9Fk=\",\"lastModifiedDateTime\": \"2018-01-23T21:50:22Z\",\"name\": \"Test Book.xlsx\",\"contentType\": \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\"size\": 8457,\"isInline\": false,\"contentId\": null,\"contentLocation\": null,\"contentBytes\": \"bytedata\"}";
Attachment result = serializer.deserializeObject(source, Attachment.class);
assert(result instanceof FileAttachment);
FileAttachment fileAttachment = (FileAttachment) result;
assertNotNull(fileAttachment.contentBytes);
JsonObject o = fileAttachment.getRawObject();
assertNotNull(o);
assertEquals("#microsoft.graph.fileAttachment", o. get("@odata.type").getAsString());
}
@Test
public void testSerializerCanSerializeVoidWithoutEmittingWarning() {
// Unfortunately does not assert for existence of Java 9 illegal access warnings
// which seem to written to the console without use of System.err/System.out (so cannot be captured AFAIK).
// @davidmoten
final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
HasVoidMember t = new HasVoidMember();
String json = serializer.serializeObject(t);
// this line will emit a warning from Java 9 about illegal access to the constructor of Void
// if gson TypeAdapterFactory is not handling Void properly
HasVoidMember t2 = serializer.deserializeObject(json, HasVoidMember.class);
assertEquals(t.x, t2.x);
assertEquals(t.y, t2.y);
}
public static final class HasVoidMember {
@SerializedName("x")
@Expose
int x = 1;
@SerializedName("y")
@Expose
Void y;
}
}