Skip to content

Commit

Permalink
Fix Open api issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisSaldabols committed Nov 23, 2023
1 parent a724934 commit ae6e306
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 107 deletions.
4 changes: 1 addition & 3 deletions src/main/java/org/folio/print/server/main/MainVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.logging.log4j.Logger;
import org.folio.okapi.common.Config;
import org.folio.okapi.common.ModuleVersionReporter;
import org.folio.print.server.resources.BatchCreationResource;
import org.folio.print.server.service.PrintService;
import org.folio.tlib.RouterCreator;
import org.folio.tlib.api.HealthApi;
Expand All @@ -37,8 +36,7 @@ public void start(Promise<Void> promise) {
RouterCreator[] routerCreators = {
printServiceService,
new Tenant2Api(printServiceService),
new HealthApi(),
new BatchCreationResource()
new HealthApi()
};

RouterCreator.mountAll(vertx, routerCreators)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.folio.print.server.service;

import io.vertx.ext.web.RoutingContext;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.pdfbox.util.Hex;
import org.folio.okapi.common.XOkapiHeaders;
import org.folio.print.server.data.PrintEntry;
import org.folio.print.server.data.PrintEntryType;
import org.folio.print.server.storage.PrintStorage;

public class BatchCreationService {
private static final Logger LOGGER = LogManager.getLogger(BatchCreationService.class);
private static final int MAX_COUNT_IN_BATCH = 1000;

private BatchCreationService() {
}

/**
* Process batch creation request.
* @param ctx Batch creation request context
*/
public static void process(RoutingContext ctx) {
String tenant = ctx.request().getHeader(XOkapiHeaders.TENANT);
LOGGER.debug("process:: tenant " + tenant);
PrintStorage printStorage = new PrintStorage(ctx.vertx(), tenant);
LocalDateTime localDateTime = LocalDateTime.now().with(LocalTime.MIDNIGHT);

printStorage.getEntriesByQuery("type=\"SINGLE\" and created > " + localDateTime
+ " sortby sortingField created", 0, MAX_COUNT_IN_BATCH)
.onSuccess(l -> processListAndSaveResult(l, printStorage))
.onFailure(e -> LOGGER.error("Failed to create print batch", e));
ctx.response().setStatusCode(204);
ctx.response().end();
}

private static void processListAndSaveResult(List<PrintEntry> entries, PrintStorage storage) {
if (!entries.isEmpty()) {
byte[] merged = PdfService.combinePdfFiles(entries);
PrintEntry batch = new PrintEntry();
batch.setId(UUID.randomUUID());
batch.setCreated(ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC));
batch.setType(PrintEntryType.BATCH);
batch.setContent(Hex.getString(merged));
storage.createEntry(batch);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
Expand Down Expand Up @@ -113,6 +112,11 @@ private void handlers(RouterBuilder routerBuilder) {
.onFailure(cause -> commonError(ctx, cause))
)
.failureHandler(this::failureHandler);

routerBuilder
.operation("createBatch")
.handler(BatchCreationService::process)
.failureHandler(this::failureHandler);
}

static PrintStorage createFromParams(Vertx vertx, RequestParameters params) {
Expand Down
16 changes: 2 additions & 14 deletions src/main/resources/openapi/batchPrint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,16 @@ paths:
description: >
Send mail to create print entry.
X-Okapi-Permissions must include mod-batch-print.print.write
operationId: createBatch
responses:
"200":
"204":
description: Print entry created
"400":
$ref: "#/components/responses/trait_400"
"403":
$ref: "#/components/responses/trait_403"
"500":
$ref: "#/components/responses/trait_500"
/print/fake:
parameters:
- $ref: headers/okapi-permissions.yaml
- $ref: headers/okapi-tenant.yaml
- $ref: headers/okapi-token.yaml
- $ref: headers/okapi-url.yaml
- $ref: headers/okapi-user.yaml
post:
description: >
Fake endpoint for service to work
responses:
"200":
description: Fake
components:
responses:
trait_400:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public void testCrudGlobalOk() {
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body(is(en.encode()));
.body("id", is(entry.getId().toString()))
.body("content", is(entry.getContent()))
.body("type", is(entry.getType().toString()))
.body("sortingField", is(entry.getSortingField()));

entry.setContent("BB");
en = JsonObject.mapFrom(entry);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.folio.print.server.resources;
package org.folio.print.server.service;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
Expand All @@ -15,7 +15,7 @@
import static org.hamcrest.Matchers.notNullValue;

@RunWith(VertxUnitRunner.class)
public class BatchCreationResourceTest extends TestBase {
public class BatchCreationServiceTest extends TestBase {

@Test
public void createBatch() throws IOException {
Expand Down Expand Up @@ -49,6 +49,7 @@ public void createBatch() throws IOException {
.baseUri(MODULE_URL)
.header(XOkapiHeaders.TENANT, TENANT_1)
.header(XOkapiHeaders.PERMISSIONS, perm.encode())
.contentType(ContentType.JSON)
.post("/print/batch-creation")
.then()
.statusCode(204);
Expand Down

0 comments on commit ae6e306

Please sign in to comment.