Skip to content

Commit

Permalink
[DE-718] DE-1045 SDK tests for reason and referral codes (#163)
Browse files Browse the repository at this point in the history
* [DE-718] DE-1045 SDK tests for reason and referral codes

* [DE-718] DE-1045 Fix error model in updateReasonCode and listReasonCodes
  • Loading branch information
michalpierog authored Nov 14, 2024
1 parent 20ee38b commit 42b966b
Show file tree
Hide file tree
Showing 11 changed files with 556 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/controllers/reason-codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ try {
| HTTP Status Code | Error Description | Exception Class |
| --- | --- | --- |
| 404 | Not Found | `ApiException` |
| 422 | Unprocessable Entity (WebDAV) | [`ErrorListResponseException`](../../doc/models/error-list-response-exception.md) |


# Delete Reason Code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ private ApiCall<ReasonCodeResponse, ApiException> prepareUpdateReasonCodeRequest
.localErrorCase("404",
ErrorCase.setTemplate("Not Found:'{$response.body}'",
(reason, context) -> new ApiException(reason, context)))
.localErrorCase("422",
ErrorCase.setTemplate("HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.",
(reason, context) -> new ErrorListResponseException(reason, context)))
.globalErrorCase(GLOBAL_ERROR_CASES))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.maxio.advancedbilling.controllers.reasoncodes;

import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.ReasonCodesController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.CreateReasonCode;
import com.maxio.advancedbilling.models.CreateReasonCodeRequest;
import com.maxio.advancedbilling.models.ReasonCode;
import com.maxio.advancedbilling.utils.TestFixtures;
import com.maxio.advancedbilling.utils.TestTeardown;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.ZonedDateTime;

import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertThatErrorListResponse;
import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized;
import static org.assertj.core.api.Assertions.assertThat;

public class ReasonCodesControllerCreateTest {
private static final ReasonCodesController REASON_CODES_CONTROLLER = TestClient.createClient().getReasonCodesController();

@AfterAll
static void deleteReasonCodes() throws IOException, ApiException {
new TestTeardown().deleteReasonCodes();
}

@Test
void shouldCreateReasonCode() throws IOException, ApiException {
// when
ReasonCode response = REASON_CODES_CONTROLLER.createReasonCode(new CreateReasonCodeRequest(new CreateReasonCode(
"NOT_INTERESTED", "I'm not interested in this product.", 1
))).getReasonCode();

// when
assertThat(response.getId()).isNotNull();
assertThat(response.getSiteId()).isEqualTo(TestFixtures.SITE_ID);
assertThat(response.getCode()).isEqualTo("NOT_INTERESTED");
assertThat(response.getPosition()).isEqualTo(1);
assertThat(response.getDescription()).isEqualTo("I'm not interested in this product.");
assertThat(response.getCreatedAt())
.isNotNull()
.isBefore(ZonedDateTime.now());
assertThat(response.getUpdatedAt())
.isEqualTo(response.getCreatedAt());

ReasonCode readResponse = REASON_CODES_CONTROLLER.readReasonCode(response.getId()).getReasonCode();
assertThat(response)
.usingRecursiveComparison()
.isEqualTo(readResponse);
}

@Test
void shouldThrowExceptionIfPropertyIsMissing() {
// when - then
assertThatErrorListResponse(() -> REASON_CODES_CONTROLLER.createReasonCode(
new CreateReasonCodeRequest(new CreateReasonCode())))
.hasErrorCode(422)
.hasErrors("Code: cannot be blank.", "Description: cannot be blank.")
.hasMessage("HTTP Response Not OK. Status code: 422. " +
"Response: '{errors:[Code: cannot be blank.,Description: cannot be blank.]}'.");
}

@Test
void shouldThrowExceptionIfCodeExists() throws IOException, ApiException {
// given
CreateReasonCodeRequest request = new CreateReasonCodeRequest(new CreateReasonCode("existing", "existing", 1));
assertThat(REASON_CODES_CONTROLLER.createReasonCode(request).getReasonCode()).isNotNull();

// when - then
assertThatErrorListResponse(() -> REASON_CODES_CONTROLLER.createReasonCode(request))
.hasErrorCode(422)
.hasErrors("Code: This code is already in use.")
.hasMessage("HTTP Response Not OK. Status code: 422. " +
"Response: '{errors:[Code: This code is already in use.]}'.");
}

@Test
void shouldThrowExceptionOnInvalidCredentials() {
// when - then
assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getReasonCodesController()
.createReasonCode(new CreateReasonCodeRequest(new CreateReasonCode("code", "desc", 1)))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.maxio.advancedbilling.controllers.reasoncodes;

import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.ReasonCodesController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.CreateReasonCode;
import com.maxio.advancedbilling.models.CreateReasonCodeRequest;
import com.maxio.advancedbilling.models.OkResponse;
import com.maxio.advancedbilling.models.ReasonCode;
import com.maxio.advancedbilling.utils.TestTeardown;
import org.junit.jupiter.api.AfterAll;
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 ReasonCodesControllerDeleteTest {
private static final ReasonCodesController REASON_CODES_CONTROLLER = TestClient.createClient().getReasonCodesController();

@AfterAll
static void deleteReasonCodes() throws IOException, ApiException {
new TestTeardown().deleteReasonCodes();
}

@Test
void shouldDeleteReasonCode() throws IOException, ApiException {
// given
ReasonCode createCodeResponse = REASON_CODES_CONTROLLER.createReasonCode(new CreateReasonCodeRequest(new CreateReasonCode(
"NOT_INTERESTED", "I'm not interested in this product.", 1
))).getReasonCode();
ReasonCode readResponse = REASON_CODES_CONTROLLER.readReasonCode(createCodeResponse.getId()).getReasonCode();
assertThat(createCodeResponse)
.usingRecursiveComparison()
.isEqualTo(readResponse);

// when
OkResponse response = REASON_CODES_CONTROLLER.deleteReasonCode(createCodeResponse.getId());
assertThat(response.getOk()).isEqualTo("ok");

assertNotFound(() -> REASON_CODES_CONTROLLER.readReasonCode(createCodeResponse.getId()));
}

@Test
void shouldThrowExceptionIfCodeDoesNotExists() {
// when - then
assertNotFound(() -> REASON_CODES_CONTROLLER.deleteReasonCode(1));
}

@Test
void shouldThrowExceptionOnInvalidCredentials() {
// when - then
assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getReasonCodesController()
.deleteReasonCode(1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.maxio.advancedbilling.controllers.reasoncodes;

import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.ReasonCodesController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.CreateReasonCode;
import com.maxio.advancedbilling.models.CreateReasonCodeRequest;
import com.maxio.advancedbilling.models.ListReasonCodesInput;
import com.maxio.advancedbilling.models.ReasonCodeResponse;
import com.maxio.advancedbilling.utils.TestTeardown;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertThatErrorListResponse;
import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized;
import static org.assertj.core.api.Assertions.assertThat;

public class ReasonCodesControllerListTest {
private static final ReasonCodesController REASON_CODES_CONTROLLER = TestClient.createClient().getReasonCodesController();
private static final List<ReasonCodeResponse> EXISTING_CODES = new ArrayList<>();

@BeforeEach
void createReasonCodes() throws IOException, ApiException {
new TestTeardown().deleteReasonCodes();
EXISTING_CODES.clear();
for (int i = 0; i < 10; ++i) {
ReasonCodeResponse response = REASON_CODES_CONTROLLER.createReasonCode(
new CreateReasonCodeRequest(
new CreateReasonCode("CODE_" + i, "Description_" + i, i)
));
EXISTING_CODES.add(response);
}
}

@AfterEach
void deleteReasonCodes() throws IOException, ApiException {
new TestTeardown().deleteReasonCodes();
}

@Test
void shouldListAllReasonCodes() throws IOException, ApiException {
// when
List<ReasonCodeResponse> reasonCodeResponses = REASON_CODES_CONTROLLER.listReasonCodes(new ListReasonCodesInput());

// then
assertThat(reasonCodeResponses)
.hasSize(10)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyElementsOf(EXISTING_CODES);
}

@Test
void shouldListReasonCodesWithPaging() throws IOException, ApiException {
// when
List<ReasonCodeResponse> firstPage = REASON_CODES_CONTROLLER.listReasonCodes(new ListReasonCodesInput(1, 2));
List<ReasonCodeResponse> secondPage = REASON_CODES_CONTROLLER.listReasonCodes(new ListReasonCodesInput(2, 2));
List<ReasonCodeResponse> bigPage = REASON_CODES_CONTROLLER.listReasonCodes(new ListReasonCodesInput(1, 8));

// then
assertThat(firstPage)
.hasSize(2)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyElementsOf(EXISTING_CODES.subList(0, 2));
assertThat(secondPage)
.hasSize(2)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyElementsOf(EXISTING_CODES.subList(2, 4));
assertThat(bigPage)
.hasSize(8)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyElementsOf(EXISTING_CODES.subList(0, 8));
}

@Test
void shouldReturnEmptyList() throws IOException, ApiException {
// given
new TestTeardown().deleteReasonCodes();

// when
List<ReasonCodeResponse> reasonCodes = REASON_CODES_CONTROLLER.listReasonCodes(new ListReasonCodesInput());

// then
assertThat(reasonCodes)
.isNotNull()
.isEmpty();
}

@Test
void shouldThrowExceptionIfPageIsInvalid() {
// when - then
assertThatErrorListResponse(() -> REASON_CODES_CONTROLLER.listReasonCodes(
new ListReasonCodesInput(0, 2)))
.hasErrorCode(422)
.hasErrors("invalid page: 0")
.hasMessage("HTTP Response Not OK. Status code: 422. Response: '{errors:[invalid page: 0]}'.");
}

@Test
void shouldThrowExceptionOnInvalidCredentials() {
// when - then
assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getReasonCodesController()
.listReasonCodes(new ListReasonCodesInput()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.maxio.advancedbilling.controllers.reasoncodes;

import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.ReasonCodesController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.CreateReasonCode;
import com.maxio.advancedbilling.models.CreateReasonCodeRequest;
import com.maxio.advancedbilling.models.ReasonCode;
import com.maxio.advancedbilling.utils.TestFixtures;
import com.maxio.advancedbilling.utils.TestTeardown;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.ZonedDateTime;

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 ReasonCodesControllerReadTest {
private static final ReasonCodesController REASON_CODES_CONTROLLER = TestClient.createClient().getReasonCodesController();

@AfterEach
void deleteReasonCodes() throws IOException, ApiException {
new TestTeardown().deleteReasonCodes();
}

@Test
void shouldReadReasonCode() throws IOException, ApiException {
// given
ReasonCode createResponse = REASON_CODES_CONTROLLER.createReasonCode(new CreateReasonCodeRequest(new CreateReasonCode(
"NOT_INTERESTED", "I'm not interested in this product.", 1
))).getReasonCode();

// when
ReasonCode response = REASON_CODES_CONTROLLER.readReasonCode(createResponse.getId()).getReasonCode();

// then
assertThat(response.getId()).isNotNull();
assertThat(response.getSiteId()).isEqualTo(TestFixtures.SITE_ID);
assertThat(response.getCode()).isEqualTo("NOT_INTERESTED");
assertThat(response.getPosition()).isEqualTo(1);
assertThat(response.getDescription()).isEqualTo("I'm not interested in this product.");
assertThat(response.getCreatedAt())
.isNotNull()
.isBefore(ZonedDateTime.now());
assertThat(response.getUpdatedAt())
.isEqualTo(response.getCreatedAt());

ReasonCode readResponse = REASON_CODES_CONTROLLER.readReasonCode(response.getId()).getReasonCode();
assertThat(response)
.usingRecursiveComparison()
.isEqualTo(readResponse);
}

@Test
void shouldThrowExceptionIfCodeDoesNotExist() {
// when - then
assertNotFound(() -> REASON_CODES_CONTROLLER.readReasonCode(1));
}

@Test
void shouldThrowExceptionOnInvalidCredentials() throws IOException, ApiException {
// given
ReasonCode createResponse = REASON_CODES_CONTROLLER.createReasonCode(new CreateReasonCodeRequest(new CreateReasonCode(
"NOT_INTERESTED", "I'm not interested in this product.", 1
))).getReasonCode();

// when - then
assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getReasonCodesController()
.readReasonCode(createResponse.getId()));
}
}
Loading

0 comments on commit 42b966b

Please sign in to comment.