Skip to content

Commit

Permalink
[MODORDERS-1098] Converting API unit tests - part 3 (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
damien-git authored May 24, 2024
1 parent 585a7ea commit fc5216f
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 455 deletions.
4 changes: 4 additions & 0 deletions src/test/java/org/folio/ApiTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.folio.rest.core.RestClientTest;
import org.folio.rest.core.exceptions.ExceptionUtilTest;
import org.folio.rest.impl.AcquisitionMethodAPITest;
import org.folio.rest.impl.BaseApiTest;
import org.folio.rest.impl.CheckinReceivingApiTest;
import org.folio.rest.impl.ExportHistoryImplTest;
import org.folio.rest.impl.HoldingsSummaryAPITest;
Expand Down Expand Up @@ -506,4 +507,7 @@ class UserServiceTestNested extends UserServiceTest {
class CirculationRequestsRetrieverTestNested extends CirculationRequestsRetrieverTest {
}

@Nested
class BaseApiTestNested extends BaseApiTest {
}
}
26 changes: 26 additions & 0 deletions src/test/java/org/folio/helper/PurchaseOrderHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.folio.helper;

import static io.vertx.core.Future.succeededFuture;
import static org.folio.TestUtils.getMinimalContentCompositePoLine;
import static org.folio.TestUtils.getMinimalContentCompositePurchaseOrder;
import static org.folio.TestUtils.getMockData;
import static org.folio.orders.utils.HelperUtils.ORDER_CONFIG_MODULE_NAME;
import static org.folio.rest.jaxrs.model.CompositePurchaseOrder.WorkflowStatus.OPEN;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -17,9 +21,14 @@
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import io.vertx.core.json.JsonObject;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import org.folio.models.CompositeOrderRetrieveHolder;
import org.folio.models.ItemStatus;
import org.folio.rest.acq.model.OrderInvoiceRelationship;
Expand Down Expand Up @@ -194,6 +203,23 @@ void testPutPendingCompositeOrder() throws IOException {
assertTrue(future.succeeded());
}

@Test
@DisplayName("Test source validation in line")
void testSourceValidationInLine() {
// Note: RMB schema validation is not reliable in unit tests with MockServer (it does not always return the same code),
// but we can check the same validation using a Validator.
CompositePurchaseOrder compPO = getMinimalContentCompositePurchaseOrder();
CompositePoLine poLine = getMinimalContentCompositePoLine();
poLine.setSource(null);
compPO.getCompositePoLines().add(poLine);
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
Validator schemaValidator = factory.getValidator();
Set<ConstraintViolation<CompositePurchaseOrder>> violations = schemaValidator.validate(compPO);
assertThat(violations, hasSize(1));
assertEquals(violations.iterator().next().getPropertyPath().toString(), "compositePoLines[0].source");
}
}

@Test
void testDeleteOrderLinkedToInvoiceWithError(VertxTestContext vertxTestContext) {
// given
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/folio/orders/utils/PoLineCommonUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.folio.rest.core.exceptions.HttpException;
import org.folio.rest.jaxrs.model.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -124,4 +126,36 @@ void testShouldThrowExceptionBecauseRequiredFieldWasUpdated() {
assertEquals(400, exception.getCode());
assertEquals(errorMessage, exception.getMessage());
}

@ParameterizedTest
@CsvSource(value = {"false:true:Other::Instance:true",
"true:false:Other:None::true",
"true:false:Other:Instance::false",
"true:false:Physical Resource:None::true",
"true:false:Physical Resource:Instance::false",
"false:true:Electronic Resource::None:true",
"false:true:Electronic Resource::Instance:false",
"true:true:P/E Mix:None:None:true",
"true:true:P/E Mix:Instance:None:false",
"true:true:P/E Mix:None:Instance:false"
}, delimiter = ':')
void testIsInventoryUpdateNotRequired(Boolean withPhysical, Boolean withEResource, String orderFormat,
String physicalCreateInventory, String eresourceCreateInventory, Boolean updateNotRequired) {
CompositePoLine poLine = new CompositePoLine();
if (withPhysical) {
poLine.setPhysical(new Physical());
}
if (withEResource) {
poLine.setEresource(new Eresource());
}
poLine.setOrderFormat(CompositePoLine.OrderFormat.fromValue(orderFormat));
if (physicalCreateInventory != null) {
poLine.getPhysical().setCreateInventory(Physical.CreateInventory.fromValue(physicalCreateInventory));
}
if (eresourceCreateInventory != null) {
poLine.getEresource().setCreateInventory(Eresource.CreateInventory.fromValue(eresourceCreateInventory));
}
boolean result = PoLineCommonUtil.isInventoryUpdateNotRequired(poLine);
assertEquals(result, updateNotRequired);
}
}
63 changes: 63 additions & 0 deletions src/test/java/org/folio/rest/impl/BaseApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.folio.rest.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import org.folio.rest.core.exceptions.HttpException;
import org.folio.rest.jaxrs.model.Errors;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import javax.ws.rs.core.Response;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class BaseApiTest {
private AutoCloseable mockitoMocks;
private BaseApi baseApi;
@Mock
Handler<AsyncResult<Response>> asyncResultHandler;
@Captor
private ArgumentCaptor<Future<Response>> asyncResultCaptor;

@BeforeEach
void beforeEach() {
mockitoMocks = MockitoAnnotations.openMocks(this);
baseApi = new BaseApi();
}

@AfterEach
void resetMocks() throws Exception {
mockitoMocks.close();
}

@Test
@DisplayName("Test handleErrorResponse")
void testHandleErrorResponse() {
// Given
Throwable t = new HttpException(123, "test");
doNothing()
.when(asyncResultHandler).handle(any());

// When
baseApi.handleErrorResponse(asyncResultHandler, t);

// Then
verify(asyncResultHandler, times(1)).handle(asyncResultCaptor.capture());
Response response = asyncResultCaptor.getAllValues().get(0).result();
assertThat(response.getStatus(), equalTo(123));
Errors errors = (Errors)response.getEntity();
assertThat(errors.getErrors().get(0).getMessage(), equalTo("test"));
}
}
Loading

0 comments on commit fc5216f

Please sign in to comment.