Skip to content

Commit

Permalink
add test to retrieve CartDiscount from discounted lineitem portion
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Jan 19, 2024
1 parent 49e14e5 commit dcae522
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

import static com.commercetools.TestUtils.stringFromResource;

import java.util.Optional;

import com.commercetools.api.models.cart.CartAddCustomLineItemAction;
import com.commercetools.api.models.cart.DirectDiscountReference;
import com.commercetools.api.models.cart.DiscountedLineItemPrice;
import com.commercetools.api.models.cart_discount.CartDiscount;
import com.commercetools.api.models.cart_discount.CartDiscountReference;
import com.commercetools.api.models.common.CentPrecisionMoneyDraft;
import com.commercetools.api.models.common.DefaultCurrencyUnits;
import com.commercetools.api.models.common.Reference;

import io.vrap.rmf.base.client.utils.json.JsonUtils;

Expand All @@ -27,6 +31,28 @@ public void discountedLineItemPortion() {
.isInstanceOf(CartDiscountReference.class);
}

private static Optional<CartDiscount> referenceMapper(Reference reference) {
return Optional.ofNullable(
reference instanceof CartDiscountReference ? ((CartDiscountReference) reference).getObj() : null);
}

@Test
public void discountedLineItemPortionHelper() {
DiscountedLineItemPrice price = JsonUtils.fromJsonString(stringFromResource("discounted-price.json"),
DiscountedLineItemPrice.class);

Optional<CartDiscount> discount1 = price.getIncludedDiscounts()
.get(0)
.getDiscount()
.withReference(CartTest::referenceMapper);
Optional<CartDiscount> discount2 = price.getIncludedDiscounts()
.get(1)
.getDiscount()
.withReference(CartTest::referenceMapper);
Assertions.assertThat(discount1.isPresent()).isFalse();
Assertions.assertThat(discount2.get()).isInstanceOf(CartDiscount.class);
}

@Test
public void cartLineItemMoneyBuilder() {
CartAddCustomLineItemAction action = CartAddCustomLineItemAction.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
{
"discount": {
"typeId": "cart-discount",
"id": "cart"
"id": "cart",
"obj": {
"id": "cart"
}
},
"discountedAmount": {
"type": "centPrecision",
Expand Down

0 comments on commit dcae522

Please sign in to comment.