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-526] Java test CRUD for customer endpoints (update) #15

Merged
merged 1 commit into from
Nov 8, 2023
Merged
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,226 @@
package com.maxio.advancedbilling.controllers;

import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.exceptions.CustomerErrorResponseException;
import com.maxio.advancedbilling.models.CreateCustomer;
import com.maxio.advancedbilling.models.CreateCustomerRequest;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.CustomerError;
import com.maxio.advancedbilling.models.CustomerResponse;
import com.maxio.advancedbilling.models.UpdateCustomer;
import com.maxio.advancedbilling.models.UpdateCustomerRequest;
import com.maxio.advancedbilling.models.containers.CustomerErrorResponseErrors;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
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.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

class CustomersControllerUpdateTest {

private final CustomersController customersController = TestClient.createClient().getCustomersController();

private Integer customerId;

@BeforeEach
void setUp() throws IOException, ApiException {
customerId = customersController
.createCustomer(new CreateCustomerRequest()
.toBuilder()
.customer(new CreateCustomer()
.toBuilder()
.firstName("Cathryn")
.lastName("Washington")
.email("[email protected]")
.reference(RandomStringUtils.randomAlphanumeric(10))
.build()
)
.build()
)
.getCustomer()
.getId();
}

@Test
void shouldUpdateCustomerWithNewFirstName() throws IOException, ApiException {
// when
Customer customer = customersController
.updateCustomer(
customerId,
new UpdateCustomerRequest(new UpdateCustomer().toBuilder().firstName("Martha").build())
)
.getCustomer();

// then
assertThat(customer.getFirstName()).isEqualTo("Martha");
assertThat(customer.getLastName()).isEqualTo("Washington");
assertThat(customer.getEmail()).isEqualTo("[email protected]");
}

@Test
void shouldUpdateCustomerWithNewLastName() throws IOException, ApiException {
// when
Customer customer = customersController
.updateCustomer(
customerId,
new UpdateCustomerRequest(new UpdateCustomer().toBuilder().lastName("Newman").build())
)
.getCustomer();

// then
assertThat(customer.getFirstName()).isEqualTo("Cathryn");
assertThat(customer.getLastName()).isEqualTo("Newman");
assertThat(customer.getEmail()).isEqualTo("[email protected]");
}

@Test
void shouldUpdateCustomerWithNewEmail() throws IOException, ApiException {
// when
Customer customer = customersController
.updateCustomer(
customerId,
new UpdateCustomerRequest(new UpdateCustomer().toBuilder().email("[email protected]").build())
)
.getCustomer();

// then
assertThat(customer.getFirstName()).isEqualTo("Cathryn");
assertThat(customer.getLastName()).isEqualTo("Washington");
assertThat(customer.getEmail()).isEqualTo("[email protected]");
}

@Test
void shouldUpdateCustomerWithNewProperties() throws IOException, ApiException {
// when
Customer customer = customersController
.updateCustomer(
customerId,
new UpdateCustomerRequest(new UpdateCustomer()
.toBuilder()
.firstName("Martha")
.lastName("Newman")
.email("[email protected]")
.country("US")
.state("NY")
.city("New York")
.build()
)
)
.getCustomer();

// then
assertThat(customer.getFirstName()).isEqualTo("Martha");
assertThat(customer.getLastName()).isEqualTo("Newman");
assertThat(customer.getEmail()).isEqualTo("[email protected]");
assertThat(customer.getCountry()).isEqualTo("US");
assertThat(customer.getState()).isEqualTo("NY");
assertThat(customer.getCity()).isEqualTo("New York");
}

@Test
void shouldReturnNullWhenNotExists() throws IOException, ApiException {
int notExistingCustomerId = 123;

// when
CustomerResponse customerResponse = customersController
.updateCustomer(notExistingCustomerId, new UpdateCustomerRequest());

// then
assertThat(customerResponse).isNull();
}

@Test
void shouldNotUpdateCustomerWhenRequestIsNull() {
// when - then
assertThatExceptionOfType(CustomerErrorResponseException.class)
.isThrownBy(() -> customersController.updateCustomer(customerId, null))
.withMessage("Unprocessable Entity (WebDAV)")
.satisfies(e -> {
assertThat(e.getResponseCode()).isEqualTo(422);
assertThat(e.getErrors().toString()).isEqualTo(
CustomerErrorResponseErrors
.fromCustomerError(new CustomerError("can't be blank"))
.toString()
);
});
}

@Test
void shouldNotUpdateCustomerWhenRequestIsEmpty() {
// when - then
assertThatExceptionOfType(CustomerErrorResponseException.class)
.isThrownBy(() -> customersController.updateCustomer(
customerId,
new UpdateCustomerRequest(new UpdateCustomer())
))
.withMessage("Unprocessable Entity (WebDAV)")
.satisfies(e -> {
assertThat(e.getResponseCode()).isEqualTo(422);
assertThat(e.getErrors().toString()).isEqualTo(
CustomerErrorResponseErrors
.fromCustomerError(new CustomerError("can't be blank"))
.toString()
);
});
}

@ParameterizedTest
@MethodSource("argsForShouldNotUpdateCustomerWhenBasicParametersAreBlank")
void shouldNotUpdateCustomerWhenBasicParametersAreBlank(UpdateCustomer updateCustomer, List<String> errorMessages) {
// when - then
assertThatExceptionOfType(CustomerErrorResponseException.class)
.isThrownBy(() -> customersController.updateCustomer(
customerId, new UpdateCustomerRequest(updateCustomer))
)
.withMessage("Unprocessable Entity (WebDAV)")
.satisfies(e -> {
assertThat(e.getResponseCode()).isEqualTo(422);
assertThat(e.getErrors().toString())
.isEqualTo(CustomerErrorResponseErrors.fromListOfString(errorMessages).toString());
});
}

private static Stream<Arguments> argsForShouldNotUpdateCustomerWhenBasicParametersAreBlank() {
return Stream.of(
Arguments.of(
new UpdateCustomer().toBuilder().firstName("").lastName("").email("").build(),
Arrays.asList("First name: cannot be blank.", "Last name: cannot be blank.", "Email address: cannot be blank.")
),
Arguments.of(
new UpdateCustomer().toBuilder().firstName("").build(),
Collections.singletonList("First name: cannot be blank.")
),
Arguments.of(
new UpdateCustomer().toBuilder().lastName("").build(),
Collections.singletonList("Last name: cannot be blank.")
),
Arguments.of(
new UpdateCustomer().toBuilder().email("").build(),
Collections.singletonList("Email address: cannot be blank.")
),
Arguments.of(
new UpdateCustomer().toBuilder().firstName("").lastName("").build(),
Arrays.asList("First name: cannot be blank.", "Last name: cannot be blank.")
),
Arguments.of(
new UpdateCustomer().toBuilder().firstName("").email("").build(),
Arrays.asList("First name: cannot be blank.", "Email address: cannot be blank.")
),
Arguments.of(
new UpdateCustomer().toBuilder().lastName("").email("").build(),
Arrays.asList("Last name: cannot be blank.", "Email address: cannot be blank.")
)
);
}
}