Skip to content

Commit

Permalink
MCBFF-21: UT
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonAntonich committed Nov 26, 2024
1 parent b0f33ab commit 5163af0
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.folio.circulationbff.api;

import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.jsonResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static org.apache.http.HttpStatus.SC_CREATED;
import static org.apache.http.HttpStatus.SC_OK;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.folio.circulationbff.domain.dto.EcsRequestExternal;
import org.folio.circulationbff.domain.dto.EcsTlr;
import org.folio.circulationbff.domain.dto.UserTenant;
import org.folio.circulationbff.domain.dto.UserTenantCollection;
import org.folio.spring.integration.XOkapiHeaders;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.github.tomakehurst.wiremock.client.WireMock;

import lombok.SneakyThrows;

class EcsExternalRequestApiTest extends BaseIT {
private static final String TLR_CREATE_ECS_EXTERNAL_REQUEST_URL =
"/tlr/create-ecs-request-external";
private static final String CIRCULATION_BFF_CREATE_ECS_EXTERNAL_REQUEST_URL =
"/circulation-bff/create-ecs-request-external";
private static final String TEST_CENTRAL_TENANT_ID = "testCentralTenantId";

@Test
@SneakyThrows
void postEcsRequestExternalTest() {
EcsRequestExternal requestExternal = buildEcsRequestExternal();
mockEcsTlrExternalRequestCreating(requestExternal);
mockUserTenants();
mockPerform(requestExternal);

wireMockServer.verify(1,
postRequestedFor(urlPathMatching(TLR_CREATE_ECS_EXTERNAL_REQUEST_URL))
.withHeader(XOkapiHeaders.TENANT, equalTo(TEST_CENTRAL_TENANT_ID)));
}

private static void mockUserTenants() {
UserTenant userTenant = new UserTenant();
userTenant.setCentralTenantId(TEST_CENTRAL_TENANT_ID);
UserTenantCollection userTenants = new UserTenantCollection(List.of(userTenant), 1);

wireMockServer.stubFor(WireMock.get(urlPathEqualTo(USER_TENANTS_URL))
.withQueryParam("limit", matching("\\d*"))
.withHeader(HEADER_TENANT, equalTo(TENANT_ID_CONSORTIUM))
.willReturn(jsonResponse(asJsonString(userTenants), SC_OK)));
}

private static void mockEcsTlrExternalRequestCreating(EcsRequestExternal requestExternal) {
EcsTlr ecsTlr = new EcsTlr();

wireMockServer.stubFor(WireMock.post(urlMatching(TLR_CREATE_ECS_EXTERNAL_REQUEST_URL))
.withRequestBody(equalToJson(asJsonString(requestExternal)))
.willReturn(jsonResponse(asJsonString(ecsTlr), SC_CREATED)));
}

private static EcsRequestExternal buildEcsRequestExternal() {
return new EcsRequestExternal(
UUID.randomUUID().toString(),
UUID.randomUUID().toString(),
EcsRequestExternal.RequestLevelEnum.ITEM,
new Date(LocalDate.of(2000, 1, 1).toEpochDay()),
EcsRequestExternal.FulfillmentPreferenceEnum.HOLD_SHELF
);
}

private void mockPerform(EcsRequestExternal requestExternal) throws Exception {
mockMvc.perform(buildRequest(MockMvcRequestBuilders.post(
CIRCULATION_BFF_CREATE_ECS_EXTERNAL_REQUEST_URL), requestExternal)
.header(XOkapiHeaders.TENANT, TENANT_ID_CONSORTIUM));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;

Expand All @@ -11,7 +12,9 @@
import org.folio.circulationbff.client.feign.UserTenantsClient;
import org.folio.circulationbff.domain.dto.UserTenant;
import org.folio.circulationbff.domain.dto.UserTenantCollection;
import org.folio.circulationbff.exception.UserTenantException;
import org.folio.circulationbff.service.impl.UserTenantsServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -39,6 +42,23 @@ void isCentralTenantIdTest(UserTenantCollection userTenantCollection, boolean ex
assertThat(userTenantsService.isCentralTenant(), equalTo(expectedValue));
}

@Test
void getCentralTenantIdTest() {
when(userTenantsClient.getUserTenants(anyInt())).thenReturn(new UserTenantCollection()
.addUserTenantsItem(new UserTenant().centralTenantId(CENTRAL_TENANT_ID)));

assertThat(userTenantsService.getCentralTenantId(), equalTo(CENTRAL_TENANT_ID));
}

@Test
void getCentralTenantIdShouldThrowUserTenantExceptionTest() {
when(userTenantsClient.getUserTenants(anyInt())).thenReturn(new UserTenantCollection()
.addUserTenantsItem(null));

assertThrows(UserTenantException.class, () -> userTenantsService.getCentralTenantId());
}


private static Stream<Arguments> userTenantCollectionToExpectedValue() {
return Stream.of(
Arguments.of(buildCollection(TENANT_ID), false),
Expand Down

0 comments on commit 5163af0

Please sign in to comment.