|
| 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