@Nullable @Override public Reference<State> getState() { return product.getState(); }
@Nullable @Override public Reference<State> getState() { return product.getState(); }
@Test public void transitionStateAndForce() { Set<Reference<State>> noTransitions = emptySet(); withStateByBuilder(client(), builder -> builder.type(PRODUCT_STATE).transitions(noTransitions), stateA -> { withStateByBuilder(client(), builder -> builder.type(PRODUCT_STATE), stateB -> { withUpdateableProduct(client(), product -> { assertThat(product.getState()).isNull(); final Product productInStateA = client().executeBlocking(ProductUpdateCommand.of(product, TransitionState.of(stateA))); //no force usage assertThatThrownBy(() -> client().executeBlocking(ProductUpdateCommand.of(productInStateA, TransitionState.of(stateB)))) .hasMessageContaining("InvalidOperation"); final ProductUpdateCommand cmd = ProductUpdateCommand.of(productInStateA, TransitionState.of(stateB, true)); final Product productInStateB = client().executeBlocking(cmd); assertThat(productInStateB.getState()).isEqualTo(stateB.toReference()); return productInStateA; }); }); }); }
@Test public void transitionState() { withStateByBuilder(client(), builder -> builder.type(PRODUCT_STATE), state -> { withUpdateableProduct(client(), product -> { assertThat(product.getState()).isNull(); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList(TransitionState.of(state)))); assertThat(updatedProduct.getState()).isEqualTo(state.toReference()); assertEventually(() -> { final PagedQueryResult<ProductStateTransitionMessage> messageQueryResult = client().executeBlocking(MessageQuery.of() .withPredicates(m -> m.resource().is(product)) .forMessageType(ProductStateTransitionMessage.MESSAGE_HINT)); assertThat(messageQueryResult.getResults()).isNotEmpty(); final ProductStateTransitionMessage message = messageQueryResult.head().get(); assertThat(message.getState()).isEqualTo(state.toReference()); }); //check query model final ProductQuery query = ProductQuery.of() .withPredicates(m -> m.id().is(product.getId()).and(m.state().is(state))); final Product productByState = client().executeBlocking(query) .head().get(); assertThat(productByState).isEqualTo(updatedProduct); return updatedProduct; }); }); }
@Test public void createProductByJsonDraft() throws Exception { final JsonNodeReferenceResolver referenceResolver = new JsonNodeReferenceResolver(); withCategory(client(), category -> { withProductType(client(), randomKey(), productType -> { withTaxCategory(client(), taxCategory -> { withStateByBuilder(client(), stateBuilder -> stateBuilder.initial(true).type(StateType.PRODUCT_STATE), state -> { referenceResolver.addResourceByKey("t-shirt-category", category); referenceResolver.addResourceByKey("t-shirt-product-type", productType); referenceResolver.addResourceByKey("standard-tax", taxCategory); referenceResolver.addResourceByKey("initial-product-state", state); final ProductDraft productDraft = draftFromJsonResource("drafts-tests/product.json", ProductDraft.class, referenceResolver); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); assertThat(product.getProductType()).isEqualTo(productType.toReference()); assertThat(product.getTaxCategory()).isEqualTo(taxCategory.toReference()); assertThat(product.getState()).isEqualTo(state.toReference()); final ProductData productData = product.getMasterData().getStaged(); assertThat(productData.getName()).isEqualTo(LocalizedString.ofEnglish("red shirt")); assertThat(productData.getCategories()).extracting("id").contains(category.getId()); client().executeBlocking(ProductDeleteCommand.of(product)); }); }); }); }); }
@Test public void createProductWithExternalImage() throws Exception { withStateByBuilder(client(), stateBuilder -> stateBuilder.initial(true).type(PRODUCT_STATE), initialProductState -> { final ProductType productType = ProductTypeFixtures.defaultProductType(client()); final Image image = Image.ofWidthAndHeight("http://www.commercetools.com/assets/img/ct_logo_farbe.gif", 460, 102, "commercetools logo"); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of() .images(image) .build(); final TaxCategory taxCategory = defaultTaxCategory(client()); final SearchKeywords searchKeywords = SearchKeywords.of(Locale.ENGLISH, asList(SearchKeyword.of("foo bar baz", CustomSuggestTokenizer.of(asList("foo, baz"))))); final ProductDraft productDraft = ProductDraftBuilder .of(productType, en("product with external image"), randomSlug(), masterVariant) .taxCategory(taxCategory) .searchKeywords(searchKeywords) .state(initialProductState) .build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Image loadedImage = product.getMasterData().getStaged().getMasterVariant().getImages().get(0); assertThat(loadedImage).isEqualTo(image); assertThat(product.getTaxCategory()).isEqualTo(taxCategory.toReference()); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); assertThat(product.getState()).isEqualTo(initialProductState.toReference()); //clean up test client().executeBlocking(ProductDeleteCommand.of(product)); }); }