From 0db9bb85c6c0008fc8e3df4b43883bd08673087f Mon Sep 17 00:00:00 2001 From: maciej-nedza <76946708+maciej-nedza@users.noreply.github.com> Date: Tue, 5 Dec 2023 13:55:40 +0100 Subject: [PATCH] [DE-563] Components endpoints tests 1 (#48) * [DE-563] Components endpoints tests 1 --- .../ComponentsControllerArchiveTest.java | 58 ++++++++ .../ComponentsControllerFindTest.java | 34 +++++ .../ComponentsControllerReadTest.java | 56 ++++++++ .../ComponentsControllerTestBase.java | 54 +++++++ ...ollerUpdateProductFamilyComponentTest.java | 135 ++++++++++++++++++ 5 files changed, 337 insertions(+) create mode 100644 tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerArchiveTest.java create mode 100644 tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerFindTest.java create mode 100644 tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerReadTest.java create mode 100644 tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerTestBase.java create mode 100644 tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateProductFamilyComponentTest.java diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerArchiveTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerArchiveTest.java new file mode 100644 index 00000000..0816fba3 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerArchiveTest.java @@ -0,0 +1,58 @@ +package com.maxio.advancedbilling.controllers.components; + +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.exceptions.ErrorListResponseException; +import com.maxio.advancedbilling.models.Component; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static com.maxio.advancedbilling.utils.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.CommonAssertions.assertUnprocessableEntity; +import static org.assertj.core.api.Assertions.assertThat; + +public class ComponentsControllerArchiveTest extends ComponentsControllerTestBase { + + @Test + void shouldArchiveComponent() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when + Component archivedComponent = COMPONENTS_CONTROLLER + .archiveComponent(productFamilyId, String.valueOf(component.getId())); + + // then + assertThat(archivedComponent.getId()).isEqualTo(component.getId()); + assertThat(archivedComponent.getArchived()).isTrue(); + assertThat(archivedComponent.getArchivedAt()).isNotNull(); + } + + @Test + void shouldNotArchiveAlreadyArchivedComponent() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when + COMPONENTS_CONTROLLER + .archiveComponent(productFamilyId, String.valueOf(component.getId())); + + // then + assertUnprocessableEntity( + ErrorListResponseException.class, + () -> COMPONENTS_CONTROLLER + .archiveComponent(productFamilyId, String.valueOf(component.getId())), + e -> assertThat(e.getErrors()) + .containsExactlyInAnyOrder("Component cannot be archived. " + + "Please make sure it hasn't already been archived.") + + ); + } + + @Test + void shouldNotArchiveNonExistentComponents() { + // when-then + assertNotFound(() -> COMPONENTS_CONTROLLER.archiveComponent(productFamilyId, "999999")); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerFindTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerFindTest.java new file mode 100644 index 00000000..a7c72206 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerFindTest.java @@ -0,0 +1,34 @@ +package com.maxio.advancedbilling.controllers.components; + +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Component; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static com.maxio.advancedbilling.utils.CommonAssertions.assertNotFound; +import static org.assertj.core.api.Assertions.assertThat; + +public class ComponentsControllerFindTest extends ComponentsControllerTestBase { + + @Test + void shouldFindComponent() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when + Component foundComponent = COMPONENTS_CONTROLLER + .readComponentByHandle(component.getHandle()) + .getComponent(); + + // then + assertThat(foundComponent).usingRecursiveComparison() + .isEqualTo(component); + } + + @Test + void shouldNotFindNonExistentComponent() { + assertNotFound(() -> COMPONENTS_CONTROLLER.readComponentByHandle("non-existent-handle")); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerReadTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerReadTest.java new file mode 100644 index 00000000..4cd71fc8 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerReadTest.java @@ -0,0 +1,56 @@ +package com.maxio.advancedbilling.controllers.components; + +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Component; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static com.maxio.advancedbilling.utils.CommonAssertions.assertNotFound; +import static org.assertj.core.api.Assertions.assertThat; + +public class ComponentsControllerReadTest extends ComponentsControllerTestBase { + + @Test + void shouldReadComponentById() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when + Component foundComponent = COMPONENTS_CONTROLLER + .readComponentById(productFamilyId, String.valueOf(component.getId())) + .getComponent(); + + // then + assertThat(foundComponent).usingRecursiveComparison() + .isEqualTo(component); + } + + @Test + void shouldReadComponentByHandle() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when + Component foundComponent = COMPONENTS_CONTROLLER + .readComponentById(productFamilyId, "handle:" + component.getHandle()) + .getComponent(); + + // then + assertThat(foundComponent).usingRecursiveComparison() + .isEqualTo(component); + } + + @Test + void shouldNotReadNonExistentComponentUsingId() { + assertNotFound(() -> COMPONENTS_CONTROLLER.readComponentById(productFamilyId, + "non-existent-id")); + } + + @Test + void shouldNotReadNonExistentComponentUsingHandle() { + assertNotFound(() -> COMPONENTS_CONTROLLER.readComponentById(productFamilyId, + "handle:non-existent-id")); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerTestBase.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerTestBase.java new file mode 100644 index 00000000..513cc4e2 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerTestBase.java @@ -0,0 +1,54 @@ +package com.maxio.advancedbilling.controllers.components; + +import com.maxio.advancedbilling.TestClient; +import com.maxio.advancedbilling.controllers.ComponentsController; +import com.maxio.advancedbilling.controllers.ProductFamiliesController; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Component; +import com.maxio.advancedbilling.models.ComponentKindPath; +import com.maxio.advancedbilling.models.CreateProductFamily; +import com.maxio.advancedbilling.models.CreateProductFamilyRequest; +import com.maxio.advancedbilling.models.CreateQuantityBasedComponent; +import com.maxio.advancedbilling.models.PricingScheme; +import com.maxio.advancedbilling.models.QuantityBasedComponent; +import com.maxio.advancedbilling.models.containers.CreateComponentBody; +import com.maxio.advancedbilling.models.containers.QuantityBasedComponentUnitPrice; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.jupiter.api.BeforeAll; + +import java.io.IOException; + +public class ComponentsControllerTestBase { + + protected static final ComponentsController COMPONENTS_CONTROLLER = TestClient.createClient().getComponentsController(); + protected static int productFamilyId; + + @BeforeAll + static void setup() throws IOException, ApiException { + ProductFamiliesController productFamiliesController = TestClient.createClient() + .getProductFamiliesController(); + productFamilyId = productFamiliesController.createProductFamily(new CreateProductFamilyRequest( + new CreateProductFamily("Test Product Family " + + RandomStringUtils.randomAlphanumeric(5).toLowerCase(), + null))) + .getProductFamily().getId(); + } + + protected Component createQuantityBasedComponent() throws IOException, ApiException { + String seed = RandomStringUtils.randomAlphanumeric(5).toLowerCase(); + QuantityBasedComponent quantityBasedComponent = new QuantityBasedComponent.Builder() + .name("testcomponent-" + seed) + .handle("test-handle-" + seed) + .unitName("unit") + .pricingScheme(PricingScheme.PER_UNIT) + .unitPrice(QuantityBasedComponentUnitPrice.fromPrecision(1.0)) + .build(); + CreateQuantityBasedComponent createQuantityBasedComponent = new CreateQuantityBasedComponent(quantityBasedComponent); + + return COMPONENTS_CONTROLLER.createComponent(productFamilyId, + ComponentKindPath.QUANTITY_BASED_COMPONENTS, + CreateComponentBody.fromCreateQuantityBasedComponent(createQuantityBasedComponent)) + .getComponent(); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateProductFamilyComponentTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateProductFamilyComponentTest.java new file mode 100644 index 00000000..2c1bcf77 --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateProductFamilyComponentTest.java @@ -0,0 +1,135 @@ +package com.maxio.advancedbilling.controllers.components; + +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.exceptions.ErrorListResponseException; +import com.maxio.advancedbilling.models.Component; +import com.maxio.advancedbilling.models.CreditType; +import com.maxio.advancedbilling.models.ItemCategory; +import com.maxio.advancedbilling.models.UpdateComponent; +import com.maxio.advancedbilling.models.UpdateComponentRequest; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static com.maxio.advancedbilling.utils.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.CommonAssertions.assertUnprocessableEntity; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +public class ComponentsControllerUpdateProductFamilyComponentTest extends ComponentsControllerTestBase { + + @Test + void shouldUpdateProductFamilyComponent() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + UpdateComponent updateComponent = new UpdateComponent.Builder() + .name("updatedName") + .handle("updated-handle-" + RandomStringUtils.randomAlphanumeric(5).toLowerCase()) + .description("updatedDescription") + .accountingCode("updatedAccountingCode") + .taxCode("taxCode") + .taxable(true) + .itemCategory(ItemCategory.ENUM_DIGITAL_SERVICES) + .displayOnHostedPage(false) + .upgradeCharge(CreditType.FULL) + .build(); + + // when + Component updatedComponent = COMPONENTS_CONTROLLER + .updateProductFamilyComponent(productFamilyId, + String.valueOf(component.getId()), + new UpdateComponentRequest(updateComponent) + ) + .getComponent(); + + // then + assertAll( + () -> assertThat(updatedComponent.getId()).isEqualTo(component.getId()), + () -> assertThat(updatedComponent.getName()).isEqualTo("updatedName"), + () -> assertThat(updatedComponent.getHandle()).isEqualTo(updateComponent.getHandle()), + () -> assertThat(updatedComponent.getAccountingCode()).isEqualTo("updatedAccountingCode"), + () -> assertThat(updatedComponent.getTaxCode()).isEqualTo("taxCode"), + () -> assertThat(updatedComponent.getTaxable()).isTrue(), + () -> assertThat(updatedComponent.getItemCategory()).isEqualTo(ItemCategory.ENUM_DIGITAL_SERVICES), + () -> assertThat(updatedComponent.getUpgradeCharge()).isEqualTo(CreditType.FULL), + () -> assertThat(updatedComponent.getUpdatedAt()).isNotNull() + ); + } + + @Test + void shouldUpdateProductFamilyComponentSettingValuesToNulls() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + UpdateComponent updateComponent = new UpdateComponent.Builder() + .description(null) + .accountingCode(null) + .taxCode(null) + .itemCategory(null) + .upgradeCharge(null) + .build(); + + // when + Component updatedComponent = COMPONENTS_CONTROLLER + .updateProductFamilyComponent(productFamilyId, + String.valueOf(component.getId()), + new UpdateComponentRequest(updateComponent) + ) + .getComponent(); + + // then + assertAll( + () -> assertThat(updatedComponent.getId()).isEqualTo(component.getId()), + () -> assertThat(updatedComponent.getName()).isEqualTo(component.getName()), + () -> assertThat(updatedComponent.getHandle()).isEqualTo(component.getHandle()), + () -> assertThat(updatedComponent.getAccountingCode()).isNull(), + () -> assertThat(updatedComponent.getTaxCode()).isNull(), + () -> assertThat(updatedComponent.getItemCategory()).isNull(), + () -> assertThat(updatedComponent.getUpgradeCharge()).isNull(), + () -> assertThat(updatedComponent.getUpdatedAt()).isNotNull() + ); + } + + @Test + void shouldReturn422WhenHandleIsIncorrect() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + UpdateComponent updateComponent = new UpdateComponent.Builder() + .handle("updatedHandle") + .build(); + + // when-then + assertUnprocessableEntity( + ErrorListResponseException.class, + () -> COMPONENTS_CONTROLLER.updateProductFamilyComponent(productFamilyId, + String.valueOf(component.getId()), new UpdateComponentRequest(updateComponent)), + e -> assertThat(e.getErrors()).containsExactlyInAnyOrder("Handle must start with a letter " + + "or number and may only contain lowercase letters, numbers, or the characters ':', '-', or '_'.") + ); + } + + @Test + void shouldReturn422WhenHandleIsAlreadyTaken() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + Component component2 = createQuantityBasedComponent(); + UpdateComponent updateComponent = new UpdateComponent.Builder() + .handle(component2.getHandle()) + .build(); + + // when-then + assertUnprocessableEntity( + ErrorListResponseException.class, + () -> COMPONENTS_CONTROLLER.updateProductFamilyComponent(productFamilyId, + String.valueOf(component.getId()), new UpdateComponentRequest(updateComponent)), + e -> assertThat(e.getErrors()).containsExactlyInAnyOrder("Handle must be unique within a Site.") + ); + } + + @Test + void shouldNotUpdateNonExistentComponent() { + assertNotFound(() -> COMPONENTS_CONTROLLER + .updateProductFamilyComponent(productFamilyId, "99999", null)); + } + +}