/** * Predicate which matches the country of a location, does not take the state into the consideration. * For considering also the state use {@link #byLocation(io.sphere.sdk.zones.Location)}. * @param countryCode the country to query for * @return query with the same values but a predicate searching for a specific country */ default ZoneQuery byCountry(final CountryCode countryCode) { return withPredicates(m -> m.locations().country().is(countryCode)); }
public CountryQueryModel<Order> country() { return new CountryQueryModel<Order>(Optional.of(this), Optional.of("country")); } }
@Test public void countryIsIn() throws Exception { assertOrderIsFoundWithPredicate(order -> MODEL.country().isIn(singletonList(CartFixtures.DEFAULT_COUNTRY))); }
@Test public void countryIsNot() throws Exception { assertOrderIsFoundWithPredicate(order -> MODEL.country().isNot(CountryCode.AF)); }
/** * Predicate which matches the country of a location, does not take the state into the consideration. * For considering also the state use {@link #byLocation(io.sphere.sdk.zones.Location)}. * @param countryCode the country to query for * @return query with the same values but a predicate searching for a specific country */ default ZoneQuery byCountry(final CountryCode countryCode) { return withPredicates(m -> m.locations().country().is(countryCode)); }
public CountryQueryModel<T> country() { return new CountryQueryModel<T>(Optional.of(this), Optional.of("country")); }
/** * Predicate which matches the country of a location, does not take the state into the consideration. * For considering also the state use {@link #byLocation(io.sphere.sdk.zones.Location)}. * @param countryCode the country to query for * @return query with the same values but a predicate searching for a specific country */ public QueryDsl<Zone> byCountry(final CountryCode countryCode) { return withPredicate(model().locations().country().is(countryCode)); }
/** * 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); } }
/** * 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); } }
/** * 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 */ public QueryDsl<Zone> byLocation(final Location location) { final Predicate<Zone> predicate = location.getState() .map(state -> model().locations().where(l -> l.country().is(location.getCountry()).and(l.state().is(state)))) .orElseGet(() -> model().locations().where(l -> l.country().is(location.getCountry()).and(l.state().isNotPresent()))); return withPredicate(predicate); } }
@Test public void country() throws Exception { assertOrderIsFoundWithPredicate(order -> MODEL.country().is(CartFixtures.DEFAULT_COUNTRY)); }
@Test public void setShippingAddress() throws Exception { final Cart cart = createCartWithCountry(client()); assertThat(cart.getShippingAddress()).isNull(); final Address address = AddressBuilder.of(DE).build(); final Cart cartWithAddress = client().executeBlocking(CartUpdateCommand.of(cart, SetShippingAddress.of(address))); assertThat(cartWithAddress.getShippingAddress()).isEqualTo(address); //you can query by shippingAddress fields final CartQuery query = CartQuery.of() .withPredicates(m -> m.shippingAddress().country().is(DE).and(m.id().is(cart.getId()))); assertThat(client().executeBlocking(query).head()).contains(cartWithAddress); final Cart cartWithoutAddress = client().executeBlocking(CartUpdateCommand.of(cartWithAddress, SetShippingAddress.of(null))); assertThat(cartWithoutAddress.getShippingAddress()).isNull(); }
@Test public void setBillingAddress() throws Exception { final Cart cart = createCartWithCountry(client()); assertThat(cart.getBillingAddress()).isNull(); final Address address = AddressBuilder.of(DE).build(); final Cart cartWithAddress = client().executeBlocking(CartUpdateCommand.of(cart, SetBillingAddress.of(address))); assertThat(cartWithAddress.getBillingAddress()).isEqualTo(address); //you can query by billingAddress fields final CartQuery query = CartQuery.of() .withPredicates(m -> m.billingAddress().country().is(DE).and(m.id().is(cart.getId()))); assertThat(client().executeBlocking(query).head()).contains(cartWithAddress); final Cart cartWithoutAddress = client().executeBlocking(CartUpdateCommand.of(cartWithAddress, SetBillingAddress.of(null))); assertThat(cartWithoutAddress.getBillingAddress()).isNull(); }
@Test public void testDraftWithMultipleAddresses() { final List<Address> itemShippingAddresses = createAddressArray(); final CartDraft draft = CartDraftBuilder.of(DefaultCurrencyUnits.EUR) .country(CountryCode.DE) .itemShippingAddresses(itemShippingAddresses) .build(); withTaxedProduct(client(), product -> { withCartDraft(client(), draft, cart -> { Assertions.assertThat(cart.getItemShippingAddresses()).containsAll(itemShippingAddresses); final String addressKey = cart.getItemShippingAddresses().get(0).getKey(); final ItemShippingDetailsDraft itemShippingDetailsDraft = ItemShippingDetailsDraftBuilder.of(Arrays.asList(ItemShippingTargetBuilder.of(addressKey, 2L).build())).build(); final int master_variant_id = product.getMasterData().getStaged().getMasterVariant().getId(); final LineItemDraft lineItemDraft = LineItemDraft.of(product, master_variant_id, 25).withShippingDetails(itemShippingDetailsDraft); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, AddLineItem.of(lineItemDraft))); PagedQueryResult<Cart> result = client().executeBlocking(CartQuery.of().plusPredicates(CartQueryModel.of().itemShippingAddresses().address().country().is(CountryCode.DE))); Assertions.assertThat(result.getResults()).isNotEmpty(); return updatedCart; }); }); }
@Test public void lineItemModel() throws Exception { withFilledCart(client(), cart -> { final LineItem lineItem = cart.getLineItems().get(0); final String englishName = lineItem.getName().get(Locale.ENGLISH); final Price price = lineItem.getPrice(); final Long centAmount = price.getValue().query(MonetaryQueries.convertMinorPart()); final ItemState itemState = lineItem.getState().stream().findFirst().get(); final CartQuery query = CartQuery.of() .withLimit(1L) .withPredicates(m -> m.lineItems().id().is(lineItem.getId()) .and(m.lineItems().quantity().is(lineItem.getQuantity())) .and(m.lineItems().productId().is(lineItem.getProductId())) .and(m.lineItems().name().locale(ENGLISH).is(englishName)) .and(m.lineItems().variant().sku().is(lineItem.getVariant().getSku())) .and(m.lineItems().price().discounted().isNotPresent()) .and(m.lineItems().price().id().is(price.getId())) .and(m.lineItems().price().value().centAmount().is(centAmount)) .and(m.lineItems().price().country().is(price.getCountry())) .and(m.lineItems().state().quantity().is(itemState.getQuantity())) .and(m.lineItems().state().state().is(itemState.getState())) ); final CartQuery cartQuery = query.plusPredicates(m -> m.id().is(cart.getId())); final Cart loadedCart = client().executeBlocking(cartQuery).head().get(); assertThat(loadedCart.getId()).isEqualTo(cart.getId()); }); }
@Test public void shippingMethodQuery() throws Exception { withShippingMethodForGermany(client(), shippingMethod -> { withCart(client(), createCartWithShippingAddress(client()), cart -> { final CartUpdateCommand updateCommand = CartUpdateCommand.of(cart, SetShippingMethod.of(shippingMethod)) .plusExpansionPaths(m -> m.shippingInfo().shippingMethod().taxCategory()) .plusExpansionPaths(m -> m.shippingInfo().taxCategory()); final Cart cartWithShippingMethod = client().executeBlocking(updateCommand); final CartShippingInfo shippingInfo = cartWithShippingMethod.getShippingInfo(); final CartQuery query = CartQuery.of() .plusPredicates(m -> m.id().is(cart.getId())) .plusPredicates(m -> m.shippingInfo().shippingMethodName().is(shippingMethod.getName())) .plusPredicates(m -> m.shippingInfo().price().centAmount().is(MoneyImpl.centAmountOf(shippingInfo.getPrice()))) .plusPredicates(m -> m.shippingInfo().taxRate().name().is(shippingInfo.getTaxRate().getName())) .plusPredicates(m -> m.shippingInfo().taxRate().includedInPrice().is(shippingInfo.getTaxRate().isIncludedInPrice())) .plusPredicates(m -> m.shippingInfo().taxRate().country().is(shippingInfo.getTaxRate().getCountry())) .plusPredicates(m -> m.shippingInfo().shippingMethod().is(shippingMethod)) .plusPredicates(m -> m.shippingInfo().discountedPrice().isNotPresent()); assertThat(client().executeBlocking(query).head().get()).isEqualTo(cartWithShippingMethod); return cartWithShippingMethod; }); }); }