From 092b9fafd882f50c49288763fc1261484c94ea78 Mon Sep 17 00:00:00 2001 From: Sarah Lander <70885646+salander85@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:35:11 +0100 Subject: [PATCH] Migrate shoppinglist syncer (#511) --- .../sync/shoppinglist/ShoppingListSyncer.java | 2 - .../shoppinglist/ShoppingListSyncerTest.java | 208 ++++++++++-------- 2 files changed, 122 insertions(+), 88 deletions(-) diff --git a/src/main/java/com/commercetools/project/sync/shoppinglist/ShoppingListSyncer.java b/src/main/java/com/commercetools/project/sync/shoppinglist/ShoppingListSyncer.java index a4c5b8ab..bac88b4f 100644 --- a/src/main/java/com/commercetools/project/sync/shoppinglist/ShoppingListSyncer.java +++ b/src/main/java/com/commercetools/project/sync/shoppinglist/ShoppingListSyncer.java @@ -29,8 +29,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -// This class compiles but not tested yet -// TODO: Test class and adjust logic if needed public final class ShoppingListSyncer extends Syncer< ShoppingList, diff --git a/src/test/java/com/commercetools/project/sync/shoppinglist/ShoppingListSyncerTest.java b/src/test/java/com/commercetools/project/sync/shoppinglist/ShoppingListSyncerTest.java index f7104343..6fd21001 100644 --- a/src/test/java/com/commercetools/project/sync/shoppinglist/ShoppingListSyncerTest.java +++ b/src/test/java/com/commercetools/project/sync/shoppinglist/ShoppingListSyncerTest.java @@ -1,13 +1,38 @@ package com.commercetools.project.sync.shoppinglist; +import static com.commercetools.project.sync.util.TestUtils.mockResourceIdsGraphQlRequest; +import static com.commercetools.project.sync.util.TestUtils.readObjectFromResource; +import static java.lang.String.format; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.commercetools.api.client.ByProjectKeyShoppingListsGet; +import com.commercetools.api.client.ByProjectKeyShoppingListsRequestBuilder; +import com.commercetools.api.client.ProjectApiRoot; +import com.commercetools.api.defaultconfig.ApiRootBuilder; +import com.commercetools.api.models.shopping_list.ShoppingList; +import com.commercetools.api.models.shopping_list.ShoppingListDraft; +import com.commercetools.api.models.shopping_list.ShoppingListPagedQueryResponse; +import com.commercetools.api.models.shopping_list.ShoppingListPagedQueryResponseBuilder; import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl; import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache; +import com.commercetools.sync.shoppinglists.ShoppingListSync; +import com.commercetools.sync.shoppinglists.utils.ShoppingListTransformUtils; +import io.vrap.rmf.base.client.ApiHttpResponse; +import java.time.Clock; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import uk.org.lidalia.slf4jtest.LoggingEvent; import uk.org.lidalia.slf4jtest.TestLogger; import uk.org.lidalia.slf4jtest.TestLoggerFactory; -// These tests aren't migrated -// TODO: Migrate tests class ShoppingListSyncerTest { private final TestLogger syncerTestLogger = TestLoggerFactory.getTestLogger(ShoppingListSyncer.class); @@ -19,88 +44,99 @@ void setup() { syncerTestLogger.clearAll(); } - // @Test - // void of_ShouldCreateShoppingListSyncerInstance() { - // // test - // final ShoppingListSyncer shoppingListSyncer = - // ShoppingListSyncer.of( - // mock(SphereClient.class), mock(SphereClient.class), mock(Clock.class)); - // - // // assertion - // assertThat(shoppingListSyncer).isNotNull(); - // assertThat(shoppingListSyncer.getSync()).isInstanceOf(ShoppingListSync.class); - // } - // - // @Test - // void transform_ShouldReplaceShoppingListReferenceIdsWithKeys() { - // // preparation - // final SphereClient sourceClient = mock(SphereClient.class); - // final ShoppingListSyncer shoppingListSyncer = - // ShoppingListSyncer.of(sourceClient, mock(SphereClient.class), mock(Clock.class)); - // final List shoppingList = - // Collections.singletonList(readObjectFromResource("shopping-list.json", - // ShoppingList.class)); - // - // mockResourceIdsGraphQlRequest( - // sourceClient, "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey"); - // - // // test - // final CompletionStage> draftsFromPageStage = - // shoppingListSyncer.transform(shoppingList); - // - // // assertion - // assertThat(draftsFromPageStage) - // .isCompletedWithValue( - // ShoppingListTransformUtils.toShoppingListDrafts( - // sourceClient, referenceIdToKeyCache, shoppingList) - // .join()); - // } - // - // @Test - // void getQuery_ShouldBuildShoppingListQuery() { - // final ShoppingListSyncer shoppingListSyncer = - // ShoppingListSyncer.of( - // mock(SphereClient.class), mock(SphereClient.class), mock(Clock.class)); - // - // // assertion - // final ShoppingListQuery query = shoppingListSyncer.getQuery(); - // - // assertThat(query.expansionPaths()).containsExactly(ExpansionPath.of("lineItems[*].variant")); - // } - // - // @Test - // void syncWithError_ShouldCallErrorCallback() { - // // preparation: shoppingList with no key is synced - // final SphereClient sourceClient = mock(SphereClient.class); - // final SphereClient targetClient = mock(SphereClient.class); - // when(sourceClient.getConfig()).thenReturn(SphereApiConfig.of("source-project")); - // when(targetClient.getConfig()).thenReturn(SphereApiConfig.of("target-project")); - // final List shoppingLists = - // Collections.singletonList( - // readObjectFromResource("shopping-list-no-key.json", ShoppingList.class)); - // - // final PagedQueryResult pagedQueryResult = mock(PagedQueryResult.class); - // when(pagedQueryResult.getResults()).thenReturn(shoppingLists); - // when(sourceClient.execute(any(ShoppingListQuery.class))) - // .thenReturn(CompletableFuture.completedFuture(pagedQueryResult)); - // - // mockResourceIdsGraphQlRequest( - // sourceClient, "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey"); - // - // // test - // final ShoppingListSyncer shoppingListSyncer = - // ShoppingListSyncer.of(sourceClient, targetClient, mock(Clock.class)); - // shoppingListSyncer.sync(null, true).toCompletableFuture().join(); - // - // // assertion - // final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1); - // assertThat(errorLog.getMessage()) - // .isEqualTo( - // "Error when trying to sync shoppingList. Existing key: <>. Update - // actions: []"); - // assertThat(errorLog.getThrowable().get().getMessage()) - // .isEqualTo( - // "ShoppingListDraft with name: LocalizedString(en -> shoppingList-name-1) doesn't - // have a key. Please make sure all shopping list drafts have keys."); - // } + @Test + void of_ShouldCreateShoppingListSyncerInstance() { + // test + final ShoppingListSyncer shoppingListSyncer = + ShoppingListSyncer.of( + mock(ProjectApiRoot.class), mock(ProjectApiRoot.class), mock(Clock.class)); + + // assertion + assertThat(shoppingListSyncer).isNotNull(); + assertThat(shoppingListSyncer.getSync()).isInstanceOf(ShoppingListSync.class); + } + + @Test + void transform_ShouldReplaceShoppingListReferenceIdsWithKeys() { + // preparation + final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class); + final ShoppingListSyncer shoppingListSyncer = + ShoppingListSyncer.of(sourceClient, mock(ProjectApiRoot.class), mock(Clock.class)); + final List shoppingList = + List.of(readObjectFromResource("shopping-list.json", ShoppingList.class)); + + mockResourceIdsGraphQlRequest( + sourceClient, "shoppingLists", "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey"); + + // test + final CompletionStage> draftsFromPageStage = + shoppingListSyncer.transform(shoppingList); + + // assertion + assertThat(draftsFromPageStage) + .isCompletedWithValue( + ShoppingListTransformUtils.toShoppingListDrafts( + sourceClient, referenceIdToKeyCache, shoppingList) + .join()); + } + + @Test + void getQuery_ShouldBuildShoppingListQuery() { + final ProjectApiRoot apiRoot = + ApiRootBuilder.of().withApiBaseUrl("baseUrl").build("testProjectKey"); + final ShoppingListSyncer shoppingListSyncer = + ShoppingListSyncer.of(apiRoot, apiRoot, mock(Clock.class)); + + // assertion + final ByProjectKeyShoppingListsGet shoppingListsGet = shoppingListSyncer.getQuery(); + + assertThat(shoppingListsGet.getExpand()).containsExactly("lineItems[*].variant"); + } + + @Test + void syncWithError_ShouldCallErrorCallback() { + // preparation: shoppingList with no key is synced + final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class); + final List shoppingLists = + List.of(readObjectFromResource("shopping-list-no-key.json", ShoppingList.class)); + final ByProjectKeyShoppingListsRequestBuilder projectKeyShoppingListsRequestBuilder = mock(); + when(sourceClient.shoppingLists()).thenReturn(projectKeyShoppingListsRequestBuilder); + final ByProjectKeyShoppingListsGet shoppingListsGet = mock(); + when(shoppingListsGet.addExpand(anyString())).thenReturn(shoppingListsGet); + when(shoppingListsGet.withLimit(anyInt())).thenReturn(shoppingListsGet); + when(shoppingListsGet.withWithTotal(anyBoolean())).thenReturn(shoppingListsGet); + when(shoppingListsGet.withSort(anyString())).thenReturn(shoppingListsGet); + + final ShoppingListPagedQueryResponse shoppingListPagedQueryResponse = + ShoppingListPagedQueryResponseBuilder.of() + .results(shoppingLists) + .limit(20L) + .offset(0L) + .count(1L) + .build(); + final ApiHttpResponse apiHttpResponse = mock(ApiHttpResponse.class); + when(apiHttpResponse.getBody()).thenReturn(shoppingListPagedQueryResponse); + when(shoppingListsGet.execute()).thenReturn(CompletableFuture.completedFuture(apiHttpResponse)); + when(sourceClient.shoppingLists().get()).thenReturn(shoppingListsGet); + + mockResourceIdsGraphQlRequest( + sourceClient, "shoppingLists", "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey"); + + // test + final ShoppingListSyncer shoppingListSyncer = + ShoppingListSyncer.of(sourceClient, mock(ProjectApiRoot.class), mock(Clock.class)); + shoppingListSyncer.sync(null, true).toCompletableFuture().join(); + + // assertion + final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1); + assertThat(errorLog.getMessage()) + .isEqualTo( + "Error when trying to sync shoppingList. Existing key: <>. Update actions: []"); + assertThat(errorLog.getThrowable().get().getMessage()) + .isEqualTo( + format( + "ShoppingListDraft with name: %s doesn't have a key. Please make sure all shopping list drafts have keys" + + ".", + shoppingLists.get(0).getName().toString())); + } }