From 36f708956778d15d67107382e23f1e616a964993 Mon Sep 17 00:00:00 2001 From: maciej-nedza <76946708+maciej-nedza@users.noreply.github.com> Date: Fri, 8 Dec 2023 13:43:34 +0100 Subject: [PATCH] [DE-604] Components tests 2 (#53) [DE-604] Components tests 2 --- doc/controllers/components.md | 40 ++++- .../controllers/ComponentsController.java | 13 +- .../com/maxio/advancedbilling/TestClient.java | 11 ++ .../ComponentsControllerArchiveTest.java | 1 + .../ComponentsControllerFindTest.java | 13 ++ ...llerListComponentForProductFamilyTest.java | 61 ++++++++ .../ComponentsControllerListTest.java | 49 ++++++ ...trollerPromotePricePointToDefaultTest.java | 104 +++++++++++++ .../ComponentsControllerReadTest.java | 12 ++ .../ComponentsControllerTestBase.java | 42 ++--- ...ollerUpdateProductFamilyComponentTest.java | 13 ++ .../ComponentsControllerUpdateTest.java | 143 ++++++++++++++++++ .../advancedbilling/utils/TestSetup.java | 21 +++ .../advancedbilling/utils/TestTeardown.java | 25 +-- .../utils/assertions/ApiExceptionAssert.java | 2 +- .../utils/assertions/CommonAssertions.java | 15 ++ 16 files changed, 515 insertions(+), 50 deletions(-) create mode 100644 tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerListComponentForProductFamilyTest.java create mode 100644 tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerListTest.java create mode 100644 tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerPromotePricePointToDefaultTest.java create mode 100644 tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateTest.java diff --git a/doc/controllers/components.md b/doc/controllers/components.md index a3923831..726da764 100644 --- a/doc/controllers/components.md +++ b/doc/controllers/components.md @@ -670,7 +670,7 @@ See [Price Points Documentation](https://chargify.zendesk.com/hc/en-us/articles/ Note: Custom price points are not able to be set as the default for a component. ```java -Void updateDefaultPricePointForComponent( +ComponentResponse updateDefaultPricePointForComponent( final int componentId, final int pricePointId) ``` @@ -684,7 +684,7 @@ Void updateDefaultPricePointForComponent( ## Response Type -`void` +[`ComponentResponse`](../../doc/models/component-response.md) ## Example Usage @@ -693,7 +693,8 @@ int componentId = 222; int pricePointId = 10; try { - componentsController.updateDefaultPricePointForComponent(componentId, pricePointId); + ComponentResponse result = componentsController.updateDefaultPricePointForComponent(componentId, pricePointId); + System.out.println(result); } catch (ApiException e) { e.printStackTrace(); } catch (IOException e) { @@ -701,6 +702,39 @@ try { } ``` +## Example Response *(as JSON)* + +```json +{ + "component": { + "id": 292609, + "name": "Text messages", + "pricing_scheme": "stairstep", + "unit_name": "text message", + "unit_price": null, + "product_family_id": 528484, + "price_per_unit_in_cents": null, + "kind": "metered_component", + "archived": false, + "taxable": false, + "description": null, + "created_at": "2019-08-02T05:54:53-04:00", + "prices": [ + { + "id": 47, + "component_id": 292609, + "starting_quantity": 1, + "ending_quantity": null, + "unit_price": "1.0", + "price_point_id": 173, + "formatted_unit_price": "$1.00" + } + ], + "default_price_point_name": "Original" + } +} +``` + # List Components for Product Family diff --git a/src/main/java/com/maxio/advancedbilling/controllers/ComponentsController.java b/src/main/java/com/maxio/advancedbilling/controllers/ComponentsController.java index 2878886a..623e9ff3 100644 --- a/src/main/java/com/maxio/advancedbilling/controllers/ComponentsController.java +++ b/src/main/java/com/maxio/advancedbilling/controllers/ComponentsController.java @@ -423,22 +423,24 @@ private ApiCall prepareUpdateComponentRequest( * @param componentId Required parameter: The Chargify id of the component to which the price * point belongs * @param pricePointId Required parameter: The Chargify id of the price point + * @return Returns the ComponentResponse response from the API call * @throws ApiException Represents error response from the server. * @throws IOException Signals that an I/O exception of some sort has occurred. */ - public void updateDefaultPricePointForComponent( + public ComponentResponse updateDefaultPricePointForComponent( final int componentId, final int pricePointId) throws ApiException, IOException { - prepareUpdateDefaultPricePointForComponentRequest(componentId, pricePointId).execute(); + return prepareUpdateDefaultPricePointForComponentRequest(componentId, + pricePointId).execute(); } /** * Builds the ApiCall object for updateDefaultPricePointForComponent. */ - private ApiCall prepareUpdateDefaultPricePointForComponentRequest( + private ApiCall prepareUpdateDefaultPricePointForComponentRequest( final int componentId, final int pricePointId) throws IOException { - return new ApiCall.Builder() + return new ApiCall.Builder() .globalConfig(getGlobalConfiguration()) .requestBuilder(requestBuilder -> requestBuilder .server(Server.ENUM_DEFAULT.value()) @@ -447,9 +449,12 @@ private ApiCall prepareUpdateDefaultPricePointForComponentRe .shouldEncode(true)) .templateParam(param -> param.key("price_point_id").value(pricePointId).isRequired(false) .shouldEncode(true)) + .headerParam(param -> param.key("accept").value("application/json")) .authenticationKey(BaseController.AUTHENTICATION_KEY) .httpMethod(HttpMethod.PUT)) .responseHandler(responseHandler -> responseHandler + .deserializer( + response -> ApiHelper.deserialize(response, ComponentResponse.class)) .nullify404(false) .globalErrorCase(GLOBAL_ERROR_CASES)) .endpointConfiguration(param -> param diff --git a/tests/src/test/java/com/maxio/advancedbilling/TestClient.java b/tests/src/test/java/com/maxio/advancedbilling/TestClient.java index bd96d1db..5cd682e0 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/TestClient.java +++ b/tests/src/test/java/com/maxio/advancedbilling/TestClient.java @@ -18,6 +18,17 @@ public static AdvancedBillingClient createClient() { .build(); } + public static AdvancedBillingClient createInvalidCredentialsClient() { + return new AdvancedBillingClient.Builder() + .httpClientConfig(configBuilder -> configBuilder + .timeout(10)) + .basicAuthCredentials("123", "abc") + .environment(Environment.PRODUCTION) + .subdomain(getEnvValue(SUBDOMAIN_ENV)) + .domain(getEnvValue(DOMAIN_ENV)) + .build(); + } + private static String getEnvValue(String key) { String envValue = System.getenv(key); if (envValue == null) { 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 index 8ed5e39d..38073c23 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerArchiveTest.java +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerArchiveTest.java @@ -55,4 +55,5 @@ void shouldNotArchiveNonExistentComponents() { 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 index b3a1ae01..a7d1bc90 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerFindTest.java +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerFindTest.java @@ -1,12 +1,15 @@ package com.maxio.advancedbilling.controllers.components; +import com.maxio.advancedbilling.TestClient; import com.maxio.advancedbilling.exceptions.ApiException; import com.maxio.advancedbilling.models.Component; +import com.maxio.advancedbilling.models.ListComponentsInput; import org.junit.jupiter.api.Test; import java.io.IOException; import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; import static org.assertj.core.api.Assertions.assertThat; public class ComponentsControllerFindTest extends ComponentsControllerTestBase { @@ -31,4 +34,14 @@ void shouldNotFindNonExistentComponent() { assertNotFound(() -> COMPONENTS_CONTROLLER.readComponentByHandle("non-existent-handle")); } + @Test + void shouldNotFindComponentWhenProvidingInvalidCredentials() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when-then + assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getComponentsController() + .readComponentByHandle(component.getHandle())); + } + } diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerListComponentForProductFamilyTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerListComponentForProductFamilyTest.java new file mode 100644 index 00000000..5cb420ba --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerListComponentForProductFamilyTest.java @@ -0,0 +1,61 @@ +package com.maxio.advancedbilling.controllers.components; + +import com.maxio.advancedbilling.TestClient; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.ComponentResponse; +import com.maxio.advancedbilling.models.ListComponentsForProductFamilyInput; +import com.maxio.advancedbilling.utils.TestTeardown; +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.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; +import static org.assertj.core.api.Assertions.assertThat; + +public class ComponentsControllerListComponentForProductFamilyTest extends ComponentsControllerTestBase { + + @BeforeAll + static void removeActiveComponents() throws IOException, ApiException { + new TestTeardown().archiveComponents(); + } + + @Test + void shouldListComponentsForProductFamily() throws IOException, ApiException { + // given + ComponentResponse component1 = new ComponentResponse(createQuantityBasedComponent()); + ComponentResponse component2 = new ComponentResponse(createMeteredComponent(2)); + + // when + List componentResponseList = COMPONENTS_CONTROLLER.listComponentsForProductFamily( + new ListComponentsForProductFamilyInput.Builder() + .productFamilyId(productFamilyId) + .includeArchived(false) + .build() + ); + + // then + assertThat(componentResponseList) + .usingRecursiveFieldByFieldElementComparator() + .containsExactlyInAnyOrder(component1, component2); + } + + @Test + void shouldNotListComponentsForNonExistentProductFamily() { + assertNotFound(() -> COMPONENTS_CONTROLLER + .listComponentsForProductFamily(new ListComponentsForProductFamilyInput.Builder() + .productFamilyId(4) + .build())); + } + + @Test + void shouldNotListComponentsForProductFamilyWhenProvidingInvalidCredentials() { + assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getComponentsController() + .listComponentsForProductFamily(new ListComponentsForProductFamilyInput.Builder() + .productFamilyId(4) + .build())); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerListTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerListTest.java new file mode 100644 index 00000000..3a8b2aab --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerListTest.java @@ -0,0 +1,49 @@ +package com.maxio.advancedbilling.controllers.components; + +import com.maxio.advancedbilling.TestClient; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.ComponentResponse; +import com.maxio.advancedbilling.models.ListComponentsInput; +import com.maxio.advancedbilling.utils.TestTeardown; +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.assertions.CommonAssertions.assertUnauthorized; +import static org.assertj.core.api.Assertions.assertThat; + +public class ComponentsControllerListTest extends ComponentsControllerTestBase { + + @BeforeAll + static void removeActiveComponents() throws IOException, ApiException { + new TestTeardown().archiveComponents(); + } + + @Test + void shouldListComponents() throws IOException, ApiException { + // given + ComponentResponse component1 = new ComponentResponse(createQuantityBasedComponent()); + ComponentResponse component2 = new ComponentResponse(createMeteredComponent(2)); + + // when + List componentResponseList = COMPONENTS_CONTROLLER.listComponents( + new ListComponentsInput.Builder() + .includeArchived(false) + .build() + ); + + // then + assertThat(componentResponseList) + .usingRecursiveFieldByFieldElementComparator() + .containsExactlyInAnyOrder(component1, component2); + } + + @Test + void shouldNotListComponentsWhenProvidingInvalidCredentials() { + assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getComponentsController() + .listComponents(new ListComponentsInput())); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerPromotePricePointToDefaultTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerPromotePricePointToDefaultTest.java new file mode 100644 index 00000000..80a8426b --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerPromotePricePointToDefaultTest.java @@ -0,0 +1,104 @@ +package com.maxio.advancedbilling.controllers.components; + +import com.maxio.advancedbilling.TestClient; +import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.Component; +import com.maxio.advancedbilling.models.ComponentPricePoint; +import com.maxio.advancedbilling.models.CreateComponentPricePoint; +import com.maxio.advancedbilling.models.CreateComponentPricePointRequest; +import com.maxio.advancedbilling.models.Price; +import com.maxio.advancedbilling.models.containers.CreateComponentPricePointRequestPricePoint; +import com.maxio.advancedbilling.models.containers.PriceEndingQuantity; +import com.maxio.advancedbilling.models.containers.PriceStartingQuantity; +import com.maxio.advancedbilling.models.containers.PriceUnitPrice; +import com.maxio.advancedbilling.utils.assertions.ApiExceptionAssert; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; + +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; +import static org.assertj.core.api.Assertions.assertThat; + +public class ComponentsControllerPromotePricePointToDefaultTest extends ComponentsControllerTestBase { + + @Test + void shouldPromoteComponentPricePointToDefault() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + ComponentPricePoint catalogPricePoint = createCatalogPricePoint(component.getId()); + + // when + Component componentWithUpdatedPricePoint = COMPONENTS_CONTROLLER + .updateDefaultPricePointForComponent( + component.getId(), + catalogPricePoint.getId() + ).getComponent(); + + // then + assertThat(componentWithUpdatedPricePoint).usingRecursiveComparison() + .ignoringFields("updatedAt", "pricingScheme", "defaultPricePointId", "prices", + "defaultPricePointName", "pricePointCount", "unitPrice") + .isEqualTo(component); + assertThat(componentWithUpdatedPricePoint.getDefaultPricePointId()).isEqualTo(catalogPricePoint.getId()); + assertThat(componentWithUpdatedPricePoint.getPricingScheme()).isEqualTo(catalogPricePoint.getPricingScheme()); + assertThat(componentWithUpdatedPricePoint.getPrices()).usingRecursiveComparison() + .isEqualTo(catalogPricePoint.getPrices()); + assertThat(componentWithUpdatedPricePoint.getDefaultPricePointName()).isEqualTo(catalogPricePoint.getName()); + assertThat(componentWithUpdatedPricePoint.getUnitPrice()).isNull(); + assertThat(componentWithUpdatedPricePoint.getPricePointCount()).isEqualTo(2); + } + + @SuppressWarnings("rawtypes") + @Test + void shouldNotPromoteComponentPricePointToDefaultUsingNotExistentPricePoint() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when-then + new ApiExceptionAssert(() -> COMPONENTS_CONTROLLER + .updateDefaultPricePointForComponent(component.getId(), 3)) + .hasErrorCode(422) + .hasHttpNotOkMessage(); + } + + @Test + void shouldNotPromoteComponentPricePointToDefaultWhenProvidingInvalidCredentials() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + ComponentPricePoint catalogPricePoint = createCatalogPricePoint(component.getId()); + + // when-then + assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getComponentsController() + .updateDefaultPricePointForComponent(component.getId(), catalogPricePoint.getId()) + ); + } + + private ComponentPricePoint createCatalogPricePoint(int componentId) throws IOException, ApiException { + CreateComponentPricePoint createComponentPricePoint = new CreateComponentPricePoint.Builder() + .name("New price point") + .handle("new-price-point-" + randomNumeric(5)) + .pricingScheme("stairstep") + .prices( + List.of( + new Price.Builder() + .unitPrice(PriceUnitPrice.fromPrecision(5.00)) + .startingQuantity(PriceStartingQuantity.fromNumber(1)) + .endingQuantity(PriceEndingQuantity.fromNumber(2)) + .build() + ) + ) + .build(); + + return COMPONENTS_CONTROLLER + .createComponentPricePoint(componentId, + new CreateComponentPricePointRequest( + CreateComponentPricePointRequestPricePoint + .fromCreateComponentPricePoint( + createComponentPricePoint + ) + )).getPricePoint(); + } + +} 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 index 24a8ad10..ff9506a9 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerReadTest.java +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerReadTest.java @@ -1,5 +1,6 @@ package com.maxio.advancedbilling.controllers.components; +import com.maxio.advancedbilling.TestClient; import com.maxio.advancedbilling.exceptions.ApiException; import com.maxio.advancedbilling.models.Component; import org.junit.jupiter.api.Test; @@ -7,6 +8,7 @@ import java.io.IOException; import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; import static org.assertj.core.api.Assertions.assertThat; public class ComponentsControllerReadTest extends ComponentsControllerTestBase { @@ -53,4 +55,14 @@ void shouldNotReadNonExistentComponentUsingHandle() { "handle:non-existent-id")); } + @Test + void shouldNotReadComponentWhenProvidingInvalidCredentials() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when-then + assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getComponentsController() + .readComponentById(productFamilyId, String.valueOf(component.getId()))); + } + } 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 index 513cc4e2..896d4c9e 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerTestBase.java +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerTestBase.java @@ -2,18 +2,10 @@ 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 com.maxio.advancedbilling.models.ProductFamily; +import com.maxio.advancedbilling.utils.TestSetup; import org.junit.jupiter.api.BeforeAll; import java.io.IOException; @@ -21,34 +13,22 @@ public class ComponentsControllerTestBase { protected static final ComponentsController COMPONENTS_CONTROLLER = TestClient.createClient().getComponentsController(); + protected static ProductFamily productFamily; protected static int productFamilyId; + protected static final TestSetup TEST_SETUP = new TestSetup(); @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(); + productFamily = TEST_SETUP.createProductFamily(); + productFamilyId = productFamily.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(); + return TEST_SETUP.createQuantityBasedComponent(productFamilyId); + } + + protected Component createMeteredComponent(double unitPrice) throws IOException, ApiException { + return TEST_SETUP.createMeteredComponent(productFamily, unitPrice); } } 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 index 19cb3eb1..2d83ea17 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateProductFamilyComponentTest.java +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateProductFamilyComponentTest.java @@ -1,5 +1,6 @@ package com.maxio.advancedbilling.controllers.components; +import com.maxio.advancedbilling.TestClient; import com.maxio.advancedbilling.exceptions.ApiException; import com.maxio.advancedbilling.exceptions.ErrorListResponseException; import com.maxio.advancedbilling.models.Component; @@ -13,6 +14,7 @@ import java.io.IOException; import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnprocessableEntity; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; @@ -132,4 +134,15 @@ void shouldNotUpdateNonExistentComponent() { .updateProductFamilyComponent(productFamilyId, "99999", null)); } + @Test + void shouldNotUpdateComponentWhenProvidingInvalidCredentials() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when-then + assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getComponentsController() + .updateProductFamilyComponent(productFamilyId, String.valueOf(component.getId()), + new UpdateComponentRequest())); + } + } diff --git a/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateTest.java b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateTest.java new file mode 100644 index 00000000..eda1441b --- /dev/null +++ b/tests/src/test/java/com/maxio/advancedbilling/controllers/components/ComponentsControllerUpdateTest.java @@ -0,0 +1,143 @@ +package com.maxio.advancedbilling.controllers.components; + +import com.maxio.advancedbilling.TestClient; +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.assertions.CommonAssertions.assertNotFound; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized; +import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnprocessableEntity; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +public class ComponentsControllerUpdateTest 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 + .updateComponent(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.updateComponent(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.updateComponent(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.updateComponent("99999", null)); + } + + @Test + void shouldNotUpdateComponentWhenProvidingInvalidCredentials() throws IOException, ApiException { + // given + Component component = createQuantityBasedComponent(); + + // when-then + assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getComponentsController() + .updateComponent(String.valueOf(component.getId()), new UpdateComponentRequest())); + } + +} diff --git a/tests/src/test/java/com/maxio/advancedbilling/utils/TestSetup.java b/tests/src/test/java/com/maxio/advancedbilling/utils/TestSetup.java index ed08d477..86a655fc 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/utils/TestSetup.java +++ b/tests/src/test/java/com/maxio/advancedbilling/utils/TestSetup.java @@ -15,16 +15,20 @@ import com.maxio.advancedbilling.models.CreateOrUpdateProductRequest; import com.maxio.advancedbilling.models.CreateProductFamily; import com.maxio.advancedbilling.models.CreateProductFamilyRequest; +import com.maxio.advancedbilling.models.CreateQuantityBasedComponent; import com.maxio.advancedbilling.models.Customer; import com.maxio.advancedbilling.models.IntervalUnit; import com.maxio.advancedbilling.models.MeteredComponent; import com.maxio.advancedbilling.models.PricingScheme; import com.maxio.advancedbilling.models.Product; import com.maxio.advancedbilling.models.ProductFamily; +import com.maxio.advancedbilling.models.QuantityBasedComponent; import com.maxio.advancedbilling.models.containers.CreateComponentBody; import com.maxio.advancedbilling.models.containers.CreateOrUpdateCouponCoupon; import com.maxio.advancedbilling.models.containers.CreateOrUpdatePercentageCouponPercentage; import com.maxio.advancedbilling.models.containers.MeteredComponentUnitPrice; +import com.maxio.advancedbilling.models.containers.QuantityBasedComponentUnitPrice; +import org.apache.commons.lang3.RandomStringUtils; import java.io.IOException; @@ -82,6 +86,23 @@ public Component createMeteredComponent(ProductFamily productFamily, double unit ).getComponent(); } + public Component createQuantityBasedComponent(int productFamilyId) 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 advancedBillingClient.getComponentsController().createComponent(productFamilyId, + ComponentKindPath.QUANTITY_BASED_COMPONENTS, + CreateComponentBody.fromCreateQuantityBasedComponent(createQuantityBasedComponent)) + .getComponent(); + } + public Customer createCustomer() throws IOException, ApiException { String firstName = "John" + randomNumeric(2); String lastName = "Doe" + randomNumeric(2); diff --git a/tests/src/test/java/com/maxio/advancedbilling/utils/TestTeardown.java b/tests/src/test/java/com/maxio/advancedbilling/utils/TestTeardown.java index 3022f2e3..95cb93dd 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/utils/TestTeardown.java +++ b/tests/src/test/java/com/maxio/advancedbilling/utils/TestTeardown.java @@ -3,9 +3,9 @@ import com.maxio.advancedbilling.AdvancedBillingClient; import com.maxio.advancedbilling.TestClient; import com.maxio.advancedbilling.exceptions.ApiException; +import com.maxio.advancedbilling.models.ComponentResponse; import com.maxio.advancedbilling.models.Customer; -import com.maxio.advancedbilling.models.CustomerResponse; -import com.maxio.advancedbilling.models.ListCustomersInput; +import com.maxio.advancedbilling.models.ListComponentsInput; import com.maxio.advancedbilling.models.SubscriptionResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,21 +34,24 @@ public void deleteCustomer(Customer customer) throws IOException, ApiException { LOGGER.info("Customer deleted: {}", customer.getId()); } - public void deleteCustomers() throws IOException, ApiException { - List customers = listCustomers(); - while (!customers.isEmpty()) { - for (CustomerResponse customer : customers) { - deleteCustomer(customer.getCustomer()); + public void archiveComponents() throws IOException, ApiException { + List components = listComponents(); + while (!components.isEmpty()) { + for (ComponentResponse component : components) { + advancedBillingClient.getComponentsController() + .archiveComponent(component.getComponent().getProductFamilyId(), + String.valueOf(component.getComponent().getId())); } - customers = listCustomers(); + components = listComponents(); } } - public List listCustomers() throws IOException, ApiException { - return advancedBillingClient.getCustomersController() - .listCustomers(new ListCustomersInput.Builder() + private List listComponents() throws IOException, ApiException { + return advancedBillingClient.getComponentsController() + .listComponents(new ListComponentsInput.Builder() .page(1) .perPage(200) .build()); } + } diff --git a/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/ApiExceptionAssert.java b/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/ApiExceptionAssert.java index ec1df59c..7b00d0a6 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/ApiExceptionAssert.java +++ b/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/ApiExceptionAssert.java @@ -12,7 +12,7 @@ public class ApiExceptionAssert ApiExceptionAssert(Callable runnable) { + public ApiExceptionAssert(Callable runnable) { super(runnable); } diff --git a/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/CommonAssertions.java b/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/CommonAssertions.java index ff1960f3..e1ebb5bb 100644 --- a/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/CommonAssertions.java +++ b/tests/src/test/java/com/maxio/advancedbilling/utils/assertions/CommonAssertions.java @@ -65,4 +65,19 @@ public static void assertUnprocessableEntity(Class e .satisfies(e -> assertThat(e.getResponseCode()).isEqualTo(422)) .satisfies(additionalRequirements); } + + public static void assertUnauthorized(ThrowableAssert.ThrowingCallable throwingCallable) { + assertUnauthorized(throwingCallable, "HTTP Response Not OK", "HTTP Basic: Access denied."); + } + + public static void assertUnauthorized(ThrowableAssert.ThrowingCallable throwingCallable, + String exceptionMessage, + String responseBody) { + assertThatExceptionOfType(ApiException.class) + .isThrownBy(throwingCallable) + .withMessage(exceptionMessage) + .returns(401, ApiException::getResponseCode) + .returns(responseBody, ex -> ex.getHttpContext().getResponse().getBody().strip()); + } + }