From 6cb25160a4e327a6f64eaeae6539e66123d056f8 Mon Sep 17 00:00:00 2001 From: lojzatran Date: Thu, 26 Oct 2023 12:29:20 +0200 Subject: [PATCH] DEVX-251 migrate customer syncer --- .../project/sync/customer/CustomerSyncer.java | 2 - .../sync/customer/CustomerSyncerTest.java | 273 ++++++++++-------- 2 files changed, 149 insertions(+), 126 deletions(-) diff --git a/src/main/java/com/commercetools/project/sync/customer/CustomerSyncer.java b/src/main/java/com/commercetools/project/sync/customer/CustomerSyncer.java index 3931cc1e..e6ddfbd1 100644 --- a/src/main/java/com/commercetools/project/sync/customer/CustomerSyncer.java +++ b/src/main/java/com/commercetools/project/sync/customer/CustomerSyncer.java @@ -28,8 +28,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 CustomerSyncer extends Syncer< Customer, diff --git a/src/test/java/com/commercetools/project/sync/customer/CustomerSyncerTest.java b/src/test/java/com/commercetools/project/sync/customer/CustomerSyncerTest.java index 333f487f..0d91ac41 100644 --- a/src/test/java/com/commercetools/project/sync/customer/CustomerSyncerTest.java +++ b/src/test/java/com/commercetools/project/sync/customer/CustomerSyncerTest.java @@ -1,13 +1,35 @@ package com.commercetools.project.sync.customer; +import static com.commercetools.project.sync.util.TestUtils.mockResourceIdsGraphQlRequest; +import static com.commercetools.project.sync.util.TestUtils.readObjectFromResource; +import static com.commercetools.sync.customers.utils.CustomerTransformUtils.toCustomerDrafts; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.commercetools.api.client.ByProjectKeyCustomersGet; +import com.commercetools.api.client.ByProjectKeyCustomersRequestBuilder; +import com.commercetools.api.client.ProjectApiRoot; +import com.commercetools.api.models.customer.Customer; +import com.commercetools.api.models.customer.CustomerDraft; +import com.commercetools.api.models.customer.CustomerPagedQueryResponse; +import com.commercetools.api.models.customer.CustomerPagedQueryResponseBuilder; import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl; import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache; +import com.commercetools.sync.customers.CustomerSync; +import io.vrap.rmf.base.client.ApiHttpResponse; +import java.time.Clock; +import java.util.Collections; +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 CustomerSyncerTest { private final TestLogger syncerTestLogger = TestLoggerFactory.getTestLogger(CustomerSyncer.class); @@ -19,126 +41,129 @@ void setup() { syncerTestLogger.clearAll(); } - // @Test - // void of_ShouldCreateCustomerSyncerInstance() { - // // test - // final CustomerSyncer customerSyncer = - // CustomerSyncer.of(mock(SphereClient.class), mock(SphereClient.class), - // mock(Clock.class)); - // - // // assertion - // assertThat(customerSyncer).isNotNull(); - // assertThat(customerSyncer.getSync()).isInstanceOf(CustomerSync.class); - // } - // - // @Test - // void transform_ShouldReplaceCustomerReferenceIdsWithKeys() { - // // preparation - // final SphereClient sourceClient = mock(SphereClient.class); - // final CustomerSyncer customerSyncer = - // CustomerSyncer.of(sourceClient, mock(SphereClient.class), mock(Clock.class)); - // final List customers = - // Collections.singletonList(readObjectFromResource("customer-key-1.json", - // Customer.class)); - // - // final String jsonStringCustomerGroups = - // "{\"results\":[{\"id\":\"d1229e6f-2b79-441e-b419-180311e52754\"," - // + "\"key\":\"customerGroupKey\"} ]}"; - // final ResourceKeyIdGraphQlResult customerGroupsResult = - // SphereJsonUtils.readObject(jsonStringCustomerGroups, ResourceKeyIdGraphQlResult.class); - // - // when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))) - // .thenReturn(CompletableFuture.completedFuture(customerGroupsResult)); - // - // // test - // final CompletionStage> draftsFromPageStage = - // customerSyncer.transform(customers); - // - // // assertion - // assertThat(draftsFromPageStage) - // .isCompletedWithValue( - // toCustomerDrafts(sourceClient, referenceIdToKeyCache, customers).join()); - // } - // - // @Test - // void getQuery_ShouldBuildCustomerQuery() { - // // test - // final CustomerSyncer customerSyncer = - // CustomerSyncer.of(mock(SphereClient.class), mock(SphereClient.class), - // mock(Clock.class)); - // - // // assertion - // final CustomerQuery query = customerSyncer.getQuery(); - // assertThat(query).isEqualTo(CustomerQuery.of()); - // } - // - // @Test - // void syncWithError_ShouldCallErrorCallback() { - // // preparation: customer 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 customers = - // Collections.singletonList(readObjectFromResource("customer-no-key.json", - // Customer.class)); - // - // final PagedQueryResult pagedQueryResult = mock(PagedQueryResult.class); - // when(pagedQueryResult.getResults()).thenReturn(customers); - // when(sourceClient.execute(any(CustomerQuery.class))) - // .thenReturn(CompletableFuture.completedFuture(pagedQueryResult)); - // - // // test - // final CustomerSyncer customerSyncer = - // CustomerSyncer.of(sourceClient, targetClient, mock(Clock.class)); - // customerSyncer.sync(null, true).toCompletableFuture().join(); - // - // // assertion - // final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1); - // assertThat(errorLog.getMessage()) - // .isEqualTo( - // "Error when trying to sync customer. Existing key: <>. Update actions: - // []"); - // assertThat(errorLog.getThrowable().get().getMessage()) - // .isEqualTo( - // "CustomerDraft with email: email@email.com doesn't have a key. Please make sure all - // customer drafts have keys."); - // } - // - // @Test - // void syncWithWarning_ShouldCallWarningCallback() { - // // preparation: source customer has a different customer number than target customer - // 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 sourceCustomers = - // Collections.singletonList(readObjectFromResource("customer-id-1.json", Customer.class)); - // final List targetCustomers = - // Collections.singletonList(readObjectFromResource("customer-id-2.json", Customer.class)); - // - // final PagedQueryResult sourcePagedQueryResult = mock(PagedQueryResult.class); - // when(sourcePagedQueryResult.getResults()).thenReturn(sourceCustomers); - // when(sourceClient.execute(any(CustomerQuery.class))) - // .thenReturn(CompletableFuture.completedFuture(sourcePagedQueryResult)); - // - // final PagedQueryResult targetPagedQueryResult = mock(PagedQueryResult.class); - // when(targetPagedQueryResult.getResults()).thenReturn(targetCustomers); - // when(targetClient.execute(any(CustomerQuery.class))) - // .thenReturn(CompletableFuture.completedFuture(targetPagedQueryResult)); - // - // // test - // final CustomerSyncer customerSyncer = - // CustomerSyncer.of(sourceClient, targetClient, mock(Clock.class)); - // customerSyncer.sync(null, true).toCompletableFuture().join(); - // - // // assertion - // final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1); - // assertThat(errorLog.getMessage()) - // .isEqualTo("Warning when trying to sync customer. Existing key: customerKey"); - // assertThat(errorLog.getThrowable().get().getMessage()) - // .isEqualTo( - // "Customer with key: \"customerKey\" has already a customer number: \"2\", once it's - // set it cannot be changed. Hereby, the update action is not created."); - // } + @Test + void of_ShouldCreateCustomerSyncerInstance() { + // test + final CustomerSyncer customerSyncer = + CustomerSyncer.of( + mock(ProjectApiRoot.class), mock(ProjectApiRoot.class), mock(Clock.class)); + + // assertion + assertThat(customerSyncer).isNotNull(); + assertThat(customerSyncer.getSync()).isInstanceOf(CustomerSync.class); + } + + @Test + void transform_ShouldReplaceCustomerReferenceIdsWithKeys() { + // preparation + final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class); + final CustomerSyncer customerSyncer = + CustomerSyncer.of(sourceClient, mock(ProjectApiRoot.class), mock(Clock.class)); + final List customers = + Collections.singletonList(readObjectFromResource("customer-key-1.json", Customer.class)); + mockResourceIdsGraphQlRequest( + sourceClient, "customerGroups", "d1229e6f-2b79-441e-b419-180311e52754", "customerGroupKey"); + + // test + final CompletionStage> draftsFromPageStage = + customerSyncer.transform(customers); + + // assertion + assertThat(draftsFromPageStage) + .isCompletedWithValue( + toCustomerDrafts(sourceClient, referenceIdToKeyCache, customers).join()); + } + + @Test + void getQuery_ShouldBuildCustomerQuery() { + final ProjectApiRoot projectApiRoot = mock(ProjectApiRoot.class); + final ByProjectKeyCustomersRequestBuilder byProjectKeyCustomersRequestBuilder = mock(); + when(projectApiRoot.customers()).thenReturn(byProjectKeyCustomersRequestBuilder); + final ByProjectKeyCustomersGet byProjectKeyCustomersGet = mock(); + when(byProjectKeyCustomersRequestBuilder.get()).thenReturn(byProjectKeyCustomersGet); + + // test + final CustomerSyncer customerSyncer = + CustomerSyncer.of(projectApiRoot, mock(ProjectApiRoot.class), mock(Clock.class)); + + // assertion + assertThat(customerSyncer.getQuery()).isEqualTo(byProjectKeyCustomersGet); + } + + @Test + void syncWithError_ShouldCallErrorCallback() { + // preparation: customer with no key is synced + final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class); + final ProjectApiRoot targetClient = mock(ProjectApiRoot.class); + final List customers = + Collections.singletonList(readObjectFromResource("customer-no-key.json", Customer.class)); + mockProjectApiRootGetRequest(sourceClient, customers); + + // test + final CustomerSyncer customerSyncer = + CustomerSyncer.of(sourceClient, targetClient, mock(Clock.class)); + customerSyncer.sync(null, true).toCompletableFuture().join(); + + // assertion + final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1); + assertThat(errorLog.getMessage()) + .isEqualTo( + "Error when trying to sync customer. Existing key: <>. Update actions: []"); + assertThat(errorLog.getThrowable().get().getMessage()) + .isEqualTo( + "CustomerDraft with email: email@email.com doesn't have a key. Please make sure all customer drafts have keys."); + } + + @Test + void syncWithWarning_ShouldCallWarningCallback() { + // preparation: source customer has a different customer number than target customer + final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class); + final ProjectApiRoot targetClient = mock(ProjectApiRoot.class); + final List sourceCustomers = + Collections.singletonList(readObjectFromResource("customer-id-1.json", Customer.class)); + final List targetCustomers = + Collections.singletonList(readObjectFromResource("customer-id-2.json", Customer.class)); + + mockProjectApiRootGetRequest(sourceClient, sourceCustomers); + mockProjectApiRootGetRequest(targetClient, targetCustomers); + + // test + final CustomerSyncer customerSyncer = + CustomerSyncer.of(sourceClient, targetClient, mock(Clock.class)); + customerSyncer.sync(null, true).toCompletableFuture().join(); + + // assertion + final LoggingEvent errorLog = syncerTestLogger.getAllLoggingEvents().get(1); + assertThat(errorLog.getMessage()) + .isEqualTo("Warning when trying to sync customer. Existing key: customerKey"); + assertThat(errorLog.getThrowable().get().getMessage()) + .isEqualTo( + "Customer with key: \"customerKey\" has already a customer number: \"2\", once it's set it cannot be changed. Hereby, the update action is not created."); + } + + private void mockProjectApiRootGetRequest( + final ProjectApiRoot projectApiRoot, final List results) { + final ByProjectKeyCustomersRequestBuilder byProjectKeyCustomersRequestBuilder = mock(); + when(projectApiRoot.customers()).thenReturn(byProjectKeyCustomersRequestBuilder); + final ByProjectKeyCustomersGet byProjectKeyCustomersGet = mock(); + when(byProjectKeyCustomersRequestBuilder.get()).thenReturn(byProjectKeyCustomersGet); + when(byProjectKeyCustomersGet.withSort(anyString())).thenReturn(byProjectKeyCustomersGet); + when(byProjectKeyCustomersGet.withWithTotal(anyBoolean())).thenReturn(byProjectKeyCustomersGet); + when(byProjectKeyCustomersGet.withLimit(anyInt())).thenReturn(byProjectKeyCustomersGet); + when(byProjectKeyCustomersGet.withWhere(anyString())).thenReturn(byProjectKeyCustomersGet); + when(byProjectKeyCustomersGet.withPredicateVar(anyString(), any())) + .thenReturn(byProjectKeyCustomersGet); + + final ApiHttpResponse response = mock(ApiHttpResponse.class); + final CustomerPagedQueryResponse customerPagedQueryResponse = + CustomerPagedQueryResponseBuilder.of() + .results(results) + .limit(20L) + .offset(0L) + .count(1L) + .build(); + when(response.getBody()).thenReturn(customerPagedQueryResponse); + when(byProjectKeyCustomersGet.execute()) + .thenReturn(CompletableFuture.completedFuture(response)); + } }