From ed5839aa0a6f7c8086df2eddfab82e3ffea5478e Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Thu, 26 Jan 2023 11:18:05 -0600 Subject: [PATCH] Add /run endpoint for running processes w/o any frontend --- .../widgets/ChildRecordListRenderer.java | 1 + .../instances/SecretsManagerUtilsTest.java | 46 +++++++++++++++++++ .../javalin/QJavalinProcessHandler.java | 33 +++++++++---- .../javalin/QJavalinProcessHandlerTest.java | 26 +++++++++++ .../qqq/backend/javalin/TestUtils.java | 1 + 5 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/instances/SecretsManagerUtilsTest.java diff --git a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/dashboard/widgets/ChildRecordListRenderer.java b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/dashboard/widgets/ChildRecordListRenderer.java index 6004475a..ce9f1e4a 100644 --- a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/dashboard/widgets/ChildRecordListRenderer.java +++ b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/dashboard/widgets/ChildRecordListRenderer.java @@ -70,6 +70,7 @@ public class ChildRecordListRenderer extends AbstractWidgetRenderer { return (new Builder(new QWidgetMetaData() .withName(join.getName()) + .withIsCard(true) .withCodeReference(new QCodeReference(ChildRecordListRenderer.class, null)) .withType(WidgetType.CHILD_RECORD_LIST.getType()) .withDefaultValue("joinName", join.getName()))); diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/instances/SecretsManagerUtilsTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/instances/SecretsManagerUtilsTest.java new file mode 100644 index 00000000..11e5c650 --- /dev/null +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/instances/SecretsManagerUtilsTest.java @@ -0,0 +1,46 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2023. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.kingsrook.qqq.backend.core.instances; + + +import java.util.Optional; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +/******************************************************************************* + ** Unit test for SecretsManagerUtils + *******************************************************************************/ +class SecretsManagerUtilsTest +{ + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testSecretsManagerWithoutVars() + { + Optional secret = SecretsManagerUtils.getSecret("foo", "bar"); + assertTrue(secret.isEmpty()); + } + +} \ No newline at end of file diff --git a/qqq-middleware-javalin/src/main/java/com/kingsrook/qqq/backend/javalin/QJavalinProcessHandler.java b/qqq-middleware-javalin/src/main/java/com/kingsrook/qqq/backend/javalin/QJavalinProcessHandler.java index 234a1b07..010af005 100644 --- a/qqq-middleware-javalin/src/main/java/com/kingsrook/qqq/backend/javalin/QJavalinProcessHandler.java +++ b/qqq-middleware-javalin/src/main/java/com/kingsrook/qqq/backend/javalin/QJavalinProcessHandler.java @@ -50,8 +50,6 @@ import com.kingsrook.qqq.backend.core.actions.processes.RunProcessAction; import com.kingsrook.qqq.backend.core.actions.reporting.GenerateReportAction; import com.kingsrook.qqq.backend.core.actions.tables.InsertAction; import com.kingsrook.qqq.backend.core.actions.values.QValueFormatter; -import com.kingsrook.qqq.backend.core.exceptions.QException; -import com.kingsrook.qqq.backend.core.exceptions.QModuleDispatchException; import com.kingsrook.qqq.backend.core.exceptions.QNotFoundException; import com.kingsrook.qqq.backend.core.exceptions.QPermissionDeniedException; import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException; @@ -119,6 +117,9 @@ public class QJavalinProcessHandler get("/init", QJavalinProcessHandler::processInit); post("/init", QJavalinProcessHandler::processInit); + get("/run", QJavalinProcessHandler::processRun); + post("/run", QJavalinProcessHandler::processRun); + path("/{processUUID}", () -> { post("/step/{step}", QJavalinProcessHandler::processStep); @@ -290,9 +291,23 @@ public class QJavalinProcessHandler ** Init a process (named in path param :process) ** *******************************************************************************/ - public static void processInit(Context context) throws QException + public static void processInit(Context context) { - doProcessInitOrStep(context, null, null); + doProcessInitOrStep(context, null, null, RunProcessInput.FrontendStepBehavior.BREAK); + } + + + + /******************************************************************************* + ** Run a process (named in path param :process) - that is - fully run, not + ** breaking on frontend steps. Note, we may still go Async - use query or + ** form body param `_qStepTimeoutMillis` to set a higher timeout to get more + ** synchronous-like behavior. + ** + *******************************************************************************/ + public static void processRun(Context context) + { + doProcessInitOrStep(context, null, null, RunProcessInput.FrontendStepBehavior.SKIP); } @@ -300,7 +315,7 @@ public class QJavalinProcessHandler /******************************************************************************* ** *******************************************************************************/ - private static void doProcessInitOrStep(Context context, String processUUID, String startAfterStep) + private static void doProcessInitOrStep(Context context, String processUUID, String startAfterStep, RunProcessInput.FrontendStepBehavior frontendStepBehavior) { Map resultForCaller = new HashMap<>(); Exception returningException = null; @@ -321,7 +336,7 @@ public class QJavalinProcessHandler QJavalinImplementation.setupSession(context, runProcessInput); runProcessInput.setProcessName(processName); - runProcessInput.setFrontendStepBehavior(RunProcessInput.FrontendStepBehavior.BREAK); + runProcessInput.setFrontendStepBehavior(frontendStepBehavior); runProcessInput.setProcessUUID(processUUID); runProcessInput.setStartAfterStep(startAfterStep); populateRunProcessRequestWithValuesFromContext(context, runProcessInput); @@ -518,7 +533,7 @@ public class QJavalinProcessHandler *******************************************************************************/ private static void archiveUploadedFile(RunProcessInput runProcessInput, QUploadedFile qUploadedFile) { - String fileName = new QValueFormatter().formatDate(LocalDate.now()) + String fileName = QValueFormatter.formatDate(LocalDate.now()) + File.separator + runProcessInput.getProcessName() + File.separator + qUploadedFile.getFilename(); @@ -588,11 +603,11 @@ public class QJavalinProcessHandler ** Run a step in a process (named in path param :processName) ** *******************************************************************************/ - public static void processStep(Context context) throws QModuleDispatchException + public static void processStep(Context context) { String processUUID = context.pathParam("processUUID"); String lastStep = context.pathParam("step"); - doProcessInitOrStep(context, processUUID, lastStep); + doProcessInitOrStep(context, processUUID, lastStep, RunProcessInput.FrontendStepBehavior.BREAK); } diff --git a/qqq-middleware-javalin/src/test/java/com/kingsrook/qqq/backend/javalin/QJavalinProcessHandlerTest.java b/qqq-middleware-javalin/src/test/java/com/kingsrook/qqq/backend/javalin/QJavalinProcessHandlerTest.java index b53930fd..29db1529 100644 --- a/qqq-middleware-javalin/src/test/java/com/kingsrook/qqq/backend/javalin/QJavalinProcessHandlerTest.java +++ b/qqq-middleware-javalin/src/test/java/com/kingsrook/qqq/backend/javalin/QJavalinProcessHandlerTest.java @@ -250,6 +250,7 @@ class QJavalinProcessHandlerTest extends QJavalinTestBase String nextStep = jsonObject.getString("nextStep"); assertNotNull(processUUID, "Process UUID should not be null."); assertNotNull(nextStep, "There should be a next step"); + assertFalse(jsonObject.getJSONObject("values").has("didSleep"), "There should not (yet) be a value from the backend step"); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // second, run the 'nextStep' (the backend step, that sleeps). run it with a long enough sleep so that it'll go async // @@ -274,6 +275,31 @@ class QJavalinProcessHandlerTest extends QJavalinTestBase String nextStep2 = jsonObject.getString("nextStep"); assertNotNull(nextStep2, "There be one more next step"); assertNotEquals(nextStep, nextStep2, "The next step should be different this time."); + assertTrue(jsonObject.getJSONObject("values").has("didSleep"), "There should be a value from the backend step"); + } + + + + /******************************************************************************* + ** test fully running a process that has frontend steps - and observing that we + ** don't stop on them. + ** + *******************************************************************************/ + @Test + public void test_processRunWithoutBreakingForFrontendSteps() throws InterruptedException + { + ///////////////////////////////////////////// + // first init the process, to get its UUID // + ///////////////////////////////////////////// + String processBasePath = BASE_URL + "/processes/" + TestUtils.PROCESS_NAME_SLEEP_INTERACTIVE; + HttpResponse response = Unirest.post(processBasePath + "/run?" + TestUtils.SleeperStep.FIELD_SLEEP_MILLIS + "=" + LESS_THAN_TIMEOUT) + .header("Content-Type", "application/json").asString(); + + JSONObject jsonObject = assertProcessStepCompleteResponse(response); + String processUUID = jsonObject.getString("processUUID"); + assertNotNull(processUUID, "Process UUID should not be null."); + assertFalse(jsonObject.has("nextStep"), "There should not be a next step"); + assertTrue(jsonObject.getJSONObject("values").has("didSleep"), "There should be a value from the backend step"); } diff --git a/qqq-middleware-javalin/src/test/java/com/kingsrook/qqq/backend/javalin/TestUtils.java b/qqq-middleware-javalin/src/test/java/com/kingsrook/qqq/backend/javalin/TestUtils.java index 5a3fcc06..e3daf4e2 100644 --- a/qqq-middleware-javalin/src/test/java/com/kingsrook/qqq/backend/javalin/TestUtils.java +++ b/qqq-middleware-javalin/src/test/java/com/kingsrook/qqq/backend/javalin/TestUtils.java @@ -402,6 +402,7 @@ public class TestUtils try { Thread.sleep(runBackendStepInput.getValueInteger(FIELD_SLEEP_MILLIS)); + runBackendStepOutput.addValue("didSleep", true); } catch(InterruptedException e) {