Skip to content

Commit 5d7b45f

Browse files
committed
DEVX-813: adding tests for CustomerUtil and fixing a bug with addresses
1 parent c1cb1e9 commit 5d7b45f

4 files changed

Lines changed: 98 additions & 4 deletions

File tree

commercetools/commercetools-importapi-utils/src/main/java/com/commercetools/sdk/CustomerUtil.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.commercetools.sdk;
33

44
import java.util.List;
5+
import java.util.Objects;
56

67
import com.commercetools.api.models.common.Address;
78
import com.commercetools.api.models.customer.Customer;
@@ -47,8 +48,8 @@ public CustomerImport toCustomerImport(Customer customer) {
4748
.customerGroup(toCustomerGroupKeyReference(customer.getCustomerGroup()))
4849
.addresses(mapToCustomerAddresses(customer.getAddresses()))
4950
.defaultBillingAddress(getAddressesId(customer.getAddresses(), customer.getDefaultBillingAddressId()))
50-
.billingAddresses(getAddressesIds(customer.getBillingAddresses()))
51-
.shippingAddresses(getAddressesIds(customer.getShippingAddresses()))
51+
.billingAddresses(getAddressesIds(customer.getAddresses(), customer.getBillingAddresses()))
52+
.shippingAddresses(getAddressesIds(customer.getAddresses(), customer.getShippingAddresses()))
5253
.defaultShippingAddress(getAddressesId(customer.getAddresses(), customer.getDefaultShippingAddressId()))
5354
.locale(customer.getLocale())
5455
.custom(util.getImportApiCustom(customer.getCustom()))
@@ -76,8 +77,8 @@ private List<StoreKeyReference> toImportApiStoreKeyReferences(
7677
return stores.stream().map(x -> StoreKeyReference.builder().key(x.getKey()).build()).toList();
7778
}
7879

79-
private List<Integer> getAddressesIds(List<Address> shippingAddresses) {
80-
return shippingAddresses.stream().map(a -> getAddressesId(shippingAddresses, a.getId())).toList();
80+
private List<Integer> getAddressesIds(List<Address> addresses, List<Address> shippingAddresses) {
81+
return shippingAddresses.stream().map(a -> getAddressesId(addresses, a.getId())).filter(Objects::nonNull).toList();
8182
}
8283

8384
private Integer getAddressesId(List<Address> addresses, String addressId) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.commercetools.sdk;
2+
3+
import com.commercetools.api.models.customer.Customer;
4+
import io.vrap.rmf.base.client.utils.json.JsonUtils;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.List;
8+
9+
import static com.commercetools.sdk.TestUtils.stringFromResource;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class CustomerUtilTest {
13+
private final CustomerUtil util = new CustomerUtil();
14+
15+
@Test
16+
void shouldMapRequiredFields() {
17+
var customer = JsonUtils.fromJsonString(stringFromResource("src/test/resources/customer.example.json"), Customer.class);
18+
var result = util.toCustomerImport(customer);
19+
20+
assertEquals("customer-key", result.getKey());
21+
assertEquals("test@example.com", result.getEmail());
22+
}
23+
24+
@Test
25+
void shouldMapCustomerGroupKeyFromExpandedObj() {
26+
var customer = JsonUtils.fromJsonString(stringFromResource("src/test/resources/customer.example.json"), Customer.class);
27+
var result = util.toCustomerImport(customer);
28+
29+
assertNotNull(result.getCustomerGroup());
30+
assertEquals("vip-group", result.getCustomerGroup().getKey()); // catches getId() bug
31+
}
32+
33+
@Test
34+
void shouldMapAddressIndices() {
35+
var customer = JsonUtils.fromJsonString(stringFromResource("src/test/resources/customer.example.json"), Customer.class);
36+
var result = util.toCustomerImport(customer);
37+
38+
// addr-0 is index 0, addr-1 is index 1
39+
assertEquals(0, result.getDefaultBillingAddress());
40+
assertEquals(1, result.getDefaultShippingAddress());
41+
assertEquals(List.of(0), result.getBillingAddresses());
42+
assertEquals(List.of(1), result.getShippingAddresses());
43+
}
44+
45+
@Test
46+
void shouldMapAddresses() {
47+
var customer = JsonUtils.fromJsonString(stringFromResource("src/test/resources/customer.example.json"), Customer.class);
48+
var result = util.toCustomerImport(customer);
49+
50+
assertEquals(2, result.getAddresses().size());
51+
assertEquals("DE", result.getAddresses().get(0).getCountry());
52+
}
53+
54+
@Test
55+
void shouldHandleNullCustomerGroup() {
56+
var customer = JsonUtils.fromJsonString(stringFromResource("src/test/resources/customer.no-group.json"), Customer.class);
57+
var result = util.toCustomerImport(customer);
58+
assertNull(result.getCustomerGroup());
59+
}
60+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"id": "cust-id-001", "version": 1, "key": "customer-key",
3+
"email": "test@example.com", "password": "hashed",
4+
"addresses": [
5+
{ "id": "addr-0", "key": "home", "country": "DE" },
6+
{ "id": "addr-1", "key": "work", "country": "US" }
7+
],
8+
"defaultBillingAddressId": "addr-0",
9+
"defaultShippingAddressId": "addr-1",
10+
"billingAddressIds": ["addr-0"],
11+
"shippingAddressIds": ["addr-1"],
12+
"customerGroup": {
13+
"typeId": "customer-group", "id": "cg-id-001",
14+
"obj": { "id": "cg-id-001", "version": 1, "key": "vip-group",
15+
"name": "VIP", "createdAt": "1970-01-01T00:00:00.001Z", "lastModifiedAt": "1970-01-01T00:00:00.001Z" }
16+
},
17+
"isEmailVerified": true, "stores": [], "authenticationMode": "Password",
18+
"createdAt": "1970-01-01T00:00:00.001Z", "lastModifiedAt": "1970-01-01T00:00:00.001Z"
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "cust-id-001", "version": 1, "key": "customer-key",
3+
"email": "test@example.com", "password": "hashed",
4+
"addresses": [
5+
{ "id": "addr-0", "key": "home", "country": "DE" },
6+
{ "id": "addr-1", "key": "work", "country": "US" }
7+
],
8+
"defaultBillingAddressId": "addr-0",
9+
"defaultShippingAddressId": "addr-1",
10+
"billingAddressIds": ["addr-0"],
11+
"shippingAddressIds": ["addr-1"],
12+
"isEmailVerified": true, "stores": [], "authenticationMode": "Password",
13+
"createdAt": "1970-01-01T00:00:00.001Z", "lastModifiedAt": "1970-01-01T00:00:00.001Z"
14+
}

0 commit comments

Comments
 (0)