From bdc2f933231278903444f6b4aad40a4ed811de47 Mon Sep 17 00:00:00 2001 From: cezary-witkowski Date: Mon, 25 Nov 2024 15:11:38 +0100 Subject: [PATCH] [ACS-9036] Improved licence upload error handling --- .../elasticsearch/upgrade/BaseACSEnv.java | 10 +++++++-- .../upgrade/LegacyAcsUpgradeScenario.java | 11 +++++----- .../elasticsearch/upgrade/RepoHttpClient.java | 21 ++++++++++++------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/BaseACSEnv.java b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/BaseACSEnv.java index 191d10f627..c873733ced 100644 --- a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/BaseACSEnv.java +++ b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/BaseACSEnv.java @@ -4,6 +4,7 @@ import static org.alfresco.elasticsearch.upgrade.Utils.waitFor; +import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URL; @@ -140,11 +141,16 @@ public void setElasticsearchSearchService() throws IOException repoHttpClient.setSearchService("elasticsearch"); } - public boolean uploadLicence(String licencePath) + public void tryToUploadLicence(String licencePath) { try { - return repoHttpClient.uploadLicense(licencePath); + File licence = new File(licencePath); + if (!licence.isFile() || !licence.canRead()) + { + throw new RuntimeException("Licence file at: %s is not a file or is not readable".formatted(licencePath)); + } + repoHttpClient.uploadLicense(licence); } catch (IOException e) { diff --git a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/LegacyAcsUpgradeScenario.java b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/LegacyAcsUpgradeScenario.java index cbad34feac..71e5f8f13e 100644 --- a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/LegacyAcsUpgradeScenario.java +++ b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/LegacyAcsUpgradeScenario.java @@ -4,6 +4,7 @@ import static org.alfresco.elasticsearch.upgrade.Utils.createTempContentStoreDirectory; import java.nio.file.Path; +import java.util.Optional; import org.testcontainers.containers.Network; @@ -72,15 +73,13 @@ public ACSEnv upgradeLegacyEnvironmentToCurrent() private void uploadLicence(ACSEnv env) { - final String licencePath = getTargetAcsLicencePath(); - if (!env.uploadLicence(licencePath)) - { - throw new RuntimeException("Failed to upload licence from `" + licencePath + "`."); - } + String licencePath = getTargetAcsLicencePath(); + env.tryToUploadLicence(licencePath); } private String getTargetAcsLicencePath() { - return System.getenv("ALF_LICENCE_LOCAL_PATH"); + return Optional.ofNullable(System.getenv("ALF_LICENCE_LOCAL_PATH")) + .orElseThrow(() -> new IllegalStateException("ALF_LICENCE_LOCAL_PATH environment variable is not set")); } } diff --git a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/RepoHttpClient.java b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/RepoHttpClient.java index 66d2568ea7..cd335fb553 100644 --- a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/RepoHttpClient.java +++ b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/upgrade/RepoHttpClient.java @@ -109,7 +109,7 @@ public void setSearchService(String implementation) throws IOException } } - public boolean uploadLicense(String licensePath) throws IOException + public void uploadLicense(File license) throws IOException { final HttpGet getCsrfToken = authenticate(new HttpGet(uploadLicenseAdminApiUri)); final HttpClientContext httpCtx = HttpClientContext.create(); @@ -128,9 +128,6 @@ public boolean uploadLicense(String licensePath) throws IOException csrfCookie = Objects.requireNonNull(cookies.get("alf-csrftoken")); } - - File license = new File(licensePath); - final URI uploadLicenseAdminCsrfApiUri = URI.create(new URIBuilder(uploadLicenseAdminApiUri) .addParameter(csrfCookie.getName(), URLDecoder.decode(csrfCookie.getValue(), StandardCharsets.US_ASCII)) .toString()); @@ -144,12 +141,11 @@ public boolean uploadLicense(String licensePath) throws IOException final HttpPost uploadRequest = authenticate(new HttpPost(uploadLicenseAdminCsrfApiUri)); uploadRequest.setEntity(uploadEntity); - try (CloseableHttpResponse response = client.execute(uploadRequest, httpCtx)) + var responseMap = executeAndGetResponseMap(uploadRequest, httpCtx); + if (!Boolean.TRUE.equals(responseMap.get("success"))) { - String responseBody = EntityUtils.toString(response.getEntity()); - return gson.fromJson(responseBody, Map.class).get("success").equals(true); + throw new IOException("Failed to upload a licence. Server response error: " + responseMap.get("error")); } - } public boolean isServerUp() throws IOException @@ -217,6 +213,15 @@ public Optional> searchForFiles(String term) throws IOException return Optional.of(names); } + private Map executeAndGetResponseMap(HttpPost httpPost, HttpClientContext httpCtx) throws IOException + { + try (CloseableHttpResponse response = client.execute(httpPost, httpCtx)) + { + String responseBody = EntityUtils.toString(response.getEntity()); + return gson.fromJson(responseBody, Map.class); + } + } + private T authenticate(T msg) { msg.setHeader("Authorization", "Basic YWRtaW46YWRtaW4=");