Skip to content

Commit

Permalink
MODTLR-141 Tests for ConsortiumService
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksandrVidinieiev committed Feb 17, 2025
1 parent e8914f9 commit 7824622
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private TenantContext getTenantContext(String tenantId) {
log.info("getTenantContext:: cache miss for tenant {}", tenantId);
tenantContext = Optional.ofNullable(userTenantsService.findFirstUserTenant())
.map(userTenant -> new TenantContext(tenantId, userTenant))
.orElseThrow();
.orElseThrow(() -> new IllegalStateException("Failed to fetch user tenant"));
log.info("getTenantContext:: populating cache: {}", tenantContext);
CACHE.put(tenantId, tenantContext);
}
Expand Down
139 changes: 139 additions & 0 deletions src/test/java/org/folio/service/ConsortiumServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package org.folio.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import org.folio.domain.dto.UserTenant;
import org.folio.service.impl.ConsortiumServiceImpl;
import org.folio.spring.FolioExecutionContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class ConsortiumServiceTest {

private static final String CENTRAL_TENANT_ID = "central_tenant";
private static final String CONSORTIUM_ID = "5f3febe1-b2a3-45cd-8606-4b160f24575b";
private static final UserTenant MOCK_USER_TENANT = new UserTenant()
.centralTenantId(CENTRAL_TENANT_ID)
.consortiumId(CONSORTIUM_ID);

@Mock
private UserTenantsService userTenantsService;

@Mock
private FolioExecutionContext folioExecutionContext;

@InjectMocks
private ConsortiumServiceImpl consortiumService;


@BeforeEach
void setUp() {
ConsortiumServiceImpl.clearCache();
}

@Test
void getCurrentTenantIdReturnsTenantIdFromFolioContext() {
mockFolioExecutionContext("test_tenant");
assertEquals("test_tenant", consortiumService.getCurrentTenantId());
}

@Test
void tenantContextIsResolvedAndCached() {
mockFolioExecutionContext("tenant1");
mockUserTenant(MOCK_USER_TENANT);

// first invocation, cache is empty
assertEquals(CENTRAL_TENANT_ID, consortiumService.getCentralTenantId());
assertEquals(CONSORTIUM_ID, consortiumService.getCurrentConsortiumId());
verify(userTenantsService, times(1)).findFirstUserTenant();

// second invocation, tenant context is resolved from cache
assertEquals(CENTRAL_TENANT_ID, consortiumService.getCentralTenantId());
assertEquals(CONSORTIUM_ID, consortiumService.getCurrentConsortiumId());
verifyNoMoreInteractions(userTenantsService);

// same steps for a different tenant
mockFolioExecutionContext("tenant2");
mockUserTenant(MOCK_USER_TENANT);

assertEquals(CENTRAL_TENANT_ID, consortiumService.getCentralTenantId());
assertEquals(CONSORTIUM_ID, consortiumService.getCurrentConsortiumId());
verify(userTenantsService, times(2)).findFirstUserTenant();

assertEquals(CENTRAL_TENANT_ID, consortiumService.getCentralTenantId());
assertEquals(CONSORTIUM_ID, consortiumService.getCurrentConsortiumId());
verifyNoMoreInteractions(userTenantsService);
}

@Test
void getCentralTenantIdThrowsExceptionIfUserTenantIsNotFound() {
mockFolioExecutionContext(CENTRAL_TENANT_ID);
mockUserTenant(null);
assertThrows(IllegalStateException.class, () -> consortiumService.getCentralTenantId());
}

@Test
void isCurrentTenantCentralReturnsTrue() {
mockFolioExecutionContext(CENTRAL_TENANT_ID);
mockUserTenant(MOCK_USER_TENANT);
assertTrue(consortiumService.isCurrentTenantCentral());
}

@Test
void isCurrentTenantCentralReturnsFalseWhenTenantIdsDoNotMatch() {
mockFolioExecutionContext("random_tenant");
mockUserTenant(MOCK_USER_TENANT);
assertFalse(consortiumService.isCurrentTenantCentral());
}

@Test
void isCurrentTenantCentralThrowsExceptionWhenCentralTenantIdIsNotFound() {
mockFolioExecutionContext(CENTRAL_TENANT_ID);
mockUserTenant(null);
assertThrows(IllegalStateException.class, () -> consortiumService.isCurrentTenantCentral());
}

@Test
void isCentralTenantReturnsTrue() {
mockFolioExecutionContext("random_tenant");
mockUserTenant(MOCK_USER_TENANT);
assertTrue(consortiumService.isCentralTenant(CENTRAL_TENANT_ID));
}

@Test
void isCentralTenantReturnsFalseIfTenantIdsDoNotMatch() {
mockFolioExecutionContext("random_tenant");
mockUserTenant(MOCK_USER_TENANT);
assertFalse(consortiumService.isCentralTenant("random_tenant"));
}

@Test
void isCentralTenantThrowsExceptionIfCentralTenantIdIsNotFound() {
mockFolioExecutionContext("random_tenant");
mockUserTenant(null);
assertThrows(IllegalStateException.class, () -> consortiumService.isCentralTenant(CENTRAL_TENANT_ID));
}

private void mockUserTenant(UserTenant userTenant) {
when(userTenantsService.findFirstUserTenant())
.thenReturn(userTenant);
}

private void mockFolioExecutionContext(String tenantId) {
when(folioExecutionContext.getTenantId())
.thenReturn(tenantId);
}

}

0 comments on commit 7824622

Please sign in to comment.