private Map<String, List<Category>> getChildrenByParentIdMap(final List<Category> allCategoriesAsFlatList) { final Map<String, List<Category>> childrenByParentId = new HashMap<>(); allCategoriesAsFlatList.forEach(category -> Optional.ofNullable(category.getParent()).ifPresent(parentReference -> { final String parentId = parentReference.getId(); final List<Category> entries = childrenByParentId.getOrDefault(parentId, new LinkedList<>()); entries.add(category); childrenByParentId.put(parentId, entries); }) ); return childrenByParentId; }
@Override public Reference<ProductDiscount> toReference() { return Reference.of(ProductDiscount.referenceTypeId(), this); } }
@SuppressWarnings("rawtypes")//at runtime generic type is not determinable @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Reference reference = (Reference) o; if (!getId().equals(reference.getId())) return false; if (!getTypeId().equals(reference.getTypeId())) return false; return true; }
@Override public void serialize(final Reference reference, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeStartObject(); jsonGenerator.writeStringField("id", reference.getId()); jsonGenerator.writeStringField("typeId", reference.getTypeId()); jsonGenerator.writeObjectField("obj", reference.getObj()); jsonGenerator.writeEndObject(); } }
@Override public boolean matches(final T value) { return value.getObj() != null; } };
@Override public JsonNode serialize(final T t) { return mapper().valueToTree(t.toReference().filled(null)); } }
@Test public void referencesAreNotByDefaultExpanded() { withCustomerInGroup(client(), (customer, customerGroup) -> { final CustomerByIdGet customerByIdGet = CustomerByIdGet.of(customer); final Customer loadedCustomer = client().executeBlocking(customerByIdGet); final Reference<CustomerGroup> customerGroupReference = loadedCustomer.getCustomerGroup(); assertThat(customerGroupReference.getId()).isEqualTo(customerGroup.getId()); assertThat(customerGroupReference.getObj()) .as("reference is not expanded") .isNull(); }); }
public Reference<T> filled(final T obj) { return filled(Optional.ofNullable(obj)); }
@Test public void implementEquals() throws Exception { assertThat(newFilledReference()).isEqualTo(newFilledReference()); assertThat(newEmptyReference()).isEqualTo(newEmptyReference()); assertThat(newFilledReference()).isEqualTo(newEmptyReference()); assertThat(newFilledReference()).isEqualTo(newEmptyReference().filled(new TestEntity("other value"))); assertThat(Reference.<String>of(typeId, "Foo", "x")).isNotEqualTo(newEmptyReference()); assertThat(Reference.<String>ofResourceTypeIdAndIdAndObj(typeId, "Foo", "x")).isNotEqualTo(newEmptyReference()); }
@Test public void attributeShouldKeepExpansions() throws Exception { final AttributeAccess<Reference<Product>> access = AttributeAccess.ofProductReference(); final NamedAttributeAccess<Reference<Product>> namedAccess = access.ofName("foo"); assertThat(EXPANDED_PRODUCT_REFERENCE.getObj()).overridingErrorMessage("product reference is expanded").isNotNull(); final Attribute attribute = Attribute.of(namedAccess, EXPANDED_PRODUCT_REFERENCE); assertThat(attribute.getValue(access).getObj()).isNotNull(); final String jsonFilledRef = SphereJsonUtils.toJsonString(EXPANDED_PRODUCT_REFERENCE); final String jsonEmptyRef = SphereJsonUtils.toJsonString(EXPANDED_PRODUCT_REFERENCE.filled(null)); assertThat(jsonFilledRef) .overridingErrorMessage("references are not expanded if serialized") .doesNotContain(EXPANDED_PRODUCT_REFERENCE.getObj().getMasterData().getStaged().getName().get(Locale.ENGLISH)) .isEqualTo(jsonEmptyRef); }
public static <T> Reference<T> of(final String typeId, final String id) { return new Reference<>(typeId, id, Optional.empty()); }
@Test public void createAttributeFromReferenceSet(){ final Product product = SphereJsonUtils.readObjectFromResource("product1.json", Product.class); final Reference<Product> expandedReference = Reference.ofResourceTypeIdAndIdAndObj(Product.referenceTypeId(), UUID.randomUUID().toString(), product); final HashSet<Reference<Product>> references = new HashSet<>(); references.add(expandedReference); final Attribute attribute = Attribute.of("attrName", AttributeAccess.ofProductReferenceSet(), references); assertThat(attribute.getValue(AttributeAccess.ofProductReferenceSet()).stream().allMatch(productReference -> productReference.getObj() != null)).isTrue(); }
public static CustomFieldsDraft ofCustomFields(final CustomFields custom) { return CustomFieldsDraft.ofTypeIdAndJson(custom.getType().getTypeId(), custom.getFieldsJsonMap()); }
@Test public void setCustomerGroup() throws Exception { withB2cCustomerGroup(client(), customerGroup -> { final Cart cart = createCartWithCountry(client()); assertThat(cart.getCustomerGroup()).isNull(); Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, SetCustomerGroup.of(customerGroup))); assertThat(updatedCart.getCustomerGroup().toReference()).isEqualTo(customerGroup.toReference()); client().executeBlocking(CartDeleteCommand.of(updatedCart)); }); }