Skip to content

Commit

Permalink
Merge branch 'sync-java-migration-v10' into DEVX-244_Migrate-syncer-f…
Browse files Browse the repository at this point in the history
…actory
  • Loading branch information
salander85 committed Oct 12, 2023
2 parents bdf0aa0 + 195b925 commit 2d2a64b
Show file tree
Hide file tree
Showing 12 changed files with 1,204 additions and 1,427 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
package com.commercetools.project.sync.cartdiscount;

import static com.commercetools.project.sync.util.TestUtils.getMockedClock;
import static com.commercetools.project.sync.util.TestUtils.mockResourceIdsGraphQlRequest;
import static com.commercetools.sync.cartdiscounts.utils.CartDiscountTransformUtils.toCartDiscountDrafts;
import static io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource;
import static java.util.Arrays.asList;
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.cartdiscounts.CartDiscountSync;
import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl;
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
import io.sphere.sdk.cartdiscounts.CartDiscount;
import io.sphere.sdk.cartdiscounts.CartDiscountDraft;
import io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery;
import io.sphere.sdk.client.SphereApiConfig;
import io.sphere.sdk.client.SphereClient;
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 java.util.stream.Collectors;
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 CartDiscountSyncerTest {

final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();

@Test
/* @Test
void of_ShouldCreateCartDiscountSyncerInstance() {
// test
final CartDiscountSyncer cartDiscountSyncer =
Expand Down Expand Up @@ -122,5 +97,5 @@ void syncWithError_ShouldCallErrorCallback() {
assertThat(errorLog.getThrowable().get().getMessage())
.isEqualTo(
"CartDiscountDraft with name: LocalizedString(en -> 1-month prepay(Go Big)) doesn't have a key. Please make sure all cart discount drafts have keys.");
}
}*/
}

Large diffs are not rendered by default.

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);

Expand All @@ -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.");
// }
}
Loading

0 comments on commit 2d2a64b

Please sign in to comment.