From 056895f6fc5653ee0083ce459e72a9a42a390727 Mon Sep 17 00:00:00 2001 From: smirnovaae Date: Fri, 15 Nov 2024 13:32:36 -0800 Subject: [PATCH] Fix duplicate lines in sonarqube --- attribution-data-file-share/build.gradle | 1 - .../AttributionDataShareHandler.java | 5 +- .../AttributionDataShareHandlerTest.java | 7 ++- .../lambdalibs/lib/ParameterStoreUtil.java | 33 +++++------ .../ab2d/lambdalibs/lib/SsmClientUtil.java | 13 ----- optout/build.gradle | 1 - .../cms/ab2d/optout/OptOutParameterStore.java | 56 ------------------- .../gov/cms/ab2d/optout/OptOutProcessor.java | 5 +- .../cms/ab2d/optout/OptOutProcessorTest.java | 9 +-- 9 files changed, 32 insertions(+), 98 deletions(-) rename attribution-data-file-share/src/main/java/gov/cms/ab2d/attributiondatashare/AttributionParameterStore.java => lambda-lib/src/main/java/gov/cms/ab2d/lambdalibs/lib/ParameterStoreUtil.java (53%) delete mode 100644 lambda-lib/src/main/java/gov/cms/ab2d/lambdalibs/lib/SsmClientUtil.java delete mode 100644 optout/src/main/java/gov/cms/ab2d/optout/OptOutParameterStore.java diff --git a/attribution-data-file-share/build.gradle b/attribution-data-file-share/build.gradle index 3f600c5..f989c28 100644 --- a/attribution-data-file-share/build.gradle +++ b/attribution-data-file-share/build.gradle @@ -16,7 +16,6 @@ dependencies { implementation 'software.amazon.awssdk:s3-transfer-manager:2.25.7' implementation 'software.amazon.awssdk.crt:aws-crt:0.29.11' implementation 'org.postgresql:postgresql:42.7.2' - implementation 'software.amazon.awssdk:ssm:2.25.7' implementation 'software.amazon.awssdk:sts:2.25.6' implementation project(path: ':lambda-lib') testImplementation 'org.mockito:mockito-core:4.8.0' diff --git a/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributiondatashare/AttributionDataShareHandler.java b/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributiondatashare/AttributionDataShareHandler.java index ea441ab..99e381c 100644 --- a/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributiondatashare/AttributionDataShareHandler.java +++ b/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributiondatashare/AttributionDataShareHandler.java @@ -4,6 +4,7 @@ import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.amazonaws.services.lambda.runtime.RequestStreamHandler; import gov.cms.ab2d.lambdalibs.lib.FileUtil; +import gov.cms.ab2d.lambdalibs.lib.ParameterStoreUtil; import software.amazon.awssdk.services.s3.S3AsyncClient; import software.amazon.awssdk.services.sts.StsClient; import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider; @@ -33,7 +34,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co var prefix = (System.getenv(BUCKET_NAME_PROP).contains("prod")) ? "P" : "T"; String fileName = prefix + REQ_FILE_NAME + currentDate; String fileFullPath = FILE_PATH + fileName; - var parameterStore = AttributionParameterStore.getParameterStore(); + var parameterStore = ParameterStoreUtil.getParameterStore(ROLE_PARAM, DB_HOST_PARAM, DB_USER_PARAM, DB_PASS_PARAM); AttributionDataShareHelper helper = helperInit(fileName, fileFullPath, logger); try (var dbConnection = DriverManager.getConnection(parameterStore.getDbHost(), parameterStore.getDbUser(), parameterStore.getDbPassword())) { @@ -48,7 +49,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co } } - public S3AsyncClient getAsyncS3Client(String endpoint, AttributionParameterStore parameterStore) { + public S3AsyncClient getAsyncS3Client(String endpoint, ParameterStoreUtil parameterStore) { var client = S3AsyncClient.crtCreate(); if (endpoint.equals(ENDPOINT)) { diff --git a/attribution-data-file-share/src/test/java/gov/cms/ab2d/attributiondatashare/AttributionDataShareHandlerTest.java b/attribution-data-file-share/src/test/java/gov/cms/ab2d/attributiondatashare/AttributionDataShareHandlerTest.java index 5dc6abb..28534f3 100644 --- a/attribution-data-file-share/src/test/java/gov/cms/ab2d/attributiondatashare/AttributionDataShareHandlerTest.java +++ b/attribution-data-file-share/src/test/java/gov/cms/ab2d/attributiondatashare/AttributionDataShareHandlerTest.java @@ -1,6 +1,7 @@ package gov.cms.ab2d.attributiondatashare; import com.amazonaws.services.lambda.runtime.LambdaLogger; +import gov.cms.ab2d.lambdalibs.lib.ParameterStoreUtil; import gov.cms.ab2d.testutils.AB2DPostgresqlContainer; import gov.cms.ab2d.testutils.TestContext; import org.junit.jupiter.api.Test; @@ -22,15 +23,15 @@ class AttributionDataShareHandlerTest { @Container private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER = new AB2DPostgresqlContainer(); LambdaLogger LOGGER = mock(LambdaLogger.class); - AttributionParameterStore parameterStore = new AttributionParameterStore("", "", "", ""); + ParameterStoreUtil parameterStore = new ParameterStoreUtil("", "", "", ""); AttributionDataShareHelper helper = mock(AttributionDataShareHelper.class); AttributionDataShareHandler handler = spy(new AttributionDataShareHandler()); @Test void attributionDataShareInvoke() { - var mockParameterStore = mockStatic(AttributionParameterStore.class); + var mockParameterStore = mockStatic(ParameterStoreUtil.class); mockParameterStore - .when(AttributionParameterStore::getParameterStore) + .when(() -> ParameterStoreUtil.getParameterStore(anyString(), anyString(), anyString(), anyString())) .thenReturn(parameterStore); Connection dbConnection = mock(Connection.class); diff --git a/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributiondatashare/AttributionParameterStore.java b/lambda-lib/src/main/java/gov/cms/ab2d/lambdalibs/lib/ParameterStoreUtil.java similarity index 53% rename from attribution-data-file-share/src/main/java/gov/cms/ab2d/attributiondatashare/AttributionParameterStore.java rename to lambda-lib/src/main/java/gov/cms/ab2d/lambdalibs/lib/ParameterStoreUtil.java index 74b5592..162a028 100644 --- a/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributiondatashare/AttributionParameterStore.java +++ b/lambda-lib/src/main/java/gov/cms/ab2d/lambdalibs/lib/ParameterStoreUtil.java @@ -1,35 +1,39 @@ -package gov.cms.ab2d.attributiondatashare; +package gov.cms.ab2d.lambdalibs.lib; -import gov.cms.ab2d.lambdalibs.lib.SsmClientUtil; +import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ssm.SsmClient; import software.amazon.awssdk.services.ssm.model.GetParameterRequest; -import static gov.cms.ab2d.attributiondatashare.AttributionDataShareConstants.*; - -public class AttributionParameterStore { +public class ParameterStoreUtil { private final String role; private final String dbHost; private final String dbUser; private final String dbPassword; - public AttributionParameterStore(String role, String dbHost, String dbUser, String dbPassword) { + public ParameterStoreUtil(String role, String dbHost, String dbUser, String dbPassword) { this.role = role; this.dbHost = dbHost; this.dbUser = dbUser; this.dbPassword = dbPassword; } - public static AttributionParameterStore getParameterStore() { - var ssmClient = SsmClientUtil.getClient(); + public static SsmClient getClient(){ + return SsmClient.builder() + .region(Region.US_EAST_1) + .build(); + } + + public static ParameterStoreUtil getParameterStore(String roleParam, String dbHostParam, String dbUserParam, String dbPasswordParam) { + var ssmClient = ParameterStoreUtil.getClient(); - var role = getValueFromParameterStore(ROLE_PARAM, ssmClient); - var dbHost = getValueFromParameterStore(DB_HOST_PARAM, ssmClient); - var dbUser = getValueFromParameterStore(DB_USER_PARAM, ssmClient); - var dbPassword = getValueFromParameterStore(DB_PASS_PARAM, ssmClient); + var role = getValueFromParameterStore(roleParam, ssmClient); + var dbHost = getValueFromParameterStore(dbHostParam, ssmClient); + var dbUser = getValueFromParameterStore(dbUserParam, ssmClient); + var dbPassword = getValueFromParameterStore(dbPasswordParam, ssmClient); ssmClient.close(); - return new AttributionParameterStore(role, dbHost, dbUser, dbPassword); + return new ParameterStoreUtil(role, dbHost, dbUser, dbPassword); } private static String getValueFromParameterStore(String key, SsmClient ssmClient) { @@ -58,6 +62,3 @@ public String getRole() { return role; } } - - - diff --git a/lambda-lib/src/main/java/gov/cms/ab2d/lambdalibs/lib/SsmClientUtil.java b/lambda-lib/src/main/java/gov/cms/ab2d/lambdalibs/lib/SsmClientUtil.java deleted file mode 100644 index 10f6f02..0000000 --- a/lambda-lib/src/main/java/gov/cms/ab2d/lambdalibs/lib/SsmClientUtil.java +++ /dev/null @@ -1,13 +0,0 @@ -package gov.cms.ab2d.lambdalibs.lib; - -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.ssm.SsmClient; - -public class SsmClientUtil { - - public static SsmClient getClient(){ - return SsmClient.builder() - .region(Region.US_EAST_1) - .build(); - } -} diff --git a/optout/build.gradle b/optout/build.gradle index 6b8d620..82db63d 100644 --- a/optout/build.gradle +++ b/optout/build.gradle @@ -16,7 +16,6 @@ dependencies { implementation 'com.amazonaws:aws-java-sdk-s3:1.12.529' implementation 'org.postgresql:postgresql:42.7.2' implementation 'software.amazon.awssdk:s3:2.25.6' - implementation 'software.amazon.awssdk:ssm:2.25.7' implementation 'software.amazon.awssdk:sts:2.25.6' implementation 'com.googlecode.json-simple:json-simple:1.1.1' implementation(project(":lambda-lib")) diff --git a/optout/src/main/java/gov/cms/ab2d/optout/OptOutParameterStore.java b/optout/src/main/java/gov/cms/ab2d/optout/OptOutParameterStore.java deleted file mode 100644 index d32bf89..0000000 --- a/optout/src/main/java/gov/cms/ab2d/optout/OptOutParameterStore.java +++ /dev/null @@ -1,56 +0,0 @@ -package gov.cms.ab2d.optout; - -import gov.cms.ab2d.lambdalibs.lib.SsmClientUtil; -import software.amazon.awssdk.services.ssm.SsmClient; -import software.amazon.awssdk.services.ssm.model.GetParameterRequest; - -import static gov.cms.ab2d.optout.OptOutConstants.*; - -public class OptOutParameterStore { - - private final String role; - private final String dbHost; - private final String dbUser; - private final String dbPassword; - public OptOutParameterStore(String role, String dbHost, String dbUser, String dbPassword) { - this.role = role; - this.dbHost = dbHost; - this.dbUser = dbUser; - this.dbPassword = dbPassword; - } - - public static OptOutParameterStore getParameterStore() { - var ssmClient = SsmClientUtil.getClient(); - - var role = getValueFromParameterStore(ROLE_PARAM, ssmClient); - var dbHost = getValueFromParameterStore(DB_HOST_PARAM, ssmClient); - var dbUser = getValueFromParameterStore(DB_USER_PARAM, ssmClient); - var dbPassword = getValueFromParameterStore(DB_PASS_PARAM, ssmClient); - - ssmClient.close(); - return new OptOutParameterStore(role, dbHost, dbUser, dbPassword); - } - - private static String getValueFromParameterStore(String key, SsmClient ssmClient) { - var parameterRequest = GetParameterRequest.builder() - .name(key) - .withDecryption(true) - .build(); - - var parameterResponse = ssmClient.getParameter(parameterRequest); - return parameterResponse.parameter().value(); - } - - public String getDbHost(){ - return dbHost; - } - public String getDbUser() { - return dbUser; - } - public String getDbPassword() { - return dbPassword; - } - public String getRole() { - return role; - } -} diff --git a/optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java b/optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java index e26ba63..ff0c6ae 100644 --- a/optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java +++ b/optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java @@ -2,6 +2,7 @@ import com.amazonaws.services.lambda.runtime.LambdaLogger; +import gov.cms.ab2d.lambdalibs.lib.ParameterStoreUtil; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.sts.StsClient; import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider; @@ -26,13 +27,13 @@ public class OptOutProcessor { public List optOutInformationList; public boolean isRejected; - OptOutParameterStore parameterStore; + ParameterStoreUtil parameterStore; public OptOutProcessor(LambdaLogger logger) { this.logger = logger; this.optOutInformationList = new ArrayList<>(); isRejected = false; - parameterStore = OptOutParameterStore.getParameterStore(); + parameterStore = ParameterStoreUtil.getParameterStore(ROLE_PARAM, DB_HOST_PARAM, DB_USER_PARAM, DB_PASS_PARAM); } public OptOutResults process(String fileName, String bfdBucket, String endpoint) throws URISyntaxException { diff --git a/optout/src/test/java/gov/cms/ab2d/optout/OptOutProcessorTest.java b/optout/src/test/java/gov/cms/ab2d/optout/OptOutProcessorTest.java index 3c9603e..57ed290 100644 --- a/optout/src/test/java/gov/cms/ab2d/optout/OptOutProcessorTest.java +++ b/optout/src/test/java/gov/cms/ab2d/optout/OptOutProcessorTest.java @@ -2,6 +2,7 @@ import com.amazonaws.services.lambda.runtime.LambdaLogger; +import gov.cms.ab2d.lambdalibs.lib.ParameterStoreUtil; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -32,7 +33,7 @@ class OptOutProcessorTest { private static final ResultSet resultSet = mock(ResultSet.class); private static final PreparedStatement preparedStatement = mock(PreparedStatement.class); private static final Connection dbConnection = mock(Connection.class); - private static final MockedStatic parameterStore = mockStatic(OptOutParameterStore.class); + private static final MockedStatic parameterStore = mockStatic(ParameterStoreUtil.class); private static final String DATE = new SimpleDateFormat(EFFECTIVE_DATE_PATTERN).format(new Date()); private static final String MBI = "DUMMY000001"; private static final String TRAILER_COUNT = "0000000001"; @@ -43,8 +44,6 @@ private static String validLine(char isOptOut) { @BeforeAll static void beforeAll() throws SQLException { - parameterStore.when(OptOutParameterStore::getParameterStore).thenReturn(new OptOutParameterStore("", "", "", "")); - mockStatic(DriverManager.class) .when(() -> DriverManager.getConnection(anyString(), anyString(), anyString())).thenReturn(dbConnection); when(dbConnection.prepareStatement(anyString())).thenReturn(preparedStatement); @@ -55,7 +54,9 @@ static void beforeAll() throws SQLException { @BeforeEach void beforeEach() throws IOException { S3MockAPIExtension.createFile(Files.readString(Paths.get("src/test/resources/" + TEST_FILE_NAME), StandardCharsets.UTF_8), TEST_FILE_NAME); - parameterStore.when(OptOutParameterStore::getParameterStore).thenReturn(new OptOutParameterStore("", "", "", "")); + parameterStore. + when(() -> ParameterStoreUtil.getParameterStore(anyString(), anyString(), anyString(), anyString())) + .thenReturn(new ParameterStoreUtil("", "", "", "")); optOutProcessing = spy(new OptOutProcessor(mock(LambdaLogger.class))); optOutProcessing.isRejected = false; }