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

MODORDERS-1077 Data Import organisations not mapping #892

Merged
merged 12 commits into from
Apr 17, 2024
Merged
14 changes: 10 additions & 4 deletions src/main/java/org/folio/service/caches/MappingParametersCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.TimeUnit;

import static java.lang.String.format;
import static org.folio.orders.utils.HelperUtils.encodeQuery;
import static org.folio.service.orders.utils.HelperUtils.collectResultsOnSuccess;

/**
Expand All @@ -39,6 +40,8 @@ public class MappingParametersCache {

private static final String ORGANIZATIONS = "/organizations/organizations";

private static final String SORT_BY_ID_QUERY = "(cql.allRecords=1) sortBy id";

@Value("${orders.cache.mapping.parameters.settings.limit:5000}")
private int settingsLimit;

Expand All @@ -61,6 +64,7 @@ public MappingParametersCache(Vertx vertx, RestClient restClient) {

/**
* Retrieves {@link MappingParameters} from cache by tenantId
*
* @param params {@link OkapiConnectionParams} connection params
* @return CompletableFuture with {@link MappingParameters} object
*/
Expand All @@ -75,6 +79,7 @@ public Future<MappingParameters> get(OkapiConnectionParams params) {

/**
* Generates {@link MappingParameters} with collection of {@link org.folio.Organization}
*
* @param params {@link OkapiConnectionParams} connection params
* @return CompletableFuture with {@link MappingParameters} object
*/
Expand All @@ -83,7 +88,7 @@ private CompletableFuture<MappingParameters> loadMappingParameters(OkapiConnecti
LOGGER.debug("loadMappingParameters:: Trying to load organizations '{}' for cache, okapi url: {}, tenantId: {}",
tenantId, params.getOkapiUrl(), params.getTenantId());

return RestUtil.doRequest(params, format(getOrganizationsLimitPath(), settingsLimit), HttpMethod.GET, null)
return RestUtil.doRequest(params, getOrganizationsSortedLimitPath(settingsLimit), HttpMethod.GET, null)
.toCompletionStage()
.toCompletableFuture()
.thenCompose(httpResponse -> {
Expand All @@ -97,6 +102,7 @@ private CompletableFuture<MappingParameters> loadMappingParameters(OkapiConnecti
return getRemainingOrganizations(params, orgCollection, mappingParameters);
}
mappingParameters.withOrganizations(orgCollection.getOrganizations());

return CompletableFuture.completedFuture(mappingParameters);
} else {
String message = format(
Expand Down Expand Up @@ -129,7 +135,7 @@ private List<Future<OrganizationCollection>> getOrganizationCollectionFutures(Ma
final int maxChunkSize = totalRecords / settingsLimit;
int offset = settingsLimit;
List<Future<OrganizationCollection>> organizationCollectionFutures = new ArrayList<>(maxChunkSize);
RequestEntry requestEntry = new RequestEntry(ORGANIZATIONS).withLimit(settingsLimit);
RequestEntry requestEntry = new RequestEntry(ORGANIZATIONS).withLimit(settingsLimit).withQuery(SORT_BY_ID_QUERY);
for (int i = 0; i < maxChunkSize; i++) {
Future<OrganizationCollection> future = restClient.get(requestEntry.withOffset(offset), OrganizationCollection.class,
new RequestContext(Vertx.currentContext(), headers));
Expand All @@ -139,7 +145,7 @@ private List<Future<OrganizationCollection>> getOrganizationCollectionFutures(Ma
return organizationCollectionFutures;
}

private String getOrganizationsLimitPath() {
return ORGANIZATIONS + "?limit=%s";
private String getOrganizationsSortedLimitPath(int limit) {
return format("%s?limit=%d&query=", ORGANIZATIONS, limit) + encodeQuery(SORT_BY_ID_QUERY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ private Future<CompositePurchaseOrder> saveOrder(DataImportEventPayload dataImpo
orderToSave.setId(orderId);
// in this handler a purchase order always is created in PENDING status despite the status that is set during mapping
orderToSave.setWorkflowStatus(WorkflowStatus.PENDING);
if (orderToSave.getVendor() == null) {
LOGGER.warn("saveOrder:: jobExecutionId: '{}', recordId: '{}', orderId: '{}' vendor for the order has not been mapped",
dataImportEventPayload.getJobExecutionId(), dataImportEventPayload.getContext().get(RECORD_ID_HEADER), orderId);
}

return purchaseOrderHelper.validateOrder(orderToSave, tenantConfig, requestContext)
.compose(errors -> {
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/org/folio/di/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
import static com.github.tomakehurst.wiremock.client.WireMock.serverError;
import static org.folio.dataimport.util.RestUtil.OKAPI_URL_HEADER;
import static org.folio.orders.utils.HelperUtils.encodeQuery;
import static org.folio.rest.util.OkapiConnectionParams.OKAPI_TENANT_HEADER;
import static org.folio.rest.util.OkapiConnectionParams.OKAPI_TOKEN_HEADER;

Expand Down Expand Up @@ -95,18 +96,19 @@ public void getMappingParametersFromCache(TestContext context) {
Async async = context.async();
ReflectionTestUtils.setField(mappingParametersCache, "settingsLimit", 5000);

String queryParam = "&query=" + encodeQuery("(cql.allRecords=1) sortBy id");
String organizationId = UUID.randomUUID().toString();
Organization organization = new Organization().withId(organizationId);

WireMock.stubFor(
get("/organizations/organizations?limit=5000")
get("/organizations/organizations?limit=5000" + queryParam)
.willReturn(okJson(new JsonObject()
.put("organizations", JsonArray.of(organization))
.put("totalRecords", 5001)
.toString())));

WireMock.stubFor(
get("/organizations/organizations?offset=5000&limit=5000")
get("/organizations/organizations?offset=5000" + queryParam + "&limit=5000")
.willReturn(okJson(new JsonObject()
.put("organizations", JsonArray.of(organization))
.put("totalRecords", 5001)
Expand All @@ -127,9 +129,9 @@ public void getMappingParametersFromCache(TestContext context) {
@Test
public void getMappingParametersFromCacheWithError(TestContext context) {
Async async = context.async();

String queryParam = "&query=" + encodeQuery("(cql.allRecords=1) sortBy id");
WireMock.stubFor(
get("/organizations/organizations?limit=0")
get("/organizations/organizations?limit=0" + queryParam)
.willReturn(serverError()));

mappingParametersCache
Expand Down
Loading