@Test public void and() throws Exception { assertThat((p1.and(p2)).toSphereQuery()).isEqualTo("masterData(current(slug(en=\"xyz-42\") and tags contains all (\"a\", \"b\", \"c\")"); }
@Override public QueryPredicate<JsonNode> predicate(final String predicateAsString) { return QueryPredicate.of(predicateAsString); } }
/** * Creates a new query that queries only for messages that can be mapped to certain Java types. This is a terminal operation so no changes can be done on the query. * * {@include.example io.sphere.sdk.messages.queries.MessageQueryIntegrationTest#queryForMultipleSpecificMessageClasses()} * * @param messageHints internal containers which register the known messages * @return new query */ default Query<Message> forMessageTypes(final List<MessageDerivateHint<? extends Message>> messageHints) { final MessageQuery queryWithPredicateForType = messageHints.stream() .map(hint -> hint.predicate()) .reduce((left, right) -> left.or(right)) .map(predicate -> plusPredicates(predicate)) .orElse(this); return new MultiTypedMessageQuery(queryWithPredicateForType, messageHints); } }
@Test public void or() throws Exception { assertThat((p1.or(p2)).toSphereQuery()).isEqualTo("masterData(current(slug(en=\"xyz-42\") or tags contains all (\"a\", \"b\", \"c\")"); }
@Test public void negated() throws Exception { final QueryPredicate<String> negated = p1.negate(); assertThat(negated.toSphereQuery()).isEqualTo("not(masterData(current(slug(en=\"xyz-42\"))"); assertThat(negated.toString()).isEqualTo("Predicate[not(masterData(current(slug(en=\"xyz-42\"))]"); } }
/** * Predicate which matches the country and state of a location. * * For ignoring the state use {@link #byCountry(com.neovisionaries.i18n.CountryCode)}. * @param location the location to query for * @return query with the same values but a predicate searching for a specific location */ default ZoneQuery byLocation(final Location location) { final QueryPredicate<Zone> predicate = Optional.ofNullable(location.getState()) .map(state -> ZoneQueryModel.of().locations().where(l -> l.country().is(location.getCountry()).and(l.state().is(state)))) .orElseGet(() -> ZoneQueryModel.of().locations().where(l -> l.country().is(location.getCountry()).and(l.state().isNotPresent()))); return withPredicates(predicate); } }
@Override public String toSphereQuery() { return leftPredicate.toSphereQuery() + " " + connectorWord + " " + rightPredicate.toSphereQuery(); } }
@Test public void combinedEmbeddedQueries() throws Exception { final QueryPredicate<EmbeddedProductDataQueryModel> predicate = DATA_QUERY_MODEL.name().lang(ENGLISH).is("Yes").or(DATA_QUERY_MODEL.name().lang(GERMAN).is("Ja")); final QueryPredicate<Product> resultPredicate = ProductQueryModel.of().masterData().where(m -> m.current().where(predicate).and(m.staged().where(predicate))); assertThat(resultPredicate.toSphereQuery()).isEqualTo("masterData(current(name(en=\"Yes\") or name(de=\"Ja\")) and staged(name(en=\"Yes\") or name(de=\"Ja\")))"); } }
@Test public void allVariantsInProductProjection() throws Exception { assertThat(ProductProjectionQueryModel.of().allVariants().where(m -> m.sku().is("bar"))) .isEqualTo(ProductProjectionQueryModel.of().where(m -> m.masterVariant().sku().is("bar").or(m.variants().sku().is("bar")))) .isEqualTo(QueryPredicate.<ProductProjection>of(embeddedProductProjectionPredicate)); } }
@Test public void queryByNegatedPredicateNameValidWithAnd() throws Exception { withCategory(client(), category1 -> withCategory(client(), category2 -> { final Query<Category> query = CategoryQuery.of(). withPredicates(m -> { final QueryPredicate<Category> predicate = m.name().lang(Locale.ENGLISH).is(category1.getName().get(Locale.ENGLISH)).negate() .and(m.id().is(category1.getId())); return predicate; }) .withSort(m -> m.createdAt().sort().desc()); final boolean category1IsPresent = client().executeBlocking(query).getResults().stream().anyMatch(cat -> cat.getId().equals(category1.getId())); assertThat(category1IsPresent).isFalse(); }) ); }
@Override public final QueryPredicate<T> not(final QueryPredicate<T> queryPredicateToNegate) { return queryPredicateToNegate.negate(); } }
/** * Predicate which matches the country and state of a location. * * For ignoring the state use {@link #byCountry(com.neovisionaries.i18n.CountryCode)}. * @param location the location to query for * @return query with the same values but a predicate searching for a specific location */ default ZoneQuery byLocation(final Location location) { final QueryPredicate<Zone> predicate = Optional.ofNullable(location.getState()) .map(state -> ZoneQueryModel.of().locations().where(l -> l.country().is(location.getCountry()).and(l.state().is(state)))) .orElseGet(() -> ZoneQueryModel.of().locations().where(l -> l.country().is(location.getCountry()).and(l.state().isNotPresent()))); return withPredicates(predicate); } }
@Override public String toSphereQuery() { return "not(" + queryPredicate.toSphereQuery() + ")"; } }
@Test public void allVariantsInProduct() throws Exception { assertThat(ProductQueryModel.of().masterData().current().allVariants().where(m -> m.sku().is("bar"))) .isEqualTo(ProductQueryModel.of().masterData().current().where(m -> m.masterVariant().sku().is("bar").or(m.variants().sku().is("bar")))) .isEqualTo(QueryPredicate.<Product>of(embeddedProductPredicate)); }
@Test public void queryByNegatedPredicateName() throws Exception { withCategory(client(), category1 -> withCategory(client(), category2 -> { final Query<Category> query = CategoryQuery.of(). withPredicates(m -> { final QueryPredicate<Category> predicate = m.name().lang(Locale.ENGLISH).is(category1.getName().get(Locale.ENGLISH)).negate(); return predicate; }) .withSort(m -> m.createdAt().sort().desc()); final boolean category1IsPresent = client().executeBlocking(query).getResults().stream().anyMatch(cat -> cat.getId().equals(category1.getId())); assertThat(category1IsPresent).isFalse(); }) ); }
@Test public void queryProductsWithAnyDiscount() throws Exception { withUpdateableProductDiscount(client(), (ProductDiscount productDiscount, Product product) -> { final ProductQuery query = ProductQuery.of() .withPredicates(m -> m.id().is(product.getId()) .and(m.masterData().staged().masterVariant().prices().discounted().isPresent())); final Duration maxWaitTime = Duration.ofMinutes(2); final Duration waitBeforeRetry = Duration.ofMillis(500); assertEventually(maxWaitTime, waitBeforeRetry, () -> { final Optional<Product> loadedProduct = client().executeBlocking(query).head(); assertThat(loadedProduct.isPresent()).isTrue(); assertThat(loadedProduct.get().getId()).isEqualTo(product.getId()); }); return productDiscount; }); }
@SuppressWarnings("rawtypes") @Override public final boolean equals(final Object o) { return o != null && (o instanceof QueryPredicate) && toSphereQuery().equals(((QueryPredicate)o).toSphereQuery()); }
/** * Creates a new query that queries only for messages that can be mapped to certain Java types. This is a terminal operation so no changes can be done on the query. * * {@include.example io.sphere.sdk.messages.queries.MessageQueryIntegrationTest#queryForMultipleSpecificMessageClasses()} * * @param messageHints internal containers which register the known messages * @return new query */ default Query<Message> forMessageTypes(final List<MessageDerivateHint<? extends Message>> messageHints) { final MessageQuery queryWithPredicateForType = messageHints.stream() .map(hint -> hint.predicate()) .reduce((left, right) -> left.or(right)) .map(predicate -> plusPredicates(predicate)) .orElse(this); return new MultiTypedMessageQuery(queryWithPredicateForType, messageHints); } }
@Override public QueryPredicate<JsonNode> predicate(final String predicateAsString) { return QueryPredicate.of(predicateAsString); } }
@Test public void execution() throws Exception { withUpdateableProductDiscount(client(), productDiscount -> { final ProductDiscountQuery discountQuery = ProductDiscountQuery.of() .withPredicates(m -> m.active().is(true).and(m.id().is(productDiscount.getId()))) .withExpansionPaths(m -> m.references()); final ProductDiscount loadedDiscount = client().executeBlocking(discountQuery).head().get(); assertThat(loadedDiscount.getId()).isEqualTo(productDiscount.getId()); assertThat(loadedDiscount.getReferences().size()).isGreaterThanOrEqualTo(1); assertThat(loadedDiscount.getReferences().stream() .filter(ref -> ref.getTypeId().equals(Product.referenceTypeId())) .findFirst() .get() ) .overridingErrorMessage("one product is expanded") .matches(ref -> ref.getObj() != null); return productDiscount; }); } }