public void demo() { ProductVariant variant = getProductVariantSomeHow(); Optional<LocalizedString> longDescription = variant.findAttribute(TShirt.LONG_DESCRIPTION); } }
@Test(expected = JsonException.class) public void wrongAttributeType() throws Exception { variant.findAttribute(wrongTypeNamedAttributeAccess); }
@Test public void localizedString() throws Exception { assertThat(variant.findAttribute(localizedStringNamedAttributeAccess).get()). isEqualTo(LocalizedString.of(GERMAN, "val-loc-string-de", ENGLISH, "val-loc-string-en")); }
private Condition<ProductProjection> productWithMatchingVariantsHavingMaxSize() { final Predicate<ProductProjection> matchingVariantsHaveMaxSize = product -> { final long productMaximumSize = product.getAllVariants().stream() .mapToLong(variant -> variant.findAttribute(SIZE_ATTRIBUTE_ACCESS).orElse(0L)) .max() .getAsLong(); return product.findMatchingVariants().stream() .mapToLong(variant -> variant.findAttribute(SIZE_ATTRIBUTE_ACCESS).get()) .allMatch(size -> size == productMaximumSize); }; return new Condition<>(matchingVariantsHaveMaxSize, "all matching variants have maximum size"); }
@Test public void attributeNotFound() throws Exception { assertThat(variant.findAttribute(notPresentNamedAttributeAccess)).isEmpty(); }
@Test public void getterWithAttributeAccess() throws Exception { assertThat(variant.findAttribute(LOC_STRING_ATTRIBUTE, ofLocalizedString()).get()). isEqualTo(LocalizedString.of(GERMAN, "val-loc-string-de", ENGLISH, "val-loc-string-en")); }
@Test public void productProjection() throws Exception { assertThat(productProjection.getMasterVariant().findAttribute(localizedStringNamedAttributeAccess).get()). isEqualTo(LocalizedString.of(GERMAN, "val-loc-string-de", ENGLISH, "val-loc-string-en")); }
public void demo() { ProductVariant variant = getProductVariantSomeHow(); Optional<LocalizedString> longDescription = variant.findAttribute(TShirt.longDescription()); } }
@Test public void getterWithJsonAttributeAccess() throws Exception { final JsonNode actual = variant.findAttribute(LOC_STRING_ATTRIBUTE, ofJsonNode()).get(); final JsonNode expected = SphereJsonUtils.parse("{\"de\":\"val-loc-string-de\",\"en\":\"val-loc-string-en\"}"); assertThat(actual).isEqualTo(expected); }
public void demo() { ProductVariant variant = getProductVariantSomeHow(); Optional<LocalizedString> longDescription = variant.findAttribute(TShirt.attributes().longDescription()); } }
@Test public void readAttributeGetValueAsWithWrongType() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final Throwable throwable = catchThrowable( () -> masterVariant.findAttribute(SIZE_ATTR_NAME) .map((Attribute a) -> a.getValueAsBoolean()) .orElse(true) ); assertThat(throwable).isInstanceOf(JsonException.class); }
@Test public void readAttributeGetValueAs() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final String attributeValue = masterVariant.findAttribute(SIZE_ATTR_NAME) .map((Attribute a) -> { final EnumValue enumValue = a.getValueAsEnumValue(); return enumValue.getLabel(); }) .orElse("not found"); assertThat(attributeValue).isEqualTo("S"); }
private Optional<LocalizedEnumValue> extractAttribute(final Product product, final NamedAttributeAccess<LocalizedEnumValue> namesAccess) { return product.getMasterData().getStaged().getMasterVariant().findAttribute(namesAccess); } }
@Test public void readAttributeWithoutProductTypeWithNamedAccessWithWrongType() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); assertThatThrownBy(() -> masterVariant.findAttribute(SIZE_ATTR_NAME, AttributeAccess.ofBoolean())) .isInstanceOf(JsonException.class); }
@Test public void notPresentAttributeRead() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final Optional<Boolean> attributeOption = masterVariant.findAttribute("notpresent", AttributeAccess.ofBoolean()); assertThat(attributeOption).isEmpty(); }
@Test public void readAttributeWithoutProductTypeWithName() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final Optional<EnumValue> attributeOption = masterVariant.findAttribute(SIZE_ATTR_NAME, AttributeAccess.ofEnumValue()); assertThat(attributeOption).contains(EnumValue.of("S", "S")); }
@Test public void readAttributeWithoutProductTypeWithNamedAccess() throws Exception { final NamedAttributeAccess<EnumValue> size = AttributeAccess.ofEnumValue().ofName(SIZE_ATTR_NAME); final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final Optional<EnumValue> attributeOption = masterVariant.findAttribute(size); assertThat(attributeOption).contains(EnumValue.of("S", "S")); }
@Test public void expandProductReferencesInProductAttributes() throws Exception { withProductWithProductReference(client(), (product, referencedProduct) -> { final Query<ProductProjection> query = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.id().is(product.getId())) .withExpansionPaths(m -> m.masterVariant().attributes().value()) .toQuery(); final ProductProjection productProjection = client().executeBlocking(query).head().get(); final NamedAttributeAccess<Reference<Product>> namedAttributeAccess = AttributeAccess.ofProductReference().ofName("productreference"); final Reference<Product> productReference = productProjection.getMasterVariant().findAttribute(namedAttributeAccess).get(); final Product expandedReferencedProduct = productReference.getObj(); assertThat(expandedReferencedProduct.getId()).isEqualTo(referencedProduct.getId()); }); }
@Test public void updateAttributesBooks() throws Exception { final Product product = createBookProduct(); final int masterVariantId = 1; final AttributeDraft attributeDraft = AttributeDraft.of(ISBN_ATTR_NAME, "978-3-86680-192-8"); final SetAttribute updateAction = SetAttribute.of(masterVariantId, attributeDraft); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, updateAction)); final ProductVariant masterVariant = updatedProduct.getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.findAttribute(ISBN_ATTR_NAME, AttributeAccess.ofText())) .contains("978-3-86680-192-8"); }
public Product createBookProduct() throws Exception { final ProductType productType = client().executeBlocking(ProductTypeQuery.of().byName(BOOK_PRODUCT_TYPE_NAME)).head().get(); final ProductVariantDraft masterVariantDraft = ProductVariantDraftBuilder.of() .plusAttribute(ISBN_ATTR_NAME, "978-3-86680-192-9") .build(); final ProductDraft draft = ProductDraftBuilder .of(productType, en("a book"), randomSlug(), masterVariantDraft) .build(); final Product product = client().executeBlocking(ProductCreateCommand.of(draft)); final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.findAttribute(ISBN_ATTR_NAME, AttributeAccess.ofText())) .contains("978-3-86680-192-9"); return product; }