@Override public LocalizedString getSlug() { return productData.getSlug(); }
@Override public LocalizedStrings getSlug() { return productData.getSlug(); }
@Override public LocalizedString getSlug() { return productData.getSlug(); }
@Override public LocalizedStrings getSlug() { return productData.getSlug(); }
@Override public LocalizedString getSlug() { return productData.getSlug(); }
public void changeSlugWithStaged(final Boolean staged) { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newSlug = LocalizedString.ofEnglish("new-slug-" + RANDOM.nextInt()); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, ChangeSlug.of(newSlug, staged))); assertThat(updatedProduct.getMasterData().getStaged().getSlug()).isEqualTo(newSlug); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
@Test public void addLineItem() throws Exception { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).isEmpty(); final long quantity = 3; final String productId = product.getId(); final AddLineItem action = AddLineItem.of(productId, MASTER_VARIANT_ID, quantity); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, action)); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); assertThat(lineItem.getProductSlug()).isEqualTo(product.getMasterData().getStaged().getSlug()); assertThat(lineItem.getVariant().getIdentifier()).isEqualTo(ByIdVariantIdentifier.of(lineItem.getProductId(), lineItem.getVariant().getId())); assertThat(lineItem.getDiscountedPricePerQuantity()).isNotNull().isEmpty(); }); }
@Test public void changeSlug() throws Exception { withUpdateableProduct(client(), product -> { final LocalizedString newSlug = LocalizedString.ofEnglish("new-slug-" + RANDOM.nextInt()); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, ChangeSlug.of(newSlug))); assertThat(updatedProduct.getMasterData().getStaged().getSlug()).isEqualTo(newSlug); //query message assertEventually(() -> { final Query<ProductSlugChangedMessage> query = MessageQuery.of() .withPredicates(m -> m.resource().is(product)) .forMessageType(ProductSlugChangedMessage.MESSAGE_HINT); final List<ProductSlugChangedMessage> results = client().executeBlocking(query).getResults(); assertThat(results).hasSize(1); final ProductSlugChangedMessage message = results.get(0); assertThat(message.getSlug()).isEqualTo(newSlug); }); return updatedProduct; }); }
@Test public void execution2() throws Exception { withProduct(client(), product -> { final String slug = product.getMasterData().getStaged().getSlug().get(Locale.ENGLISH); final ProductProjectionQuery normalSphereRequest = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.slug().locale(Locale.ENGLISH).is(slug)) .plusExpansionPaths(m -> m.productType()); final JsonNodeSphereRequest jsonNodeSphereRequest = JsonNodeSphereRequest.of(normalSphereRequest); assertThat(normalSphereRequest.httpRequestIntent()) .as("a JsonNodeSphereRequest creates the same request to the platform, but differs in the response") .isEqualTo(jsonNodeSphereRequest.httpRequestIntent()); final PagedQueryResult<ProductProjection> productProjectionPagedSearchResult = client().executeBlocking(normalSphereRequest); final JsonNode jsonNode = client().executeBlocking(jsonNodeSphereRequest);//all will be returned as JSON assertThat(jsonNode.get("results").get(0).get("productType").get("obj").get("description").asText()) .as("the expansion paths are honored") .isEqualTo("a 'T' shaped cloth"); }); } }
@Test public void addLineItemOfDraftOfSku() throws Exception { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).isEmpty(); final long quantity = 3; final Channel inventorySupplyChannel = ChannelFixtures.persistentChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY); final Channel distributionChannel = ChannelFixtures.persistentChannelOfRole(client(), ChannelRole.PRODUCT_DISTRIBUTION); final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final LineItemDraft lineItemDraft = io.sphere.sdk.carts.LineItemDraftBuilder.ofSku(sku, quantity) .distributionChannel(distributionChannel) .supplyChannel(inventorySupplyChannel) .build(); final AddLineItem action = AddLineItem.of(lineItemDraft); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, action)); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); assertThat(lineItem.getProductSlug()).isEqualTo(product.getMasterData().getStaged().getSlug()); assertThat(lineItem.getVariant().getIdentifier()).isEqualTo(ByIdVariantIdentifier.of(lineItem.getProductId(), lineItem.getVariant().getId())); assertThat(lineItem.getSupplyChannel().toReference()).isEqualTo(inventorySupplyChannel.toReference()); assertThat(lineItem.getDistributionChannel().toReference()).isEqualTo(distributionChannel.toReference()); assertThat(lineItem.getDiscountedPricePerQuantity()).isNotNull().isEmpty(); }); }
@Test public void addLineItemOfDraftOfVariantIdentifier() throws Exception { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).isEmpty(); final long quantity = 3; final Channel inventorySupplyChannel = ChannelFixtures.persistentChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY); final Channel distributionChannel = ChannelFixtures.persistentChannelOfRole(client(), ChannelRole.PRODUCT_DISTRIBUTION); ByIdVariantIdentifier variantIdentifier = product.getMasterData().getStaged().getMasterVariant().getIdentifier(); final LineItemDraft lineItemDraft = io.sphere.sdk.carts.LineItemDraftBuilder.ofVariantIdentifier(variantIdentifier, quantity) .distributionChannel(distributionChannel) .supplyChannel(inventorySupplyChannel) .build(); final AddLineItem action = AddLineItem.of(lineItemDraft); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, action)); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); assertThat(lineItem.getProductSlug()).isEqualTo(product.getMasterData().getStaged().getSlug()); assertThat(lineItem.getVariant().getIdentifier()).isEqualTo(ByIdVariantIdentifier.of(lineItem.getProductId(), lineItem.getVariant().getId())); assertThat(lineItem.getSupplyChannel().toReference()).isEqualTo(inventorySupplyChannel.toReference()); assertThat(lineItem.getDistributionChannel().toReference()).isEqualTo(distributionChannel.toReference()); assertThat(lineItem.getDiscountedPricePerQuantity()).isNotNull().isEmpty(); }); }
@Test public void queryBySlug() throws Exception { with2products("queryBySlug", (p1, p2) ->{ final Query<ProductProjection> query1 = ProductProjectionQuery.of(STAGED).bySlug(ENGLISH, p1.getMasterData().getStaged().getSlug().get(ENGLISH)); assertThat(ids(client().executeBlocking(query1))).containsOnly(p1.getId()); }); }
@Test public void message() { ProductFixtures.withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().isPublished()).isFalse(); final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); Query<ProductPublishedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(product)) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .withLimit(1L) .forMessageType(ProductPublishedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<ProductPublishedMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final ProductPublishedMessage message = queryResult.head().get(); assertThat(message.getResource().getId()).isEqualTo(product.getId()); assertThat(message.getProductProjection().getMasterVariant()).isEqualTo(publishedProduct.getMasterData().getCurrent().getMasterVariant()); assertThat(message.getResource().getObj().getMasterData().getCurrent().getSlug()).isEqualTo(message.getProductProjection().getSlug()); assertThat(message.getRemovedImageUrls().size()).isEqualTo(0); }); return publishedProduct; }); } }
@Test public void createPublishedProduct() { withEmptyProductType(client(), randomKey(), productType -> { final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().build(); final LocalizedString name = randomSlug(); final LocalizedString slug = randomSlug(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, name, slug, masterVariant) .publish(true) .build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); assertThat(product.getMasterData().isPublished()).isTrue(); assertThat(product.getMasterData().getCurrent().getSlug()).isEqualTo(slug); assertEventually(() -> { final ProductProjectionSearch search = ProductProjectionSearch.ofCurrent() .withQueryFilters(m -> m.id().is(product.getId())); final PagedSearchResult<ProductProjection> searchResult = client().executeBlocking(search); assertThat(searchResult.getResults()).hasSize(1); }); unpublishAndDelete(product); }); }