From bc6edc44dab4310a403bd6353779e7bde025a71f Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Fri, 4 Oct 2024 17:45:33 +0300 Subject: [PATCH] Increase test coverage --- .../SerializationWriterFactoryRegistry.java | 2 +- .../SerializationHelpersTest.java | 24 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/SerializationWriterFactoryRegistry.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/SerializationWriterFactoryRegistry.java index 36477f4df..23f82768b 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/SerializationWriterFactoryRegistry.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/SerializationWriterFactoryRegistry.java @@ -81,7 +81,7 @@ public SerializationWriterFactoryRegistry() { return contentTypeAssociatedFactories.get(contentTypeWrapper.cleanedContentType); } final String cleanedContentType = - getCleanedVendorSpecificContentType(contentTypeWrapper.cleanedContentType); + getCleanedVendorSpecificContentType(vendorSpecificContentType); if (contentTypeAssociatedFactories.containsKey(cleanedContentType)) { contentTypeWrapper.cleanedContentType = cleanedContentType; return contentTypeAssociatedFactories.get(contentTypeWrapper.cleanedContentType); diff --git a/components/abstractions/src/test/java/com/microsoft/kiota/serialization/SerializationHelpersTest.java b/components/abstractions/src/test/java/com/microsoft/kiota/serialization/SerializationHelpersTest.java index 1e2f3bca2..35d19d506 100644 --- a/components/abstractions/src/test/java/com/microsoft/kiota/serialization/SerializationHelpersTest.java +++ b/components/abstractions/src/test/java/com/microsoft/kiota/serialization/SerializationHelpersTest.java @@ -109,6 +109,7 @@ void serializesAllValuesInBackedModel() throws IOException { entity.setId("123"); entity.setOfficeLocation("Nairobi"); + // Sets the backing store to be initialized. No properties are dirty entity.getBackingStore().setIsInitializationCompleted(true); final String result = KiotaJsonSerialization.serializeAsString(false, entity); @@ -136,7 +137,6 @@ void serializesChangedValuesInBackedModel() throws IOException { final TestBackedModelEntity entity = new TestBackedModelEntity(); entity.setId("123"); entity.setOfficeLocation("Nairobi"); - entity.getBackingStore().setIsInitializationCompleted(true); final String result = KiotaJsonSerialization.serializeAsString(true, entity); @@ -150,4 +150,26 @@ void serializesChangedValuesInBackedModel() throws IOException { new ArrayList<>(Arrays.asList(entity))); assertEquals("[{}]", collectionResult); } + + @Test + void cleansContentType() throws IOException { + final BackingStoreSerializationWriterProxyFactory + backingStoreSerializationWriterProxyFactory = + new BackingStoreSerializationWriterProxyFactory( + new JsonSerializationWriterFactory()); + SerializationWriterFactoryRegistry.defaultInstance.contentTypeAssociatedFactories.put( + _jsonContentType, backingStoreSerializationWriterProxyFactory); + + final TestBackedModelEntity entity = new TestBackedModelEntity(); + entity.setId("123"); + entity.setOfficeLocation("Nairobi"); + + final String result = KiotaSerialization.serializeAsString("application/json;odata.metadata=minimal", entity); + assertEquals("{\"id\":\"123\",\"officeLocation\":\"Nairobi\"}", result); + + // Because onAfterObjectSerialization, returnOnlyChangedValues is set to false & initialization is completed + entity.getBackingStore().setIsInitializationCompleted(false); + final String otherResult = KiotaSerialization.serializeAsString("application/vnd.github.raw+json", entity); + assertEquals("{\"id\":\"123\",\"officeLocation\":\"Nairobi\"}", otherResult); + } }