Skip to content

Commit 85f79e2

Browse files
committed
fix NPE in category tree factory
Closes #1033
1 parent 6ac355c commit 85f79e2

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/category/CategoryTreeFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private Map<LocalizedStringEntry, Category> getLocalizedStringEntryCategoryMap(
6464
.values()
6565
.keySet()
6666
.stream()
67-
.map(Locale::new)
67+
.map(Locale::forLanguageTag)
6868
.collect(Collectors.toSet());
6969
localesForTheCategory.forEach(locale -> {
7070
final LocalizedStringEntry stringsEntry = LocalizedStringEntry.of(locale,

commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/CategoriesTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,64 @@ public void testCategoryTree() {
6666
var result = tree.findBySlug(Locale.ENGLISH, "women");
6767
Assertions.assertThat(result).isNotEmpty();
6868
}
69+
70+
@Test
71+
public void testGetLocalizedStringEntryCategoryMap()
72+
{
73+
List<Category> allCategoriesAsFlatList = List.of(
74+
Category.builder()
75+
.name(LocalizedString.of(Locale.forLanguageTag("en-US"), "Women"))
76+
.slug(LocalizedString.of(Locale.forLanguageTag("en-US"), "women"))
77+
.id("1")
78+
.version(1L)
79+
.createdAt(ZonedDateTime.now())
80+
.lastModifiedAt(ZonedDateTime.now())
81+
.ancestors()
82+
.orderHint("c2")
83+
.build(),
84+
Category.builder()
85+
.name(LocalizedString.of(Locale.forLanguageTag("en"), "Men"))
86+
.slug(LocalizedString.of(Locale.forLanguageTag("en"), "men"))
87+
.id("2")
88+
.version(1L)
89+
.createdAt(ZonedDateTime.now())
90+
.lastModifiedAt(ZonedDateTime.now())
91+
.ancestors()
92+
.orderHint("c2")
93+
.build(),
94+
Category.builder()
95+
.name(LocalizedString.of(Locale.forLanguageTag("en-us"), "Kids"))
96+
.slug(LocalizedString.of(Locale.forLanguageTag("en-us"), "kids"))
97+
.id("3")
98+
.version(1L)
99+
.createdAt(ZonedDateTime.now())
100+
.lastModifiedAt(ZonedDateTime.now())
101+
.ancestors()
102+
.orderHint("c2")
103+
.build());
104+
CategoryTreeFactory factory = CategoryTreeFactory.of();
105+
106+
var tree = factory.create(allCategoriesAsFlatList);
107+
var result = tree.findBySlug(Locale.forLanguageTag("en-US"), "women");
108+
Assertions.assertThat(result).isNotEmpty();
109+
var enUsResult = tree.findBySlug(Locale.forLanguageTag("en-us"), "women");
110+
Assertions.assertThat(enUsResult).isNotEmpty();
111+
112+
var enResult = tree.findBySlug(Locale.forLanguageTag("en"), "women");
113+
Assertions.assertThat(enResult).isEmpty();
114+
115+
var menResult = tree.findBySlug(Locale.forLanguageTag("en-US"), "men");
116+
Assertions.assertThat(menResult).isEmpty();
117+
var menEnUsResult = tree.findBySlug(Locale.forLanguageTag("en-us"), "men");
118+
Assertions.assertThat(menEnUsResult).isEmpty();
119+
var menEnResult = tree.findBySlug(Locale.forLanguageTag("en"), "men");
120+
Assertions.assertThat(menEnResult).isNotEmpty();
121+
122+
var kidsResult = tree.findBySlug(Locale.forLanguageTag("en-US"), "kids");
123+
Assertions.assertThat(kidsResult).isNotEmpty();
124+
var kidsEnUsResult = tree.findBySlug(Locale.forLanguageTag("en-us"), "kids");
125+
Assertions.assertThat(kidsEnUsResult).isNotEmpty();
126+
var kidsEnResult = tree.findBySlug(Locale.forLanguageTag("en"), "kids");
127+
Assertions.assertThat(kidsEnResult).isEmpty();
128+
}
69129
}

0 commit comments

Comments
 (0)