Skip to content

Commit 1305d42

Browse files
committed
Added unit test covering case in PR #249.
1 parent ba9993b commit 1305d42

File tree

7 files changed

+149
-0
lines changed

7 files changed

+149
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.microsoft.graph.models;
2+
3+
import com.google.gson.annotations.Expose;
4+
import com.google.gson.annotations.SerializedName;
5+
6+
/**
7+
* This class shouldn't be moved as it's location in this
8+
* particular package is tightly coupled with the logic present at:
9+
* com/microsoft/graph/serializer/DerivedClassIdentifier.java:38
10+
*/
11+
public class MessageStub extends TestIJsonBackedObject {
12+
13+
@SerializedName("name")
14+
@Expose()
15+
public String name;
16+
17+
@SerializedName("reaction")
18+
@Expose()
19+
public ReactionStub reaction;
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.microsoft.graph.models;
2+
3+
import com.microsoft.graph.http.BaseCollectionResponse;
4+
5+
public class MessagesCollectionResponseStub extends BaseCollectionResponse<MessageStub> {
6+
7+
public MessagesCollectionResponseStub() {
8+
}
9+
10+
}
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+
/**
8+
* This class shouldn't be moved as it's location in this
9+
* particular package is tightly coupled with the logic present at:
10+
* com/microsoft/graph/serializer/DerivedClassIdentifier.java:38
11+
*/
12+
public class ReactionStub extends TestIJsonBackedObject {
13+
14+
/**
15+
* the OData type of the object as returned by the service
16+
*/
17+
@SerializedName("@odata.type")
18+
@Expose
19+
@Nullable
20+
public String oDataType;
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.microsoft.graph.models;
2+
3+
/**
4+
* This class shouldn't be moved as it's location in this
5+
* particular package is tightly coupled with the logic present at:
6+
* com/microsoft/graph/serializer/DerivedClassIdentifier.java:38
7+
*/
8+
public class SubReactionStub1 extends ReactionStub {
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.microsoft.graph.models;
2+
3+
/**
4+
* This class shouldn't be moved as it's location in this
5+
* particular package is tightly coupled with the logic present at:
6+
* com/microsoft/graph/serializer/DerivedClassIdentifier.java:38
7+
*/
8+
public class SubReactionStub2 extends ReactionStub {
9+
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.microsoft.graph.models;
2+
3+
import com.google.gson.JsonObject;
4+
import com.microsoft.graph.serializer.AdditionalDataManager;
5+
import com.microsoft.graph.serializer.IJsonBackedObject;
6+
import com.microsoft.graph.serializer.ISerializer;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import static org.mockito.Mockito.mock;
11+
12+
public abstract class TestIJsonBackedObject implements IJsonBackedObject {
13+
14+
@Override
15+
public void setRawObject(@NotNull ISerializer serializer, @NotNull JsonObject json) {
16+
// Do nothing
17+
}
18+
19+
@Nullable
20+
@Override
21+
public AdditionalDataManager additionalDataManager() {
22+
return mock(AdditionalDataManager.class);
23+
}
24+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.microsoft.graph.serializer;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.reflect.TypeToken;
6+
import com.microsoft.graph.http.BaseCollectionResponse;
7+
import com.microsoft.graph.logger.ILogger;
8+
import com.microsoft.graph.models.MessageStub;
9+
import com.microsoft.graph.models.MessagesCollectionResponseStub;
10+
import com.microsoft.graph.models.SubReactionStub1;
11+
import com.microsoft.graph.models.SubReactionStub2;
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
14+
import org.junit.jupiter.api.Test;
15+
16+
import java.lang.reflect.Type;
17+
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
import static org.mockito.Mockito.mock;
21+
22+
public class FallbackTypeAdapterFactoryTest {
23+
24+
/**
25+
* This test covers the scenario of objects not being
26+
* properly deserialized (@odata.type not taken into account):
27+
* https://github.com/microsoftgraph/msgraph-sdk-java-core/pull/249
28+
*/
29+
@Test
30+
public void testDeserializationOfNestedODataTypeAnnotatedObjects() {
31+
final ILogger logger = mock(ILogger.class);
32+
final Gson gsonInstance = GsonFactory.getGsonInstance(logger);
33+
34+
final Type listType = new TypeToken<MessagesCollectionResponseStub>(){}.getType();
35+
final String testJsonResponse =
36+
"{\"value\": " +
37+
" [" +
38+
" {\"name\": \"parent1\",\"reaction\": {\"@odata.type\": \"#microsoft.graph.subReactionStub1\"}}," +
39+
" {\"name\": \"parent2\",\"reaction\": {\"@odata.type\": \"#microsoft.graph.subReactionStub2\"}}" +
40+
" ]" +
41+
"}";
42+
43+
final BaseCollectionResponse<MessageStub> baseCollectionResponse = gsonInstance.fromJson(testJsonResponse, listType);
44+
45+
assertNotNull(baseCollectionResponse.value);
46+
47+
final MessageStub messageStub1 = baseCollectionResponse.value.get(0);
48+
final MessageStub messageStub2 = baseCollectionResponse.value.get(1);
49+
50+
assertTrue(messageStub1.reaction instanceof SubReactionStub1);
51+
assertTrue(messageStub2.reaction instanceof SubReactionStub2);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)