@Override public String toSphereQuery() { return leftPredicate.toSphereQuery() + " " + connectorWord + " " + rightPredicate.toSphereQuery(); } }
@Override public String toSphereQuery() { return "not(" + queryPredicate.toSphereQuery() + ")"; } }
@SuppressWarnings("rawtypes") @Override public final boolean equals(final Object o) { return o != null && (o instanceof QueryPredicate) && toSphereQuery().equals(((QueryPredicate)o).toSphereQuery()); }
private void assertResult(final Function<CustomObjectQueryModel<CustomObject<Void>>, QueryPredicate<CustomObject<Void>>> f, final String expected) { final CustomObjectQueryModel<CustomObject<Void>> m = CustomObjectQueryModel.<CustomObject<Void>>of(); assertThat(f.apply(m).toSphereQuery()).isEqualTo(expected); } }
@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\"))]"); } }
@Override protected String render() { final String sphereQuery = predicate.toSphereQuery(); return getQueryModel().getParent() != null || getQueryModel().getPathSegment() != null ? "(" + sphereQuery + ")" : sphereQuery; } }
@Test public void generateSimpleQueries() throws Exception { assertThat(model.is("foo").toSphereQuery()).isEqualTo("id=\"foo\""); }
@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\")"); }
@Test public void isLessThan() throws Exception { assertThat(model.isLessThan("x").toSphereQuery()).isEqualTo("id<\"x\""); }
@Test public void isGreaterThan() throws Exception { assertThat(model.isGreaterThan("x").toSphereQuery()).isEqualTo("id>\"x\""); }
@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 isIn() throws Exception { assertThat(model.lang(Locale.ENGLISH).isIn(asList("foo", "bar")).toSphereQuery()) .isEqualTo("thepath(en in (\"foo\", \"bar\"))"); }
@Test public void lang() throws Exception { assertThat(model.lang(Locale.ENGLISH).is("bar").toSphereQuery()) .isEqualTo("thepath(en=\"bar\")"); }
@Test public void isNot() throws Exception { assertThat(model.lang(Locale.ENGLISH).isNot("bar").toSphereQuery()) .isEqualTo("thepath(en<>\"bar\")"); } }
@Test public void whereClosure() throws Exception { final ProductVariantQueryModel<String> model = ProductVariantQueryModel.of(null, "foo"); final QueryPredicate<String> normalWay = model.where(EmbeddedProductVariantQueryModel.of().sku().is("x")); final QueryPredicate<String> closureWay = model.where(m -> m.sku().is("x")); assertThat(normalWay.toSphereQuery()).isEqualTo(closureWay.toSphereQuery()).isEqualTo("foo(sku=\"x\")"); } }
@Test public void not() throws Exception { final QueryPredicate<Category> predicate = CategoryQuery.of().withPredicates(m -> m.not(m.id().is("foo"))).predicates().get(0); assertThat(predicate.toSphereQuery()).isEqualTo("not(id=\"foo\")"); } }
@Test public void variants() throws Exception { assertThat(ProductQueryModel.of().masterData().current().variants().sku().is("bar").toSphereQuery()) .isEqualTo("masterData(current(variants(sku=\"bar\")))"); }
@Test public void pure() throws Exception { final QueryPredicate<Product> purePredicate = ProductQueryModel.of().masterData().current().name().lang(ENGLISH).is("Yes"); assertThat(purePredicate.toSphereQuery()).isEqualTo("masterData(current(name(en=\"Yes\")))"); }
@Test public void generateHierarchicalQueries() throws Exception { final QueryModelImpl<Fixtures.Product> parent = new QueryModelImpl<>(new QueryModelImpl<>(new QueryModelImpl<>(new QueryModelImpl<>(null, "x1"), "x2"), "x3"), "x4"); assertThat(new StringQuerySortingModelImpl<>(parent, "x5").is("foo").toSphereQuery()).isEqualTo("x1(x2(x3(x4(x5=\"foo\"))))"); }
@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\")))"); } }