Skip to content

Commit 5c55675

Browse files
author
Valentin Popov
committed
1 parent d2fcdab commit 5c55675

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.microsoft.graph.serializer;
22

3+
import com.google.common.annotations.VisibleForTesting;
34
import com.google.common.base.CaseFormat;
45
import com.google.gson.JsonObject;
56
import com.microsoft.graph.logger.ILogger;
67

8+
import java.util.Locale;
79
import java.util.Objects;
810
import javax.annotation.Nonnull;
911
import javax.annotation.Nullable;
@@ -14,12 +16,13 @@ public class DerivedClassIdentifier {
1416
private final static String ODATA_TYPE_KEY = "@odata.type";
1517

1618
private final ILogger logger;
19+
1720
/**
1821
* Creates a new instance of the dereived class identifier.
1922
* @param logger The logger to use.
2023
*/
2124
public DerivedClassIdentifier(@Nonnull ILogger logger) {
22-
this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");;
25+
this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");
2326
}
2427

2528
/**
@@ -36,14 +39,9 @@ public Class<?> identify(@Nonnull final JsonObject jsonObject, @Nullable final C
3639
Objects.requireNonNull(jsonObject, "parameter jsonObject cannot be null");
3740
//Identify the odata.type information if provided
3841
if (jsonObject.get(ODATA_TYPE_KEY) != null) {
39-
/** #microsoft.graph.user or #microsoft.graph.callrecords.callrecord */
42+
// #microsoft.graph.user or #microsoft.graph.callrecords.callrecord
4043
final String odataType = jsonObject.get(ODATA_TYPE_KEY).getAsString();
41-
final int lastDotIndex = odataType.lastIndexOf(".");
42-
final String derivedType = (odataType.substring(0, lastDotIndex) +
43-
".models." +
44-
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL,
45-
odataType.substring(lastDotIndex + 1)))
46-
.replace("#", "com.");
44+
final String derivedType = oDataTypeToClassName(odataType);
4745
try {
4846
Class<?> derivedClass = Class.forName(derivedType);
4947
//Check that the derived class inherits from the given parent class
@@ -61,4 +59,20 @@ public Class<?> identify(@Nonnull final JsonObject jsonObject, @Nullable final C
6159
//If there is no defined OData type, return null
6260
return null;
6361
}
62+
63+
/**
64+
* Convert {@code @odata.type} to proper java class name
65+
*
66+
* @param odataType to convert
67+
* @return converted class name
68+
*/
69+
@VisibleForTesting
70+
static String oDataTypeToClassName(@Nonnull String odataType) {
71+
Objects.requireNonNull(odataType);
72+
final int lastDotIndex = odataType.lastIndexOf(".");
73+
return (odataType.substring(0, lastDotIndex).toLowerCase(Locale.ROOT) +
74+
".models." +
75+
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, odataType.substring(lastDotIndex + 1)))
76+
.replace("#", "com.");
77+
}
6478
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.microsoft.graph.serializer;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
/**
9+
* Created by Valentin Popov valentin@archiva.ru on 31.08.2021.
10+
*/
11+
class DerivedClassIdentifierTest {
12+
13+
@Test
14+
void oDataTypeToClassName() {
15+
Assertions.assertEquals("com.microsoft.graph.models.Message",
16+
DerivedClassIdentifier.oDataTypeToClassName("#Microsoft.Graph.Message"));
17+
}
18+
}

0 commit comments

Comments
 (0)