Skip to content

Commit

Permalink
[MODORDERS-1209]. Remove try-catch, change piece status validation
Browse files Browse the repository at this point in the history
  • Loading branch information
BKadirkhodjaev committed Dec 4, 2024
1 parent 4caf259 commit 15cfc54
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 76 deletions.
1 change: 0 additions & 1 deletion src/test/java/org/folio/rest/impl/ClaimingApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public class ClaimingApiTest {

private static final String ORGANIZATIONS_KEY = "organizations";
private static final String PO_LINES_KEY = "poLines";
private static final String PIECES_KEY = "pieces";
private static final String CLAIMING_MOCK_DATA_FOLDER = "claiming/";

@BeforeAll
Expand Down
146 changes: 71 additions & 75 deletions src/test/java/org/folio/rest/impl/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1935,89 +1935,85 @@ private void handleGetPieces(RoutingContext ctx) {
String requestQuery = ctx.request().getParam("query");
MultiMap requestHeaders = ctx.request().headers();
logger.info("handleGetPieces requestPath: {}, requestQuery: {}, requestHeaders: {}", requestPath, requestQuery, requestHeaders);
try {
if (requestQuery.contains(ID_FOR_PIECES_INTERNAL_SERVER_ERROR)) {
logger.info("handleGetPieces (with internal server error)");
addServerRqRsData(HttpMethod.GET, PIECES_STORAGE, new JsonObject());
serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
if (Objects.nonNull(requestQuery) && requestQuery.contains(ID_FOR_PIECES_INTERNAL_SERVER_ERROR)) {
logger.info("handleGetPieces (with internal server error)");
addServerRqRsData(HttpMethod.GET, PIECES_STORAGE, new JsonObject());
serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
} else {
PieceCollection pieces;
if (getMockEntries(PIECES_STORAGE, Piece.class).isPresent()) {
logger.info("handleGetPieces (all records)");
try {
List<Piece> piecesList = getMockEntries(PIECES_STORAGE, Piece.class).get();
pieces = new PieceCollection().withPieces(piecesList);
pieces.setTotalRecords(pieces.getPieces().size());
} catch (Exception e) {
throw new IllegalStateException(String.format("Cannot retrieved mock data for requestPath: %s, requestQuery: %s", requestPath, requestQuery));
}
} else {
PieceCollection pieces;
if (getMockEntries(PIECES_STORAGE, Piece.class).isPresent()) {
logger.info("handleGetPieces (all records)");
try {
List<Piece> piecesList = getMockEntries(PIECES_STORAGE, Piece.class).get();
pieces = new PieceCollection().withPieces(piecesList);
pieces.setTotalRecords(pieces.getPieces().size());
} catch (Exception e) {
throw new IllegalStateException(String.format("Cannot retrieved mock data for requestPath: %s, requestQuery: %s", requestPath, requestQuery));
}
} else {
try {
if (requestQuery.contains("poLineId==")) {
logger.info("handleGetPieces (by poLineId)");
List<String> conditions = StreamEx
.split(requestQuery, " or ")
.flatMap(s -> StreamEx.split(s, " and "))
.toList();

String polId = EMPTY;
String status = EMPTY;
for (String condition : conditions) {
if (condition.startsWith("poLineId")) {
polId = condition.split("poLineId==")[1];
} else if (condition.startsWith("receivingStatus")) {
status = condition.split("receivingStatus==")[1];
}
try {
if (requestQuery.contains("poLineId==")) {
logger.info("handleGetPieces (by poLineId)");
List<String> conditions = StreamEx
.split(requestQuery, " or ")
.flatMap(s -> StreamEx.split(s, " and "))
.toList();

String polId = EMPTY;
String status = EMPTY;
for (String condition : conditions) {
if (condition.startsWith("poLineId")) {
polId = condition.split("poLineId==")[1];
} else if (condition.startsWith("receivingStatus")) {
status = condition.split("receivingStatus==")[1];
}
logger.info("poLineId: {}", polId);
logger.info("receivingStatus: {}", status);

String path = PIECE_RECORDS_MOCK_DATA_PATH + String.format("pieceRecords-%s.json", polId);
pieces = new JsonObject(getMockData(path)).mapTo(PieceCollection.class);
// Filter piece records by receiving status
if (StringUtils.isNotEmpty(status)) {
Piece.ReceivingStatus receivingStatus = Piece.ReceivingStatus.fromValue(status);
pieces.getPieces()
.removeIf(piece -> receivingStatus != piece.getReceivingStatus());
}
} else if (requestQuery.contains("id==")) {
logger.info("handleGetPieces (by id)");
pieces = new JsonObject(getMockData(PIECES_COLLECTION)).mapTo(PieceCollection.class);
List<String> pieceIds = extractIdsFromQuery(requestQuery);
pieces.getPieces().removeIf(piece -> !pieceIds.contains(piece.getId()));
// fix consistency with titles: the piece's title id should be the same as one of the titles ids
// returned for the piece's po line
pieces.getPieces().forEach(piece -> {
String poLineId = piece.getPoLineId();
List<Title> titlesForPoLine = getTitlesByPoLineIds(List.of(poLineId)).mapTo(TitleCollection.class).getTitles();
if (!titlesForPoLine.isEmpty() && titlesForPoLine.stream().noneMatch(title -> title.getId().equals(piece.getTitleId()))) {
piece.setTitleId(titlesForPoLine.get(0).getId());
}
});
} else {
logger.info("handleGetPieces (with empty piece collection)");
pieces = new PieceCollection();
}

pieces.setTotalRecords(pieces.getPieces().size());

} catch (Exception e) {
logger.info("handleGetPieces (with empty piece collection on exception)");
logger.info("poLineId: {}", polId);
logger.info("receivingStatus: {}", status);

String path = PIECE_RECORDS_MOCK_DATA_PATH + String.format("pieceRecords-%s.json", polId);
pieces = new JsonObject(getMockData(path)).mapTo(PieceCollection.class);
// Filter piece records by receiving status
if (StringUtils.isNotEmpty(status)) {
Piece.ReceivingStatus receivingStatus = Piece.ReceivingStatus.fromValue(status);
pieces.getPieces()
.removeIf(piece -> receivingStatus != piece.getReceivingStatus());
}
} else if (requestQuery.contains("id==")) {
logger.info("handleGetPieces (by id)");
pieces = new JsonObject(getMockData(PIECES_COLLECTION)).mapTo(PieceCollection.class);
List<String> pieceIds = extractIdsFromQuery(requestQuery);
pieces.getPieces().removeIf(piece -> !pieceIds.contains(piece.getId()));
// fix consistency with titles: the piece's title id should be the same as one of the titles ids
// returned for the piece's po line
pieces.getPieces().forEach(piece -> {
String poLineId = piece.getPoLineId();
List<Title> titlesForPoLine = getTitlesByPoLineIds(List.of(poLineId)).mapTo(TitleCollection.class).getTitles();
if (!titlesForPoLine.isEmpty() && titlesForPoLine.stream().noneMatch(title -> title.getId().equals(piece.getTitleId()))) {
piece.setTitleId(titlesForPoLine.get(0).getId());
}
});
} else {
logger.info("handleGetPieces (with empty piece collection)");
pieces = new PieceCollection();
pieces.setTotalRecords(0);
}
}

JsonObject data = JsonObject.mapFrom(pieces);
addServerRqRsData(HttpMethod.GET, PIECES_STORAGE, data);
pieces.setTotalRecords(pieces.getPieces().size());

ctx.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
.end(data.encodePrettily());
} catch (Exception e) {
logger.info("handleGetPieces (with empty piece collection on exception)");
pieces = new PieceCollection();
pieces.setTotalRecords(0);
}
}
} catch (Exception e) {
logger.error("Error handling pieces: ", e);

JsonObject data = JsonObject.mapFrom(pieces);
addServerRqRsData(HttpMethod.GET, PIECES_STORAGE, data);

ctx.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
.end(data.encodePrettily());
}
}

Expand Down

0 comments on commit 15cfc54

Please sign in to comment.