Skip to content

Commit 88eca42

Browse files
committed
Increased unit tests coverage.
1 parent 3b6be7c commit 88eca42

File tree

5 files changed

+125
-104
lines changed

5 files changed

+125
-104
lines changed

src/main/java/com/microsoft/graph/serializer/FallbackTypeAdapterFactory.java

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424

2525
import com.google.common.base.CaseFormat;
2626
import com.google.gson.Gson;
27-
import com.google.gson.JsonElement;
2827
import com.google.gson.TypeAdapter;
2928
import com.google.gson.TypeAdapterFactory;
30-
import com.google.gson.internal.Streams;
3129
import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory;
3230
import com.google.gson.reflect.TypeToken;
3331
import com.google.gson.stream.JsonReader;
@@ -105,58 +103,13 @@ public <T> TypeAdapter<T> create(@Nonnull final Gson gson, @Nonnull final TypeTo
105103
return null;
106104
}
107105

108-
return (TypeAdapter<T>) new ODataTypeParametrizedIJsonBackedObjectAdapter(gson, delegatedAdapter, (TypeToken<IJsonBackedObject>) type, logger);
106+
return (TypeAdapter<T>) new ODataTypeParametrizedIJsonBackedTypedAdapter(this, gson, delegatedAdapter, (TypeToken<IJsonBackedObject>) type, logger);
109107
}
110108
else {
111109
return null;
112110
}
113111
}
114112

