Skip to content

Commit

Permalink
[MODEXPW-528] Add new tests for exporting EDI
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Dec 5, 2024
1 parent 462fdda commit 463fc94
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.folio.dew.batch.acquisitions.edifact.exceptions;

public class EntitiesNotFoundException extends RuntimeException {

private static final String EXCEPTION_MESSAGE = "Entities not found: %s";

public EntitiesNotFoundException(Class<?> entityClass) {
super(EXCEPTION_MESSAGE.formatted(entityClass.getSimpleName()), null, false, false);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.apache.commons.collections4.CollectionUtils;
import org.folio.dew.batch.acquisitions.edifact.PurchaseOrdersToEdifactMapper;
import org.folio.dew.batch.acquisitions.edifact.exceptions.EntitiesNotFoundException;
import org.folio.dew.batch.acquisitions.edifact.services.OrdersService;
import org.folio.dew.domain.dto.Piece;
import org.folio.dew.domain.dto.VendorEdiOrdersExportConfig;
Expand All @@ -17,11 +18,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.log4j.Log4j2;

@Component
@StepScope
@Log4j2
public class MapToEdifactClaimsTasklet extends MapToEdifactTasklet {

public static final String CLAIM_PIECE_IDS = "claimPieceIds";
Expand All @@ -40,6 +38,10 @@ protected List<String> getExportConfigMissingFields(VendorEdiOrdersExportConfig
@Override
protected EdifactExportHolder buildEdifactExportHolder(ChunkContext chunkContext, VendorEdiOrdersExportConfig ediExportConfig, Map<String, Object> jobParameters) {
var pieces = ordersService.getPiecesByIdsAndReceivingStatus(ediExportConfig.getClaimPieceIds(), Piece.ReceivingStatusEnum.LATE);
if (pieces.isEmpty()) {
throw new EntitiesNotFoundException(Piece.class);
}

var poLineQuery = convertIdsToCqlQuery(pieces.stream().map(Piece::getPoLineId).toList());
var compOrders = getCompositeOrders(poLineQuery);
return new EdifactExportHolder(compOrders, pieces);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.folio.dew.batch.acquisitions.edifact.PurchaseOrdersToEdifactMapper;
import org.folio.dew.batch.acquisitions.edifact.exceptions.CompositeOrderMappingException;
import org.folio.dew.batch.acquisitions.edifact.exceptions.EdifactException;
import org.folio.dew.batch.acquisitions.edifact.exceptions.OrderNotFoundException;
import org.folio.dew.batch.acquisitions.edifact.exceptions.EntitiesNotFoundException;
import org.folio.dew.batch.acquisitions.edifact.services.OrdersService;
import org.folio.dew.domain.dto.CompositePoLine;
import org.folio.dew.domain.dto.CompositePurchaseOrder;
Expand Down Expand Up @@ -90,7 +90,7 @@ protected List<CompositePurchaseOrder> getCompositeOrders(String poLineQuery) {

log.debug("getCompositeOrders:: {}", compOrders);
if (compOrders.isEmpty()) {
throw new OrderNotFoundException("Orders for export not found", false);
throw new EntitiesNotFoundException(PurchaseOrder.class);
}
return compOrders;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.folio.dew.batch.acquisitions.edifact.jobs;

import static org.assertj.core.api.Assertions.assertThat;
import static org.folio.dew.utils.QueryUtils.convertIdsToCqlQuery;
import static org.folio.dew.utils.TestUtils.getMockData;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.folio.dew.domain.dto.Piece;
import org.folio.dew.domain.dto.PieceCollection;
import org.folio.dew.domain.dto.PoLine;
import org.folio.dew.domain.dto.PoLineCollection;
import org.folio.dew.domain.dto.PurchaseOrder;
import org.folio.dew.domain.dto.PurchaseOrderCollection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.node.ObjectNode;

import lombok.SneakyThrows;

class MapToEdifactClaimsTaskletTest extends MapToEdifactTaskletAbstractTest {

private static final String SAMPLE_PIECES_PATH = "edifact/acquisitions/pieces_collection.json";

@Autowired
Job edifactClaimsExportJob;

private List<PurchaseOrder> orders;
private List<PoLine> poLines;
private List<Piece> pieces;
private List<String> pieceIds;

@BeforeEach
@SneakyThrows
protected void setUp() {
super.setUp();
edifactExportJob = edifactClaimsExportJob;
orders = objectMapper.readValue(getMockData(SAMPLE_PURCHASE_ORDERS_PATH), PurchaseOrderCollection.class).getPurchaseOrders();
poLines = objectMapper.readValue(getMockData(SAMPLE_PO_LINES_PATH), PoLineCollection.class).getPoLines();
pieces = objectMapper.readValue(getMockData(SAMPLE_PIECES_PATH), PieceCollection.class).getPieces();

pieceIds = pieces.stream().map(Piece::getId).toList();
doReturn(pieces).when(ordersService).getPiecesByIdsAndReceivingStatus(pieceIds, Piece.ReceivingStatusEnum.LATE);
}

@Test
void testEdifactClaimsExport() throws Exception {
JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob);
String poLineQuery = convertIdsToCqlQuery(pieces.stream().map(Piece::getPoLineId).toList());

doReturn(poLines).when(ordersService).getPoLinesByQuery(poLineQuery);
doReturn(orders).when(ordersService).getPurchaseOrdersByIds(anyList());
doReturn("test1").when(purchaseOrdersToEdifactMapper).convertOrdersToEdifact(any(), any(), anyString());

var exportConfig = getEdifactExportConfig(SAMPLE_EDI_ORDERS_EXPORT, pieceIds);
JobExecution jobExecution = testLauncher.launchStep(MAP_TO_EDIFACT_STEP, getJobParameters(exportConfig));

assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
verify(ordersService).getPiecesByIdsAndReceivingStatus(pieceIds, Piece.ReceivingStatusEnum.LATE);
verify(ordersService).getPoLinesByQuery(poLineQuery);
verify(ordersService).getPurchaseOrdersByIds(anyList());
}

@Test
void testEdifactClaimsExportNoPiecesFound() throws Exception {
JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob);
doReturn(List.of()).when(ordersService).getPiecesByIdsAndReceivingStatus(pieceIds, Piece.ReceivingStatusEnum.LATE);

var exportConfig = getEdifactExportConfig(SAMPLE_EDI_ORDERS_EXPORT, pieceIds);
JobExecution jobExecution = testLauncher.launchStep(MAP_TO_EDIFACT_STEP, getJobParameters(exportConfig));

assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo(ExitStatus.FAILED.getExitCode());
assertThat(jobExecution.getExitStatus().getExitDescription()).contains("Entities not found: Piece");
verify(ordersService).getPiecesByIdsAndReceivingStatus(pieceIds, Piece.ReceivingStatusEnum.LATE);
}

@Test
void testEdifactClaimsExportMissingRequiredFields() throws Exception {
JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob);
var exportConfig = getEdifactExportConfig(SAMPLE_EDI_ORDERS_EXPORT, List.of());

JobExecution jobExecution = testLauncher.launchStep(MAP_TO_EDIFACT_STEP, getJobParameters(exportConfig));
var status = new ArrayList<>(jobExecution.getStepExecutions()).get(0).getStatus();

assertEquals(BatchStatus.FAILED, status);
assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo(ExitStatus.FAILED.getExitCode());
assertThat(jobExecution.getExitStatus().getExitDescription()).contains("Export configuration is incomplete, missing required fields: [claimPieceIds]");
}

@Override
protected ObjectNode getEdifactExportConfig(String path) throws IOException {
return getEdifactExportConfig(path, pieceIds);
}

protected ObjectNode getEdifactExportConfig(String path, List<String> pieceIds) throws IOException {
var exportConfig = super.getEdifactExportConfig(path);
var arr = exportConfig.putArray("claimPieceIds");
pieceIds.forEach(arr::add);
return exportConfig;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.util.List;

import org.assertj.core.api.Assertions;
Expand All @@ -23,25 +24,33 @@
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.node.ObjectNode;

import lombok.SneakyThrows;

class MapToEdifactOrderTaskletTest extends MapToEdifactTaskletAbstractTest {

private static final String DATA_EXPORT_CONFIGS_PATH = "edifact/dataExportConfigs.json";

@Autowired
Job edifactOrdersExportJob;

private List<PurchaseOrder> orders;
private List<PoLine> poLines;

@BeforeEach
@Override
@SneakyThrows
protected void setUp() {
super.setUp();
edifactExportJob = edifactOrdersExportJob;
orders = objectMapper.readValue(getMockData(SAMPLE_PURCHASE_ORDERS_PATH), PurchaseOrderCollection.class).getPurchaseOrders();
poLines = objectMapper.readValue(getMockData(SAMPLE_PO_LINES_PATH), PoLineCollection.class).getPoLines();

}

@Test
void edifactOrdersExportJobTestSuccess() throws Exception {
void testEdifactOrdersExport() throws Exception {
JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob);
List<PurchaseOrder> orders = objectMapper.readValue(getMockData(
"edifact/acquisitions/purchase_order_collection.json"), PurchaseOrderCollection.class).getPurchaseOrders();
List<PoLine> poLines = objectMapper.readValue(getMockData("edifact/acquisitions/po_line_collection.json"),
PoLineCollection.class).getPoLines();
String cqlString = "purchaseOrder.workflowStatus==Open" +
" AND purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1" +
" AND (cql.allRecords=1 NOT purchaseOrder.manualPo==true)" +
Expand All @@ -53,20 +62,16 @@ void edifactOrdersExportJobTestSuccess() throws Exception {
doReturn(orders).when(ordersService).getPurchaseOrdersByIds(anyList());
doReturn("test1").when(purchaseOrdersToEdifactMapper).convertOrdersToEdifact(any(), any(), anyString());

JobExecution jobExecution = testLauncher.launchStep("mapToEdifactStep", getJobParameters(false));
JobExecution jobExecution = testLauncher.launchStep(MAP_TO_EDIFACT_STEP, getJobParameters(getEdifactExportConfig(SAMPLE_EDI_ORDERS_EXPORT)));

Assertions.assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
verify(ordersService).getPoLinesByQuery(cqlString);
verify(ordersService).getPurchaseOrdersByIds(anyList());
}

@Test
void edifactOrdersExportJobIfDefaultConfigTestSuccess() throws Exception {
void testEdifactOrdersExportDefaultConfig() throws Exception {
JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob);
List<PurchaseOrder> orders = objectMapper.readValue(getMockData(
"edifact/acquisitions/purchase_order_collection.json"), PurchaseOrderCollection.class).getPurchaseOrders();
List<PoLine> poLines = objectMapper.readValue(getMockData("edifact/acquisitions/po_line_collection.json"),
PoLineCollection.class).getPoLines();
String cqlString = "purchaseOrder.workflowStatus==Open" +
" AND purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1" +
" AND (cql.allRecords=1 NOT purchaseOrder.manualPo==true)" +
Expand All @@ -82,20 +87,17 @@ void edifactOrdersExportJobIfDefaultConfigTestSuccess() throws Exception {
doReturn(orders).when(ordersService).getPurchaseOrdersByIds(anyList());
doReturn("test1").when(purchaseOrdersToEdifactMapper).convertOrdersToEdifact(any(), any(), anyString());

JobExecution jobExecution = testLauncher.launchStep("mapToEdifactStep", getJobParameters(true));
var exportConfig = getEdifactExportConfig(SAMPLE_EDI_ORDERS_EXPORT, true);
JobExecution jobExecution = testLauncher.launchStep(MAP_TO_EDIFACT_STEP, getJobParameters(exportConfig));

Assertions.assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
verify(ordersService).getPoLinesByQuery(cqlString);
verify(ordersService).getPurchaseOrdersByIds(anyList());
}

@Test
void edifactOrdersExportJobIfDefaultConfigNotOneTestSuccess() throws Exception {
void testEdifactOrdersExportDefaultConfigWithTwoExportConfigs() throws Exception {
JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob);
List<PurchaseOrder> orders = objectMapper.readValue(getMockData(
"edifact/acquisitions/purchase_order_collection.json"), PurchaseOrderCollection.class).getPurchaseOrders();
List<PoLine> poLines = objectMapper.readValue(getMockData("edifact/acquisitions/po_line_collection.json"),
PoLineCollection.class).getPoLines();
String cqlString = "purchaseOrder.workflowStatus==Open" +
" AND purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1" +
" AND (cql.allRecords=1 NOT purchaseOrder.manualPo==true)" +
Expand All @@ -104,18 +106,23 @@ void edifactOrdersExportJobIfDefaultConfigNotOneTestSuccess() throws Exception {
" AND acquisitionMethod==(\"306489dd-0053-49ee-a068-c316444a8f55\")" +
" AND cql.allRecords=1 NOT vendorDetail.vendorAccount==(\"org1\" OR \"org2\")";
String configSql = "configName==EDIFACT_ORDERS_EXPORT_d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1*";
ExportConfigCollection exportConfigCollection = objectMapper.readValue(getMockData("edifact/dataExportConfigs.json"), ExportConfigCollection.class);
ExportConfigCollection exportConfigCollection = objectMapper.readValue(getMockData(DATA_EXPORT_CONFIGS_PATH), ExportConfigCollection.class);
poLines.get(0).getVendorDetail().setVendorAccount(null);
doReturn(poLines).when(ordersService).getPoLinesByQuery(cqlString);
doReturn(exportConfigCollection).when(dataExportSpringClient).getExportConfigs(configSql);
doReturn(orders).when(ordersService).getPurchaseOrdersByIds(anyList());
doReturn("test1").when(purchaseOrdersToEdifactMapper).convertOrdersToEdifact(any(), any(), anyString());

JobExecution jobExecution = testLauncher.launchStep("mapToEdifactStep", getJobParameters(true));
var exportConfig = getEdifactExportConfig(SAMPLE_EDI_ORDERS_EXPORT, true);
JobExecution jobExecution = testLauncher.launchStep(MAP_TO_EDIFACT_STEP, getJobParameters(exportConfig));

Assertions.assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
verify(ordersService).getPoLinesByQuery(cqlString);
verify(ordersService).getPurchaseOrdersByIds(anyList());
}

protected ObjectNode getEdifactExportConfig(String path, boolean isDefaultConfig) throws IOException {
return getEdifactExportConfig(path).put("isDefaultConfig", isDefaultConfig);
}

}
Loading

0 comments on commit 463fc94

Please sign in to comment.