Skip to content

Commit 549e5ca

Browse files
authored
Add integration tests of CustomerSync. (#596)
1 parent 92fb312 commit 549e5ca

5 files changed

Lines changed: 598 additions & 10 deletions

File tree

src/integration-test/java/com/commercetools/sync/integration/commons/utils/CustomerGroupITUtils.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,9 @@
1111
import javax.annotation.Nonnull;
1212

1313
import static com.commercetools.sync.integration.commons.utils.ITUtils.queryAndExecute;
14-
import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT;
15-
import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT;
1614
import static com.commercetools.tests.utils.CompletionStageUtil.executeBlocking;
1715

1816
public final class CustomerGroupITUtils {
19-
/**
20-
* Deletes all CustomerGroup from CTP projects defined by the {@code CTP_SOURCE_CLIENT} and
21-
* {@code CTP_TARGET_CLIENT}.
22-
*/
23-
public static void deleteCustomerGroupsFromTargetAndSource() {
24-
deleteCustomerGroups(CTP_TARGET_CLIENT);
25-
deleteCustomerGroups(CTP_SOURCE_CLIENT);
26-
}
2717

2818
/**
2919
* Deletes all CustomerGroups from the CTP project defined by the {@code ctpClient}.

src/integration-test/java/com/commercetools/sync/integration/commons/utils/CustomerITUtils.java

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
package com.commercetools.sync.integration.commons.utils;
22

3+
import com.neovisionaries.i18n.CountryCode;
34
import io.sphere.sdk.client.SphereClient;
5+
import io.sphere.sdk.customergroups.CustomerGroup;
6+
import io.sphere.sdk.customers.Customer;
7+
import io.sphere.sdk.customers.CustomerDraft;
8+
import io.sphere.sdk.customers.CustomerDraftBuilder;
9+
import io.sphere.sdk.customers.CustomerSignInResult;
10+
import io.sphere.sdk.customers.commands.CustomerCreateCommand;
411
import io.sphere.sdk.customers.commands.CustomerDeleteCommand;
512
import io.sphere.sdk.customers.queries.CustomerQuery;
13+
import io.sphere.sdk.models.Address;
14+
import io.sphere.sdk.models.ResourceIdentifier;
15+
import io.sphere.sdk.stores.Store;
16+
import io.sphere.sdk.types.CustomFieldsDraft;
17+
import io.sphere.sdk.types.ResourceTypeIdsSetBuilder;
18+
import io.sphere.sdk.types.Type;
19+
import org.apache.commons.lang3.tuple.ImmutablePair;
620

721
import javax.annotation.Nonnull;
22+
import java.time.LocalDate;
23+
import java.util.Locale;
824

25+
import static com.commercetools.sync.integration.commons.utils.CustomerGroupITUtils.createCustomerGroup;
26+
import static com.commercetools.sync.integration.commons.utils.CustomerGroupITUtils.deleteCustomerGroups;
27+
import static com.commercetools.sync.integration.commons.utils.ITUtils.createTypeIfNotAlreadyExisting;
28+
import static com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes;
929
import static com.commercetools.sync.integration.commons.utils.ITUtils.queryAndExecute;
30+
import static com.commercetools.sync.integration.commons.utils.StoreITUtils.createStore;
31+
import static com.commercetools.sync.integration.commons.utils.StoreITUtils.deleteStores;
32+
import static com.commercetools.tests.utils.CompletionStageUtil.executeBlocking;
33+
import static java.util.Arrays.asList;
34+
import static java.util.Collections.emptyMap;
35+
import static java.util.Collections.singletonList;
1036

1137
public final class CustomerITUtils {
1238

39+
/**
40+
* Deletes all customers, types, stores and customer groups from the CTP project defined by the {@code ctpClient}.
41+
*
42+
* @param ctpClient defines the CTP project to delete the customer sync test data.
43+
*/
44+
public static void deleteCustomerSyncTestData(@Nonnull final SphereClient ctpClient) {
45+
deleteCustomers(ctpClient);
46+
deleteTypes(ctpClient);
47+
deleteStores(ctpClient);
48+
deleteCustomerGroups(ctpClient);
49+
}
50+
1351
/**
1452
* Deletes all customers from CTP project, represented by provided {@code ctpClient}.
1553
*
@@ -19,6 +57,118 @@ public static void deleteCustomers(@Nonnull final SphereClient ctpClient) {
1957
queryAndExecute(ctpClient, CustomerQuery.of(), CustomerDeleteCommand::of);
2058
}
2159

60+
public static ImmutablePair<Customer, CustomerDraft> createSampleCustomerJohnDoe(
61+
@Nonnull final SphereClient ctpClient) {
62+
63+
final Store storeBerlin = createStore(ctpClient, "store-berlin");
64+
final Store storeHamburg = createStore(ctpClient, "store-hamburg");
65+
final Store storeMunich = createStore(ctpClient, "store-munich");
66+
67+
final Type customTypeGoldMember = createCustomerCustomType("customer-type-gold", Locale.ENGLISH,
68+
"gold customers", ctpClient);
69+
70+
final CustomerGroup customerGroupGoldMembers = createCustomerGroup(ctpClient, "gold members", "gold");
71+
72+
final CustomerDraft customerDraftJohnDoe = CustomerDraftBuilder
73+
.of("john@example.com", "12345")
74+
.customerNumber("gold-1")
75+
.key("customer-key-john-doe")
76+
.stores(asList(
77+
ResourceIdentifier.ofKey(storeBerlin.getKey()),
78+
ResourceIdentifier.ofKey(storeHamburg.getKey()),
79+
ResourceIdentifier.ofKey(storeMunich.getKey())))
80+
.firstName("John")
81+
.lastName("Doe")
82+
.middleName("Jr")
83+
.title("Mr")
84+
.salutation("Dear")
85+
.dateOfBirth(LocalDate.now().minusYears(28))
86+
.companyName("Acme Corporation")
87+
.vatId("DE999999999")
88+
.emailVerified(true)
89+
.customerGroup(ResourceIdentifier.ofKey(customerGroupGoldMembers.getKey()))
90+
.addresses(asList(
91+
Address.of(CountryCode.DE).withCity("berlin").withKey("address1"),
92+
Address.of(CountryCode.DE).withCity("hamburg").withKey("address2"),
93+
Address.of(CountryCode.DE).withCity("munich").withKey("address3")))
94+
.defaultBillingAddress(0)
95+
.billingAddresses(asList(0, 1))
96+
.defaultShippingAddress(2)
97+
.shippingAddresses(singletonList(2))
98+
.custom(CustomFieldsDraft.ofTypeKeyAndJson(customTypeGoldMember.getKey(), emptyMap()))
99+
.locale(Locale.ENGLISH)
100+
.build();
101+
102+
final Customer customer = createCustomer(ctpClient, customerDraftJohnDoe);
103+
return ImmutablePair.of(customer, customerDraftJohnDoe);
104+
}
105+
106+
public static void createSampleCustomerJaneDoe(@Nonnull final SphereClient ctpClient) {
107+
final CustomerDraft customerDraftJaneDoe = CustomerDraftBuilder
108+
.of("jane@example.com", "12345")
109+
.customerNumber("random-1")
110+
.key("customer-key-jane-doe")
111+
.firstName("Jane")
112+
.lastName("Doe")
113+
.middleName("Jr")
114+
.title("Miss")
115+
.salutation("Dear")
116+
.dateOfBirth(LocalDate.now().minusYears(25))
117+
.companyName("Acme Corporation")
118+
.vatId("FR000000000")
119+
.emailVerified(false)
120+
.addresses(asList(
121+
Address.of(CountryCode.DE).withCity("cologne").withKey("address1"),
122+
Address.of(CountryCode.DE).withCity("berlin").withKey("address2")))
123+
.defaultBillingAddress(0)
124+
.billingAddresses(singletonList(0))
125+
.defaultShippingAddress(1)
126+
.shippingAddresses(singletonList(1))
127+
.locale(Locale.ENGLISH)
128+
.build();
129+
130+
createCustomer(ctpClient, customerDraftJaneDoe);
131+
}
132+
133+
/**
134+
* Creates a {@link Customer} in the CTP project defined by the {@code ctpClient} in a blocking fashion.
135+
*
136+
* @param ctpClient defines the CTP project to create the CustomerGroup in.
137+
* @param customerDraft the draft of the customer to create.
138+
* @return the created customer.
139+
*/
140+
public static Customer createCustomer(
141+
@Nonnull final SphereClient ctpClient,
142+
@Nonnull final CustomerDraft customerDraft) {
143+
144+
final CustomerSignInResult customerSignInResult = executeBlocking(
145+
ctpClient.execute(CustomerCreateCommand.of(customerDraft)));
146+
return customerSignInResult.getCustomer();
147+
}
148+
149+
/**
150+
* This method blocks to create a customer custom type on the CTP project defined by the supplied
151+
* {@code ctpClient}, with the supplied data.
152+
*
153+
* @param typeKey the type key
154+
* @param locale the locale to be used for specifying the type name and field definitions names.
155+
* @param name the name of the custom type.
156+
* @param ctpClient defines the CTP project to create the type on.
157+
*/
158+
public static Type createCustomerCustomType(
159+
@Nonnull final String typeKey,
160+
@Nonnull final Locale locale,
161+
@Nonnull final String name,
162+
@Nonnull final SphereClient ctpClient) {
163+
164+
return createTypeIfNotAlreadyExisting(
165+
typeKey,
166+
locale,
167+
name,
168+
ResourceTypeIdsSetBuilder.of().addCustomers(),
169+
ctpClient);
170+
}
171+
22172
private CustomerITUtils() {
23173
}
24174
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.commercetools.sync.integration.commons.utils;
2+
3+
import io.sphere.sdk.client.SphereClient;
4+
import io.sphere.sdk.stores.Store;
5+
import io.sphere.sdk.stores.StoreDraft;
6+
import io.sphere.sdk.stores.StoreDraftBuilder;
7+
import io.sphere.sdk.stores.commands.StoreCreateCommand;
8+
import io.sphere.sdk.stores.commands.StoreDeleteCommand;
9+
import io.sphere.sdk.stores.queries.StoreQuery;
10+
11+
import javax.annotation.Nonnull;
12+
13+
import static com.commercetools.sync.integration.commons.utils.ITUtils.queryAndExecute;
14+
import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT;
15+
import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT;
16+
import static com.commercetools.tests.utils.CompletionStageUtil.executeBlocking;
17+
18+
public final class StoreITUtils {
19+
/**
20+
* Deletes all stores from CTP projects defined by the {@code CTP_SOURCE_CLIENT} and
21+
* {@code CTP_TARGET_CLIENT}.
22+
*/
23+
public static void deleteStoresFromTargetAndSource() {
24+
deleteStores(CTP_TARGET_CLIENT);
25+
deleteStores(CTP_SOURCE_CLIENT);
26+
}
27+
28+
/**
29+
* Deletes all stores from the CTP project defined by the {@code ctpClient}.
30+
*
31+
* @param ctpClient defines the CTP project to delete the stores from.
32+
*/
33+
public static void deleteStores(@Nonnull final SphereClient ctpClient) {
34+
queryAndExecute(ctpClient, StoreQuery.of(), StoreDeleteCommand::of);
35+
}
36+
37+
/**
38+
* Creates a {@link Store} in the CTP project defined by the {@code ctpClient} in a blocking fashion.
39+
*
40+
* @param ctpClient defines the CTP project to create the Store in.
41+
* @param key the key of the Store to create.
42+
* @return the created store.
43+
*/
44+
public static Store createStore(@Nonnull final SphereClient ctpClient, @Nonnull final String key) {
45+
final StoreDraft storeDraft = StoreDraftBuilder.of(key).build();
46+
return executeBlocking(ctpClient.execute(StoreCreateCommand.of(storeDraft)));
47+
}
48+
49+
private StoreITUtils() {
50+
}
51+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.commercetools.sync.integration.ctpprojectsource.customers;
2+
3+
import com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics;
4+
import com.commercetools.sync.customers.CustomerSync;
5+
import com.commercetools.sync.customers.CustomerSyncOptions;
6+
import com.commercetools.sync.customers.CustomerSyncOptionsBuilder;
7+
import com.commercetools.sync.customers.helpers.CustomerSyncStatistics;
8+
import com.neovisionaries.i18n.CountryCode;
9+
import io.sphere.sdk.customers.Customer;
10+
import io.sphere.sdk.customers.CustomerDraft;
11+
import io.sphere.sdk.customers.CustomerDraftBuilder;
12+
import io.sphere.sdk.models.Address;
13+
import io.sphere.sdk.models.ResourceIdentifier;
14+
import io.sphere.sdk.stores.Store;
15+
import io.sphere.sdk.types.CustomFieldsDraft;
16+
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Test;
19+
20+
import javax.annotation.Nonnull;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
25+
import static com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat;
26+
import static com.commercetools.sync.customers.utils.CustomerReferenceResolutionUtils.buildCustomerQuery;
27+
import static com.commercetools.sync.customers.utils.CustomerReferenceResolutionUtils.mapToCustomerDrafts;
28+
import static com.commercetools.sync.integration.commons.utils.CustomerITUtils.createSampleCustomerJaneDoe;
29+
import static com.commercetools.sync.integration.commons.utils.CustomerITUtils.createSampleCustomerJohnDoe;
30+
import static com.commercetools.sync.integration.commons.utils.CustomerITUtils.deleteCustomerSyncTestData;
31+
import static com.commercetools.sync.integration.commons.utils.ITUtils.createCustomFieldsJsonMap;
32+
import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT;
33+
import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT;
34+
import static com.commercetools.sync.integration.commons.utils.StoreITUtils.createStore;
35+
import static java.util.Collections.singletonList;
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
class CustomerSyncIT {
39+
private List<String> errorMessages;
40+
private List<Throwable> exceptions;
41+
private CustomerSync customerSync;
42+
43+
@BeforeEach
44+
void setup() {
45+
deleteCustomerSyncTestDataFromProjects();
46+
47+
createSampleCustomerJohnDoe(CTP_SOURCE_CLIENT);
48+
createSampleCustomerJaneDoe(CTP_SOURCE_CLIENT);
49+
50+
createSampleCustomerJohnDoe(CTP_TARGET_CLIENT);
51+
52+
setUpCustomerSync();
53+
}
54+
55+
@AfterAll
56+
static void tearDown() {
57+
deleteCustomerSyncTestDataFromProjects();
58+
}
59+
60+
private static void deleteCustomerSyncTestDataFromProjects() {
61+
deleteCustomerSyncTestData(CTP_SOURCE_CLIENT);
62+
deleteCustomerSyncTestData(CTP_TARGET_CLIENT);
63+
}
64+
65+
private void setUpCustomerSync() {
66+
errorMessages = new ArrayList<>();
67+
exceptions = new ArrayList<>();
68+
final CustomerSyncOptions customerSyncOptions = CustomerSyncOptionsBuilder
69+
.of(CTP_TARGET_CLIENT)
70+
.errorCallback((exception, oldResource, newResource, actions) -> {
71+
errorMessages.add(exception.getMessage());
72+
exceptions.add(exception);
73+
})
74+
.build();
75+
customerSync = new CustomerSync(customerSyncOptions);
76+
}
77+
78+
@Test
79+
void sync_WithoutUpdates_ShouldReturnProperStatistics() {
80+
81+
final List<Customer> customers = CTP_SOURCE_CLIENT
82+
.execute(buildCustomerQuery())
83+
.toCompletableFuture()
84+
.join()
85+
.getResults();
86+
87+
final List<CustomerDraft> customerDrafts = mapToCustomerDrafts(customers);
88+
89+
final CustomerSyncStatistics customerSyncStatistics = customerSync
90+
.sync(customerDrafts)
91+
.toCompletableFuture()
92+
.join();
93+
94+
assertThat(errorMessages).isEmpty();
95+
assertThat(exceptions).isEmpty();
96+
97+
assertThat(customerSyncStatistics).hasValues(2, 1, 0, 0);
98+
assertThat(customerSyncStatistics
99+
.getReportMessage())
100+
.isEqualTo("Summary: 2 customers were processed in total (1 created, 0 updated and 0 failed to sync).");
101+
}
102+
103+
@Test
104+
void sync_WithUpdates_ShouldReturnProperStatistics() {
105+
106+
final List<Customer> customers = CTP_SOURCE_CLIENT
107+
.execute(buildCustomerQuery())
108+
.toCompletableFuture()
109+
.join()
110+
.getResults();
111+
112+
final List<CustomerDraft> updatedCustomerDrafts = prepareUpdatedCustomerDrafts(customers);
113+
final CustomerSyncStatistics customerSyncStatistics = customerSync
114+
.sync(updatedCustomerDrafts)
115+
.toCompletableFuture()
116+
.join();
117+
118+
assertThat(errorMessages).isEmpty();
119+
assertThat(exceptions).isEmpty();
120+
121+
AssertionsForStatistics.assertThat(customerSyncStatistics).hasValues(2, 1, 1, 0);
122+
assertThat(customerSyncStatistics
123+
.getReportMessage())
124+
.isEqualTo("Summary: 2 customers were processed in total (1 created, 1 updated and 0 failed to sync).");
125+
}
126+
127+
private List<CustomerDraft> prepareUpdatedCustomerDrafts(@Nonnull final List<Customer> customers) {
128+
129+
final Store storeCologne = createStore(CTP_TARGET_CLIENT, "store-cologne");
130+
131+
return mapToCustomerDrafts(customers)
132+
.stream()
133+
.map(customerDraft ->
134+
CustomerDraftBuilder
135+
.of(customerDraft)
136+
.plusStores(ResourceIdentifier.ofKey(storeCologne.getKey()))
137+
.custom(CustomFieldsDraft.ofTypeKeyAndJson("customer-type-gold",
138+
createCustomFieldsJsonMap()))
139+
.addresses(singletonList(Address.of(CountryCode.DE).withCity("cologne").withKey("address1")))
140+
.defaultBillingAddress(0)
141+
.billingAddresses(singletonList(0))
142+
.defaultShippingAddress(0)
143+
.shippingAddresses(singletonList(0))
144+
.build())
145+
.collect(Collectors.toList());
146+
}
147+
}

0 commit comments

Comments
 (0)