11package com .commercetools .sync .integration .commons .utils ;
22
3+ import com .neovisionaries .i18n .CountryCode ;
34import 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 ;
411import io .sphere .sdk .customers .commands .CustomerDeleteCommand ;
512import 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
721import 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 ;
929import 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
1137public 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}
0 commit comments