From 51809fac5f8a131af49632d5ba129068d416abf5 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Fri, 6 Sep 2024 11:30:05 +0300 Subject: [PATCH] Add failing test case --- ...BaseCollectionPaginationCountResponse.java | 127 ++++++++++++++++++ .../java/com/microsoft/kiota/TestEntity.java | 6 + .../kiota/TestEntityCollectionResponse.java | 65 +++++++++ .../kiota/store/InMemoryBackingStoreTest.java | 32 +++++ 4 files changed, 230 insertions(+) create mode 100644 components/abstractions/src/test/java/com/microsoft/kiota/BaseCollectionPaginationCountResponse.java create mode 100644 components/abstractions/src/test/java/com/microsoft/kiota/TestEntityCollectionResponse.java diff --git a/components/abstractions/src/test/java/com/microsoft/kiota/BaseCollectionPaginationCountResponse.java b/components/abstractions/src/test/java/com/microsoft/kiota/BaseCollectionPaginationCountResponse.java new file mode 100644 index 000000000..a2ee6fd6e --- /dev/null +++ b/components/abstractions/src/test/java/com/microsoft/kiota/BaseCollectionPaginationCountResponse.java @@ -0,0 +1,127 @@ +package com.microsoft.kiota; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import com.microsoft.kiota.serialization.AdditionalDataHolder; +import com.microsoft.kiota.serialization.Parsable; +import com.microsoft.kiota.serialization.ParseNode; +import com.microsoft.kiota.serialization.SerializationWriter; +import com.microsoft.kiota.store.BackedModel; +import com.microsoft.kiota.store.BackingStore; +import com.microsoft.kiota.store.BackingStoreFactorySingleton; + +public class BaseCollectionPaginationCountResponse implements AdditionalDataHolder, BackedModel, Parsable { + + /** + * Stores model information. + */ + @jakarta.annotation.Nonnull + protected BackingStore backingStore; + /** + * Instantiates a new {@link BaseCollectionPaginationCountResponse} and sets the default values. + */ + public BaseCollectionPaginationCountResponse() { + this.backingStore = BackingStoreFactorySingleton.instance.createBackingStore(); + this.setAdditionalData(new HashMap<>()); + } + /** + * Creates a new instance of the appropriate class based on discriminator value + * @param parseNode The parse node to use to read the discriminator value and create the object + * @return a {@link BaseCollectionPaginationCountResponse} + */ + @jakarta.annotation.Nonnull + public static BaseCollectionPaginationCountResponse createFromDiscriminatorValue(@jakarta.annotation.Nonnull final ParseNode parseNode) { + Objects.requireNonNull(parseNode); + return new BaseCollectionPaginationCountResponse(); + } + /** + * Gets the AdditionalData property value. Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well. + * @return a {@link Map} + */ + @jakarta.annotation.Nonnull + public Map getAdditionalData() { + Map value = this.backingStore.get("additionalData"); + if(value == null) { + value = new HashMap<>(); + this.setAdditionalData(value); + } + return value; + } + /** + * Gets the backingStore property value. Stores model information. + * @return a {@link BackingStore} + */ + @jakarta.annotation.Nonnull + public BackingStore getBackingStore() { + return this.backingStore; + } + /** + * The deserialization information for the current model + * @return a {@link Map>} + */ + @jakarta.annotation.Nonnull + public Map> getFieldDeserializers() { + final HashMap> deserializerMap = new HashMap>(2); + deserializerMap.put("@odata.count", (n) -> { this.setOdataCount(n.getLongValue()); }); + deserializerMap.put("@odata.nextLink", (n) -> { this.setOdataNextLink(n.getStringValue()); }); + return deserializerMap; + } + /** + * Gets the @odata.count property value. The OdataCount property + * @return a {@link Long} + */ + @jakarta.annotation.Nullable + public Long getOdataCount() { + return this.backingStore.get("odataCount"); + } + /** + * Gets the @odata.nextLink property value. The OdataNextLink property + * @return a {@link String} + */ + @jakarta.annotation.Nullable + public String getOdataNextLink() { + return this.backingStore.get("odataNextLink"); + } + /** + * Serializes information the current object + * @param writer Serialization writer to use to serialize this model + */ + public void serialize(@jakarta.annotation.Nonnull final SerializationWriter writer) { + Objects.requireNonNull(writer); + writer.writeLongValue("@odata.count", this.getOdataCount()); + writer.writeStringValue("@odata.nextLink", this.getOdataNextLink()); + writer.writeAdditionalData(this.getAdditionalData()); + } + /** + * Sets the AdditionalData property value. Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well. + * @param value Value to set for the AdditionalData property. + */ + public void setAdditionalData(@jakarta.annotation.Nullable final Map value) { + this.backingStore.set("additionalData", value); + } + /** + * Sets the backingStore property value. Stores model information. + * @param value Value to set for the backingStore property. + */ + public void setBackingStore(@jakarta.annotation.Nonnull final BackingStore value) { + Objects.requireNonNull(value); + this.backingStore = value; + } + /** + * Sets the @odata.count property value. The OdataCount property + * @param value Value to set for the @odata.count property. + */ + public void setOdataCount(@jakarta.annotation.Nullable final Long value) { + this.backingStore.set("odataCount", value); + } + /** + * Sets the @odata.nextLink property value. The OdataNextLink property + * @param value Value to set for the @odata.nextLink property. + */ + public void setOdataNextLink(@jakarta.annotation.Nullable final String value) { + this.backingStore.set("odataNextLink", value); + } + +} diff --git a/components/abstractions/src/test/java/com/microsoft/kiota/TestEntity.java b/components/abstractions/src/test/java/com/microsoft/kiota/TestEntity.java index 03d5111c1..7f2174c4f 100644 --- a/components/abstractions/src/test/java/com/microsoft/kiota/TestEntity.java +++ b/components/abstractions/src/test/java/com/microsoft/kiota/TestEntity.java @@ -13,6 +13,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Consumer; public class TestEntity implements Parsable, AdditionalDataHolder, BackedModel { @@ -107,4 +108,9 @@ public void serialize(@Nonnull SerializationWriter writer) { // TODO Auto-generated method stub } + + public static TestEntity createFromDiscriminatorValue(@jakarta.annotation.Nonnull final ParseNode parseNode) { + Objects.requireNonNull(parseNode); + return new TestEntity(); + } } diff --git a/components/abstractions/src/test/java/com/microsoft/kiota/TestEntityCollectionResponse.java b/components/abstractions/src/test/java/com/microsoft/kiota/TestEntityCollectionResponse.java new file mode 100644 index 000000000..6bafe66a0 --- /dev/null +++ b/components/abstractions/src/test/java/com/microsoft/kiota/TestEntityCollectionResponse.java @@ -0,0 +1,65 @@ +package com.microsoft.kiota; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + + +import com.microsoft.kiota.serialization.Parsable; +import com.microsoft.kiota.serialization.ParseNode; +import com.microsoft.kiota.serialization.SerializationWriter; + +public class TestEntityCollectionResponse extends BaseCollectionPaginationCountResponse implements Parsable { + + /** + * Instantiates a new {@link TestEntityCollectionResponse} and sets the default values. + */ + public TestEntityCollectionResponse() { + super(); + } + /** + * Creates a new instance of the appropriate class based on discriminator value + * @param parseNode The parse node to use to read the discriminator value and create the object + * @return a {@link TestEntityCollectionResponse} + */ + @jakarta.annotation.Nonnull + public static TestEntityCollectionResponse createFromDiscriminatorValue(@jakarta.annotation.Nonnull final ParseNode parseNode) { + Objects.requireNonNull(parseNode); + return new TestEntityCollectionResponse(); + } + /** + * The deserialization information for the current model + * @return a {@link Map>} + */ + @jakarta.annotation.Nonnull + public Map> getFieldDeserializers() { + final HashMap> deserializerMap = new HashMap>(super.getFieldDeserializers()); + deserializerMap.put("value", (n) -> { this.setValue(n.getCollectionOfObjectValues(TestEntity::createFromDiscriminatorValue)); }); + return deserializerMap; + } + /** + * Gets the value property value. The value property + * @return a {@link java.util.List} + */ + @jakarta.annotation.Nullable + public java.util.List getValue() { + return this.backingStore.get("value"); + } + /** + * Serializes information the current object + * @param writer Serialization writer to use to serialize this model + */ + public void serialize(@jakarta.annotation.Nonnull final SerializationWriter writer) { + Objects.requireNonNull(writer); + super.serialize(writer); + writer.writeCollectionOfObjectValues("value", this.getValue()); + } + /** + * Sets the value property value. The value property + * @param value Value to set for the value property. + */ + public void setValue(@jakarta.annotation.Nullable final java.util.List value) { + this.backingStore.set("value", value); + } + +} diff --git a/components/abstractions/src/test/java/com/microsoft/kiota/store/InMemoryBackingStoreTest.java b/components/abstractions/src/test/java/com/microsoft/kiota/store/InMemoryBackingStoreTest.java index 081dc0f47..817aa5f71 100644 --- a/components/abstractions/src/test/java/com/microsoft/kiota/store/InMemoryBackingStoreTest.java +++ b/components/abstractions/src/test/java/com/microsoft/kiota/store/InMemoryBackingStoreTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.*; import com.microsoft.kiota.TestEntity; +import com.microsoft.kiota.TestEntityCollectionResponse; import org.junit.jupiter.api.Test; @@ -422,4 +423,35 @@ void TestsBackingStoreEmbeddedWithMultipleNestedModelsCollectionsAndAdditionalDa assertEquals(2, invocationCount.get()); // only for setting the id and the colleagues } + + @Test + void TestsBackingStoreUpdateToItemInNestedCollectionWithAnotherBackedModel() { + // Arrange dummy user with initialized backing store + var colleagues = new ArrayList(); + for (int i = 0; i < 10; i++) { + var colleague = new TestEntity(); + colleague.setId(UUID.randomUUID().toString()); + colleague.setBusinessPhones(Arrays.asList(new String[] {"+1 234 567 891"})); + colleague.getAdditionalData().put("count", i); + colleagues.add(colleague); + colleague.getBackingStore().setIsInitializationCompleted(true); + } + + var testUserCollectionResponse = new TestEntityCollectionResponse(); + testUserCollectionResponse.setValue(colleagues); + testUserCollectionResponse.getBackingStore().setIsInitializationCompleted(true); + + // Act on the data by making a change + var manager = new TestEntity(); + manager.setId("2fe22fe5-1132-42cf-90f9-1dc17e325a74"); + manager.getBackingStore().setIsInitializationCompleted(true); + testUserCollectionResponse.getValue().get(0).setManager(manager); + + // Assert by retrieving only changed values + var changedValues = testUserCollectionResponse.getBackingStore().enumerate(); + assertEquals(1, changedValues.size()); + assertEquals("value", changedValues.keySet().toArray()[0]); + assertEquals(10, ((List) changedValues.values().toArray()[0]).size()); + assertTrue(((TestEntity)((List)changedValues.values().toArray()[0]).get(0)).getBackingStore().enumerate().containsKey("manager")); + } }