From 353ee3f9138c9f6b06bb89f677e55d89672448f7 Mon Sep 17 00:00:00 2001 From: Rob Rudin Date: Mon, 7 Jul 2025 13:31:09 -0400 Subject: [PATCH] MLE-22812 Refactor: Created ForestPlanner This is just to move as much logic out of the Gradle task (hard to test) into a regular Java class in ml-app-deployer (easy to test). --- .../command/forests/ForestPlanner.java | 52 +++++++++++++++++++ .../command/forests/ForestPlannerTest.java | 26 ++++++++++ .../task/forests/PrintForestPlanTask.groovy | 30 ++--------- 3 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 ml-app-deployer/src/main/java/com/marklogic/appdeployer/command/forests/ForestPlanner.java create mode 100644 ml-app-deployer/src/test/java/com/marklogic/appdeployer/command/forests/ForestPlannerTest.java diff --git a/ml-app-deployer/src/main/java/com/marklogic/appdeployer/command/forests/ForestPlanner.java b/ml-app-deployer/src/main/java/com/marklogic/appdeployer/command/forests/ForestPlanner.java new file mode 100644 index 000000000..2a5791ad3 --- /dev/null +++ b/ml-app-deployer/src/main/java/com/marklogic/appdeployer/command/forests/ForestPlanner.java @@ -0,0 +1,52 @@ +/* + * Copyright © 2025 MarkLogic Corporation. All Rights Reserved. + */ +package com.marklogic.appdeployer.command.forests; + +import com.marklogic.appdeployer.AppConfig; +import com.marklogic.appdeployer.command.CommandContext; +import com.marklogic.appdeployer.command.databases.DatabasePlan; +import com.marklogic.appdeployer.command.databases.DeployDatabaseCommand; +import com.marklogic.appdeployer.command.databases.DeployOtherDatabasesCommand; +import com.marklogic.mgmt.ManageClient; +import com.marklogic.mgmt.api.forest.Forest; + +import java.util.ArrayList; +import java.util.List; + +/** + * Intended to be the central interface for "Give me a database name and a set of inputs, and I'll give you back a list + * of forests with replicas that should be created for that database". We'll sort out the naming of this and the + * related classes later. + */ +public class ForestPlanner { + + private final ManageClient manageClient; + + public ForestPlanner(ManageClient manageClient) { + this.manageClient = manageClient; + } + + public List previewForestPlan(String database, AppConfig appConfig) { + // We unfortunately still need a CommandContext here, even though this is just for previewing forests. The + // classes that this depends on would need to be modified first to only require an AppConfig and/or ManageClient. + final CommandContext context = new CommandContext(appConfig, this.manageClient, null); + + List plans = new DeployOtherDatabasesCommand().buildDatabasePlans(context); + DeployDatabaseCommand dbCommand = null; + for (DatabasePlan plan : plans) { + if (plan.getDatabaseName().equals(database)) { + dbCommand = plan.getDeployDatabaseCommand(); + break; + } + } + if (dbCommand == null) { + throw new IllegalArgumentException("Did not find any database plan with a database name of: " + database); + } + + DeployForestsCommand deployForestsCommand = dbCommand.buildDeployForestsCommand(database, context); + return deployForestsCommand != null ? + deployForestsCommand.buildForests(context, true) : + new ArrayList<>(); + } +} diff --git a/ml-app-deployer/src/test/java/com/marklogic/appdeployer/command/forests/ForestPlannerTest.java b/ml-app-deployer/src/test/java/com/marklogic/appdeployer/command/forests/ForestPlannerTest.java new file mode 100644 index 000000000..ae209f850 --- /dev/null +++ b/ml-app-deployer/src/test/java/com/marklogic/appdeployer/command/forests/ForestPlannerTest.java @@ -0,0 +1,26 @@ +/* + * Copyright © 2025 MarkLogic Corporation. All Rights Reserved. + */ +package com.marklogic.appdeployer.command.forests; + +import com.marklogic.appdeployer.AbstractAppDeployerTest; +import com.marklogic.mgmt.api.forest.Forest; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ForestPlannerTest extends AbstractAppDeployerTest { + + private static final String DB_NAME = "sample-app-content"; + + @Test + void test() { + List forests = new ForestPlanner(manageClient).previewForestPlan(DB_NAME, appConfig); + assertEquals(3, forests.size()); + assertEquals("sample-app-content-1", forests.get(0).getForestName()); + assertEquals("sample-app-content-2", forests.get(1).getForestName()); + assertEquals("sample-app-content-3", forests.get(2).getForestName()); + } +} diff --git a/ml-gradle/src/main/groovy/com/marklogic/gradle/task/forests/PrintForestPlanTask.groovy b/ml-gradle/src/main/groovy/com/marklogic/gradle/task/forests/PrintForestPlanTask.groovy index 000b25f45..61d3d8f61 100644 --- a/ml-gradle/src/main/groovy/com/marklogic/gradle/task/forests/PrintForestPlanTask.groovy +++ b/ml-gradle/src/main/groovy/com/marklogic/gradle/task/forests/PrintForestPlanTask.groovy @@ -15,14 +15,10 @@ */ package com.marklogic.gradle.task.forests -import com.marklogic.appdeployer.command.databases.DatabasePlan -import com.marklogic.appdeployer.command.databases.DeployDatabaseCommand -import com.marklogic.appdeployer.command.databases.DeployOtherDatabasesCommand -import com.marklogic.appdeployer.command.forests.DeployForestsCommand -import com.marklogic.appdeployer.impl.SimpleAppDeployer + +import com.marklogic.appdeployer.command.forests.ForestPlanner import com.marklogic.gradle.task.MarkLogicTask import com.marklogic.mgmt.api.forest.Forest -import org.gradle.api.GradleException import org.gradle.api.tasks.TaskAction /** @@ -41,27 +37,7 @@ class PrintForestPlanTask extends MarkLogicTask { } String database = project.property("database") - - SimpleAppDeployer appDeployer = getAppDeployer() - DeployOtherDatabasesCommand command = appDeployer.getCommandOfType(DeployOtherDatabasesCommand.class) - List plans = command.buildDatabasePlans(getCommandContext()) - - DeployDatabaseCommand deployDatabaseCommand - for (DatabasePlan plan : plans) { - if (database.equals(plan.getDatabaseName())) { - deployDatabaseCommand = plan.getDeployDatabaseCommand() - break - } - } - - if (deployDatabaseCommand == null) { - throw new GradleException("Did not find any database plan with a database name of: " + database) - } - - DeployForestsCommand deployForestsCommand = deployDatabaseCommand.buildDeployForestsCommand(database, getCommandContext()) - List forests = deployForestsCommand != null ? - deployForestsCommand.buildForests(getCommandContext(), true) : - new ArrayList<>() + List forests = new ForestPlanner(getManageClient()).previewForestPlan(database, getAppConfig()) if (forests.isEmpty()) { println "\nNo primary forests will be created the next time the database '" + database + "' is deployed. This is " +