private static Map<String, Category> buildByIdMap(Collection<Category> categories) { final Map<String, Category> map = new HashMap<>(categories.size()); for (Category c: categories) { map.put(c.getId(), c); } return map; }
private static Map<String, Category> buildByIdMap(Collection<Category> categories) { final Map<String, Category> map = new HashMap<>(categories.size()); for (Category c: categories) { map.put(c.getId(), c); } return map; }
private static Map<String, Category> buildByIdMap(Collection<Category> categories) { final Map<String, Category> map = new HashMap<>(categories.size()); for (Category c: categories) { map.put(c.getId(), c); } return map; }
@Override public List<Category> findSiblings(final Collection<? extends Identifiable<Category>> categoryIdentifiables) { return categoryIdentifiables.stream() .flatMap(category -> getSiblings(CategoryTreeUtils.getCategoryOrThrow(category, this)).stream()) .distinct() .filter(sibling -> !categoryIdentifiables.stream().anyMatch(c -> sibling.getId().equals(c.getId()))) .collect(toList()); }
@Override public List<Category> findSiblings(final Collection<? extends Identifiable<Category>> categoryIdentifiables) { return categoryIdentifiables.stream() .flatMap(category -> getSiblings(CategoryTreeUtils.getCategoryOrThrow(category, this)).stream()) .distinct() .filter(sibling -> !categoryIdentifiables.stream().anyMatch(c -> sibling.getId().equals(c.getId()))) .collect(toList()); }
@Override default Reference<Category> toReference() { return Reference.of(referenceTypeId(), getId(), this); }
@Override default Reference<Category> toReference() { return Reference.of(referenceTypeId(), getId(), this); }
private List<String> getCategoryIds(final String ... names) { return Arrays.stream(names) .map(name -> categoryTree.findByExternalId(name).get().getId()) .collect(Collectors.toList()); }
@Test public void searchBySlug() throws Exception { final CategoryTree tree = CategoryTree.of(createCategoryHierarchyAsFlatList()); assertThat(tree.findBySlug(locale, "slug-0bu").get().getId()).isEqualTo("0bu"); final Locale absentLocale = Locale.GERMAN; assertThat(tree.findBySlug(absentLocale, "slug-0bu")).isEqualTo(Optional.empty()); }
@Test public void searchById() throws Exception { final CategoryTree tree = CategoryTree.of(createCategoryHierarchyAsFlatList()); assertThat(tree.findById("0bu").get().getId()).isEqualTo("0bu"); assertThat(tree.findById("not-present")).isEqualTo(Optional.empty()); }
@Test public void toReference() { final Category category = getCategory1(); final Reference<Category> categoryReference = category.toReference(); assertThat(category.getId()).isEqualTo(categoryReference.getId()); }
@Test public void setCategoryOrderHint() throws Exception { withProductInCategory(client(), (product, category) -> { final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, SetCategoryOrderHint.of(category.getId(), "0.1234"))); final CategoryOrderHints actual = updatedProduct.getMasterData().getStaged().getCategoryOrderHints(); assertThat(actual).isEqualTo(CategoryOrderHints.of(category.getId(), "0.1234")); assertThat(actual.getAsMap()).isEqualTo(Collections.singletonMap(category.getId(), "0.1234")); }); }
@Test public void queryByName() throws Exception { withCategory(client(), category -> { final LocalizedStringEntry name = category.getName().stream().findAny().get(); final Query<Category> query = CategoryQuery.of().byName(name.getLocale(), name.getValue()); assertThat(client().executeBlocking(query).head().get().getId()).isEqualTo(category.getId()); }); }
@Test public void getSubtreeFromRootCategory() { final CategoryTree tree = createAbcCategoryTree(); final Category category = tree.findById("B").get(); final CategoryTree subtree = tree.getSubtree(singletonList(category)); assertThat(subtree.getAllAsFlatList()).hasSize(13); assertThat(subtree.findById(category.getId())).isPresent(); assertThat(subtree.getRoots()).hasSize(1); assertThat(subtree.getSubtreeRoots()).isEqualTo(subtree.getRoots()); assertThat(subtree.findById("B-1")).isPresent(); assertThat(subtree.findById("C-1")).isEmpty(); }
@Test public void execution() throws Exception { CategoryFixtures.withCategoryAndParentCategory(client(), (category, parent) -> { final String id = category.getId(); final Get<Category> fetch = CategoryByIdGet.of(id) .withExpansionPaths(m -> m.parent()); final Category loadedCategory = client().executeBlocking(fetch); assertThat(loadedCategory.getId()).isEqualTo(id); assertThat(loadedCategory.getParent()).is(expanded(parent)); }); } }
public static CategoryBuilder of(final Category category) { return new CategoryBuilder(category.getId(), category.getName(), category.getSlug()). version(category.getVersion()).createdAt(category.getCreatedAt()). lastModifiedAt(category.getLastModifiedAt()). name(category.getName()).slug(category.getSlug()).description(category.getDescription()). ancestors(category.getAncestors()).parent(category.getParent()). orderHint(category.getOrderHint()).children(category.getChildren()). pathInTree(category.getPathInTree()); }
public static CategoryBuilder of(final Category category) { return new CategoryBuilder(category.getId(), category.getName(), category.getSlug()). version(category.getVersion()).createdAt(category.getCreatedAt()). lastModifiedAt(category.getLastModifiedAt()). name(category.getName()).slug(category.getSlug()).description(category.getDescription()). ancestors(category.getAncestors()).parent(category.getParent()). orderHint(category.getOrderHint()).externalId(category.getExternalId()).children(category.getChildren()). pathInTree(category.getPathInTree()); }
public static CategoryBuilder of(final Category category) { return new CategoryBuilder(category.getId(), category.getName(), category.getSlug()). version(category.getVersion()).createdAt(category.getCreatedAt()). lastModifiedAt(category.getLastModifiedAt()). name(category.getName()).slug(category.getSlug()).description(category.getDescription()). ancestors(category.getAncestors()).parent(category.getParent()). orderHint(category.getOrderHint()).externalId(category.getExternalId()).children(category.getChildren()). pathInTree(category.getPathInTree()); }
@Test public void executionForDeletionByKey() throws Exception { final String key = randomKey(); final CategoryDraft draft = CategoryDraftBuilder.of(randomLocalizedString(), randomSlug()).key(key).build(); final Category category = client().executeBlocking(CategoryCreateCommand.of(draft)); client().executeBlocking(CategoryDeleteCommand.ofKey(key,category.getVersion())); assertThat(client().executeBlocking(CategoryQuery.of().byId(category.getId())).head()).isEmpty(); }
public static CategoryBuilder of(final Category category) { return new CategoryBuilder(category.getId(), category.getName(), category.getSlug()). version(category.getVersion()).createdAt(category.getCreatedAt()). lastModifiedAt(category.getLastModifiedAt()). name(category.getName()).slug(category.getSlug()).description(category.getDescription()). ancestors(category.getAncestors()).parent(category.getParent()).assets(category.getAssets()). orderHint(category.getOrderHint()).externalId(category.getExternalId()); }