Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import dev.galasa.framework.IFileSystem;
import dev.galasa.framework.api.ras.internal.common.ArtifactsJson;
import dev.galasa.framework.api.ras.internal.common.ArtifactsProperties;
import dev.galasa.framework.api.ras.internal.common.IRunRootArtifact;
import dev.galasa.framework.api.ras.internal.common.RunLogArtifact;
import dev.galasa.framework.api.ras.internal.common.StructureJsonArtifact;
Expand Down Expand Up @@ -75,7 +74,6 @@ public RunArtifactsDownloadRoute(ResponseBuilder responseBuilder, IFileSystem fi

rootArtifacts.put("run.log", new RunLogArtifact());
rootArtifacts.put("structure.json", new StructureJsonArtifact());
rootArtifacts.put("artifacts.properties", new ArtifactsProperties(this));
rootArtifacts.put("artifacts.json", new ArtifactsJson(this));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import dev.galasa.framework.IFileSystem;
import dev.galasa.framework.api.ras.internal.common.ArtifactsJson;
import dev.galasa.framework.api.ras.internal.common.ArtifactsProperties;
import dev.galasa.framework.api.ras.internal.common.IRunRootArtifact;
import dev.galasa.framework.api.ras.internal.common.RunLogArtifact;
import dev.galasa.framework.api.ras.internal.common.StructureJsonArtifact;
Expand Down Expand Up @@ -58,7 +57,6 @@ public RunArtifactsListRoute(
rootArtifacts = Arrays.asList(
new RunLogArtifact(),
new StructureJsonArtifact(),
new ArtifactsProperties(this),
new ArtifactsJson(this)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ protected IFileSystem getFileSystem() {
* "contentType": "application/json",
* "size": 1301
* },
* {
* "path": "/artifacts.properties",
* "contentType": "text/plain",
* "size": 378
* }
* ]
* </code>
* </pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,21 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class TestRunArtifactsDownloadServlet extends RasServletTest {

private String generateExpectedArtifactsJson(String path, String contentType) {
JsonArray artifactsJson = new JsonArray();
JsonObject artifactInfo = new JsonObject();
artifactInfo.addProperty("path", path);
artifactInfo.addProperty("contentType", contentType);
artifactInfo.addProperty("size", 0); // MockFileSystem returns 0 for file.size()
artifactsJson.add(artifactInfo);
return gson.toJson(artifactsJson);
}

@Before
public void setUp() {
mockFileSystem = new MockFileSystem();
Expand Down Expand Up @@ -505,11 +518,11 @@ public void testDownloadArtifactWithUnknownContentTypeDefaultsToBinaryFile() thr
}

@Test
public void testGoodRunIdAndArtifactsPropertiesReturnsOKAndFile() throws Exception {
public void testGoodRunIdAndArtifactsJsonReturnsOKAndFile() throws Exception {
//Given..
String runId = "12345";
String runName = "testA";
String artifactPath = "/artifacts.properties";
String artifactPath = "/artifacts.json";
MockPath existingPath = new MockPath("/testA/term002.gz", mockFileSystem);
String fileContent = "dummy content";
List<IRunResult> mockInputRunResults = generateTestData(runId, runName, null);
Expand All @@ -532,17 +545,17 @@ public void testGoodRunIdAndArtifactsPropertiesReturnsOKAndFile() throws Excepti
// Then...
// Expecting:
assertThat(resp.getStatus()).isEqualTo(200);
assertThat(outStream.toString()).isEqualTo("/artifacts" + existingPath + "=" + "application/x-gzip");
assertThat(resp.getContentType()).isEqualTo("text/plain");
assertThat(outStream.toString()).isEqualTo(generateExpectedArtifactsJson("/artifacts" + existingPath, "application/x-gzip"));
assertThat(resp.getContentType()).isEqualTo("application/json");
assertThat(resp.getHeader("Content-Disposition")).isEqualTo("attachment");
}

@Test
public void testUnknownContentTypeAndArtifactsPropertiesDefaultsToBinaryType() throws Exception {
public void testUnknownContentTypeAndArtifactsJsonDefaultsToBinaryType() throws Exception {
//Given..
String runId = "12345";
String runName = "testA";
String artifactPath = "/artifacts.properties";
String artifactPath = "/artifacts.json";
MockPath existingPath = new MockPath("/testA/unknown.artifact", mockFileSystem);
String fileContent = "dummy content";
List<IRunResult> mockInputRunResults = generateTestData(runId, runName, null);
Expand All @@ -565,8 +578,8 @@ public void testUnknownContentTypeAndArtifactsPropertiesDefaultsToBinaryType() t
// Then...
// Expecting:
assertThat(resp.getStatus()).isEqualTo(200);
assertThat(outStream.toString()).isEqualTo("/artifacts" + existingPath.toString() + "=application/octet-stream");
assertThat(resp.getContentType()).isEqualTo("text/plain");
assertThat(outStream.toString()).isEqualTo(generateExpectedArtifactsJson("/artifacts" + existingPath.toString(), "application/octet-stream"));
assertThat(resp.getContentType()).isEqualTo("application/json");
assertThat(resp.getHeader("Content-Disposition")).isEqualTo("attachment");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ private Map<String, String> getArtifactFields(String path, String contentType, S
private void checkRootArtifactsJson(String jsonString) throws Exception {
checkJsonArrayStructure(jsonString, getArtifactFields("/run.log", "text/plain", null));
checkJsonArrayStructure(jsonString, getArtifactFields("/structure.json", "application/json", null));
checkJsonArrayStructure(jsonString, getArtifactFields("/artifacts.properties", "text/plain", null));
checkJsonArrayStructure(jsonString, getArtifactFields("/artifacts.json", "application/json", null));
}

Expand Down Expand Up @@ -348,11 +347,6 @@ public void testMultipleArtifactsToListReturnsOKWithArtifacts() throws Exception
// "size": 71
// },
// {
// "path": "/artifacts.properties",
// "contentType": "text/plain",
// "size": 240
// }
// {
// "path": "/artifacts.json",
// "contentType": "application/json",
// "size": 313
Expand All @@ -365,7 +359,7 @@ public void testMultipleArtifactsToListReturnsOKWithArtifacts() throws Exception
assertThat(jsonElement).isNotNull().as("Failed to parse the body to a json object.");

JsonArray jsonArray = jsonElement.getAsJsonArray();
assertThat(jsonArray.size()).isEqualTo(7);
assertThat(jsonArray.size()).isEqualTo(6);

List<Path> expectedArtifactPaths = dummyArtifactPaths.stream()
.map(path -> new MockPath("/artifacts" + path.toString(), mockFileSystem))
Expand Down Expand Up @@ -431,11 +425,6 @@ public void testOneArtifactToListReturnsOKWithArtifact() throws Exception {
// "size": "82",
// },
// {
// "path": "/artifacts.properties",
// "contentType": "text/plain",
// "size": "240",
// }
// {
// "path": "/artifacts.json",
// "contentType": "application/json",
// "size": "240",
Expand All @@ -448,7 +437,7 @@ public void testOneArtifactToListReturnsOKWithArtifact() throws Exception {
assertThat(jsonElement).isNotNull().as("Failed to parse the body to a json object.");

JsonArray jsonArray = jsonElement.getAsJsonArray();
assertThat(jsonArray.size()).isEqualTo(5);
assertThat(jsonArray.size()).isEqualTo(4);

MockPath expectedArtifactPath = new MockPath("/artifacts" + dummyArtifactPath.toString(), mockFileSystem);
MockJsonObject expectedArtifact = new MockJsonObject(expectedArtifactPath, mockArtifact.getContentType(), mockArtifact.getSize());
Expand Down Expand Up @@ -499,11 +488,6 @@ public void testNoArtifactsToListGivesRootArtifacts() throws Exception {
// "size": 82,
// },
// {
// "path": "/artifacts.properties",
// "contentType": "text/plain",
// "size": 240,
// }
// {
// "path": "/artifacts.json",
// "contentType": "application/json",
// "size": 240,
Expand All @@ -516,7 +500,7 @@ public void testNoArtifactsToListGivesRootArtifacts() throws Exception {
assertThat(jsonElement).isNotNull().as("Failed to parse the body to a json object.");

JsonArray jsonArray = jsonElement.getAsJsonArray();
assertThat(jsonArray.size()).isEqualTo(4);
assertThat(jsonArray.size()).isEqualTo(3);

checkRootArtifactsJson(jsonString);
String expectedJson = generateExpectedJsonArtifacts(new ArrayList<>());
Expand Down Expand Up @@ -591,7 +575,7 @@ public void testNoArtifactsToListWithAcceptHeaderGivesRootArtifacts() throws Exc
assertThat(jsonElement).isNotNull().as("Failed to parse the body to a json object.");

JsonArray jsonArray = jsonElement.getAsJsonArray();
assertThat(jsonArray.size()).isEqualTo(4);
assertThat(jsonArray.size()).isEqualTo(3);

checkRootArtifactsJson(jsonString);
String expectedJson = generateExpectedJsonArtifacts(new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
package dev.galasa.framework.internal.ras.directory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.AccessDeniedException;
import java.nio.file.AccessMode;
import java.nio.file.DirectoryStream;
Expand All @@ -30,12 +30,13 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import dev.galasa.framework.spi.ras.ArtifactMetadata;
import dev.galasa.framework.spi.ras.ResultArchiveStoreFileSystem;
import dev.galasa.framework.spi.ras.ResultArchiveStoreFileSystemProvider;
import dev.galasa.framework.spi.ras.ResultArchiveStorePath;
import dev.galasa.framework.spi.utils.GalasaGson;
import dev.galasa.ResultArchiveStoreContentType;
import dev.galasa.ResultArchiveStoreFileAttributeView;
import dev.galasa.SetContentType;
Expand All @@ -51,9 +52,11 @@

private static final String RAS_CONTENT_TYPE = "ras:contentType";

private final Path artifactDirectory;
private final Path artifactPropertesFile;
private final Properties contentTypeProperties = new Properties();
private final Path artifactDirectory;
private final Path artifactJsonFile;
private final Map<String, ArtifactMetadata> artifacts = new HashMap<>();

private final GalasaGson gson = new GalasaGson();

/**
* Create the Directory RAS provider for stored artifacts
Expand All @@ -65,17 +68,20 @@
super(runDirectory.getFileSystem().provider().getFileStore(runDirectory));

this.artifactDirectory = runDirectory.resolve("artifacts");
this.artifactPropertesFile = runDirectory.resolve("artifacts.properties");

// *** Load the content type properties file, contains the content type of all
// the files if set
if (Files.exists(this.artifactPropertesFile)) {
try (InputStream is = Files.newInputStream(this.artifactPropertesFile)) {
this.contentTypeProperties.load(is);
} catch (final Exception e) {
throw new IOException("Unable to read the artifacts contenttypes", e);
this.artifactJsonFile = runDirectory.resolve("artifacts.json");

if (!this.artifactJsonFile.toAbsolutePath().startsWith(runDirectory)) {
throw new IllegalArgumentException("Invalid filename");
}

if (Files.exists(this.artifactJsonFile)) {
try (InputStreamReader reader = new InputStreamReader(Files.newInputStream(this.artifactJsonFile), StandardCharsets.UTF_8)) {
ArtifactMetadata[] artifactMetadataList = gson.fromJson(reader, ArtifactMetadata[].class);
for (ArtifactMetadata artifactMetadata : artifactMetadataList) {

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
artifacts.put(artifactMetadata.getPath(), artifactMetadata);
}
}
}

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
}

/**
Expand All @@ -83,21 +89,22 @@
*
* @param path - the stored artifact to set the content type for
* @param contentType - the content type
* @throws IOException - if unable to save the properties file
* @throws IOException - if unable to save the artifacts.json file
*/
private void setContentType(Path path, ResultArchiveStoreContentType contentType) throws IOException {
if (!path.isAbsolute()) {
path = path.toAbsolutePath();
}

this.contentTypeProperties.setProperty(path.toString(), contentType.value());

// *** Make hardcopy
try (OutputStream os = Files.newOutputStream(this.artifactPropertesFile)) {
this.contentTypeProperties.store(os, null);
} catch (final Exception e) {
throw new IOException("Unable to write the artifacts contenttypes", e);
String pathString = path.toString();
ArtifactMetadata metadata = artifacts.get(pathString);
if (metadata == null) {
metadata = new ArtifactMetadata(pathString, contentType.value());
artifacts.put(pathString, metadata);
} else {
metadata.setContentType(contentType.value());
}
Files.writeString(this.artifactJsonFile, gson.toJson(new ArrayList<>(artifacts.values())), StandardCharsets.UTF_8);
}

/**
Expand All @@ -111,11 +118,11 @@
path = path.toAbsolutePath();
}
ResultArchiveStoreContentType contentType = null;
final String sContentType = this.contentTypeProperties.getProperty(path.toString());
if (sContentType == null) {
ArtifactMetadata metadata = artifacts.get(path.toString());
if (metadata == null || metadata.getContentType() == null) {
contentType = ResultArchiveStoreContentType.TEXT;
} else {
contentType = new ResultArchiveStoreContentType(sContentType);
contentType = new ResultArchiveStoreContentType(metadata.getContentType());
}
return contentType;
}
Expand Down
Loading
Loading