115-
/**
116-
* This adapter is responsible for deserialization of IJsonBackedObjects where service
117-
* returns one of several derived types of a base object, which is defined using the
118-
* odata.type parameter. If odata.type parameter is not found, the Gson default
119-
* (delegated) type adapter is used.
120-
*/
121-
private class ODataTypeParametrizedIJsonBackedObjectAdapter extends TypeAdapter<IJsonBackedObject> {
122-
123-
private final Gson gson;
124-
private final TypeAdapter<IJsonBackedObject> delegatedAdapter;
125-
private final TypeToken<IJsonBackedObject> type;
126-
private final DerivedClassIdentifier derivedClassIdentifier;
127-
128-
public ODataTypeParametrizedIJsonBackedObjectAdapter(@Nonnull Gson gson, @Nonnull TypeAdapter<IJsonBackedObject> delegatedAdapter, @Nonnull final TypeToken<IJsonBackedObject> type, @Nonnull final ILogger logger) {
129-
super();
130-
this.gson = Objects.requireNonNull(gson, "parameter gson cannot be null");
131-
this.delegatedAdapter = Objects.requireNonNull(delegatedAdapter, "object delegated adapted cannot be null");
132-
this.type = Objects.requireNonNull(type, "object type cannot be null");
133-
this.derivedClassIdentifier = new DerivedClassIdentifier(logger);
134-
}
135-
136-
@Override
137-
public void write(JsonWriter out, IJsonBackedObject value)
138-
throws IOException
139-
{
140-
this.delegatedAdapter.write(out, value);
141-
}
142-
143-
@Override
144-
public IJsonBackedObject read(JsonReader in) {
145-
JsonElement jsonElement = Streams.parse(in);
146-
147-
if (jsonElement.isJsonObject()) {
148-
final Class<?> derivedClass = derivedClassIdentifier.identify(jsonElement.getAsJsonObject(), type.getRawType());
149-
150-
if (derivedClass != null) {
151-
final TypeAdapter<?> subTypeAdapter = gson.getDelegateAdapter(FallbackTypeAdapterFactory.this, TypeToken.get(derivedClass));
152-
return (IJsonBackedObject) subTypeAdapter.fromJsonTree(jsonElement);
153-
}
154-
}
155-
156-
return delegatedAdapter.fromJsonTree(jsonElement);
157-
}
158-
}
159-
160113
private static final class EnumTypeAdapter<T> extends TypeAdapter<T> {
161114

162115
private final Map<String, T> enumValues;

src/test/java/com/microsoft/graph/models/MessageStub.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
*/
1111
public class MessageStub extends TestIJsonBackedObject {
1212

13-
@SerializedName("name")
13+
@SerializedName("body")
1414
@Expose()
15-
public String name;
15+
public String body;
1616

1717
@SerializedName("reaction")
1818
@Expose()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.microsoft.graph.models;
2+
3+
import com.google.gson.annotations.Expose;
4+
import com.google.gson.annotations.SerializedName;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.util.List;
8+
9+
/**
10+
* This class shouldn't be moved as it's location in this
11+
* particular package is tightly coupled with the logic present at:
12+
* com/microsoft/graph/serializer/DerivedClassIdentifier.java:38
13+
*/
14+
public class ReactionsStub extends TestIJsonBackedObject {
15+
16+
@SerializedName("reactions")
17+
@Expose
18+
@Nullable
19+
public List<ReactionStub> reactions;
20+
21+
}

src/test/java/com/microsoft/graph/serializer/FallbackTypeAdapterFactoryTest.java

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.microsoft.graph.serializer;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
5+
import com.microsoft.graph.http.BaseCollectionResponse;
6+
import com.microsoft.graph.logger.ILogger;
7+
import com.microsoft.graph.models.MessageStub;
8+
import com.microsoft.graph.models.MessagesCollectionResponseStub;
9+
import com.microsoft.graph.models.ReactionStub;
10+
import com.microsoft.graph.models.ReactionsStub;
11+
import com.microsoft.graph.models.SubReactionStub1;
12+
import com.microsoft.graph.models.SubReactionStub2;
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.lang.reflect.Type;
16+
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
import static org.mockito.Mockito.mock;
20+
21+
/**
22+
* Covers the scenario of objects not being properly deserialized
23+
* (@odata.type not taken into account):
24+
* https://github.com/microsoftgraph/msgraph-sdk-java-core/pull/249
25+
*/
26+
public class ODataTypeParametrizedIJsonBackedTypedAdapterTest {
27+
28+
final ILogger logger = mock(ILogger.class);
29+
final Gson gsonInstance = GsonFactory.getGsonInstance(logger);
30+
31+
@Test
32+
public void testDeserializationOfObjectWithODataTypeProperty() {
33+
// Given
34+
final String testJsonResponse =
35+
"{\"@odata.type\": \"#microsoft.graph.subReactionStub1\"}";
36+
37+
// When
38+
ReactionStub reaction = gsonInstance.fromJson(testJsonResponse, ReactionStub.class);
39+
40+
// Then
41+
assertTrue(reaction instanceof SubReactionStub1);
42+
}
43+
44+
@Test
45+
public void testDeserializationOfPropertyWithODataTypeProperty() {
46+
// Given
47+
final String testJsonResponse =
48+
"{\"body\": \"message1\",\"reaction\": {\"@odata.type\": \"#microsoft.graph.subReactionStub2\"}}";
49+
50+
// When
51+
MessageStub message = gsonInstance.fromJson(testJsonResponse, MessageStub.class);
52+
53+
// Then
54+
assertTrue(message.reaction instanceof SubReactionStub2);
55+
}
56+
57+
@Test
58+
public void testDeserializationOfCollectionPropertyContainingObjectsWithODataTypeProperty() {
59+
// Given
60+
final String testJsonResponse =
61+
"{ reactions : [" +
62+
"{\"@odata.type\": \"#microsoft.graph.subReactionStub1\"}," +
63+
"{\"@odata.type\": \"#microsoft.graph.subReactionStub2\"}," +
64+
"{\"@odata.type\": \"#microsoft.graph.subReactionStub1\"}," +
65+
"]}";
66+
67+
// When
68+
ReactionsStub reactions = gsonInstance.fromJson(testJsonResponse, ReactionsStub.class);
69+
70+
// Then
71+
assertNotNull(reactions.reactions);
72+
assertTrue(reactions.reactions.get(0) instanceof SubReactionStub1);
73+
assertTrue(reactions.reactions.get(1) instanceof SubReactionStub2);
74+
assertTrue(reactions.reactions.get(2) instanceof SubReactionStub1);
75+
}
76+
77+
@Test
78+
public void testDeserializationOfNestedODataTypeAnnotatedObjects() {
79+
// Given
80+
final Type listType = new TypeToken<MessagesCollectionResponseStub>(){}.getType();
81+
final String testJsonResponse =
82+
"{\"value\": " +
83+
" [" +
84+
" {\"body\": \"message1\",\"reaction\": {\"@odata.type\": \"#microsoft.graph.subReactionStub1\"}}," +
85+
" {\"body\": \"message2\",\"reaction\": {\"@odata.type\": \"#microsoft.graph.subReactionStub2\"}}" +
86+
" ]" +
87+
"}";
88+
89+
// When
90+
final BaseCollectionResponse<MessageStub> baseCollectionResponse = gsonInstance.fromJson(testJsonResponse, listType);
91+
92+
// Then
93+
assertNotNull(baseCollectionResponse.value);
94+
95+
final MessageStub messageStub1 = baseCollectionResponse.value.get(0);
96+
final MessageStub messageStub2 = baseCollectionResponse.value.get(1);
97+
98+
assertTrue(messageStub1.reaction instanceof SubReactionStub1);
99+
assertTrue(messageStub2.reaction instanceof SubReactionStub2);
100+
}
101+
}

0 commit comments

Comments
 (0)