Skip to content

Commit

Permalink
change generic to object parameter and make HashHelper a singleton class
Browse files Browse the repository at this point in the history
  • Loading branch information
pluckyswan committed Nov 22, 2024
1 parent 1944f11 commit 36cbaa4
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import gov.hhs.cdc.trustedintermediary.domainconnector.DomainConnectorConstructionException;
import gov.hhs.cdc.trustedintermediary.domainconnector.DomainResponseHelper;
import gov.hhs.cdc.trustedintermediary.domainconnector.UnableToReadOpenApiSpecificationException;
import gov.hhs.cdc.trustedintermediary.etor.utils.security.HashHelper;
import gov.hhs.cdc.trustedintermediary.external.apache.ApacheClient;
import gov.hhs.cdc.trustedintermediary.external.azure.AzureDatabaseCredentialsProvider;
import gov.hhs.cdc.trustedintermediary.external.azure.AzureSecrets;
Expand Down Expand Up @@ -85,6 +86,7 @@ private static void registerClasses() {
ApplicationContext.register(YamlCombiner.class, Jackson.getInstance());
ApplicationContext.register(OpenApi.class, OpenApi.getInstance());
ApplicationContext.register(HttpClient.class, ApacheClient.getInstance());
ApplicationContext.register(HashHelper.class, HashHelper.getInstance());
ApplicationContext.register(AuthEngine.class, JjwtEngine.getInstance());
ApplicationContext.register(Cache.class, KeyCache.getInstance());
ApplicationContext.register(DomainResponseHelper.class, DomainResponseHelper.getInstance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
/** The overall logic to receive, convert to OML, and subsequently send a lab order. */
public class SendOrderUseCase implements SendMessageUseCase<Order<?>> {
private static final SendOrderUseCase INSTANCE = new SendOrderUseCase();
private final HashHelper hashHelper = new HashHelper();
@Inject TransformationRuleEngine transformationEngine;
@Inject OrderSender sender;
@Inject MetricMetadata metadata;
@Inject SendMessageHelper sendMessageHelper;
@Inject Logger logger;
@Inject HashHelper hashHelper;

private SendOrderUseCase() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
/** Use case for converting and sending a lab result message. */
public class SendResultUseCase implements SendMessageUseCase<Result<?>> {
private static final SendResultUseCase INSTANCE = new SendResultUseCase();
private final HashHelper hashHelper = new HashHelper();

@Inject TransformationRuleEngine transformationEngine;
@Inject ResultSender sender;
Expand All @@ -22,6 +21,8 @@ public class SendResultUseCase implements SendMessageUseCase<Result<?>> {

@Inject Logger logger;

@Inject HashHelper hashHelper;

private SendResultUseCase() {}

public static SendResultUseCase getInstance() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package gov.hhs.cdc.trustedintermediary.etor.utils.security;

import gov.hhs.cdc.trustedintermediary.wrappers.Logger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import javax.inject.Inject;

public class HashHelper implements SecureHash {
@Inject Logger logger;

private static final HashHelper INSTANCE = new HashHelper();

public static HashHelper getInstance() {
return INSTANCE;
}

@Override
public <T> String generateHash(T input) {
public String generateHash(Object input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA3-512");
byte[] objBytes = input.toString().getBytes(StandardCharsets.UTF_8);
byte[] hashBytes = digest.digest(objBytes);
return HexFormat.of().formatHex(hashBytes);
} catch (NoSuchAlgorithmException e) {
logger.logError("Algorithm does not exist!", e);
throw new RuntimeException(e);
throw new RuntimeException("Algorithm does not exist!", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.security.NoSuchAlgorithmException;

public interface SecureHash {
<T> String generateHash(T hash) throws NoSuchAlgorithmException;
String generateHash(Object hash) throws NoSuchAlgorithmException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataExce
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataOrchestrator

import gov.hhs.cdc.trustedintermediary.etor.ruleengine.transformation.TransformationRuleEngine
import gov.hhs.cdc.trustedintermediary.etor.utils.security.HashHelper
import gov.hhs.cdc.trustedintermediary.wrappers.Logger
import gov.hhs.cdc.trustedintermediary.wrappers.MetricMetadata
import spock.lang.Specification
Expand All @@ -29,6 +30,7 @@ class SendOrderUseCaseTest extends Specification {
TestApplicationContext.register(SendMessageHelper, SendMessageHelper.getInstance())
TestApplicationContext.register(TransformationRuleEngine, mockEngine)
TestApplicationContext.register(OrderSender, mockSender)
TestApplicationContext.register(HashHelper, HashHelper.getInstance())
TestApplicationContext.register(Logger, mockLogger)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadata
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataException
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataOrchestrator
import gov.hhs.cdc.trustedintermediary.etor.ruleengine.transformation.TransformationRuleEngine
import gov.hhs.cdc.trustedintermediary.etor.utils.security.HashHelper
import gov.hhs.cdc.trustedintermediary.wrappers.Logger
import gov.hhs.cdc.trustedintermediary.wrappers.MetricMetadata
import spock.lang.Specification
Expand All @@ -30,6 +31,7 @@ class SendResultUseCaseTest extends Specification {
TestApplicationContext.register(TransformationRuleEngine, mockEngine)
TestApplicationContext.register(ResultSender, mockSender)
TestApplicationContext.register(Logger, mockLogger)
TestApplicationContext.register(HashHelper, HashHelper.getInstance())
TestApplicationContext.injectRegisteredImplementations()
}

Expand Down

0 comments on commit 36cbaa4

Please sign in to comment.