Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODCONSKC-58. Skip creating shadow system users for Eureka #132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/org/folio/consortia/domain/dto/UserType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ public enum UserType {
PATRON("patron"),
STAFF("staff"),
SHADOW("shadow"),
SYSTEM("system");
SYSTEM("system"),
DCB("dcb");

UserType(String name) {
this.name = name;
}

private final String name;

public static UserType fromName(String name) {
for (UserType userType : values()) {
if (userType.name.equals(name)) {
return userType;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void syncPrimaryAffiliations(UUID consortiumId, String tenantId, String c
log.info("Start syncing user primary affiliations for tenant {}", tenantId);
List<User> users = new ArrayList<>();
try {
users = userService.getUsersByQuery("(cql.allRecords=1 NOT type=\"patron\" NOT type=\"dcb\" NOT type=\"shadow\")", 0, Integer.MAX_VALUE);
users = userService.getUsersByQuery("(cql.allRecords=1 NOT type=\"patron\" NOT type=\"dcb\" NOT type=\"shadow\" NOT type=\"system\")", 0, Integer.MAX_VALUE);
} catch (Exception e) {
log.error("syncPrimaryAffiliations:: failed to retrieve '{}' users", tenantId, e);
tenantService.updateTenantSetupStatus(tenantId, centralTenantId, SetupStatusEnum.FAILED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import static org.folio.consortia.utils.TenantContextUtils.prepareContextForTenant;

import feign.FeignException;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.folio.consortia.client.UsersClient;
Expand All @@ -29,6 +33,7 @@
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
private static final String USER_ID = "userId";
private static final Set<UserType> NOT_APPLICABLE_USER_TYPES = EnumSet.of(UserType.PATRON, UserType.DCB, UserType.SHADOW, UserType.SYSTEM);

private final UsersKeycloakClient usersKeycloakClient;
private final UsersClient usersClient;
Expand Down Expand Up @@ -92,6 +97,11 @@ public User prepareShadowUser(UUID userId, String tenantId) {
log.warn("Could not find real user with id: {} in his home tenant: {}", userId.toString(), tenantId);
throw new ResourceNotFoundException(USER_ID, userId.toString());
}
UserType userType = Optional.ofNullable(realUser.getType()).map(UserType::fromName).orElse(null);
if (NOT_APPLICABLE_USER_TYPES.contains(userType)) {
log.warn("User with id: {} has type: {} which is not applicable for shadow user creation", userId.toString(), realUser.getType());
throw new IllegalStateException("User type is not applicable for shadow user creation");
}

var shadowUser = new User();
shadowUser.setId(userId.toString());
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/folio/consortia/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.folio.spring.FolioModuleMetadata;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
Expand Down Expand Up @@ -89,6 +91,24 @@ void shouldPrepareShadowUser() {
assertNull(shadow.getBarcode());
}

@ParameterizedTest
@EnumSource(value = UserType.class, names = {"PATRON", "DCB", "SHADOW", "SYSTEM"})
void shouldThrowIllegalStateExceptionForNotApplicableUserType(UserType userType) {
String tenantId = "diku";
when(folioExecutionContext.getTenantId()).thenReturn(tenantId);
Map<String, Collection<String>> okapiHeaders = createOkapiHeaders();
when(folioExecutionContext.getOkapiHeaders()).thenReturn(okapiHeaders);

UUID userId = UUID.randomUUID();
User realUser = createUserEntity(true);
realUser.setType(userType.getName());

when(usersKeycloakClient.getUsersByUserId(userId.toString())).thenReturn(realUser);

IllegalStateException exception = assertThrows(IllegalStateException.class, () -> userService.prepareShadowUser(userId, tenantId));
assertEquals("User type is not applicable for shadow user creation", exception.getMessage());
}

private void mockOkapiHeaders() {
when(folioExecutionContext.getTenantId()).thenReturn("diku");
Map<String, Collection<String>> okapiHeaders = createOkapiHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -51,6 +52,8 @@
@EnableAutoConfiguration(exclude = BatchAutoConfiguration.class)
@EntityScan(basePackageClasses = UserTenantEntity.class)
class SyncPrimaryAffiliationServiceImplTest {
private static final String CQL_GET_USERS = "(cql.allRecords=1 NOT type=\"patron\" NOT type=\"dcb\" NOT type=\"shadow\" NOT type=\"system\")";

@InjectMocks
SyncPrimaryAffiliationServiceImpl syncPrimaryAffiliationService;
@Mock
Expand Down Expand Up @@ -96,7 +99,7 @@ void createPrimaryUserAffiliationsWhenCentralTenantSaving() throws JsonProcessin
when(tenantService.getByTenantId(anyString())).thenReturn(tenantEntity1);
when(userTenantRepository.findAnyByUserId(any(), any())).thenReturn(new PageImpl<>(Collections.emptyList()));
when(tenantRepository.findById(anyString())).thenReturn(Optional.of(tenantEntity1));
when(userService.getUsersByQuery(anyString(), anyInt(), anyInt())).thenReturn(userCollection);
when(userService.getUsersByQuery(eq(CQL_GET_USERS), anyInt(), anyInt())).thenReturn(userCollection);
when(consortiaConfigurationService.getCentralTenantId(anyString())).thenReturn(tenantId);

syncPrimaryAffiliationService.createPrimaryUserAffiliations(consortiumId, centralTenantId, spab);
Expand Down Expand Up @@ -127,7 +130,7 @@ void createPrimaryUserAffiliationsWhenLocalTenantSaving() throws JsonProcessingE
when(tenantService.getByTenantId(anyString())).thenReturn(tenantEntity1);
when(userTenantRepository.findAnyByUserId(any(), any())).thenReturn(new PageImpl<>(Collections.emptyList()));
when(tenantRepository.findById(anyString())).thenReturn(Optional.of(tenantEntity1));
when(userService.getUsersByQuery(anyString(), anyInt(), anyInt())).thenReturn(userCollection);
when(userService.getUsersByQuery(eq(CQL_GET_USERS), anyInt(), anyInt())).thenReturn(userCollection);
when(consortiaConfigurationService.getCentralTenantId(anyString())).thenReturn(centralTenantId);

syncPrimaryAffiliationService.createPrimaryUserAffiliations(consortiumId, centralTenantId, spab);
Expand All @@ -151,7 +154,7 @@ void syncPrimaryUserAffiliationsWhenTenantSaving() throws JsonProcessingExceptio
var spab = getSyncBody(tenantId);

// stub collection of 2 users
when(userService.getUsersByQuery(anyString(), anyInt(), anyInt())).thenReturn(userCollection);
when(userService.getUsersByQuery(eq(CQL_GET_USERS), anyInt(), anyInt())).thenReturn(userCollection);

syncPrimaryAffiliationService.syncPrimaryAffiliations(consortiumId, tenantId, centralTenantId);

Expand All @@ -171,7 +174,7 @@ void syncPrimaryAffiliationsException() throws JsonProcessingException {
var spab = getSyncBody(tenantId);

// stub collection of 2 users
when(userService.getUsersByQuery(anyString(), anyInt(), anyInt())).thenReturn(userCollection);
when(userService.getUsersByQuery(eq(CQL_GET_USERS), anyInt(), anyInt())).thenReturn(userCollection);
when(syncClient.savePrimaryAffiliations(spab, String.valueOf(consortiumId), tenantId, centralTenantId))
.thenThrow(FeignException.FeignClientException.class);

Expand All @@ -189,7 +192,7 @@ void syncPrimaryAffiliationsGetUsersException() {
var tenantId = "ABC1";
var centralTenantId = "diku";

when(userService.getUsersByQuery(anyString(), anyInt(), anyInt()))
when(userService.getUsersByQuery(eq(CQL_GET_USERS), anyInt(), anyInt()))
.thenThrow(FeignException.FeignClientException.class);

syncPrimaryAffiliationService.syncPrimaryAffiliations(consortiumId, tenantId, centralTenantId);
Expand Down
Loading