Skip to content

Commit

Permalink
[MODORSERS-1026] - Implemented api to execute template-request api
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Mar 29, 2024
1 parent 5326d4e commit 1276445
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/folio/config/ApplicationConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -39,7 +39,7 @@ public void postOrdersRoutingLists(RoutingList entity, Map<String, String> okapi
@Override
public void postOrdersRoutingListsProcessTemplateById(String id, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
routingListService.processTemplateEngine(id, new RequestContext(vertxContext, okapiHeaders))
routingListsService.processTemplateRequest(id, new RequestContext(vertxContext, okapiHeaders))
.onFailure(t -> handleErrorResponse(asyncResultHandler, t));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<JsonObject> processTemplateEngine(String id, RequestContext requestContext) {
return getRoutingListById(id, requestContext)
public Future<JsonObject> 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));
}
Expand All @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/folio/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -27,10 +27,10 @@ public static String getCurrentUserId(Map<String, String> okapiHeaders) {
}

public Future<JsonObject> getUsersByIds(List<String> 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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/folio/ApiTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -470,4 +471,8 @@ class FiscalYearServiceTestNested extends FiscalYearServiceTest {
@Nested
class TagServiceTestNested extends TagServiceTest {
}

@Nested
class RoutingListsServiceTestNested extends RoutingListsServiceTest {
}
}
5 changes: 5 additions & 0 deletions src/test/java/org/folio/TestConstants.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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");
Expand All @@ -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";
}
17 changes: 7 additions & 10 deletions src/test/java/org/folio/rest/impl/TitlesApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -121,20 +118,20 @@ 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());

// 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);
}

Expand Down Expand Up @@ -163,7 +160,7 @@ void postTitleWithInvalidClaimingConfig() throws Exception {

String reqData = getMockData(TITLES_MOCK_DATA_PATH + "title_invalid_claiming_config.json");

List<Error> 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<Error> 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();

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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<JsonObject> future = routingListService.processTemplateEngine(ROUTING_LIST_ID, requestContextMock);
Future<JsonObject> future = routingListsService.processTemplateRequest(ROUTING_LIST_ID, requestContextMock);

vertxTestContext.assertComplete(future)
.onComplete(result -> {
Expand Down

0 comments on commit 1276445

Please sign in to comment.