From 127644537735dc6cf7441fa0924b3651d1b1c450 Mon Sep 17 00:00:00 2001 From: Azizbek Khushvakov Date: Fri, 29 Mar 2024 13:40:58 +0500 Subject: [PATCH] [MODORSERS-1026] - Implemented api to execute template-request api --- .../org/folio/config/ApplicationConfig.java | 6 +++--- .../orders/utils/ResourcePathResolver.java | 6 ++++-- ...RoutingListAPI.java => RoutingListsAPI.java} | 10 +++++----- ...istService.java => RoutingListsService.java} | 13 +++++++------ .../java/org/folio/service/UserService.java | 6 +++--- src/test/java/org/folio/ApiTestSuite.java | 5 +++++ src/test/java/org/folio/TestConstants.java | 5 +++++ .../java/org/folio/rest/impl/TitlesApiTest.java | 17 +++++++---------- ...ceTest.java => RoutingListsServiceTest.java} | 11 +++++------ 9 files changed, 44 insertions(+), 35 deletions(-) rename src/main/java/org/folio/rest/impl/{RoutingListAPI.java => RoutingListsAPI.java} (82%) rename src/main/java/org/folio/service/{routinglist/RoutingListService.java => RoutingListsService.java} (83%) rename src/test/java/org/folio/service/{routinglist/RoutingListServiceTest.java => RoutingListsServiceTest.java} (86%) diff --git a/src/main/java/org/folio/config/ApplicationConfig.java b/src/main/java/org/folio/config/ApplicationConfig.java index 0dab116a6..52c05c36b 100644 --- a/src/main/java/org/folio/config/ApplicationConfig.java +++ b/src/main/java/org/folio/config/ApplicationConfig.java @@ -112,7 +112,7 @@ import org.folio.service.pieces.flows.update.PieceUpdateFlowInventoryManager; import org.folio.service.pieces.flows.update.PieceUpdateFlowManager; import org.folio.service.pieces.flows.update.PieceUpdateFlowPoLineService; -import org.folio.service.routinglist.RoutingListService; +import org.folio.service.RoutingListsService; import org.folio.service.titles.TitleValidationService; import org.folio.service.titles.TitlesService; import org.springframework.beans.factory.annotation.Qualifier; @@ -437,8 +437,8 @@ CompositeOrderDynamicDataPopulateService combinedPopulateService(CompositeOrderR } @Bean - RoutingListService routingListService(RestClient restClient, UserService userService) { - return new RoutingListService(restClient, userService); + RoutingListsService routingListService(RestClient restClient, UserService userService) { + return new RoutingListsService(restClient, userService); } @Bean diff --git a/src/main/java/org/folio/orders/utils/ResourcePathResolver.java b/src/main/java/org/folio/orders/utils/ResourcePathResolver.java index c03ffcdfc..fbc72ccdf 100644 --- a/src/main/java/org/folio/orders/utils/ResourcePathResolver.java +++ b/src/main/java/org/folio/orders/utils/ResourcePathResolver.java @@ -28,8 +28,9 @@ private ResourcePathResolver() { public static final String PAYMENT_STATUS = "paymentStatus"; public static final String ORDER_TEMPLATES = "orderTemplates"; public static final String TITLES = "titles"; - public static final String ROUTING_LISTS = "routing-lists"; - public static final String TEMPLATE_REQUEST = "template-request"; + public static final String ROUTING_LISTS = "routingLists"; + public static final String TEMPLATE_REQUEST = "templateRequest"; + public static final String PROCESS_TEMPLATE = "processTemplate"; public static final String USERS = "users"; public static final String FUNDS = "finance.funds"; public static final String BUDGETS = "finance.budgets"; @@ -71,6 +72,7 @@ private ResourcePathResolver() { apis.put(ORDER_TEMPLATES, "/orders-storage/order-templates"); apis.put(ROUTING_LISTS, "/orders-storage/routing-lists"); apis.put(TEMPLATE_REQUEST, "/template-request"); + apis.put(PROCESS_TEMPLATE, "/process-template"); apis.put(FUNDS, "/finance/funds"); apis.put(BUDGETS, "/finance/budgets"); apis.put(LEDGERS, "/finance-storage/ledgers"); diff --git a/src/main/java/org/folio/rest/impl/RoutingListAPI.java b/src/main/java/org/folio/rest/impl/RoutingListsAPI.java similarity index 82% rename from src/main/java/org/folio/rest/impl/RoutingListAPI.java rename to src/main/java/org/folio/rest/impl/RoutingListsAPI.java index e834e5692..430bb79d3 100644 --- a/src/main/java/org/folio/rest/impl/RoutingListAPI.java +++ b/src/main/java/org/folio/rest/impl/RoutingListsAPI.java @@ -11,16 +11,16 @@ import org.folio.rest.core.models.RequestContext; import org.folio.rest.jaxrs.model.RoutingList; import org.folio.rest.jaxrs.resource.OrdersRoutingLists; -import org.folio.service.routinglist.RoutingListService; +import org.folio.service.RoutingListsService; import org.folio.spring.SpringContextUtil; import org.springframework.beans.factory.annotation.Autowired; -public class RoutingListAPI extends BaseApi implements OrdersRoutingLists { +public class RoutingListsAPI extends BaseApi implements OrdersRoutingLists { @Autowired - private RoutingListService routingListService; + private RoutingListsService routingListsService; - public RoutingListAPI() { + public RoutingListsAPI() { SpringContextUtil.autowireDependencies(this, Vertx.currentContext()); } @@ -39,7 +39,7 @@ public void postOrdersRoutingLists(RoutingList entity, Map okapi @Override public void postOrdersRoutingListsProcessTemplateById(String id, Map okapiHeaders, Handler> asyncResultHandler, Context vertxContext) { - routingListService.processTemplateEngine(id, new RequestContext(vertxContext, okapiHeaders)) + routingListsService.processTemplateRequest(id, new RequestContext(vertxContext, okapiHeaders)) .onFailure(t -> handleErrorResponse(asyncResultHandler, t)); } } diff --git a/src/main/java/org/folio/service/routinglist/RoutingListService.java b/src/main/java/org/folio/service/RoutingListsService.java similarity index 83% rename from src/main/java/org/folio/service/routinglist/RoutingListService.java rename to src/main/java/org/folio/service/RoutingListsService.java index 99ab848d6..b6b064ebf 100644 --- a/src/main/java/org/folio/service/routinglist/RoutingListService.java +++ b/src/main/java/org/folio/service/RoutingListsService.java @@ -1,4 +1,4 @@ -package org.folio.service.routinglist; +package org.folio.service; import static org.folio.orders.utils.ResourcePathResolver.ROUTING_LISTS; import static org.folio.orders.utils.ResourcePathResolver.TEMPLATE_REQUEST; @@ -15,23 +15,23 @@ import org.folio.rest.core.models.RequestContext; import org.folio.rest.core.models.RequestEntry; import org.folio.rest.jaxrs.model.RoutingList; -import org.folio.service.UserService; @Log4j2 -public class RoutingListService { +public class RoutingListsService { private static final String ENDPOINT = resourcesPath(ROUTING_LISTS); private static final String BY_ID_ENDPOINT = ENDPOINT + "/{id}"; private final RestClient restClient; private final UserService userService; - public RoutingListService(RestClient restClient, UserService userService) { + public RoutingListsService(RestClient restClient, UserService userService) { this.restClient = restClient; this.userService = userService; } - public Future processTemplateEngine(String id, RequestContext requestContext) { - return getRoutingListById(id, requestContext) + public Future processTemplateRequest(String routingListId, RequestContext requestContext) { + log.debug("processTemplateRequest: Tying to process template request for routingListId={}", routingListId); + return getRoutingListById(routingListId, requestContext) .compose(routingList -> fetchUsersAndCreateTemplate(routingList, requestContext)) .compose(templateProcessingRequest -> postTemplateRequest(templateProcessingRequest, requestContext)); } @@ -51,6 +51,7 @@ private TemplateProcessingRequest createTemplateRequest(RoutingList routingList, var userListForContext = createUserListForContext(users); var context = new TemplateProcessingRequest.Context().withUsers(userListForContext); templateRequest.withContext(context); + log.info("createTemplateRequest:: TemplateProcessingRequest object created : {}", JsonObject.mapFrom(templateRequest).encodePrettily()); return templateRequest; } diff --git a/src/main/java/org/folio/service/UserService.java b/src/main/java/org/folio/service/UserService.java index 017c8e355..8b088d55b 100644 --- a/src/main/java/org/folio/service/UserService.java +++ b/src/main/java/org/folio/service/UserService.java @@ -15,7 +15,7 @@ import org.folio.rest.core.models.RequestEntry; public class UserService { - private static final String ENDPOINT = resourcesPath(USERS); + private static final String USERS_ENDPOINT = resourcesPath(USERS); private final RestClient restClient; public UserService(RestClient restClient) { @@ -27,10 +27,10 @@ public static String getCurrentUserId(Map okapiHeaders) { } public Future getUsersByIds(List userIds, RequestContext requestContext) { - var requestEntry = new RequestEntry(USERS) + var requestEntry = new RequestEntry(USERS_ENDPOINT) .withOffset(0) .withLimit(Integer.MAX_VALUE) - .withQuery(convertIdsToCqlQuery(userIds, "sourceInvoiceId")); + .withQuery(convertIdsToCqlQuery(userIds, "id")); return restClient.get(requestEntry, JsonObject.class, requestContext); } diff --git a/src/test/java/org/folio/ApiTestSuite.java b/src/test/java/org/folio/ApiTestSuite.java index aaa6890d9..9d83c7424 100644 --- a/src/test/java/org/folio/ApiTestSuite.java +++ b/src/test/java/org/folio/ApiTestSuite.java @@ -97,6 +97,7 @@ import org.folio.service.pieces.flows.update.PieceUpdateFlowManagerTest; import org.folio.service.pieces.flows.update.PieceUpdateFlowPoLineServiceTest; import org.folio.service.pieces.validators.PieceValidatorUtilTest; +import org.folio.service.RoutingListsServiceTest; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Nested; @@ -470,4 +471,8 @@ class FiscalYearServiceTestNested extends FiscalYearServiceTest { @Nested class TagServiceTestNested extends TagServiceTest { } + + @Nested + class RoutingListsServiceTestNested extends RoutingListsServiceTest { + } } diff --git a/src/test/java/org/folio/TestConstants.java b/src/test/java/org/folio/TestConstants.java index 809f13c6e..8297479bd 100644 --- a/src/test/java/org/folio/TestConstants.java +++ b/src/test/java/org/folio/TestConstants.java @@ -1,5 +1,6 @@ package org.folio; +import static org.folio.orders.utils.PermissionsUtil.OKAPI_HEADER_PERMISSIONS; import static org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT; import static org.folio.rest.RestVerticle.OKAPI_HEADER_TOKEN; import static org.folio.rest.RestVerticle.OKAPI_USERID_HEADER; @@ -9,6 +10,8 @@ import java.util.UUID; import io.restassured.http.Header; +import io.vertx.core.json.JsonArray; +import org.folio.orders.utils.AcqDesiredPermissions; public final class TestConstants { @@ -63,6 +66,7 @@ private TestConstants() {} public static final Header NON_EXIST_LOAN_TYPE_TENANT_HEADER = new Header(OKAPI_HEADER_TENANT, NON_EXIST_LOAN_TYPE_TENANT); public static final Header NON_EXIST_CONFIG_X_OKAPI_TENANT = new Header(OKAPI_HEADER_TENANT, "ordersimpltest"); public static final Header X_OKAPI_USER_ID = new Header(OKAPI_USERID_HEADER, "440c89e3-7f6c-578a-9ea8-310dad23605e"); + public static final Header ALL_DESIRED_ACQ_PERMISSIONS_HEADER = new Header(OKAPI_HEADER_PERMISSIONS, new JsonArray(AcqDesiredPermissions.getValuesExceptBypass()).encode()); public static final Header X_OKAPI_USER_ID_WITH_ACQ_UNITS = new Header(OKAPI_USERID_HEADER, USER_ID_ASSIGNED_TO_ACQ_UNITS); public static final Header X_OKAPI_TOKEN = new Header(OKAPI_HEADER_TOKEN, "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJkaWt1X2FkbWluIiwidXNlcl9pZCI6ImJmZTI2MjM0LTMzNjktNTdhYS05ZjhhLWU2ZWVhY2M0YTgzYiIsImlhdCI6MTU4MzE1Nzg5OCwidGVuYW50IjoiZGlrdSJ9.Mk7u4KaCywSuYtBgCT44oGcVC0C8jUMY9KjsUnug48I"); public static final Header EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10 = new Header(OKAPI_HEADER_TENANT, "test_diku_limit_10"); @@ -85,4 +89,5 @@ private TestConstants() {} public static final String PIECE_PATH = BASE_MOCK_DATA_PATH + "pieces/"; public static final String TILES_PATH = BASE_MOCK_DATA_PATH + "titles/"; public static final String ID_FOR_TEMPLATE_NAME_ALREADY_EXISTS = "cd0619fb-a628-4d90-be41-df8943e97768"; + public static final String ROUTING_LIST_ID = "eee951de-ea49-400a-96e8-705ae5a1e1e8"; } diff --git a/src/test/java/org/folio/rest/impl/TitlesApiTest.java b/src/test/java/org/folio/rest/impl/TitlesApiTest.java index d5b7004b7..73dc5cf8d 100644 --- a/src/test/java/org/folio/rest/impl/TitlesApiTest.java +++ b/src/test/java/org/folio/rest/impl/TitlesApiTest.java @@ -10,6 +10,7 @@ import static org.folio.TestConfig.clearServiceInteractions; import static org.folio.TestConfig.initSpringContext; import static org.folio.TestConfig.isVerticleNotDeployed; +import static org.folio.TestConstants.ALL_DESIRED_ACQ_PERMISSIONS_HEADER; import static org.folio.TestConstants.EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10; import static org.folio.TestConstants.ID_BAD_FORMAT; import static org.folio.TestConstants.ID_DOES_NOT_EXIST; @@ -20,7 +21,6 @@ import static org.folio.TestUtils.getMinimalContentCompositePoLine; import static org.folio.TestUtils.getMockAsJson; import static org.folio.TestUtils.getMockData; -import static org.folio.orders.utils.PermissionsUtil.OKAPI_HEADER_PERMISSIONS; import static org.folio.orders.utils.ResourcePathResolver.PO_LINES_STORAGE; import static org.folio.rest.core.exceptions.ErrorCodes.*; import static org.folio.rest.impl.MockServer.TITLES_MOCK_DATA_PATH; @@ -38,14 +38,12 @@ import java.util.concurrent.TimeoutException; import io.restassured.http.Header; -import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.folio.ApiTestSuite; import org.folio.HttpStatus; import org.folio.config.ApplicationConfig; -import org.folio.orders.utils.AcqDesiredPermissions; import org.folio.rest.acq.model.Title; import org.folio.rest.jaxrs.model.CompositePoLine; import org.folio.rest.jaxrs.model.Details; @@ -72,7 +70,6 @@ public class TitlesApiTest { public static final String SAMPLE_TITLE_ID = "9a665b22-9fe5-4c95-b4ee-837a5433c95d"; private final JsonObject titleJsonReqData = getMockAsJson(TITLES_MOCK_DATA_PATH + "title.json"); private final JsonObject packageTitleJsonReqData = getMockAsJson(TITLES_MOCK_DATA_PATH + "package_title.json"); - public static final Header ALL_DESIRED_PERMISSIONS_HEADER = new Header(OKAPI_HEADER_PERMISSIONS, new JsonArray(AcqDesiredPermissions.getValuesExceptBypass()).encode()); private static boolean runningOnOwn; @@ -121,7 +118,7 @@ void testPostTitle() { assertThat(postTitleRq.getId(), nullValue()); Title postTitleRs = verifyPostResponse(TITLES_ENDPOINT, JsonObject.mapFrom(postTitleRq).encode(), - prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID_WITH_ACQ_UNITS, ALL_DESIRED_PERMISSIONS_HEADER), APPLICATION_JSON, HttpStatus.HTTP_CREATED.toInt()).as(Title.class); + prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID_WITH_ACQ_UNITS, ALL_DESIRED_ACQ_PERMISSIONS_HEADER), APPLICATION_JSON, HttpStatus.HTTP_CREATED.toInt()).as(Title.class); // Title id not null assertThat(postTitleRs.getId(), Matchers.notNullValue()); @@ -129,12 +126,12 @@ void testPostTitle() { // Negative cases // Unable to create title test int status400 = HttpStatus.HTTP_BAD_REQUEST.toInt(); - verifyPostResponse(TITLES_ENDPOINT, JsonObject.mapFrom(postTitleRq).encode(), prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID_WITH_ACQ_UNITS, ALL_DESIRED_PERMISSIONS_HEADER, + verifyPostResponse(TITLES_ENDPOINT, JsonObject.mapFrom(postTitleRq).encode(), prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID_WITH_ACQ_UNITS, ALL_DESIRED_ACQ_PERMISSIONS_HEADER, new Header(X_ECHO_STATUS, String.valueOf(status400))), APPLICATION_JSON, status400); // Internal error on mod-orders-storage test int status500 = HttpStatus.HTTP_INTERNAL_SERVER_ERROR.toInt(); - verifyPostResponse(TITLES_ENDPOINT, JsonObject.mapFrom(postTitleRq).encode(), prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID_WITH_ACQ_UNITS, ALL_DESIRED_PERMISSIONS_HEADER, + verifyPostResponse(TITLES_ENDPOINT, JsonObject.mapFrom(postTitleRq).encode(), prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID_WITH_ACQ_UNITS, ALL_DESIRED_ACQ_PERMISSIONS_HEADER, new Header(X_ECHO_STATUS, String.valueOf(status500))), APPLICATION_JSON, status500); } @@ -163,7 +160,7 @@ void postTitleWithInvalidClaimingConfig() throws Exception { String reqData = getMockData(TITLES_MOCK_DATA_PATH + "title_invalid_claiming_config.json"); - List errors = verifyPostResponse(TITLES_ENDPOINT, reqData, prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID, ALL_DESIRED_PERMISSIONS_HEADER), APPLICATION_JSON, 422) + List errors = verifyPostResponse(TITLES_ENDPOINT, reqData, prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID, ALL_DESIRED_ACQ_PERMISSIONS_HEADER), APPLICATION_JSON, 422) .as(Errors.class) .getErrors(); @@ -192,7 +189,7 @@ void titleShouldBePopulatedFromPackagePoLine() { addMockEntry(PO_LINES_STORAGE, JsonObject.mapFrom(packagePoLine)); Title titleWithPackagePoLineRS = verifyPostResponse(TITLES_ENDPOINT, JsonObject.mapFrom(titleWithPackagePoLineRQ).encode(), - prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID, ALL_DESIRED_PERMISSIONS_HEADER), APPLICATION_JSON, HttpStatus.HTTP_CREATED.toInt()).as(Title.class); + prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, X_OKAPI_USER_ID, ALL_DESIRED_ACQ_PERMISSIONS_HEADER), APPLICATION_JSON, HttpStatus.HTTP_CREATED.toInt()).as(Title.class); assertEquals(titleWithPackagePoLineRS.getPackageName(), packageTitleName); assertNotNull(titleWithPackagePoLineRS.getExpectedReceiptDate()); @@ -231,7 +228,7 @@ void testPutTitlesByIdTest() { .withAcqUnitIds(List.of(ACQ_UNIT_ID)); verifyPut(String.format(TITLES_ID_PATH, SAMPLE_TITLE_ID), JsonObject.mapFrom(reqData).encode(), - prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, ALL_DESIRED_PERMISSIONS_HEADER), "", 204); + prepareHeaders(EXIST_CONFIG_X_OKAPI_TENANT_LIMIT_10, ALL_DESIRED_ACQ_PERMISSIONS_HEADER), "", 204); } @Test diff --git a/src/test/java/org/folio/service/routinglist/RoutingListServiceTest.java b/src/test/java/org/folio/service/RoutingListsServiceTest.java similarity index 86% rename from src/test/java/org/folio/service/routinglist/RoutingListServiceTest.java rename to src/test/java/org/folio/service/RoutingListsServiceTest.java index 23b620f77..f1bdf6e68 100644 --- a/src/test/java/org/folio/service/routinglist/RoutingListServiceTest.java +++ b/src/test/java/org/folio/service/RoutingListsServiceTest.java @@ -1,6 +1,7 @@ -package org.folio.service.routinglist; +package org.folio.service; import static io.vertx.core.Future.succeededFuture; +import static org.folio.TestConstants.ROUTING_LIST_ID; import static org.folio.TestUtils.getMockData; import static org.folio.rest.impl.MockServer.ROUTING_LIST_MOCK_DATA_PATH; import static org.folio.rest.impl.MockServer.USERS_MOCK_DATA_PATH; @@ -19,7 +20,6 @@ import org.folio.rest.core.models.RequestContext; import org.folio.rest.core.models.RequestEntry; import org.folio.rest.jaxrs.model.RoutingList; -import org.folio.service.UserService; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -29,11 +29,10 @@ import org.mockito.MockitoAnnotations; @ExtendWith(VertxExtension.class) -public class RoutingListServiceTest { +public class RoutingListsServiceTest { - private static final String ROUTING_LIST_ID = "eee951de-ea49-400a-96e8-705ae5a1e1e8"; @InjectMocks - RoutingListService routingListService; + RoutingListsService routingListsService; @Mock private RestClient restClient; @Mock @@ -55,7 +54,7 @@ void processTemplate(VertxTestContext vertxTestContext) throws IOException { doReturn(succeededFuture(users)).when(userService).getUsersByIds(eq(routingList.getUserIds()), any(RequestContext.class)); doReturn(succeededFuture(new JsonObject())).when(restClient).post(anyString(), any(), eq(JsonObject.class), any()); - Future future = routingListService.processTemplateEngine(ROUTING_LIST_ID, requestContextMock); + Future future = routingListsService.processTemplateRequest(ROUTING_LIST_ID, requestContextMock); vertxTestContext.assertComplete(future) .onComplete(result -> {