Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DE-563] Components endpoints tests 1 #48

Merged
merged 7 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
@@ -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())
maciej-nedza marked this conversation as resolved.
Show resolved Hide resolved
.getComponent();

// then
assertThat(foundComponent).usingRecursiveComparison()
.isEqualTo(component);
}

@Test
void shouldNotFindNonExistentComponent() {
assertNotFound(() -> COMPONENTS_CONTROLLER.readComponentByHandle("non-existent-handle"));
}

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

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

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

}