Skip to content

Commit

Permalink
[MODORDERS-1174] Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Sep 12, 2024
1 parent 6772de2 commit 8740022
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.folio.service.pieces;

import static io.vertx.core.Future.succeededFuture;
import static org.assertj.core.api.Assertions.assertThat;
import static org.folio.TestConfig.autowireDependencies;
import static org.folio.TestConfig.clearServiceInteractions;
import static org.folio.TestConfig.clearVertxContext;
import static org.folio.TestConfig.getFirstContextFromVertx;
import static org.folio.TestConfig.getVertx;
import static org.folio.TestConfig.initSpringContext;
import static org.folio.TestConfig.isVerticleNotDeployed;
import static org.folio.TestUtils.getMockAsJson;
import static org.folio.rest.impl.MockServer.BASE_MOCK_DATA_PATH;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -27,6 +30,7 @@
import java.util.concurrent.TimeoutException;

import org.folio.ApiTestSuite;
import org.folio.models.consortium.ConsortiumConfiguration;
import org.folio.rest.core.RestClient;
import org.folio.rest.core.models.RequestContext;
import org.folio.rest.core.models.RequestEntry;
Expand Down Expand Up @@ -55,12 +59,21 @@
@ExtendWith(VertxExtension.class)
public class PieceStorageServiceTest {

private static final String USER_TENANTS_PATH = BASE_MOCK_DATA_PATH + "userTenants/";
private static final String USER_TENANTS_MOCK = "userTenants";

private static final String PIECES_PATH = BASE_MOCK_DATA_PATH + "pieces/";
private static final String PIECES_MOCK = "pieces-for-user-tenants";

@Autowired
PieceStorageService pieceStorageService;

@Autowired
ConsortiumConfigurationService consortiumConfigurationService;

@Autowired
ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever;

@Autowired
private RestClient restClientMock;

Expand Down Expand Up @@ -133,6 +146,55 @@ void testShouldDeleteItems() {
verify(pieceStorageService, times(1)).deletePiece(any(String.class), eq(requestContext));
}

@Test
void testGetPiecesFilterByUserTenants(VertxTestContext vertxTestContext) {
var userTenantsMockData = getMockAsJson(USER_TENANTS_PATH, USER_TENANTS_MOCK);
var piecesMockData = getMockAsJson(PIECES_PATH, PIECES_MOCK).mapTo(PieceCollection.class);
var consortiumConfiguration = Optional.of(new ConsortiumConfiguration("tenantId0", UUID.randomUUID().toString()));

doReturn(Future.succeededFuture(consortiumConfiguration)).when(consortiumConfigurationService).getConsortiumConfiguration(any(RequestContext.class));
doReturn(Future.succeededFuture(piecesMockData)).when(restClientMock).get(any(RequestEntry.class), eq(PieceCollection.class), any(RequestContext.class));
doReturn(Future.succeededFuture(userTenantsMockData)).when(restClientMock).getAsJsonObject(any(), any(RequestContext.class));

var future = pieceStorageService.getPieces(Integer.MAX_VALUE, 0, null, requestContext);

verify(restClientMock, times(1)).get(any(RequestEntry.class), eq(PieceCollection.class), eq(requestContext));
verify(restClientMock, times(1)).getAsJsonObject(any(), any(RequestContext.class));
verify(consortiumConfigurationService, times(1)).getConsortiumConfiguration(eq(requestContext));

vertxTestContext.assertComplete(future)
.onComplete(f -> {
var result = f.result();
assertThat(result).isNotNull();
assertThat(result.getTotalRecords()).isEqualTo(2);
assertThat(result.getPieces()).hasSize(2);
vertxTestContext.completeNow();
});
}

@Test
void testGetPiecesFilterByUserTenantsNonECS(VertxTestContext vertxTestContext) {
var piecesMockData = getMockAsJson(PIECES_PATH, PIECES_MOCK).mapTo(PieceCollection.class);

doReturn(Future.succeededFuture(Optional.empty())).when(consortiumConfigurationService).getConsortiumConfiguration(any(RequestContext.class));
doReturn(Future.succeededFuture(piecesMockData)).when(restClientMock).get(any(RequestEntry.class), eq(PieceCollection.class), any(RequestContext.class));

var future = pieceStorageService.getPieces(Integer.MAX_VALUE, 0, null, requestContext);

verify(restClientMock, times(1)).get(any(RequestEntry.class), eq(PieceCollection.class), eq(requestContext));
verify(restClientMock, times(0)).getAsJsonObject(any(), any(RequestContext.class));
verify(consortiumConfigurationService, times(1)).getConsortiumConfiguration(eq(requestContext));

vertxTestContext.assertComplete(future)
.onComplete(f -> {
var result = f.result();
assertThat(result).isNotNull();
assertThat(result.getTotalRecords()).isEqualTo(3);
assertThat(result.getPieces()).hasSize(3);
vertxTestContext.completeNow();
});
}

private static class ContextConfiguration {

@Bean
Expand Down
16 changes: 16 additions & 0 deletions src/test/resources/mockdata/pieces/pieces-for-user-tenants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"pieces": [
{
"id": "05a95f03-eb00-4248-9f2e-2bd05957ff04",
"receivingTenantId": "tenantId1"
},
{
"id": "05a95f03-eb00-4248-9f2e-2bd05957ff05",
"receivingTenantId": "tenantId2"
},
{
"id": "05a95f03-eb00-4248-9f2e-2bd05957ff06"
}
],
"totalRecords": 3
}
13 changes: 13 additions & 0 deletions src/test/resources/mockdata/userTenants/userTenants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"userTenants": [
{
"id": "05c34b3d-33fa-46fc-8906-4ade19a570fd",
"userId": "440c89e3-7f6c-578a-9ea8-310dad23605e",
"username": "testUser",
"tenantId": "tenantId1",
"tenantName": "Tenant1",
"isPrimary": true
}
],
"totalRecords": 1
}

0 comments on commit 8740022

Please sign in to comment.