|
| 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 | +} |
0 commit comments