-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Luke Sikina
committed
Nov 28, 2024
1 parent
daf9d4a
commit 055b705
Showing
7 changed files
with
213 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
uploader/src/main/java/edu/harvard/dbmi/avillach/dataupload/hpds/HPDSConnectionVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
uploader/src/main/java/edu/harvard/dbmi/avillach/dataupload/hpds/UUIDGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...r/src/test/java/edu/harvard/dbmi/avillach/dataupload/hpds/HPDSConnectionVerifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |