Skip to content

Commit f923bb4

Browse files
fix: create tests for the new java-sync
1 parent 45d9c35 commit f923bb4

6 files changed

Lines changed: 292 additions & 0 deletions

File tree

src/test/java/com/commercetools/project/sync/customer/CustomerSyncerTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,59 @@ void syncWithWarning_ShouldCallWarningCallback() {
142142
"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.");
143143
}
144144

145+
@Test
146+
void transform_WithAdditionalAddressInfoAndState_ShouldPreserveAddressFields() {
147+
// preparation
148+
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
149+
final CustomerSyncer customerSyncer =
150+
CustomerSyncer.of(sourceClient, mock(ProjectApiRoot.class), mock(Clock.class));
151+
final List<Customer> customers =
152+
Collections.singletonList(
153+
readObjectFromResource("customer-with-state-reference.json", Customer.class));
154+
155+
// test
156+
final CompletionStage<List<CustomerDraft>> draftsFromPageStage =
157+
customerSyncer.transform(customers);
158+
159+
// assertion
160+
final List<CustomerDraft> customerDrafts = draftsFromPageStage.toCompletableFuture().join();
161+
assertThat(customerDrafts).isNotEmpty();
162+
assertThat(customerDrafts.get(0).getAddresses()).isNotEmpty();
163+
assertThat(customerDrafts.get(0).getAddresses().get(0).getState()).isEqualTo("New York");
164+
assertThat(customerDrafts.get(0).getAddresses().get(0).getAdditionalAddressInfo())
165+
.isEqualTo("Building B, Floor 5");
166+
}
167+
168+
@Test
169+
void transform_WithMultipleAddressesWithStateAndAdditionalInfo_ShouldPreserveAllAddressFields() {
170+
// preparation
171+
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
172+
final CustomerSyncer customerSyncer =
173+
CustomerSyncer.of(sourceClient, mock(ProjectApiRoot.class), mock(Clock.class));
174+
final List<Customer> customers =
175+
Collections.singletonList(
176+
readObjectFromResource("customer-with-multiple-addresses.json", Customer.class));
177+
178+
// test
179+
final CompletionStage<List<CustomerDraft>> draftsFromPageStage =
180+
customerSyncer.transform(customers);
181+
182+
// assertion
183+
final List<CustomerDraft> customerDrafts = draftsFromPageStage.toCompletableFuture().join();
184+
assertThat(customerDrafts).isNotEmpty();
185+
assertThat(customerDrafts.get(0).getAddresses()).hasSize(2);
186+
187+
// Verify first address
188+
assertThat(customerDrafts.get(0).getAddresses().get(0).getState()).isEqualTo("California");
189+
assertThat(customerDrafts.get(0).getAddresses().get(0).getAdditionalAddressInfo())
190+
.isEqualTo("Ring doorbell twice");
191+
192+
// Verify second address
193+
assertThat(customerDrafts.get(0).getAddresses().get(1).getState()).isEqualTo("California");
194+
assertThat(customerDrafts.get(0).getAddresses().get(1).getAdditionalAddressInfo())
195+
.isEqualTo("Suite 300, Reception on 3rd floor");
196+
}
197+
145198
private void mockProjectApiRootGetRequest(
146199
final ProjectApiRoot projectApiRoot, final List<Customer> results) {
147200
final ByProjectKeyCustomersRequestBuilder byProjectKeyCustomersRequestBuilder = mock();

src/test/java/com/commercetools/project/sync/shoppinglist/ShoppingListSyncerTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,58 @@ void syncWithError_ShouldCallErrorCallback() {
139139
+ ".",
140140
shoppingLists.get(0).getName().toString()));
141141
}
142+
143+
@Test
144+
void transform_WithStoreReference_ShouldReplaceStoreReferenceIdsWithKeys() {
145+
// preparation
146+
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
147+
final ShoppingListSyncer shoppingListSyncer =
148+
ShoppingListSyncer.of(sourceClient, mock(ProjectApiRoot.class), mock(Clock.class));
149+
final List<ShoppingList> shoppingLists =
150+
List.of(readObjectFromResource("shopping-list-with-store.json", ShoppingList.class));
151+
152+
// Mock the store reference resolution
153+
mockResourceIdsGraphQlRequest(
154+
sourceClient, "stores", "store-id-456", "store-key-456");
155+
mockResourceIdsGraphQlRequest(
156+
sourceClient, "shoppingLists", "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey");
157+
158+
// test
159+
final CompletionStage<List<ShoppingListDraft>> draftsFromPageStage =
160+
shoppingListSyncer.transform(shoppingLists);
161+
162+
// assertion
163+
assertThat(draftsFromPageStage)
164+
.isCompletedWithValue(
165+
ShoppingListTransformUtils.toShoppingListDrafts(
166+
sourceClient, referenceIdToKeyCache, shoppingLists)
167+
.join());
168+
}
169+
170+
@Test
171+
void transform_WithMultipleStoreReferences_ShouldResolveAllStoreReferences() {
172+
// preparation
173+
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
174+
final ShoppingListSyncer shoppingListSyncer =
175+
ShoppingListSyncer.of(sourceClient, mock(ProjectApiRoot.class), mock(Clock.class));
176+
final List<ShoppingList> shoppingLists =
177+
List.of(
178+
readObjectFromResource("shopping-list-with-multiple-stores.json", ShoppingList.class));
179+
180+
// Mock multiple store reference resolutions
181+
mockResourceIdsGraphQlRequest(sourceClient, "stores", "store-id-1", "store-key-1");
182+
mockResourceIdsGraphQlRequest(sourceClient, "stores", "store-id-2", "store-key-2");
183+
mockResourceIdsGraphQlRequest(
184+
sourceClient, "shoppingLists", "5ebfa80e-f4aa-4c0b-be64-e348e09a855a", "customTypeKey");
185+
186+
// test
187+
final CompletionStage<List<ShoppingListDraft>> draftsFromPageStage =
188+
shoppingListSyncer.transform(shoppingLists);
189+
190+
// assertion
191+
final List<ShoppingListDraft> shoppingListDrafts =
192+
draftsFromPageStage.toCompletableFuture().join();
193+
assertThat(shoppingListDrafts).isNotEmpty();
194+
assertThat(shoppingListDrafts.get(0).getStore()).isNotNull();
195+
}
142196
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"key": "customerMultipleAddresses",
3+
"customerNumber": "2",
4+
"email": "customer2@example.com",
5+
"firstName": "Jane",
6+
"lastName": "Smith",
7+
"password": "xxx",
8+
"addresses": [
9+
{
10+
"key": "homeAddress",
11+
"id": "addr001",
12+
"firstName": "Jane",
13+
"lastName": "Smith",
14+
"streetName": "Oak Avenue",
15+
"streetNumber": "456",
16+
"postalCode": "67890",
17+
"city": "Los Angeles",
18+
"region": "CA",
19+
"state": "California",
20+
"country": "US",
21+
"additionalAddressInfo": "Ring doorbell twice"
22+
},
23+
{
24+
"key": "officeAddress",
25+
"id": "addr002",
26+
"firstName": "Jane",
27+
"lastName": "Smith",
28+
"streetName": "Business Blvd",
29+
"streetNumber": "789",
30+
"postalCode": "11111",
31+
"city": "San Francisco",
32+
"region": "CA",
33+
"state": "California",
34+
"country": "US",
35+
"additionalAddressInfo": "Suite 300, Reception on 3rd floor"
36+
}
37+
],
38+
"shippingAddressIds": ["addr001"],
39+
"billingAddressIds": ["addr002"],
40+
"isEmailVerified": true,
41+
"stores": []
42+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"key": "customerWithAdditionalAddressInfo",
3+
"customerNumber": "1",
4+
"email": "customer@example.com",
5+
"firstName": "John",
6+
"lastName": "Doe",
7+
"password": "xxx",
8+
"addresses": [
9+
{
10+
"key": "addressKey",
11+
"id": "addr123",
12+
"firstName": "John",
13+
"lastName": "Doe",
14+
"streetName": "Main Street",
15+
"streetNumber": "123",
16+
"postalCode": "12345",
17+
"city": "New York",
18+
"region": "NY",
19+
"state": "New York",
20+
"country": "US",
21+
"additionalAddressInfo": "Building B, Floor 5"
22+
}
23+
],
24+
"shippingAddressIds": [],
25+
"billingAddressIds": [],
26+
"isEmailVerified": false,
27+
"stores": []
28+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"key": "shoppingListMultipleStores",
3+
"slug": {
4+
"en": "shopping-list-multi-store"
5+
},
6+
"name": {
7+
"en": "Shopping List with Multiple Store References"
8+
},
9+
"description": {
10+
"en": "A shopping list with store reference"
11+
},
12+
"store": {
13+
"typeId": "store",
14+
"id": "store-id-1"
15+
},
16+
"customer": {
17+
"typeId": "customer",
18+
"id": "customer-id-789"
19+
},
20+
"lineItems": [
21+
{
22+
"id": "line-item-1",
23+
"productId": "product-id-1",
24+
"name": {
25+
"en": "Product One"
26+
},
27+
"productType": {
28+
"typeId": "product-type",
29+
"id": "product-type-id-1"
30+
},
31+
"variantId": 1,
32+
"quantity": 3,
33+
"addedAt": "2020-10-16T07:52:56.960Z"
34+
},
35+
{
36+
"id": "line-item-2",
37+
"productId": "product-id-2",
38+
"name": {
39+
"en": "Product Two"
40+
},
41+
"productType": {
42+
"typeId": "product-type",
43+
"id": "product-type-id-2"
44+
},
45+
"variantId": 1,
46+
"quantity": 2,
47+
"addedAt": "2020-10-16T08:00:00.000Z"
48+
}
49+
],
50+
"textLineItems": [
51+
{
52+
"id": "text-line-item-1",
53+
"name": {
54+
"en": "Additional Note"
55+
},
56+
"quantity": 1,
57+
"addedAt": "2020-10-16T09:57:12.335Z"
58+
}
59+
],
60+
"custom": {
61+
"type": {
62+
"typeId": "type",
63+
"id": "5ebfa80e-f4aa-4c0b-be64-e348e09a855a"
64+
},
65+
"fields": {
66+
"textField": "multi-store shopping list"
67+
}
68+
}
69+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"key": "shoppingListWithStore",
3+
"slug": {
4+
"en": "shopping-list-store-1"
5+
},
6+
"name": {
7+
"en": "Shopping List with Store"
8+
},
9+
"description": {
10+
"en": "A shopping list assigned to a store"
11+
},
12+
"store": {
13+
"typeId": "store",
14+
"id": "store-id-456"
15+
},
16+
"customer": {
17+
"typeId": "customer",
18+
"id": "368ddf13-cd6c-4ff4-ad38-73038aeed5ba"
19+
},
20+
"lineItems": [
21+
{
22+
"id": "5f456090-a1b6-48b2-9e63-522b7560a0c2",
23+
"productId": "130a7d5c-55ea-4888-abbe-f76d474de351",
24+
"name": {
25+
"en": "Test Product"
26+
},
27+
"productType": {
28+
"typeId": "product-type",
29+
"id": "eb6ef3da-40a9-4370-8252-33c7b657a016"
30+
},
31+
"variantId": 1,
32+
"quantity": 5,
33+
"addedAt": "2020-10-16T07:52:56.960Z"
34+
}
35+
],
36+
"textLineItems": [],
37+
"custom": {
38+
"type": {
39+
"typeId": "type",
40+
"id": "5ebfa80e-f4aa-4c0b-be64-e348e09a855a"
41+
},
42+
"fields": {
43+
"textField": "store shopping list"
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)