static SearchKeyword of(final String text) { return of(text, null); }
public static SearchKeyword of(final String text, final Optional<SuggestTokenizer> suggestTokenizer) { return new SearchKeyword(text, suggestTokenizer); } }
public static SearchKeyword of(final String text) { return of(text, Optional.empty()); }
public static SearchKeyword of(final String text, final SuggestTokenizer suggestTokenizer) { return of(text, Optional.of(suggestTokenizer)); }
public static void withSuggestProduct(final BlockingSphereClient client, final Consumer<Product> consumer) { withEmptyProductType(client, randomKey(), productType -> { final SearchKeywords searchKeywords = SearchKeywords.of( Locale.ENGLISH, asList(SearchKeyword.of("Multi tool"), SearchKeyword.of("Swiss Army Knife", WhiteSpaceSuggestTokenizer.of())), Locale.GERMAN, singletonList(SearchKeyword.of("Schweizer Messer", CustomSuggestTokenizer.of(asList("schweizer messer", "offiziersmesser", "sackmesser")))) ); final ProductDraftBuilder productDraftBuilder = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), ProductVariantDraftBuilder.of().build()) .searchKeywords(searchKeywords); withProduct(client, productDraftBuilder, consumer); }); }
@Test public void executionWithCustomTokenizer() { withSuggestProduct(client(), product -> { final SearchKeywords searchKeywords = SearchKeywords.of( Locale.ENGLISH, asList(SearchKeyword.of("Multi tool"), SearchKeyword.of("Swiss Army Knife", WhiteSpaceSuggestTokenizer.of())), Locale.GERMAN, singletonList(SearchKeyword.of("Schweizer Messer", CustomSuggestTokenizer.of(asList("schweizer messer", "offiziersmesser", "sackmesser")))) ); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); final SuggestQuery suggestQuery = SuggestQuery.of(LocalizedStringEntry.of(Locale.GERMAN, "offiz")) .withStaged(true); assertEventually(() -> { final SuggestionResult suggestionResult = client().executeBlocking(suggestQuery); assertThat(suggestionResult.getSuggestionsForLocale(Locale.GERMAN)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Schweizer Messer"))); }); }); }
public void setSearchKeywordsWithStaged(final Boolean staged) { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final SearchKeywords searchKeywords = SearchKeywords.of(Locale.ENGLISH, asList(SearchKeyword.of("Raider", CustomSuggestTokenizer.of(singletonList("Twix"))))); final ProductUpdateCommand command = ProductUpdateCommand.of(product, SetSearchKeywords.of(searchKeywords, staged)); final Product updatedProduct = client().executeBlocking(command); final SearchKeywords actualKeywords = updatedProduct.getMasterData().getStaged().getSearchKeywords(); assertThat(actualKeywords).isEqualTo(searchKeywords); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
@Test public void execution() { withSuggestProduct(client(), product -> { final SearchKeywords searchKeywords = SearchKeywords.of( Locale.ENGLISH, asList(SearchKeyword.of("Multi tool"), SearchKeyword.of("Swiss Army Knife", WhiteSpaceSuggestTokenizer.of())), Locale.GERMAN, singletonList(SearchKeyword.of("Schweizer Messer", CustomSuggestTokenizer.of(asList("schweizer messer", "offiziersmesser", "sackmesser")))) ); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); final SuggestQuery suggestQuery = SuggestQuery.of(LocalizedStringEntry.of(Locale.ENGLISH, "knife")) .withStaged(true); assertEventually(() -> { final SuggestionResult suggestionResult = client().executeBlocking(suggestQuery); assertThat(suggestionResult.getSuggestionsForLocale(Locale.ENGLISH)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Swiss Army Knife"))); }); }); }
@Test public void fuzzy() { withSuggestProduct(client(), product -> { final SearchKeywords searchKeywords = SearchKeywords.of( Locale.ENGLISH, asList(SearchKeyword.of("Multi tool"), SearchKeyword.of("Swiss Army Knife", WhiteSpaceSuggestTokenizer.of())), Locale.GERMAN, singletonList(SearchKeyword.of("Schweizer Messer", CustomSuggestTokenizer.of(asList("schweizer messer", "offiziersmesser", "sackmesser")))) ); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); final SuggestQuery suggestQuery = SuggestQuery.of(LocalizedStringEntry.of(Locale.ENGLISH, "knive")) .withStaged(true).withFuzzy(true); assertEventually(() -> { final SuggestionResult suggestionResult = client().executeBlocking(suggestQuery); assertThat(suggestionResult.getSuggestionsForLocale(Locale.ENGLISH)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Swiss Army Knife"))); }); }); } }
@Test public void suggestionForMultipleLanguages() { withSuggestProduct(client(), product -> { final SearchKeywords searchKeywords = SearchKeywords.of( Locale.ENGLISH, asList(SearchKeyword.of("Multi tool"), SearchKeyword.of("Swiss Army Knife", WhiteSpaceSuggestTokenizer.of())), Locale.GERMAN, singletonList(SearchKeyword.of("Schweizer Messer", CustomSuggestTokenizer.of(asList("schweizer messer", "offiziersmesser", "sackmesser")))) ); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); final List<LocalizedStringEntry> keywords = asList( LocalizedStringEntry.of(Locale.GERMAN, "offiz"), LocalizedStringEntry.of(Locale.ENGLISH, "multi") ); final SuggestQuery suggestQuery = SuggestQuery.of(keywords) .withStaged(true); assertEventually(() -> { final SuggestionResult suggestionResult = client().executeBlocking(suggestQuery); assertThat(suggestionResult.getSuggestionsForLocale(Locale.GERMAN)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Schweizer Messer"))); assertThat(suggestionResult.getSuggestionsForLocale(Locale.ENGLISH)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Multi tool"))); }); }); }
@Test public void setSearchKeywords() throws Exception { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final SearchKeywords searchKeywords = SearchKeywords.of(Locale.ENGLISH, asList(SearchKeyword.of("Raider", CustomSuggestTokenizer.of(singletonList("Twix"))))); final ProductUpdateCommand command = ProductUpdateCommand.of(product, SetSearchKeywords.of(searchKeywords)); final Product updatedProduct = client().executeBlocking(command); final SearchKeywords actualKeywords = updatedProduct.getMasterData().getStaged().getSearchKeywords(); assertThat(actualKeywords).isEqualTo(searchKeywords); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isTrue(); return updatedProduct; }); }
final Reference<ProductType> productType = ProductType.referenceOfId("product-type-id"); final Reference<TaxCategory> taxCategory = TaxCategory.referenceOfId("tax-category-id"); final SearchKeywords searchKeywords = SearchKeywords.of(Locale.ENGLISH, singletonList(SearchKeyword.of(randomString()))); final Reference<State> state = State.referenceOfId("state-reference-id"); final CategoryOrderHints categoryOrderHints = CategoryOrderHints.of("category-id", "5");
@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)); }); }