Skip to content

Commit

Permalink
[DE-572] + [DE-599] Java CRUD for Product Price Points endpoints part…
Browse files Browse the repository at this point in the history
… 1 & 2 (#45)
  • Loading branch information
patryk-grudzien-keen authored Dec 4, 2023
1 parent 46bae5e commit 56dd6a8
Show file tree
Hide file tree
Showing 12 changed files with 903 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.junit.jupiter.api.BeforeAll;

import java.io.IOException;
import java.util.EnumSet;
import java.util.List;

import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
Expand Down Expand Up @@ -83,8 +82,10 @@ protected static ProductPricePointResponse createProductPricePointWithDelay(long
fail(e.getMessage(), e);
}

return PRODUCT_PRICE_POINTS_CONTROLLER
.createProductPricePoint(CreateProductPricePointProductId.fromNumber(productId), new CreateProductPricePointRequest(createProductPricePoint));
return PRODUCT_PRICE_POINTS_CONTROLLER.createProductPricePoint(
CreateProductPricePointProductId.fromNumber(productId),
new CreateProductPricePointRequest(createProductPricePoint)
);
}

protected static CreateProductPricePoint.Builder defaultBuilder() {
Expand All @@ -103,16 +104,17 @@ protected static CreateProductPricePoint.Builder defaultBuilder() {
}

// when archiving default price point, {"errors":["Cannot archive the default price point."]} is returned
protected static void archiveAllSitePricePointsExcludingDefault() throws IOException, ApiException {
List<ProductPricePoint> sitePricePointsExcludingDefault = listAllSitePricePointsPerPage200ExcludingDefault();
while (!sitePricePointsExcludingDefault.isEmpty()) {
for (ProductPricePoint pricePoint : sitePricePointsExcludingDefault) {
// when archiving custom price point, {"errors":["The custom product price point cannot be archived."]} is returned
protected static void archiveAllSiteCatalogPricePoints() throws IOException, ApiException {
List<ProductPricePoint> siteCatalogPricePoints = listAllSiteCatalogPricePointsPerPage200();
while (!siteCatalogPricePoints.isEmpty()) {
for (ProductPricePoint pricePoint : siteCatalogPricePoints) {
PRODUCT_PRICE_POINTS_CONTROLLER.archiveProductPricePoint(
ArchiveProductPricePointProductId.fromNumber(pricePoint.getProductId()),
ArchiveProductPricePointPricePointId.fromNumber(pricePoint.getId())
);
}
sitePricePointsExcludingDefault = listAllSitePricePointsPerPage200ExcludingDefault();
siteCatalogPricePoints = listAllSiteCatalogPricePointsPerPage200();
}
}

Expand All @@ -126,20 +128,16 @@ protected static void archiveAllSiteProducts() throws IOException, ApiException
}
}

protected static List<PricePointType> pricePointTypesExcludingDefault() {
return EnumSet.complementOf(EnumSet.of(PricePointType.ENUM_DEFAULT)).stream().toList();
}

private static List<ProductResponse> listAllSiteProductsPerPage200() throws IOException, ApiException {
return PRODUCTS_CONTROLLER.listProducts(
new ListProductsInput.Builder().perPage(200).build()
);
}

private static List<ProductPricePoint> listAllSitePricePointsPerPage200ExcludingDefault() throws IOException, ApiException {
private static List<ProductPricePoint> listAllSiteCatalogPricePointsPerPage200() throws IOException, ApiException {
return PRODUCT_PRICE_POINTS_CONTROLLER
.listAllProductPricePoints(new ListAllProductPricePointsInput.Builder()
.filterType(pricePointTypesExcludingDefault())
.filterType(List.of(PricePointType.CATALOG))
.perPage(200)
.build()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.maxio.advancedbilling.controllers.productpricepoints;

import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.exceptions.ErrorListResponseException;
import com.maxio.advancedbilling.models.Product;
import com.maxio.advancedbilling.models.ProductPricePoint;
import com.maxio.advancedbilling.models.containers.ArchiveProductPricePointPricePointId;
import com.maxio.advancedbilling.models.containers.ArchiveProductPricePointProductId;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

import static com.maxio.advancedbilling.utils.CommonAssertions.assertNotFound;
import static com.maxio.advancedbilling.utils.CommonAssertions.assertUnprocessableEntity;
import static org.assertj.core.api.Assertions.assertThat;

class ProductPricePointsControllerArchiveTest extends ProductPricePointsBaseTest {

private static Product product;

@BeforeAll
static void beforeAll() throws IOException, ApiException {
product = createProduct();
}

@Test
void shouldReturn404WhenProductNotExists() {
// when - then
assertNotFound(() -> PRODUCT_PRICE_POINTS_CONTROLLER.archiveProductPricePoint(
ArchiveProductPricePointProductId.fromNumber(12345),
ArchiveProductPricePointPricePointId.fromNumber(product.getProductPricePointId())
)
);
}

@Test
void shouldReturn404WhenProductPricePointNotExists() {
// when - then
assertNotFound(() -> PRODUCT_PRICE_POINTS_CONTROLLER.archiveProductPricePoint(
ArchiveProductPricePointProductId.fromNumber(product.getId()),
ArchiveProductPricePointPricePointId.fromNumber(12345)
)
);
}

@Test
void shouldReturn422WhenArchivingDefaultProductPricePoint() {
// when - then
assertUnprocessableEntity(
ErrorListResponseException.class,
() -> PRODUCT_PRICE_POINTS_CONTROLLER.archiveProductPricePoint(
ArchiveProductPricePointProductId.fromNumber(product.getId()),
ArchiveProductPricePointPricePointId.fromNumber(product.getProductPricePointId())
),
e -> assertThat(e.getErrors())
.usingRecursiveComparison()
.isEqualTo(List.of("Cannot archive the default price point."))

);
}

@Test
void shouldArchiveNonDefaultProductPricePointById() throws IOException, ApiException {
// given
ProductPricePoint savedProductPricePoint = createProductPricePoint(
product.getId(), defaultBuilder().name("price-point-name").build()
).getPricePoint();

// when
ProductPricePoint archivedPricePoint = PRODUCT_PRICE_POINTS_CONTROLLER
.archiveProductPricePoint(
ArchiveProductPricePointProductId.fromNumber(product.getId()),
ArchiveProductPricePointPricePointId.fromNumber(savedProductPricePoint.getId())
)
.getPricePoint();

// then
assertThat(archivedPricePoint.getArchivedAt()).isNotNull();
}

@Test
void shouldArchiveNonDefaultProductPricePointByHandle() throws IOException, ApiException {
// given
ProductPricePoint savedProductPricePoint = createProductPricePoint(
product.getId(), defaultBuilder().name("price-point-name").handle("handle-123").build()
).getPricePoint();

// when
ProductPricePoint archivedPricePoint = PRODUCT_PRICE_POINTS_CONTROLLER
.archiveProductPricePoint(
ArchiveProductPricePointProductId.fromString("handle:" + product.getHandle()),
ArchiveProductPricePointPricePointId.fromString("handle:" + savedProductPricePoint.getHandle())
)
.getPricePoint();

// then
assertThat(archivedPricePoint.getArchivedAt()).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.maxio.advancedbilling.controllers.productpricepoints;

import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.BulkCreateProductPricePointsRequest;
import com.maxio.advancedbilling.models.CreateProductPricePoint;
import com.maxio.advancedbilling.models.Product;
import com.maxio.advancedbilling.models.ProductPricePoint;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;

import static com.maxio.advancedbilling.utils.CommonAssertions.assertNotFound;
import static com.maxio.advancedbilling.utils.CommonAssertions.assertUnprocessableEntity;
import static org.assertj.core.api.Assertions.assertThat;

class ProductPricePointsControllerBulkCreateTest extends ProductPricePointsBaseTest {

private static Product product;

@BeforeAll
static void beforeAll() throws IOException, ApiException {
product = createProduct();
}

@Test
void shouldReturn201AndCreatePricePoints() throws IOException, ApiException {
// when
List<ProductPricePoint> pricePoints = PRODUCT_PRICE_POINTS_CONTROLLER
.createProductPricePoints(
product.getId(),
new BulkCreateProductPricePointsRequest(List.of(
defaultBuilder().name("price-point-name-1").build(),
defaultBuilder().name("price-point-name-2").build()
))
)
.getPricePoints();

// then
assertThat(pricePoints)
.hasSize(3)
.extracting(ProductPricePoint::getName)
.containsExactlyInAnyOrder("Original", "price-point-name-1", "price-point-name-2");
}

@Test
void shouldReturn404WhenCreatingPricePointsForNotExistingProduct() {
// when - then
assertNotFound(() -> PRODUCT_PRICE_POINTS_CONTROLLER
.createProductPricePoints(12345, new BulkCreateProductPricePointsRequest())
);
}

@ParameterizedTest
@MethodSource("argsForShouldReturn201WithDefaultProductPricePointOnlyWhenRequestIsNullOrContainsNullOrEmptyList")
void shouldReturn201WithDefaultProductPricePointOnlyWhenRequestIsNullOrContainsNullOrEmptyList(BulkCreateProductPricePointsRequest request)
throws IOException, ApiException {
// when
List<ProductPricePoint> pricePoints = PRODUCT_PRICE_POINTS_CONTROLLER
.createProductPricePoints(createProduct().getId(), request) // new different product
.getPricePoints();

// then
assertThat(pricePoints)
.hasSize(1)
.extracting(ProductPricePoint::getName)
.containsExactly("Original");
}

private static Stream<Arguments> argsForShouldReturn201WithDefaultProductPricePointOnlyWhenRequestIsNullOrContainsNullOrEmptyList() {
return Stream.of(
Arguments.of((Object) null),
Arguments.of(new BulkCreateProductPricePointsRequest(null)),
Arguments.of(new BulkCreateProductPricePointsRequest(List.of()))
);
}

@Test
void shouldReturn422WhenRequestContainsListWithoutRequiredFields() {
// when - then
assertUnprocessableEntity(
ApiException.class,
() -> PRODUCT_PRICE_POINTS_CONTROLLER.createProductPricePoints(
product.getId(),
new BulkCreateProductPricePointsRequest(List.of(
new CreateProductPricePoint(),
new CreateProductPricePoint()
))
),
e -> assertThat(e.getHttpContext().getResponse().getBody()).isEqualTo("{\"price_points[0].interval\":[\"Recurring Interval: must be greater than or equal to 1.\"],\"price_points[0].interval_unit\":[\"Interval unit: cannot be blank.\",\"Interval unit: must be 'month' or 'day'.\"],\"price_points[0].name\":[\"Name: cannot be blank.\"],\"price_points[1].interval\":[\"Recurring Interval: must be greater than or equal to 1.\"],\"price_points[1].interval_unit\":[\"Interval unit: cannot be blank.\",\"Interval unit: must be 'month' or 'day'.\"],\"price_points[1].name\":[\"Name: cannot be blank.\"]}")
);
}
}
Loading

0 comments on commit 56dd6a8

Please sign in to comment.