From 233eddbcfaaf2c61ce367964b64ec73d9b5a1321 Mon Sep 17 00:00:00 2001 From: Sanjay Dutt Date: Wed, 10 Jul 2024 21:52:04 +0530 Subject: [PATCH] SOLR-17321: Fix TestSolrCoreSnapshots.testBackupRestore (#2539) --- .../solr/handler/BackupRestoreUtils.java | 64 ++++++++++--------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/solr/test-framework/src/java/org/apache/solr/handler/BackupRestoreUtils.java b/solr/test-framework/src/java/org/apache/solr/handler/BackupRestoreUtils.java index 200709ef9a2..bd610200895 100644 --- a/solr/test-framework/src/java/org/apache/solr/handler/BackupRestoreUtils.java +++ b/solr/test-framework/src/java/org/apache/solr/handler/BackupRestoreUtils.java @@ -21,11 +21,13 @@ import java.io.InputStream; import java.lang.invoke.MethodHandles; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Random; +import org.apache.http.client.utils.URIBuilder; import org.apache.lucene.tests.util.TestUtil; import org.apache.solr.SolrTestCase; import org.apache.solr.client.solrj.SolrClient; @@ -75,42 +77,46 @@ public static void verifyDocs(int nDocs, SolrClient leaderClient, String collect public static void runCoreAdminCommand( String baseUrl, String coreName, String action, Map params) - throws IOException { - StringBuilder builder = new StringBuilder(); - builder.append(baseUrl); - builder.append("/admin/cores?action="); - builder.append(action); - builder.append("&core="); - builder.append(coreName); - for (Map.Entry p : params.entrySet()) { - builder.append("&"); - builder.append(p.getKey()); - builder.append("="); - builder.append(p.getValue()); + throws IOException, URISyntaxException { + final URI uri = new URI(baseUrl); + final var oldPath = uri.getPath() != null ? uri.getPath().substring(1) : ""; + final var newPath = "admin/cores"; + final var finalPath = oldPath.isEmpty() ? newPath : oldPath + "/" + newPath; + + final URIBuilder builder = + new URIBuilder(uri) + .setPath(finalPath) + .addParameter("action", action) + .addParameter("core", coreName); + + // Add additional parameters using loop + for (Map.Entry entry : params.entrySet()) { + builder.addParameter(entry.getKey(), entry.getValue()); } - String leaderUrl = builder.toString(); - executeHttpRequest(leaderUrl); + + executeHttpRequest(builder.build()); } public static void runReplicationHandlerCommand( String baseUrl, String coreName, String action, String repoName, String backupName) - throws IOException { - String leaderUrl = - baseUrl - + "/" - + coreName - + ReplicationHandler.PATH - + "?command=" - + action - + "&repository=" - + repoName - + "&name=" - + backupName; - executeHttpRequest(leaderUrl); + throws IOException, URISyntaxException { + final URI uri = new URI(baseUrl); + final var oldPath = uri.getPath() != null ? uri.getPath().substring(1) : ""; + final var newPath = coreName + ReplicationHandler.PATH; + final var finalPath = oldPath.isEmpty() ? newPath : oldPath + "/" + newPath; + + final URI finalURI = + new URIBuilder(uri) + .setPath(finalPath) + .addParameter("command", action) + .addParameter("repository", repoName) + .addParameter("name", backupName) + .build(); + executeHttpRequest(finalURI); } - static void executeHttpRequest(String requestUrl) throws IOException { - URL url = URI.create(requestUrl).toURL(); + private static void executeHttpRequest(URI uri) throws IOException { + URL url = uri.toURL(); try (InputStream stream = url.openStream()) { assert stream != null; }