Skip to content

Commit fb9d93d

Browse files
committed
DEVX-813: adding tests for CustomerUtil
1 parent 6f0cea2 commit fb9d93d

4 files changed

Lines changed: 103 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import com.commercetools.importapi.models.customers.AuthenticationMode;
1212
import com.commercetools.importapi.models.customers.CustomerAddress;
1313
import com.commercetools.importapi.models.customers.CustomerImport;
14-
15-
import org.jetbrains.annotations.NotNull;
14+
import jakarta.validation.constraints.NotNull;
1615

1716
public class CustomerUtil {
1817
private final KeyResolverService keyResolverService;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.commercetools.sdk;
2+
3+
import com.commercetools.api.models.category.Category;
4+
import com.commercetools.api.models.category.CategoryImpl;
5+
import io.vrap.rmf.base.client.utils.json.JsonUtils;
6+
import org.junit.jupiter.api.Test;
7+
import tools.jackson.databind.ObjectMapper;
8+
import tools.jackson.databind.json.JsonMapper;
9+
10+
import static com.commercetools.sdk.TestUtils.stringFromResource;
11+
import static org.junit.jupiter.api.Assertions.*;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
public class CategoryUtilTest {
15+
String categoryProjectionExample = "src/test/resources/category.example.json";
16+
private final ObjectMapper objectMapper = new JsonMapper();
17+
CategoryUtil util = new CategoryUtil();
18+
19+
@Test
20+
void shouldDeserializeCategoryCorrectly() throws Exception {
21+
var testCategory = JsonUtils.fromJsonString(stringFromResource(categoryProjectionExample),
22+
Category.class);
23+
24+
assertNotNull(testCategory, "The category object should not be null.");
25+
assertInstanceOf(CategoryImpl.class, testCategory,
26+
"The category should be an instance of Category based on the annotation.");
27+
}
28+
@Test
29+
void shouldMapRequiredFields() {
30+
var category = JsonUtils.fromJsonString(stringFromResource(categoryProjectionExample), Category.class);
31+
var result = util.toCategoryImport(category);
32+
33+
assertEquals("category-key", result.getKey());
34+
assertEquals("My Category", result.getName().values().get("en"));
35+
assertEquals("my-category", result.getSlug().values().get("en"));
36+
}
37+
38+
@Test
39+
void shouldMapParentKeyFromExpandedObj() {
40+
var category = JsonUtils.fromJsonString(stringFromResource(categoryProjectionExample), Category.class);
41+
var result = util.toCategoryImport(category);
42+
43+
assertNotNull(result.getParent());
44+
assertEquals("parent-key", result.getParent().getKey()); // key, not UUID
45+
}
46+
47+
@Test
48+
void shouldMapExternalId() {
49+
var category = JsonUtils.fromJsonString(stringFromResource(categoryProjectionExample), Category.class);
50+
var result = util.toCategoryImport(category);
51+
assertEquals("ext-001", result.getExternalId()); // catches the getId() bug
52+
}
53+
54+
@Test
55+
void shouldHandleNullParent() {
56+
// fixture without parent field
57+
var category = JsonUtils.fromJsonString(stringFromResource("src/test/resources/category.no-parent.json"), Category.class);
58+
var result = util.toCategoryImport(category);
59+
assertNull(result.getParent());
60+
}
61+
62+
@Test
63+
void shouldHandleNullCustom() {
64+
var category = JsonUtils.fromJsonString(stringFromResource(categoryProjectionExample), Category.class);
65+
assertDoesNotThrow(() -> util.toCategoryImport(category)); // no NPE when custom is absent
66+
}
67+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"id": "cat-id-001",
3+
"version": 1,
4+
"key": "category-key",
5+
"name": { "en": "My Category" },
6+
"slug": { "en": "my-category" },
7+
"description": { "en": "A description" },
8+
"orderHint": "0.5",
9+
"externalId": "ext-001",
10+
"parent": {
11+
"typeId": "category",
12+
"id": "parent-id-001",
13+
"obj": { "id": "parent-id-001", "version": 1, "key": "parent-key",
14+
"name": { "en": "Parent" }, "slug": { "en": "parent" },
15+
"createdAt": "1970-01-01T00:00:00.001Z", "lastModifiedAt": "1970-01-01T00:00:00.001Z" }
16+
},
17+
"ancestors": [],
18+
"assets": [],
19+
"createdAt": "1970-01-01T00:00:00.001Z",
20+
"lastModifiedAt": "1970-01-01T00:00:00.001Z"
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "cat-id-001",
3+
"version": 1,
4+
"key": "category-key",
5+
"name": { "en": "My Category" },
6+
"slug": { "en": "my-category" },
7+
"description": { "en": "A description" },
8+
"orderHint": "0.5",
9+
"externalId": "ext-001",
10+
"ancestors": [],
11+
"assets": [],
12+
"createdAt": "1970-01-01T00:00:00.001Z",
13+
"lastModifiedAt": "1970-01-01T00:00:00.001Z"
14+
}

0 commit comments

Comments
 (0)