generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FilePartnerMetadataStorage implementation (#704)
* Added initial implementation for localfile saving + added custom exception * Added missing excpetion handling in SendOrderUseCase * Updated Jackson formatter to handle Instant * Added implementation for readMetadata + exception handling * Added test coverage * Added JavaDoc for PartnerMetadataException * 672: Test PartnerMetadataException and PartnerMetadata * Fixed sonarcloud security risk --------- Co-authored-by: halprin <[email protected]>
- Loading branch information
1 parent
8bf21a8
commit 17ae3f7
Showing
9 changed files
with
200 additions
and
6 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...src/main/java/gov/hhs/cdc/trustedintermediary/etor/metadata/PartnerMetadataException.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,9 @@ | ||
package gov.hhs.cdc.trustedintermediary.etor.metadata; | ||
|
||
/** Custom exception class use to catch partner metadata exceptions */ | ||
public class PartnerMetadataException extends Exception { | ||
|
||
public PartnerMetadataException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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
53 changes: 50 additions & 3 deletions
53
...n/java/gov/hhs/cdc/trustedintermediary/external/localfile/FilePartnerMetadataStorage.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 |
---|---|---|
@@ -1,24 +1,71 @@ | ||
package gov.hhs.cdc.trustedintermediary.external.localfile; | ||
|
||
import gov.hhs.cdc.trustedintermediary.etor.metadata.PartnerMetadata; | ||
import gov.hhs.cdc.trustedintermediary.etor.metadata.PartnerMetadataException; | ||
import gov.hhs.cdc.trustedintermediary.etor.metadata.PartnerMetadataStorage; | ||
import gov.hhs.cdc.trustedintermediary.wrappers.Logger; | ||
import gov.hhs.cdc.trustedintermediary.wrappers.formatter.Formatter; | ||
import gov.hhs.cdc.trustedintermediary.wrappers.formatter.FormatterProcessingException; | ||
import gov.hhs.cdc.trustedintermediary.wrappers.formatter.TypeReference; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.FileAttribute; | ||
import java.nio.file.attribute.PosixFilePermissions; | ||
import javax.inject.Inject; | ||
|
||
/** Implements the {@link PartnerMetadataStorage} using local files. */ | ||
public class FilePartnerMetadataStorage implements PartnerMetadataStorage { | ||
|
||
private static final FilePartnerMetadataStorage INSTANCE = new FilePartnerMetadataStorage(); | ||
|
||
@Inject Formatter formatter; | ||
@Inject Logger logger; | ||
|
||
private static final Path metadataTempDirectory; | ||
|
||
static { | ||
try { | ||
FileAttribute<?> onlyOwnerAttrs = | ||
PosixFilePermissions.asFileAttribute( | ||
PosixFilePermissions.fromString("rwx------")); | ||
metadataTempDirectory = Files.createTempDirectory("metadata", onlyOwnerAttrs); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private FilePartnerMetadataStorage() {} | ||
|
||
public static FilePartnerMetadataStorage getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public PartnerMetadata readMetadata(final String uniqueId) { | ||
return null; | ||
public PartnerMetadata readMetadata(final String uniqueId) throws PartnerMetadataException { | ||
Path filePath = getFilePath(uniqueId); | ||
try { | ||
String content = Files.readString(filePath); | ||
return formatter.convertJsonToObject(content, new TypeReference<>() {}); | ||
} catch (IOException | FormatterProcessingException e) { | ||
throw new PartnerMetadataException("Unable to read the metadata file", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void saveMetadata(final PartnerMetadata metadata) {} | ||
public void saveMetadata(final PartnerMetadata metadata) throws PartnerMetadataException { | ||
Path metadataFilePath = getFilePath(metadata.uniqueId()); | ||
try { | ||
String content = formatter.convertToJsonString(metadata); | ||
Files.writeString(metadataFilePath, content); | ||
logger.logInfo("Saved metadata for " + metadata.uniqueId() + " to " + metadataFilePath); | ||
} catch (IOException | FormatterProcessingException e) { | ||
throw new PartnerMetadataException( | ||
"Error saving metadata for " + metadata.uniqueId(), e); | ||
} | ||
} | ||
|
||
private Path getFilePath(String uniqueId) { | ||
return metadataTempDirectory.resolve(uniqueId + ".json"); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
.../groovy/gov/hhs/cdc/trustedintermediary/etor/metadata/PartnerMetadataExceptionTest.groovy
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,20 @@ | ||
package gov.hhs.cdc.trustedintermediary.etor.metadata | ||
|
||
|
||
import spock.lang.Specification | ||
|
||
class PartnerMetadataExceptionTest extends Specification { | ||
def "constructor works"() { | ||
|
||
given: | ||
def message = "something blew up!" | ||
def cause = new NullPointerException() | ||
|
||
when: | ||
def exception = new PartnerMetadataException(message, cause) | ||
|
||
then: | ||
exception.getMessage() == message | ||
exception.getCause() == cause | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
.../src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/metadata/PartnerMetadataTest.groovy
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,35 @@ | ||
package gov.hhs.cdc.trustedintermediary.etor.metadata | ||
|
||
|
||
import gov.hhs.cdc.trustedintermediary.PojoTestUtils | ||
import java.time.Instant | ||
import spock.lang.Specification | ||
|
||
class PartnerMetadataTest extends Specification { | ||
def "test getters and setters"() { | ||
when: | ||
PojoTestUtils.validateGettersAndSetters(PartnerMetadata) | ||
|
||
then: | ||
noExceptionThrown() | ||
} | ||
|
||
def "test constructor"() { | ||
given: | ||
def uniqueId = "uniqueId" | ||
def sender = "sender" | ||
def receiver = "receiver" | ||
def timeReceived = Instant.now() | ||
def hash = "abcd" | ||
|
||
when: | ||
def metadata = new PartnerMetadata(uniqueId, sender, receiver, timeReceived, hash) | ||
|
||
then: | ||
metadata.uniqueId() == uniqueId | ||
metadata.sender() == sender | ||
metadata.receiver() == receiver | ||
metadata.timeReceived() == timeReceived | ||
metadata.hash() == hash | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
.../gov/hhs/cdc/trustedintermediary/external/localfile/FilePartnerMetadataStorageTest.groovy
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,67 @@ | ||
package gov.hhs.cdc.trustedintermediary.external.localfile | ||
|
||
import gov.hhs.cdc.trustedintermediary.context.TestApplicationContext | ||
import gov.hhs.cdc.trustedintermediary.etor.metadata.PartnerMetadata | ||
import gov.hhs.cdc.trustedintermediary.etor.metadata.PartnerMetadataException | ||
import gov.hhs.cdc.trustedintermediary.external.jackson.Jackson | ||
import gov.hhs.cdc.trustedintermediary.wrappers.formatter.Formatter | ||
import gov.hhs.cdc.trustedintermediary.wrappers.formatter.FormatterProcessingException | ||
import gov.hhs.cdc.trustedintermediary.wrappers.formatter.TypeReference | ||
import spock.lang.Specification | ||
|
||
import java.time.Instant | ||
|
||
class FilePartnerMetadataStorageTest extends Specification { | ||
|
||
def setup() { | ||
TestApplicationContext.reset() | ||
TestApplicationContext.init() | ||
TestApplicationContext.register(FilePartnerMetadataStorage, FilePartnerMetadataStorage.getInstance()) | ||
} | ||
|
||
def "save and read metadata successfully"() { | ||
given: | ||
def expectedUniqueId = "uniqueId" | ||
PartnerMetadata metadata = new PartnerMetadata(expectedUniqueId, "sender", "receiver", Instant.parse("2023-12-04T18:51:48.941875Z"), "abcd") | ||
|
||
TestApplicationContext.register(Formatter, Jackson.getInstance()) | ||
TestApplicationContext.injectRegisteredImplementations() | ||
|
||
when: | ||
FilePartnerMetadataStorage.getInstance().saveMetadata(metadata) | ||
def actualMetadata = FilePartnerMetadataStorage.getInstance().readMetadata(expectedUniqueId) | ||
|
||
then: | ||
actualMetadata == metadata | ||
} | ||
|
||
def "saveMetadata throws PartnerMetadataException when unable to save file"() { | ||
given: | ||
PartnerMetadata metadata = new PartnerMetadata("uniqueId", "sender", "receiver", Instant.now(), "abcd") | ||
|
||
def mockFormatter = Mock(Formatter) | ||
mockFormatter.convertToJsonString(_ as PartnerMetadata) >> {throw new FormatterProcessingException("error", new Exception())} | ||
TestApplicationContext.register(Formatter, mockFormatter) | ||
TestApplicationContext.injectRegisteredImplementations() | ||
|
||
when: | ||
FilePartnerMetadataStorage.getInstance().saveMetadata(metadata) | ||
|
||
then: | ||
thrown(PartnerMetadataException) | ||
} | ||
|
||
def "readMetadata throws PartnerMetadataException when unable to read file"() { | ||
given: | ||
def mockFormatter = Mock(Formatter) | ||
mockFormatter.convertJsonToObject(_ as String, _ as TypeReference) >> {throw new FormatterProcessingException("error", new Exception())} | ||
TestApplicationContext.register(Formatter, mockFormatter) | ||
TestApplicationContext.injectRegisteredImplementations() | ||
|
||
when: | ||
FilePartnerMetadataStorage.getInstance().readMetadata("uniqueId") | ||
|
||
then: | ||
thrown(PartnerMetadataException) | ||
} | ||
} |
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