Skip to content

Commit

Permalink
[ALS-7539] Test HPDS connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sikina committed Nov 28, 2024
1 parent daf9d4a commit 055b705
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.harvard.dbmi.avillach.dataupload.aws;

import edu.harvard.dbmi.avillach.dataupload.hpds.HPDSConnectionVerifier;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -24,16 +25,23 @@ public class S3StateVerifier {
private static final String testTempPrefix = "S3_DAEMON_INIT_TEST";
private static final Logger LOG = LoggerFactory.getLogger(S3StateVerifier.class);

@Autowired
private Map<String, SiteAWSInfo> sites;
private final Map<String, SiteAWSInfo> sites;
private final AWSClientBuilder clientBuilder;
private final HPDSConnectionVerifier hpdsVerifier;

@Autowired
private AWSClientBuilder clientBuilder;
public S3StateVerifier(Map<String, SiteAWSInfo> sites, AWSClientBuilder clientBuilder, HPDSConnectionVerifier hpdsVerifier) {
this.sites = sites;
this.clientBuilder = clientBuilder;
this.hpdsVerifier = hpdsVerifier;
}

@PostConstruct
private void verifyS3Status() {
sites.values().forEach(inst -> Thread.ofVirtual().start(() -> asyncVerify(inst)));

if (!hpdsVerifier.verifyConnection()) {
throw new RuntimeException("Not correctly connected to HPDS");
}
}

private void asyncVerify(SiteAWSInfo institution) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class HPDSClient {
@Autowired
private HttpClientContext context;

public boolean writeTestData(Query query) {
return writeData(query, "test_upload");
}

public boolean writePhenotypicData(Query query) {
return writeData(query, "phenotypic");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package edu.harvard.dbmi.avillach.dataupload.hpds;

import edu.harvard.dbmi.avillach.dataupload.hpds.hpdsartifactsdonotchange.Query;
import edu.harvard.dbmi.avillach.dataupload.hpds.hpdsartifactsdonotchange.ResultType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Path;

@Service
public class HPDSConnectionVerifier {

private static final Logger log = LoggerFactory.getLogger(HPDSConnectionVerifier.class);

private final HPDSClient client;
private final Path sharingRoot;
private final UUIDGenerator uuidGenerator;

@Autowired
public HPDSConnectionVerifier(HPDSClient client, Path sharingRoot, UUIDGenerator uuidGenerator) {
this.client = client;
this.sharingRoot = sharingRoot;
this.uuidGenerator = uuidGenerator;
}

public boolean verifyConnection() {
log.info("Verifying connection to hpds by asking it to create a file and then verifying that the file exists.");
Query testQuery = new Query();
testQuery.setPicSureId(uuidGenerator.generate().toString());
testQuery.setId(testQuery.getPicSureId());
testQuery.setExpectedResultType(ResultType.COUNT);
log.info("Created test query with UUID {}", testQuery.getPicSureId());

log.info("Sending test query to HPDS");
boolean hpdsResponse = client.writeTestData(testQuery);
if (!hpdsResponse) {
log.info("HPDS returned non 200 exit code. Assuming failure.");
return false;
}
log.info("HPDS reported successfully writing the data. Verifying that the file exists");
File testData = Path.of(sharingRoot.toString(), testQuery.getPicSureId(), "test_data.txt").toFile();

if (testData.exists() && testData.isFile()) {
log.info("File found! Connection to HPDS verified!");
return testData.delete();
}
log.info(
"File {} not found. HPDS is running, but the shared directory is probably misconfigured",
testData.getPath()
);
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package edu.harvard.dbmi.avillach.dataupload.hpds;

import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UUIDGenerator {

public UUID generate() {
return UUID.randomUUID();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,16 @@ public VariantInfoFilter(VariantInfoFilter filter) {
public Map<String, String[]> categoryVariantInfoFilters;
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
Query query = (Query) object;
return getExpectedResultType() == query.getExpectedResultType() && Objects.equals(getId(), query.getId()) && Objects.equals(getPicSureId(), query.getPicSureId());
}

@Override
public int hashCode() {
return Objects.hash(getExpectedResultType(), getId(), getPicSureId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,21 @@ void shouldNotWriteGenomicData() throws IOException, InterruptedException {

Assertions.assertFalse(actual);
}

@Test
void shouldWriteTestData() throws IOException {
Query query = new Query();
query.setPicSureId("my id");

Mockito.when(response.getStatusLine())
.thenReturn(line);
Mockito.when(line.getStatusCode())
.thenReturn(200);
Mockito.when(client.execute(Mockito.any(), Mockito.eq(context)))
.thenReturn(response);

boolean actual = subject.writeTestData(query);

Assertions.assertTrue(actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package edu.harvard.dbmi.avillach.dataupload.hpds;

import edu.harvard.dbmi.avillach.dataupload.hpds.hpdsartifactsdonotchange.Query;
import edu.harvard.dbmi.avillach.dataupload.hpds.hpdsartifactsdonotchange.ResultType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentMatcher;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class HPDSConnectionVerifierTest {

@MockBean
private HPDSClient client;

@MockBean
private UUIDGenerator generator;

private final Query query = new Query();
{
query.setPicSureId("9f1fc383-611b-4c6a-af37-a33c07feea5e");
query.setId("9f1fc383-611b-4c6a-af37-a33c07feea5e");
query.setExpectedResultType(ResultType.COUNT);
}

@Test
void shouldFailWhenHPDS400s(@TempDir Path sharingRoot) {
Mockito.when(client.writeTestData(query))
.thenReturn(false);
Mockito.when(generator.generate())
.thenReturn(UUID.fromString("9f1fc383-611b-4c6a-af37-a33c07feea5e"));
HPDSConnectionVerifier subject = new HPDSConnectionVerifier(client, sharingRoot, generator);

boolean result = subject.verifyConnection();

Assertions.assertFalse(result);
Mockito.verify(client, Mockito.times(1))
.writeTestData(query);
}

@Test
void shouldFailWhenHPDSDoesNotWrite(@TempDir Path sharingRoot) {
Mockito.when(client.writeTestData(query))
.thenReturn(true);
Mockito.when(generator.generate())
.thenReturn(UUID.fromString("9f1fc383-611b-4c6a-af37-a33c07feea5e"));
HPDSConnectionVerifier subject = new HPDSConnectionVerifier(client, sharingRoot, generator);

boolean result = subject.verifyConnection();

Assertions.assertFalse(result);
Mockito.verify(client, Mockito.times(1))
.writeTestData(query);
}

@Test
void shouldFailWhenHPDSMakesADirectory(@TempDir Path sharingRoot) throws IOException {
Files.createDirectory(Path.of(sharingRoot.toString(), "9f1fc383-611b-4c6a-af37-a33c07feea5e"));
Files.createDirectory(Path.of(sharingRoot.toString(), "9f1fc383-611b-4c6a-af37-a33c07feea5e", "test_data.txt"));

Mockito.when(client.writeTestData(query))
.thenReturn(true);
Mockito.when(generator.generate())
.thenReturn(UUID.fromString("9f1fc383-611b-4c6a-af37-a33c07feea5e"));
HPDSConnectionVerifier subject = new HPDSConnectionVerifier(client, sharingRoot, generator);

Assertions.assertFalse(subject.verifyConnection());
Mockito.verify(client, Mockito.times(1))
.writeTestData(query);
}

@Test
void shouldPass(@TempDir Path sharingRoot) throws IOException {
Files.createDirectory(Path.of(sharingRoot.toString(), "9f1fc383-611b-4c6a-af37-a33c07feea5e"));
Files.writeString(
Path.of(sharingRoot.toString(), "9f1fc383-611b-4c6a-af37-a33c07feea5e", "test_data.txt"),
"Howdy :)"
);

Mockito.when(client.writeTestData(query))
.thenReturn(true);
Mockito.when(generator.generate())
.thenReturn(UUID.fromString("9f1fc383-611b-4c6a-af37-a33c07feea5e"));
HPDSConnectionVerifier subject = new HPDSConnectionVerifier(client, sharingRoot, generator);

Assertions.assertTrue(subject.verifyConnection());
Mockito.verify(client, Mockito.times(1))
.writeTestData(query);
}
}

0 comments on commit 055b705

Please sign in to comment.