-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
salander85
committed
Oct 12, 2023
1 parent
060af6f
commit 195b925
Showing
14 changed files
with
3,438 additions
and
3,727 deletions.
There are no files selected for viewing
2,015 changes: 993 additions & 1,022 deletions
2,015
src/test/java/com/commercetools/project/sync/CliRunnerTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
2,519 changes: 1,241 additions & 1,278 deletions
2,519
src/test/java/com/commercetools/project/sync/SyncerFactoryTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
332 changes: 154 additions & 178 deletions
332
src/test/java/com/commercetools/project/sync/category/CategorySyncerTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,13 @@ | ||
package com.commercetools.project.sync.customer; | ||
|
||
import static com.commercetools.sync.customers.utils.CustomerTransformUtils.toCustomerDrafts; | ||
import static io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest; | ||
import com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult; | ||
import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl; | ||
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache; | ||
import com.commercetools.sync.customers.CustomerSync; | ||
import io.sphere.sdk.client.SphereApiConfig; | ||
import io.sphere.sdk.client.SphereClient; | ||
import io.sphere.sdk.customers.Customer; | ||
import io.sphere.sdk.customers.CustomerDraft; | ||
import io.sphere.sdk.customers.queries.CustomerQuery; | ||
import io.sphere.sdk.json.SphereJsonUtils; | ||
import io.sphere.sdk.queries.PagedQueryResult; | ||
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); | ||
|
||
|
@@ -41,119 +19,126 @@ 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<Customer> 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<List<CustomerDraft>> 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<Customer> customers = | ||
Collections.singletonList(readObjectFromResource("customer-no-key.json", Customer.class)); | ||
|
||
final PagedQueryResult<Customer> 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: <<not present>>. Update actions: []"); | ||
assertThat(errorLog.getThrowable().get().getMessage()) | ||
.isEqualTo( | ||
"CustomerDraft with email: [email protected] 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<Customer> sourceCustomers = | ||
Collections.singletonList(readObjectFromResource("customer-id-1.json", Customer.class)); | ||
final List<Customer> targetCustomers = | ||
Collections.singletonList(readObjectFromResource("customer-id-2.json", Customer.class)); | ||
|
||
final PagedQueryResult<Customer> sourcePagedQueryResult = mock(PagedQueryResult.class); | ||
when(sourcePagedQueryResult.getResults()).thenReturn(sourceCustomers); | ||
when(sourceClient.execute(any(CustomerQuery.class))) | ||
.thenReturn(CompletableFuture.completedFuture(sourcePagedQueryResult)); | ||
|
||
final PagedQueryResult<Customer> 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(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<Customer> 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<List<CustomerDraft>> 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<Customer> customers = | ||
// Collections.singletonList(readObjectFromResource("customer-no-key.json", | ||
// Customer.class)); | ||
// | ||
// final PagedQueryResult<Customer> 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: <<not present>>. Update actions: | ||
// []"); | ||
// assertThat(errorLog.getThrowable().get().getMessage()) | ||
// .isEqualTo( | ||
// "CustomerDraft with email: [email protected] 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<Customer> sourceCustomers = | ||
// Collections.singletonList(readObjectFromResource("customer-id-1.json", Customer.class)); | ||
// final List<Customer> targetCustomers = | ||
// Collections.singletonList(readObjectFromResource("customer-id-2.json", Customer.class)); | ||
// | ||
// final PagedQueryResult<Customer> sourcePagedQueryResult = mock(PagedQueryResult.class); | ||
// when(sourcePagedQueryResult.getResults()).thenReturn(sourceCustomers); | ||
// when(sourceClient.execute(any(CustomerQuery.class))) | ||
// .thenReturn(CompletableFuture.completedFuture(sourcePagedQueryResult)); | ||
// | ||
// final PagedQueryResult<Customer> 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."); | ||
// } | ||
} |
Oops, something went wrong.