Skip to content

Commit

Permalink
msearch-453: add manually components missing from merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
mukhiddin-yusuf committed Sep 7, 2023
1 parent 4790a69 commit 5e49571
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.folio.search.model.context;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.With;
import org.folio.spring.FolioExecutionContext;
import org.folio.spring.FolioModuleMetadata;
import org.folio.spring.model.SystemUser;
import org.springframework.stereotype.Component;

@Component("searchFolioExecutionContextBuilder")
@RequiredArgsConstructor
public class FolioExecutionContextBuilder {
private final FolioModuleMetadata moduleMetadata;

public FolioExecutionContextBuilder.Builder builder() {
return new Builder(moduleMetadata);
}

public FolioExecutionContext forSystemUser(SystemUser systemUser) {
return builder()
.withTenantId(systemUser.tenantId())
.withOkapiUrl(systemUser.okapiUrl())
.withToken(systemUser.token() == null ? null : systemUser.token().accessToken())
.build();
}

public FolioExecutionContext dbOnlyContext(String tenantId) {
return builder().withTenantId(tenantId).build();
}

@With
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static class Builder {
private final FolioModuleMetadata moduleMetadata;
private String tenantId;
private String okapiUrl;
private String token;
private String username;
private final Map<String, Collection<String>> allHeaders;
private final Map<String, Collection<String>> okapiHeaders;

public Builder(FolioModuleMetadata moduleMetadata) {
this.moduleMetadata = moduleMetadata;
this.allHeaders = new HashMap<>();
this.okapiHeaders = new HashMap<>();
}

public FolioExecutionContext build() {
return new FolioExecutionContext() {
@Override
public String getTenantId() {
return tenantId;
}

@Override
public String getOkapiUrl() {
return okapiUrl;
}

@Override
public String getToken() {
return token;
}

@Override
public Map<String, Collection<String>> getAllHeaders() {
return allHeaders;
}

@Override
public Map<String, Collection<String>> getOkapiHeaders() {
return okapiHeaders;
}

@Override
public FolioModuleMetadata getFolioModuleMetadata() {
return moduleMetadata;
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.folio.search.service;

import java.util.concurrent.Callable;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.folio.search.model.context.FolioExecutionContextBuilder;
import org.folio.spring.FolioExecutionContext;
import org.folio.spring.scope.FolioExecutionContextSetter;
import org.folio.spring.service.SystemUserService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class TenantScopedExecutionService {
private final FolioExecutionContextBuilder contextBuilder;
private final SystemUserService systemUserService;

/**
* Executes given job tenant scoped.
*
* @param tenantId - The tenant name.
* @param job - Job to be executed in tenant scope.
* @param <T> - Optional return value for the job.
* @return Result of job.
* @throws RuntimeException - Wrapped exception from the job.
*/
@SneakyThrows
public <T> T executeTenantScoped(String tenantId, Callable<T> job) {
try (var fex = new FolioExecutionContextSetter(folioExecutionContext(tenantId))) {
return job.call();
}
}

/**
* Executes given job in scope of tenant asynchronously.
*
* @param tenantId - The tenant name.
* @param job - Job to be executed in tenant scope.
*/
@Async
public void executeAsyncTenantScoped(String tenantId, Runnable job) {
try (var fex = new FolioExecutionContextSetter(folioExecutionContext(tenantId))) {
job.run();
}
}

private FolioExecutionContext folioExecutionContext(String tenant) {
return contextBuilder.forSystemUser(systemUserService.getAuthedSystemUser(tenant));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.folio.search.service.consortium.ConsortiumTenantExecutor;
import org.folio.search.service.converter.preprocessor.EventPreProcessor;
import org.folio.search.service.metadata.ResourceDescriptionService;
import org.folio.spring.service.SystemUserScopedExecutionService;
import org.folio.spring.FolioExecutionContext;
import org.springframework.stereotype.Component;

Expand All @@ -32,7 +31,6 @@
public class MultiTenantSearchDocumentConverter {

private final SearchDocumentConverter searchDocumentConverter;
private final SystemUserScopedExecutionService executionService;
private final ResourceDescriptionService resourceDescriptionService;
private final Map<String, EventPreProcessor> eventPreProcessorBeans;
private final ConsortiumTenantExecutor consortiumTenantExecutor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import org.folio.search.domain.dto.Holding;
import org.folio.search.domain.dto.Instance;
import org.folio.search.domain.dto.Item;
import org.folio.search.support.extension.EnablePostgres;
import org.folio.spring.FolioExecutionContext;
import org.folio.spring.FolioModuleMetadata;
import org.folio.spring.test.extension.EnablePostgres;
import org.folio.spring.test.type.IntegrationTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@ public abstract class BaseIntegrationTest {
protected static InventoryApi inventoryApi;
protected static KafkaTemplate<String, ResourceEvent> kafkaTemplate;
protected static OkapiConfiguration okapi;
protected static String centralTenant;

@RegisterExtension
static OkapiExtension okapiExtension =
new OkapiExtension(new InventoryViewResponseBuilder(), new ResponseTemplateTransformer(true));

protected static String centralTenant;

@BeforeAll
static void setUpDefaultTenant(
@Autowired MockMvc mockMvc,
Expand Down

0 comments on commit 5e49571

Please sign in to comment.