Skip to content

Commit

Permalink
[DE-604] Components tests 2 (#53)
Browse files Browse the repository at this point in the history
 [DE-604] Components tests 2
  • Loading branch information
maciej-nedza authored Dec 8, 2023
1 parent 2324429 commit 36f7089
Show file tree
Hide file tree
Showing 16 changed files with 515 additions and 50 deletions.
40 changes: 37 additions & 3 deletions doc/controllers/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand All @@ -684,7 +684,7 @@ Void updateDefaultPricePointForComponent(

## Response Type

`void`
[`ComponentResponse`](../../doc/models/component-response.md)

## Example Usage

Expand All @@ -693,14 +693,48 @@ 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) {
e.printStackTrace();
}
```

## 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,22 +423,24 @@ private ApiCall<ComponentResponse, ApiException> 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<Void, ApiException> prepareUpdateDefaultPricePointForComponentRequest(
private ApiCall<ComponentResponse, ApiException> prepareUpdateDefaultPricePointForComponentRequest(
final int componentId,
final int pricePointId) throws IOException {
return new ApiCall.Builder<Void, ApiException>()
return new ApiCall.Builder<ComponentResponse, ApiException>()
.globalConfig(getGlobalConfiguration())
.requestBuilder(requestBuilder -> requestBuilder
.server(Server.ENUM_DEFAULT.value())
Expand All @@ -447,9 +449,12 @@ private ApiCall<Void, ApiException> 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
Expand Down
11 changes: 11 additions & 0 deletions tests/src/test/java/com/maxio/advancedbilling/TestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ void shouldNotArchiveNonExistentComponents() {
assertNotFound(() -> COMPONENTS_CONTROLLER.archiveComponent(productFamilyId, "999999"));
}


}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()));
}

}
Original file line number Diff line number Diff line change
@@ -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<ComponentResponse> 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()));
}

}
Original file line number Diff line number Diff line change
@@ -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<ComponentResponse> 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()));
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Loading

0 comments on commit 36f7089

Please sign in to comment.