diff --git a/src/main/java/com/microsoft/graph/serializer/DerivedClassIdentifier.java b/src/main/java/com/microsoft/graph/serializer/DerivedClassIdentifier.java index e01346a02..2d2fd1d8a 100644 --- a/src/main/java/com/microsoft/graph/serializer/DerivedClassIdentifier.java +++ b/src/main/java/com/microsoft/graph/serializer/DerivedClassIdentifier.java @@ -1,25 +1,32 @@ package com.microsoft.graph.serializer; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.CaseFormat; import com.google.gson.JsonObject; import com.microsoft.graph.logger.ILogger; +import java.util.Locale; import java.util.Objects; import javax.annotation.Nonnull; import javax.annotation.Nullable; -/** This class provides methods to get the derived class corresponding to the OData type when deserializing payloads. */ +/** + * This class provides methods to get the derived class corresponding to the OData type when deserializing payloads. + */ public class DerivedClassIdentifier { private final static String ODATA_TYPE_KEY = "@odata.type"; private final ILogger logger; + /** * Creates a new instance of the dereived class identifier. + * * @param logger The logger to use. */ public DerivedClassIdentifier(@Nonnull ILogger logger) { - this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");; + this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null"); + ; } /** @@ -29,7 +36,7 @@ public DerivedClassIdentifier(@Nonnull ILogger logger) { * * @param jsonObject the raw JSON object of the response * @param parentClass the parent class the derived class should inherit from - * @return the derived class if found, or null if not applicable + * @return the derived class if found, or null if not applicable */ @Nullable public Class identify(@Nonnull final JsonObject jsonObject, @Nullable final Class parentClass) { @@ -39,11 +46,7 @@ public Class identify(@Nonnull final JsonObject jsonObject, @Nullable final C /** #microsoft.graph.user or #microsoft.graph.callrecords.callrecord */ final String odataType = jsonObject.get(ODATA_TYPE_KEY).getAsString(); final int lastDotIndex = odataType.lastIndexOf("."); - final String derivedType = (odataType.substring(0, lastDotIndex) + - ".models." + - CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, - odataType.substring(lastDotIndex + 1))) - .replace("#", "com."); + final String derivedType = oDataTypeToClassName(odataType); try { Class derivedClass = Class.forName(derivedType); //Check that the derived class inherits from the given parent class @@ -61,4 +64,20 @@ public Class identify(@Nonnull final JsonObject jsonObject, @Nullable final C //If there is no defined OData type, return null return null; } + + /** + * Convert {@code @odata.type} to proper java class name + * + * @param odataType to convert + * @return converted class name + */ + @VisibleForTesting + static String oDataTypeToClassName(@Nonnull String odataType) { + Objects.requireNonNull(odataType); + final int lastDotIndex = odataType.lastIndexOf("."); + return (odataType.substring(0, lastDotIndex).toLowerCase(Locale.ROOT) + + ".models." + + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, odataType.substring(lastDotIndex + 1))) + .replace("#", "com."); + } } diff --git a/src/test/java/com/microsoft/graph/serializer/DerivedClassIdentifierTest.java b/src/test/java/com/microsoft/graph/serializer/DerivedClassIdentifierTest.java new file mode 100644 index 000000000..b73a29f25 --- /dev/null +++ b/src/test/java/com/microsoft/graph/serializer/DerivedClassIdentifierTest.java @@ -0,0 +1,18 @@ +package com.microsoft.graph.serializer; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Created by Valentin Popov valentin@archiva.ru on 31.08.2021. + */ +class DerivedClassIdentifierTest { + + @Test + void oDataTypeToClassName() { + Assertions.assertEquals("com.microsoft.graph.models.Message", + DerivedClassIdentifier.oDataTypeToClassName("#Microsoft.Graph.Message")); + } +}