From cdbb1db153a0e51e755e9bbaf3a9906190271eab Mon Sep 17 00:00:00 2001 From: Eamonn Mansour <47121388+eamansour@users.noreply.github.com> Date: Wed, 11 Sep 2024 11:42:57 +0100 Subject: [PATCH] Separate loading run artifacts into IRunResult method, fetchRun now only gets run details Signed-off-by: Eamonn Mansour <47121388+eamansour@users.noreply.github.com> --- .../internal/CouchdbDirectoryService.java | 47 +++++----------- .../couchdb/internal/CouchdbRunResult.java | 19 +++++-- .../internal/CouchdbDirectoryServiceTest.java | 54 ++++--------------- 3 files changed, 38 insertions(+), 82 deletions(-) diff --git a/galasa-extensions-parent/dev.galasa.ras.couchdb/src/main/java/dev/galasa/ras/couchdb/internal/CouchdbDirectoryService.java b/galasa-extensions-parent/dev.galasa.ras.couchdb/src/main/java/dev/galasa/ras/couchdb/internal/CouchdbDirectoryService.java index 1f58b165..e1127731 100644 --- a/galasa-extensions-parent/dev.galasa.ras.couchdb/src/main/java/dev/galasa/ras/couchdb/internal/CouchdbDirectoryService.java +++ b/galasa-extensions-parent/dev.galasa.ras.couchdb/src/main/java/dev/galasa/ras/couchdb/internal/CouchdbDirectoryService.java @@ -94,7 +94,7 @@ private CouchdbRasFileSystemProvider createFileSystemProvider() { return new CouchdbRasFileSystemProvider(fileStore, store, logFactory); } - private Path getRunArtifactPath(TestStructureCouchdb ts) throws CouchdbRasException { + public Path getRunArtifactPath(TestStructureCouchdb ts) throws CouchdbRasException { CouchdbRasFileSystemProvider runProvider = createFileSystemProvider(); if (ts.getArtifactRecordIds() == null || ts.getArtifactRecordIds().isEmpty()) { return runProvider.getRoot(); @@ -184,17 +184,7 @@ private Path getRunArtifactPath(TestStructureCouchdb ts) throws CouchdbRasExcept } private CouchdbRunResult fetchRun(String id) throws ParseException, IOException, ResultArchiveStoreException { - CouchdbRunResult runResult = fetchRunWithoutArtifacts(id); - TestStructureCouchdb testStructure = (TestStructureCouchdb) runResult.getTestStructure(); - - // Populate the run's artifacts filesystem - Path runArtifactPath = getRunArtifactPath(testStructure); - - runResult = new CouchdbRunResult(store, testStructure, runArtifactPath); - return runResult; - } - - private CouchdbRunResult fetchRunWithoutArtifacts(String id) throws ParseException, IOException, CouchdbRasException { + CouchdbRunResult runResult = null; HttpGet httpGet = requestFactory.getHttpGetRequest(store.getCouchdbUri() + "/" + CouchdbRasStore.RUNS_DB + "/" + id); try (CloseableHttpResponse response = store.getHttpClient().execute(httpGet)) { @@ -207,10 +197,9 @@ private CouchdbRunResult fetchRunWithoutArtifacts(String id) throws ParseExcepti String responseEntity = EntityUtils.toString(entity); TestStructureCouchdb ts = store.getGson().fromJson(responseEntity, TestStructureCouchdb.class); - // *** Add this run to the results - CouchdbRunResult cdbrr = new CouchdbRunResult(store, ts, createFileSystemProvider().getRoot()); - return cdbrr; + runResult = new CouchdbRunResult(store, ts, logFactory); } + return runResult; } @Override @@ -375,11 +364,10 @@ private RasRunResultPage getRunsPageFromCouchdb(HttpPost httpPost, Find query) t for (TestStructureCouchdb ts : found.docs) { if (ts.isValid()) { - // Don't load the artifacts for the found runs, just set a root location for artifacts - CouchdbRasFileSystemProvider runProvider = createFileSystemProvider(); - // Add this run to the results - runs.add(new CouchdbRunResult(store, ts, runProvider.getRoot())); + // Don't load the artifacts for the found runs, just set a root location for artifacts + // and add this run to the results + runs.add(new CouchdbRunResult(store, ts, logFactory)); } } @@ -442,21 +430,14 @@ private JsonArray buildQuerySortJson(@NotNull RasSortField primarySort) { return runs; } - public void discardRun(String id) throws ResultArchiveStoreException { + public void discardRun(@NotNull TestStructureCouchdb runTestStructure) throws ResultArchiveStoreException { try { - CouchdbRunResult run = fetchRunWithoutArtifacts(id); - if (run == null) { - logger.info("Run with ID " + id + " does not exist or has already been discarded"); - } else { - TestStructureCouchdb testStructure = (TestStructureCouchdb) run.getTestStructure(); - - discardRunLogs(testStructure.getLogRecordIds()); - discardRunArtifacts(testStructure.getArtifactRecordIds()); - - discardRecord(CouchdbRasStore.RUNS_DB, id, testStructure._rev); - } - } catch (CouchdbRasException | ParseException | IOException e) { - throw new ResultArchiveStoreException("Failed to discard run: " + id, e); + discardRunLogs(runTestStructure.getLogRecordIds()); + discardRunArtifacts(runTestStructure.getArtifactRecordIds()); + + discardRecord(CouchdbRasStore.RUNS_DB, runTestStructure._id, runTestStructure._rev); + } catch (CouchdbRasException | ParseException e) { + throw new ResultArchiveStoreException("Failed to discard run: " + runTestStructure._id, e); } } diff --git a/galasa-extensions-parent/dev.galasa.ras.couchdb/src/main/java/dev/galasa/ras/couchdb/internal/CouchdbRunResult.java b/galasa-extensions-parent/dev.galasa.ras.couchdb/src/main/java/dev/galasa/ras/couchdb/internal/CouchdbRunResult.java index 0e868549..d503bdfb 100644 --- a/galasa-extensions-parent/dev.galasa.ras.couchdb/src/main/java/dev/galasa/ras/couchdb/internal/CouchdbRunResult.java +++ b/galasa-extensions-parent/dev.galasa.ras.couchdb/src/main/java/dev/galasa/ras/couchdb/internal/CouchdbRunResult.java @@ -7,25 +7,30 @@ import java.nio.file.Path; +import dev.galasa.extensions.common.api.LogFactory; import dev.galasa.framework.spi.IRunResult; import dev.galasa.framework.spi.ResultArchiveStoreException; +import dev.galasa.framework.spi.ras.ResultArchiveStoreFileStore; import dev.galasa.framework.spi.teststructure.TestStructure; import dev.galasa.ras.couchdb.internal.pojos.TestStructureCouchdb; public class CouchdbRunResult implements IRunResult { private final TestStructureCouchdb testStructure; - private final Path path; private final CouchdbRasStore store; + private Path path; - public CouchdbRunResult(CouchdbRasStore store, TestStructureCouchdb testStructure, Path path) { + public CouchdbRunResult(CouchdbRasStore store, TestStructureCouchdb testStructure, LogFactory logFactory) { this.store = store; if (testStructure == null) { this.testStructure = new TestStructureCouchdb(); } else { this.testStructure = testStructure; } - this.path = path; + + // Create an empty artifact filesystem and set the artifacts path to the root of this filesystem + ResultArchiveStoreFileStore fileStore = new ResultArchiveStoreFileStore(); + this.path = new CouchdbRasFileSystemProvider(fileStore, store, logFactory).getRoot(); } @Override @@ -46,7 +51,7 @@ public String getLog() throws ResultArchiveStoreException { @Override public void discard() throws ResultArchiveStoreException { CouchdbDirectoryService storeService = (CouchdbDirectoryService) store.getDirectoryServices().get(0); - storeService.discardRun(this.testStructure._id); + storeService.discardRun(this.testStructure); } @Override @@ -54,4 +59,10 @@ public String getRunId() { return "cdb-" + this.testStructure._id; } + @Override + public void loadArtifacts() throws ResultArchiveStoreException { + CouchdbDirectoryService storeService = (CouchdbDirectoryService) store.getDirectoryServices().get(0); + this.path = storeService.getRunArtifactPath(this.testStructure); + } + } diff --git a/galasa-extensions-parent/dev.galasa.ras.couchdb/src/test/java/dev/galasa/ras/couchdb/internal/CouchdbDirectoryServiceTest.java b/galasa-extensions-parent/dev.galasa.ras.couchdb/src/test/java/dev/galasa/ras/couchdb/internal/CouchdbDirectoryServiceTest.java index 8cee62bb..1840cf84 100644 --- a/galasa-extensions-parent/dev.galasa.ras.couchdb/src/test/java/dev/galasa/ras/couchdb/internal/CouchdbDirectoryServiceTest.java +++ b/galasa-extensions-parent/dev.galasa.ras.couchdb/src/test/java/dev/galasa/ras/couchdb/internal/CouchdbDirectoryServiceTest.java @@ -562,7 +562,7 @@ public void testGetRunsPageWithNilBookmarkReturnsPageWithNoNextCursor() throws E public void testDiscardRunDeletesRunOk() throws Exception { // Given... String runId = "ABC123"; - TestStructureCouchdb mockRun1 = createRunTestStructure("run1"); + TestStructureCouchdb mockRun1 = createRunTestStructure(runId); IdRev mockIdRev = new IdRev(); String revision = "this-is-a-revision"; @@ -584,10 +584,7 @@ public void testDiscardRunDeletesRunOk() throws Exception { String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId; String artifactsDbUri = baseUri + "/" + CouchdbRasStore.ARTIFACTS_DB; String logsDbUri = baseUri + "/" + CouchdbRasStore.LOG_DB; - List interactions = List.of( - // Fetch the run to be deleted - new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_OK, mockRun1), - + List interactions = List.of( // Start discarding the run's log records new GetDocumentByIdFromCouchdbInteraction(logsDbUri + "/" + logId1, HttpStatus.SC_OK, mockIdRev), new DeleteDocumentFromCouchdbInteraction(logsDbUri + "/" + logId1 + "?rev=" + revision, HttpStatus.SC_OK), @@ -609,7 +606,7 @@ public void testDiscardRunDeletesRunOk() throws Exception { CouchdbDirectoryService directoryService = new CouchdbDirectoryService(mockRasStore, mockLogFactory, new HttpRequestFactoryImpl()); // When... - directoryService.discardRun(runId); + directoryService.discardRun(mockRun1); // Then... // The assertions in the interactions should not have failed @@ -619,7 +616,7 @@ public void testDiscardRunDeletesRunOk() throws Exception { public void testDiscardRunWithNoArtifactsDeletesRunOk() throws Exception { // Given... String runId = "ABC123"; - TestStructureCouchdb mockRun1 = createRunTestStructure("run1"); + TestStructureCouchdb mockRun1 = createRunTestStructure(runId); IdRev mockIdRev = new IdRev(); String revision = "this-is-a-revision"; @@ -636,9 +633,6 @@ public void testDiscardRunWithNoArtifactsDeletesRunOk() throws Exception { String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId; String logsDbUri = baseUri + "/" + CouchdbRasStore.LOG_DB; List interactions = List.of( - // Fetch the run to be deleted - new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_OK, mockRun1), - // Start discarding the run's log records new GetDocumentByIdFromCouchdbInteraction(logsDbUri + "/" + logId1, HttpStatus.SC_OK, mockIdRev), new DeleteDocumentFromCouchdbInteraction(logsDbUri + "/" + logId1 + "?rev=" + revision, HttpStatus.SC_OK), @@ -654,7 +648,7 @@ public void testDiscardRunWithNoArtifactsDeletesRunOk() throws Exception { CouchdbDirectoryService directoryService = new CouchdbDirectoryService(mockRasStore, mockLogFactory, new HttpRequestFactoryImpl()); // When... - directoryService.discardRun(runId); + directoryService.discardRun(mockRun1); // Then... // The assertions in the interactions should not have failed @@ -664,14 +658,11 @@ public void testDiscardRunWithNoArtifactsDeletesRunOk() throws Exception { public void testDiscardRunWithNoArtifactsAndLogsDeletesRunOk() throws Exception { // Given... String runId = "ABC123"; - TestStructureCouchdb mockRun1 = createRunTestStructure("run1"); + TestStructureCouchdb mockRun1 = createRunTestStructure(runId); String baseUri = "http://my.uri"; String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId; List interactions = List.of( - // Fetch the run to be deleted - new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_OK, mockRun1), - // Delete the record of the run new DeleteDocumentFromCouchdbInteraction(runDbUri + "?rev=" + mockRun1._rev, HttpStatus.SC_OK) ); @@ -681,7 +672,7 @@ public void testDiscardRunWithNoArtifactsAndLogsDeletesRunOk() throws Exception CouchdbDirectoryService directoryService = new CouchdbDirectoryService(mockRasStore, mockLogFactory, new HttpRequestFactoryImpl()); // When... - directoryService.discardRun(runId); + directoryService.discardRun(mockRun1); // Then... // The assertions in the interactions should not have failed @@ -691,14 +682,11 @@ public void testDiscardRunWithNoArtifactsAndLogsDeletesRunOk() throws Exception public void testDiscardRunWithCouchdbServerErrorThrowsCorrectError() throws Exception { // Given... String runId = "ABC123"; - TestStructureCouchdb mockRun1 = createRunTestStructure("run1"); + TestStructureCouchdb mockRun1 = createRunTestStructure(runId); String baseUri = "http://my.uri"; String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId; List interactions = List.of( - // Fetch the run to be deleted - new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_OK, mockRun1), - // Delete the record of the run new DeleteDocumentFromCouchdbInteraction(runDbUri + "?rev=" + mockRun1._rev, HttpStatus.SC_INTERNAL_SERVER_ERROR) ); @@ -709,7 +697,7 @@ public void testDiscardRunWithCouchdbServerErrorThrowsCorrectError() throws Exce // When... ResultArchiveStoreException thrown = catchThrowableOfType(() -> { - directoryService.discardRun(runId); + directoryService.discardRun(mockRun1); }, ResultArchiveStoreException.class); // Then... @@ -717,28 +705,4 @@ public void testDiscardRunWithCouchdbServerErrorThrowsCorrectError() throws Exce assertThat(thrown).isNotNull(); assertThat(thrown.getMessage()).contains("Unable to delete run", runId); } - - @Test - public void testDiscardRunWithNonExistantRunDoesNotThrowError() throws Exception { - // Given... - String runId = "ABC123"; - TestStructureCouchdb mockRun1 = createRunTestStructure("run1"); - - String baseUri = "http://my.uri"; - String runDbUri = baseUri + "/" + CouchdbRasStore.RUNS_DB + "/" + runId; - List interactions = List.of( - new GetRunByIdFromCouchdbInteraction(runDbUri, HttpStatus.SC_INTERNAL_SERVER_ERROR, mockRun1) - ); - - MockLogFactory mockLogFactory = new MockLogFactory(); - CouchdbRasStore mockRasStore = fixtures.createCouchdbRasStore(interactions, mockLogFactory); - CouchdbDirectoryService directoryService = new CouchdbDirectoryService(mockRasStore, mockLogFactory, new HttpRequestFactoryImpl()); - - // When... - directoryService.discardRun(runId); - - // Then... - // The assertions in the interactions should not have failed - assertThat(mockLogFactory.toString()).contains(runId, "does not exist or has already been discarded"); - } }