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