Skip to content

Commit

Permalink
Merge branch 'main' into plt-400-import-integration-workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
gsf committed May 2, 2024
2 parents 77e780a + 7b8719e commit de8648c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,22 @@ void copyDataToFile(Connection connection) {
writer.newLine();
long records = 0;
while (rs.next()) {
var line = getResponseLine(rs.getString(1), rs.getTimestamp(2), rs.getBoolean(3));
var line = getResponseLine(rs.getString("mbi"), rs.getDate("effective_date"), rs.getBoolean("opt_out_flag"));
writer.write(line);
writer.newLine();
records++;
}
writer.write(AB2D_TRAILER_REQ + date + String.format("%010d", records));

String lastLine = AB2D_TRAILER_REQ + date + String.format("%010d", records);
writer.write(lastLine);
logger.log("File trailer: " + lastLine);
} catch (SQLException | IOException ex) {
String errorMessage = "An error occurred while exporting data to a file. ";
logger.log(errorMessage + ex.getMessage());
throw new AttributionDataShareException(errorMessage, ex);
}
}

String getResponseLine(String currentMbi, Timestamp effectiveDate, Boolean optOutFlag) {
String getResponseLine(String currentMbi, Date effectiveDate, Boolean optOutFlag) {
var result = new StringBuilder();
result.append(currentMbi);
// Adding spaces to the end of a string to achieve the required position index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
import java.util.*;

import static gov.cms.ab2d.attributionDataShare.AttributionDataShareConstants.*;
import static gov.cms.ab2d.attributionDataShare.AttributionDataShareHelper.getExecuteQuery;
Expand All @@ -43,7 +40,7 @@ public class AttributionDataShareTest {
String FILE_FULL_PATH = FILE_PATH + FILE_NAME;
String MBI_1 = "DUMMY000001";
String MBI_2 = "DUMMY000002";
Timestamp DATE = Timestamp.valueOf("2024-02-26 00:00:00");
Date DATE = new GregorianCalendar(2024, Calendar.FEBRUARY, 26).getTime();
AttributionDataShareHelper helper;

@BeforeEach
Expand All @@ -57,7 +54,7 @@ void copyDataToFileTest() throws IOException, SQLException {
var stmt = mock(Statement.class);
var rs = new MockResultSet("");
rs.addColumn("mbi", Arrays.asList(MBI_1, MBI_2));
rs.addColumn("effective_date", Arrays.asList(DATE, null));
rs.addColumn("effective_date", Arrays.asList("2024-02-26", null));
rs.addColumn("opt_out_flag", Arrays.asList(true, null));
when(connection.createStatement()).thenReturn(stmt);

Expand Down
7 changes: 3 additions & 4 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ public class OptOutConstants {
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static final String CONF_FILE_NAME = "#EFT.ON.AB2D.NGD.CONF.";
public static final String CONF_FILE_NAME_PATTERN = "'D'yyMMdd.'T'HHmmsss";
public static final String UPDATE_STATEMENT = "UPDATE public.coverage\n" +
"SET opt_out_flag = ?, effective_date = current_timestamp\n" +
"WHERE current_mbi = ? OR historic_mbis iLike CONCAT( '%',?,'%')";

public static final String UPDATE_STATEMENT = "UPDATE public.current_mbi\n" +
"SET opt_out_flag = ?, effective_date = current_date\n" +
"WHERE mbi = ?";

private OptOutConstants() {
}
Expand Down
66 changes: 31 additions & 35 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,33 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.List;

import static gov.cms.ab2d.optout.OptOutConstants.*;

public class OptOutProcessor {
private final LambdaLogger logger;
public SortedMap<Long, OptOutInformation> optOutInformationMap;
public List<OptOutInformation> optOutInformationList;
public boolean isRejected;

OptOutParameterStore parameterStore;

public OptOutProcessor(LambdaLogger logger) {
this.logger = logger;
this.optOutInformationMap = new TreeMap<>();
this.optOutInformationList = new ArrayList<>();
isRejected = false;
parameterStore = OptOutParameterStore.getParameterStore();
}

public void process(String fileName, String bfdBucket, String endpoint) throws URISyntaxException {
var optOutS3 = new OptOutS3(getS3Client(endpoint), fileName, bfdBucket, logger);

processFileFromS3(optOutS3.openFileS3());
updateOptOut();
var name = optOutS3.createResponseOptOutFile(createResponseContent());
logger.log("File with name " + name + " was uploaded to bucket: " + bfdBucket);
if (!isRejected)
Expand Down Expand Up @@ -76,17 +74,14 @@ public S3Client getS3Client(String endpoint) throws URISyntaxException {

public void processFileFromS3(BufferedReader reader) {
String line;
var lineNumber = 0L;
try (var dbConnection = DriverManager.getConnection(parameterStore.getDbHost(), parameterStore.getDbUser(), parameterStore.getDbPassword())){
try {
while ((line = reader.readLine()) != null) {
if (!line.startsWith(HEADER_RESP) && !line.startsWith(TRAILER_RESP)) {
var optOutInformation = createOptOutInformation(line);
optOutInformationMap.put(lineNumber, optOutInformation);
updateOptOut(lineNumber, optOutInformation, dbConnection);
optOutInformationList.add(optOutInformation);
}
lineNumber++;
}
} catch (IOException | SQLException ex) {
} catch (IOException ex) {
logger.log("An error occurred during file processing. " + ex.getMessage());
}
}
Expand All @@ -97,34 +92,42 @@ public OptOutInformation createOptOutInformation(String information) {
return new OptOutInformation(mbi, optOutFlag);
}

public void updateOptOut(long lineNumber, OptOutInformation optOutInformation, Connection dbConnection) {
try (var statement = dbConnection.prepareStatement(UPDATE_STATEMENT)) {
prepareInsert(optOutInformation, statement);
statement.execute();
public void updateOptOut() {
try (var dbConnection = DriverManager.getConnection(parameterStore.getDbHost(), parameterStore.getDbUser(), parameterStore.getDbPassword())){
var statement = dbConnection.prepareStatement(UPDATE_STATEMENT);
for (var optOutInformation : optOutInformationList) {
statement.setBoolean(1, optOutInformation.getOptOutFlag());
statement.setString(2, optOutInformation.getMbi());
statement.addBatch();
}
statement.executeBatch();
} catch (SQLException ex) {
logger.log("There is an insertion error on the line " + lineNumber);
logger.log("There is an insertion error " + ex.getMessage());
isRejected = true;
}
}
public String createResponseContent() {
var date = new SimpleDateFormat(EFFECTIVE_DATE_PATTERN).format(new Date());
var responseContent = new StringBuilder();
responseContent.append(AB2D_HEADER_CONF).append(date);
responseContent.append(LINE_SEPARATOR);
var responseContent = new StringBuilder()
.append(AB2D_HEADER_CONF)
.append(date)
.append(LINE_SEPARATOR);
var recordStatus = getRecordStatus();
var effectiveDate = getEffectiveDate(date);

for (var optOutResult : optOutInformationMap.entrySet()) {
var info = optOutResult.getValue();

responseContent.append(info.getMbi())
for (var optOutResult : optOutInformationList) {
responseContent.append(optOutResult.getMbi())
.append(effectiveDate)
.append((info.getOptOutFlag()) ? 'Y' : 'N')
.append((optOutResult.getOptOutFlag()) ? 'Y' : 'N')
.append(recordStatus)
.append(LINE_SEPARATOR);
}
responseContent.append(AB2D_TRAILER_CONF).append(date).append(String.format("%010d", optOutInformationMap.size()));

var lastLine = new StringBuilder()
.append(AB2D_TRAILER_CONF)
.append(date)
.append(String.format("%010d", optOutInformationList.size()));
responseContent.append(lastLine);
logger.log("File trailer: " + lastLine);
return responseContent.toString();
}

Expand All @@ -136,11 +139,4 @@ public String getEffectiveDate(String date) {
return (isRejected) ? " ".repeat(EFFECTIVE_DATE_LENGTH) : date;
}

private static void prepareInsert(OptOutInformation optOut, PreparedStatement statement) throws SQLException {
statement.setBoolean(1, optOut.getOptOutFlag());
statement.setString(2, optOut.getMbi());
statement.setString(3, optOut.getMbi());
statement.addBatch();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ void afterEach() {
void processTest() throws URISyntaxException {
optOutProcessing.isRejected = false;
optOutProcessing.process(TEST_FILE_NAME, TEST_BFD_BUCKET_NAME, TEST_ENDPOINT);
assertEquals(7, optOutProcessing.optOutInformationMap.size());
assertEquals(7, optOutProcessing.optOutInformationList.size());
}

@Test
void processEmptyFileTest() throws IOException, URISyntaxException {
var emptyFileName = "emptyDummy.txt";
S3MockAPIExtension.createFile(Files.readString(Paths.get("src/test/resources/" + emptyFileName), StandardCharsets.UTF_8), emptyFileName);
optOutProcessing.process(emptyFileName, TEST_BFD_BUCKET_NAME, TEST_ENDPOINT);
assertEquals(0, optOutProcessing.optOutInformationMap.size());
assertEquals(0, optOutProcessing.optOutInformationList.size());
S3MockAPIExtension.deleteFile(emptyFileName);
}

Expand All @@ -92,7 +92,7 @@ void createFalseOptOutInformationTest() {

@Test
void createAcceptedResponseTest() {
optOutProcessing.optOutInformationMap.put(1L, new OptOutInformation(MBI, true));
optOutProcessing.optOutInformationList.add(new OptOutInformation(MBI, true));
var expectedLine = MBI + DATE + "Y" + RecordStatus.ACCEPTED;
var expectedText = AB2D_HEADER_CONF + DATE + LINE_SEPARATOR
+ expectedLine + LINE_SEPARATOR
Expand All @@ -103,7 +103,7 @@ void createAcceptedResponseTest() {
@Test
void createRejectedResponseTest() {
optOutProcessing.isRejected = true;
optOutProcessing.optOutInformationMap.put(1L, new OptOutInformation(MBI, false));
optOutProcessing.optOutInformationList.add(new OptOutInformation(MBI, false));
var expectedLine = MBI + " " + "N" + RecordStatus.REJECTED;
var expectedText = AB2D_HEADER_CONF + DATE + LINE_SEPARATOR
+ expectedLine + LINE_SEPARATOR
Expand All @@ -113,16 +113,14 @@ void createRejectedResponseTest() {

@Test
void updateOptOutTest() {
var optOutInformation = optOutProcessing.createOptOutInformation(validLine('Y'));
optOutProcessing.updateOptOut(1L, optOutInformation, dbConnection);
optOutProcessing.updateOptOut();
assertFalse(optOutProcessing.isRejected);
}

@Test
void updateOptOutExceptionTest() throws SQLException {
var optOutInformation = optOutProcessing.createOptOutInformation(validLine('Y'));
when(dbConnection.prepareStatement(anyString())).thenThrow(SQLException.class);
optOutProcessing.updateOptOut(1L, optOutInformation, dbConnection);
optOutProcessing.updateOptOut();
// Insertion error exists
assertTrue(optOutProcessing.isRejected);
assertTrue(S3MockAPIExtension.isObjectExists(TEST_FILE_NAME));
Expand Down
4 changes: 2 additions & 2 deletions optout/src/test/resources/optOutDummy.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
HDR_BENEDATARSP20240123
1S00E00JG37N
1S00E00JG37Y
7SP1D00AA00N
2SY1D00AA00Y
7SF9C00AA00N
6SF9C00AA00N
7SF6F00AA00N
DUMMY000006Y
DUMMY000007N
TRL_BENEDATARSP202401230000000007

0 comments on commit de8648c

Please sign in to comment.