diff --git a/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributionDataShare/AttributionDataShareConstants.java b/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributionDataShare/AttributionDataShareConstants.java index cffcb26..ed00d6c 100644 --- a/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributionDataShare/AttributionDataShareConstants.java +++ b/attribution-data-file-share/src/main/java/gov/cms/ab2d/attributionDataShare/AttributionDataShareConstants.java @@ -12,9 +12,9 @@ private AttributionDataShareConstants() { public static final String TEST_ENDPOINT = "http://127.0.0.1:8001"; public static final Region S3_REGION = Region.US_EAST_1; public static final String FILE_PATH = "/tmp/"; - public static final String FILE_PARTIAL_NAME = "ab2d-beneids_"; - public static final String FILE_FORMAT = ".txt"; - public static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; + public static final String REQ_FILE_NAME = "P.AB2D.NGD.REQ."; + public static final String REQ_FILE_NAME_PATTERN = "'D'yyMMdd.'T'hhmmsss"; + public static final String REQ_FILE_FORMAT = ".OUT"; public static final String FIRST_LINE = "HDR_BENEDATAREQ_"; public static final String LAST_LINE = "TLR_BENEDATAREQ_"; 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 f8e9919..9b3f84f 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 @@ -21,14 +21,14 @@ public class AttributionDataShareHandler implements RequestStreamHandler { // Writes out a file to the FILE_PATH. - // I.E: "ab2d-beneids_2023-08-16T12:08:56.235-0700.txt" + // I.E: "P.AB2D.NGD.REQ.D240209.T1122001.OUT" public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { LambdaLogger logger = context.getLogger(); logger.log("AttributionDataShare Lambda is started"); - String currentDate = new SimpleDateFormat(PATTERN).format(new Date()); - String fileName = FILE_PARTIAL_NAME + currentDate + FILE_FORMAT; + String currentDate = new SimpleDateFormat(REQ_FILE_NAME_PATTERN).format(new Date()); + String fileName = REQ_FILE_NAME + currentDate + REQ_FILE_FORMAT; String fileFullPath = FILE_PATH + fileName; AttributionDataShareHelper helper = helperInit(fileName, fileFullPath, logger); try { diff --git a/attribution-data-file-share/src/test/java/gov/cms/ab2d/attributionDataShare/AttributionDataShareTest.java b/attribution-data-file-share/src/test/java/gov/cms/ab2d/attributionDataShare/AttributionDataShareTest.java index fbc85ee..9521912 100644 --- a/attribution-data-file-share/src/test/java/gov/cms/ab2d/attributionDataShare/AttributionDataShareTest.java +++ b/attribution-data-file-share/src/test/java/gov/cms/ab2d/attributionDataShare/AttributionDataShareTest.java @@ -34,7 +34,7 @@ public class AttributionDataShareTest { private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER = new AB2DPostgresqlContainer(); LambdaLogger LOGGER = mock(LambdaLogger.class); - String FILE_NAME = FILE_PARTIAL_NAME + new SimpleDateFormat(PATTERN).format(new Date()) + FILE_FORMAT; + String FILE_NAME = REQ_FILE_NAME + new SimpleDateFormat(REQ_FILE_NAME_PATTERN).format(new Date()) + REQ_FILE_FORMAT; String FILE_FULL_PATH = FILE_PATH + FILE_NAME; String MBI = "DUMMY000001"; diff --git a/optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java b/optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java index 5bd8645..f515986 100644 --- a/optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java +++ b/optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java @@ -13,25 +13,24 @@ public class OptOutConstants { public static final String TEST_FILE_NAME = "optOutDummy.txt"; public static final String BFD_S3_BUCKET_NAME = "ab2d-opt-out-temp-349849222861-us-east-1"; public static final Region S3_REGION = Region.US_EAST_1; - public static final String FIRST_LINE = "HDR_BENEDATASHR"; - public static final String LAST_LINE = "TRL_BENEDATASHR"; - public static final int DEFAULT_LINE_LENGTH = 459; + public static final String HEADER_RESP = "HDR_BENEDATARSP"; + public static final String TRAILER_RESP = "TLR_BENEDATARSP"; + public static final String AB2D_HEADER_CONF = "HDR_BENECONFIRM"; + public static final String AB2D_TRAILER_CONF = "TLR_BENECONFIRM"; public static final int MBI_INDEX_START = 0; public static final int MBI_INDEX_END = 11; - public static final int EFFECTIVE_DATE_INDEX_START = 354; - public static final int EFFECTIVE_DATE_INDEX_END = 362; - public static final int OPTOUT_FLAG_INDEX = 368; - public static final String RECORD_STATUS_PATTERN = "%-10s"; + public static final int OPTOUT_FLAG_INDEX = 11; public static final String EFFECTIVE_DATE_PATTERN = "yyyyMMdd"; + public static final int EFFECTIVE_DATE_LENGTH = 8; public static final String LINE_SEPARATOR = System.getProperty("line.separator"); - public static final String RESPONSE_FILE_NAME = "P.AB2D.DPRF.RSP."; - public static final String RESPONSE_FILE_NAME_PATTERN = "'D'yyMMdd.'T'hhmmssss"; - + public static final String CONF_FILE_NAME = "P.AB2D.NGD.CONF."; + public static final String CONF_FILE_NAME_PATTERN = "'D'yyMMdd.'T'hhmmsss"; + public static final String CONF_FILE_FORMAT = ".OUT"; public static final String UPDATE_STATEMENT = "UPDATE public.coverage\n" + - "SET opt_out_flag = ?, effective_date = ?\n" + + "SET opt_out_flag = ?, effective_date = current_timestamp\n" + "WHERE current_mbi = ? OR historic_mbis LIKE CONCAT( '%',?,'%')"; private OptOutConstants() { } -} +} \ No newline at end of file diff --git a/optout/src/main/java/gov/cms/ab2d/optout/OptOutInformation.java b/optout/src/main/java/gov/cms/ab2d/optout/OptOutInformation.java index 4979588..bdc86c7 100644 --- a/optout/src/main/java/gov/cms/ab2d/optout/OptOutInformation.java +++ b/optout/src/main/java/gov/cms/ab2d/optout/OptOutInformation.java @@ -1,44 +1,17 @@ package gov.cms.ab2d.optout; -import java.sql.Timestamp; - public class OptOutInformation { - private final String text; - private String mbi; - private Timestamp effectiveDate; - private boolean optOutFlag; - private final long lineNumber; - - public OptOutInformation(String mbi, Timestamp effectiveDate, boolean optOutFlag, long lineNumber, String text) { + private final String mbi; + private final Boolean optOutFlag; + public OptOutInformation(String mbi, Boolean optOutFlag) { this.mbi = mbi; - this.effectiveDate = effectiveDate; this.optOutFlag = optOutFlag; - this.lineNumber = lineNumber; - this.text = text; } - - public OptOutInformation(long lineNumber, String text) { - this.lineNumber = lineNumber; - this.text = text; + public Boolean getOptOutFlag() { + return optOutFlag; } - public String getMbi() { return mbi; } - public Timestamp getEffectiveDate() { - return effectiveDate; - } - - public boolean isOptOut() { - return optOutFlag; - } - - public long getLineNumber() { - return lineNumber; - } - - public String getText() { - return text; - } -} +} \ No newline at end of file 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 99e876c..3b02dd9 100644 --- a/optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java +++ b/optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java @@ -12,10 +12,8 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; -import java.sql.Timestamp; -import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Optional; +import java.util.Date; import java.util.SortedMap; import java.util.TreeMap; @@ -23,23 +21,27 @@ public class OptOutProcessor { private final LambdaLogger logger; - public SortedMap optOutResultMap; + public SortedMap optOutInformationMap; + public boolean isRejected; private final OptOutS3 optOutS3; public OptOutProcessor(String fileName, String endpoint, LambdaLogger logger) throws URISyntaxException { this.logger = logger; - this.optOutResultMap = new TreeMap<>(); + this.optOutInformationMap = new TreeMap<>(); var s3Client = S3Client.builder() // .credentialsProvider(credentials) .region(S3_REGION) .endpointOverride(new URI(endpoint)) .build(); + isRejected = false; optOutS3 = new OptOutS3(s3Client, fileName, logger); } public void process() { processFileFromS3(optOutS3.openFileS3()); optOutS3.createResponseOptOutFile(createResponseContent()); + if (!isRejected) + optOutS3.deleteFileFromS3(); } public void processFileFromS3(BufferedReader reader) { @@ -48,9 +50,11 @@ public void processFileFromS3(BufferedReader reader) { var lineNumber = 0L; try { while ((line = reader.readLine()) != null) { - var optOutInformation = createOptOutInformation(line, lineNumber); - // If the file line was parsed successfully, update the optout values in the database - optOutInformation.ifPresent(information -> updateOptOut(information, dbConnection)); + if (!line.startsWith(HEADER_RESP) && !line.startsWith(TRAILER_RESP)) { + var optOutInformation = createOptOutInformation(line); + optOutInformationMap.put(lineNumber, optOutInformation); + updateOptOut(lineNumber, optOutInformation, dbConnection); + } lineNumber++; } } catch (IOException ex) { @@ -59,80 +63,58 @@ public void processFileFromS3(BufferedReader reader) { } } - public Optional createOptOutInformation(String information, long lineNumber) throws IllegalArgumentException { - try { - var mbi = information.substring(MBI_INDEX_START, MBI_INDEX_END).trim(); - var effectiveDate = convertToDate(information.substring(EFFECTIVE_DATE_INDEX_START, EFFECTIVE_DATE_INDEX_END)); - var optOutFlag = (information.charAt(OPTOUT_FLAG_INDEX) == 'Y'); - - var optOutInformation = new OptOutInformation(mbi, effectiveDate, optOutFlag, lineNumber, information); - // The file line was parsed successfully - optOutResultMap.put(lineNumber, new OptOutResult(optOutInformation, RecordStatus.ACCEPTED, ReasonCode.ACCEPTED)); - return Optional.of(optOutInformation); - } catch (NumberFormatException | StringIndexOutOfBoundsException | ParseException ex) { - logger.log("Lambda can not parse the line: " + lineNumber); - // The file line was parsed with an error - optOutResultMap.put(lineNumber, new OptOutResult(new OptOutInformation(lineNumber, information), RecordStatus.REJECTED, ReasonCode.PARSE_ERROR)); - if (!information.startsWith(FIRST_LINE) && !information.startsWith(LAST_LINE)) { - logger.log("There is a parsing error on the line " + lineNumber); - } - } - return Optional.empty(); + public OptOutInformation createOptOutInformation(String information) { + var mbi = information.substring(MBI_INDEX_START, MBI_INDEX_END).trim(); + var optOutFlag = (information.charAt(OPTOUT_FLAG_INDEX) == 'Y'); + return new OptOutInformation(mbi, optOutFlag); } - public void updateOptOut(OptOutInformation optOutInformation, Connection dbConnection) { + public void updateOptOut(long lineNumber, OptOutInformation optOutInformation, Connection dbConnection) { try (var statement = dbConnection.prepareStatement(UPDATE_STATEMENT)) { - logger.log("Mbi: " + optOutInformation.getMbi() + ", OptOut Flag: " + optOutInformation.isOptOut()); prepareInsert(optOutInformation, statement); statement.execute(); } catch (SQLException ex) { - optOutResultMap.put(optOutInformation.getLineNumber(), - new OptOutResult( - new OptOutInformation(optOutInformation.getLineNumber(), optOutInformation.getText()), - RecordStatus.REJECTED, - ReasonCode.INSERT_ERROR)); - logger.log("There is an insertion error on the line " + optOutInformation.getLineNumber()); + logger.log("There is an insertion error on the line " + lineNumber); logger.log(ex.getMessage()); + isRejected = true; } } public String createResponseContent() { + var date = new SimpleDateFormat(EFFECTIVE_DATE_PATTERN).format(new Date()); var responseContent = new StringBuilder(); - for (var optOutResult : optOutResultMap.entrySet()) { - var line = optOutResult.getValue(); - var text = line.getOptOutInformation().getText(); - // First and last lines don't contain optout data and are written as is - if (optOutResult.getKey() == 0 || optOutResult.getKey() == optOutResultMap.size() - 1) { - responseContent.append(text); - } else { - var result = new StringBuilder(text); - // Adding spaces to the end of a string to achieve the RecordStatus position index - if (text.length() < DEFAULT_LINE_LENGTH) - result.append(" ".repeat(Math.max(0, DEFAULT_LINE_LENGTH - text.length()))); - - result.append(String.format(RECORD_STATUS_PATTERN, line.getRecordStatus())); - result.append(line.getReasonCode()); - responseContent.append(result); - } - responseContent.append(LINE_SEPARATOR); + responseContent.append(AB2D_HEADER_CONF).append(date); + responseContent.append(LINE_SEPARATOR); + var recordStatus = getRecordStatus(); + var effectiveDate = getEffectiveDate(date); + + for (var optOutResult : optOutInformationMap.entrySet()) { + var info = optOutResult.getValue(); + + responseContent.append(info.getMbi()) + .append(effectiveDate) + .append((info.getOptOutFlag()) ? 'Y' : 'N') + .append(recordStatus) + .append(LINE_SEPARATOR); } - // Remove last empty line - responseContent.delete(responseContent.lastIndexOf(LINE_SEPARATOR), responseContent.length()); + responseContent.append(AB2D_TRAILER_CONF).append(date).append(String.format("%010d", optOutInformationMap.size())); + return responseContent.toString(); } + public String getRecordStatus() { + return (isRejected) ? RecordStatus.REJECTED.toString() : RecordStatus.ACCEPTED.toString(); + } + + 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.isOptOut()); - statement.setTimestamp(2, optOut.getEffectiveDate()); + statement.setBoolean(1, optOut.getOptOutFlag()); + statement.setString(2, optOut.getMbi()); statement.setString(3, optOut.getMbi()); - statement.setString(4, optOut.getMbi()); statement.addBatch(); } - private Timestamp convertToDate(String date) throws ParseException { - var dateFormat = new SimpleDateFormat(EFFECTIVE_DATE_PATTERN); - var parsedDate = dateFormat.parse(date); - return new Timestamp(parsedDate.getTime()); - } - -} +} \ No newline at end of file diff --git a/optout/src/main/java/gov/cms/ab2d/optout/OptOutResult.java b/optout/src/main/java/gov/cms/ab2d/optout/OptOutResult.java deleted file mode 100644 index 209787a..0000000 --- a/optout/src/main/java/gov/cms/ab2d/optout/OptOutResult.java +++ /dev/null @@ -1,26 +0,0 @@ -package gov.cms.ab2d.optout; - -public class OptOutResult { - - private final OptOutInformation optOutInformation; - private final String reasonCode; - private final String recordStatus; - - public OptOutResult(OptOutInformation optOutInformation, String recordStatus, String reasonCode) { - this.optOutInformation = optOutInformation; - this.reasonCode = reasonCode; - this.recordStatus = recordStatus; - } - - public OptOutInformation getOptOutInformation() { - return optOutInformation; - } - - public String getRecordStatus() { - return recordStatus; - } - - public String getReasonCode() { - return reasonCode; - } -} diff --git a/optout/src/main/java/gov/cms/ab2d/optout/OptOutS3.java b/optout/src/main/java/gov/cms/ab2d/optout/OptOutS3.java index 8ae9003..238fe08 100644 --- a/optout/src/main/java/gov/cms/ab2d/optout/OptOutS3.java +++ b/optout/src/main/java/gov/cms/ab2d/optout/OptOutS3.java @@ -60,7 +60,10 @@ public BufferedReader openFileS3() { public String createResponseOptOutFile(String responseContent) { try { - var key = RESPONSE_FILE_NAME + new SimpleDateFormat(RESPONSE_FILE_NAME_PATTERN).format(new Date()); + var key = CONF_FILE_NAME + + new SimpleDateFormat(CONF_FILE_NAME_PATTERN).format(new Date()) + + CONF_FILE_FORMAT; + var objectRequest = PutObjectRequest.builder() .bucket(BFD_S3_BUCKET_NAME) .key(key) @@ -89,4 +92,4 @@ public void deleteFileFromS3() { } -} +} \ No newline at end of file diff --git a/optout/src/main/java/gov/cms/ab2d/optout/ReasonCode.java b/optout/src/main/java/gov/cms/ab2d/optout/ReasonCode.java deleted file mode 100644 index 6a7204b..0000000 --- a/optout/src/main/java/gov/cms/ab2d/optout/ReasonCode.java +++ /dev/null @@ -1,12 +0,0 @@ -package gov.cms.ab2d.optout; - -public final class ReasonCode { - - public static final String ACCEPTED = "00"; - public static final String PARSE_ERROR = "01"; - public static final String INSERT_ERROR = "02"; - - private ReasonCode() { - } - -} diff --git a/optout/src/main/java/gov/cms/ab2d/optout/RecordStatus.java b/optout/src/main/java/gov/cms/ab2d/optout/RecordStatus.java index 9c789f1..a617f89 100644 --- a/optout/src/main/java/gov/cms/ab2d/optout/RecordStatus.java +++ b/optout/src/main/java/gov/cms/ab2d/optout/RecordStatus.java @@ -1,11 +1,17 @@ package gov.cms.ab2d.optout; -public final class RecordStatus { - public static final String ACCEPTED = "Accepted"; - public static final String REJECTED = "Rejected"; +public enum RecordStatus { - private RecordStatus() { + ACCEPTED("Accepted 00"), + REJECTED("Rejected 02"); + private final String status; + RecordStatus(final String status) { + this.status = status; + } + @Override + public String toString() { + return status; } } 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 fb7477c..77e6a31 100644 --- a/optout/src/test/java/gov/cms/ab2d/optout/OptOutProcessorTest.java +++ b/optout/src/test/java/gov/cms/ab2d/optout/OptOutProcessorTest.java @@ -3,10 +3,7 @@ import com.amazonaws.services.lambda.runtime.LambdaLogger; import gov.cms.ab2d.databasemanagement.DatabaseUtil; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import org.junit.jupiter.api.extension.ExtendWith; import java.io.IOException; @@ -17,10 +14,8 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; -import java.sql.Timestamp; -import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Optional; +import java.util.Date; import static gov.cms.ab2d.optout.OptOutConstants.*; import static org.junit.jupiter.api.Assertions.*; @@ -28,14 +23,17 @@ @ExtendWith({S3MockAPIExtension.class}) public class OptOutProcessorTest { - private static final LambdaLogger logger = mock(LambdaLogger.class); private static final Connection dbConnection = mock(Connection.class); private static final PreparedStatement statement = mock(PreparedStatement.class); + private static final String DATE = new SimpleDateFormat(EFFECTIVE_DATE_PATTERN).format(new Date()); private static final String MBI = "DUMMY000001"; - private static final String VALID_LINE = MBI + " NAME LASTNAME 111 DUMMY ADDRESS TESTDATA DUMMY11DUMMY20230726202307261-800TY"; - private final String INVALID_LINE = "TRL_BENEDATARSP202307260000000009"; - private final String EXPECTED_ACCEPTED_LINE = VALID_LINE + " Accepted 00"; - static OptOutProcessor optOutProcessor; + private static final String TRAILER_COUNT = "0000000001"; + + private static String validLine(char isOptOut) { + return MBI + isOptOut; + } + + static OptOutProcessor optOutProcessing; @BeforeAll static void beforeAll() throws SQLException { @@ -45,109 +43,90 @@ static void beforeAll() throws SQLException { } @BeforeEach - void beforeEach() throws URISyntaxException { - // var creds = StaticCredentialsProvider.create(AwsSessionCredentials.create("test", "test", "")); - optOutProcessor = spy(new OptOutProcessor(TEST_FILE_NAME, TEST_ENDPOINT, logger)); - } - - @Test - void processTest() throws IOException { + void beforeEach() throws URISyntaxException, IOException { S3MockAPIExtension.createFile(Files.readString(Paths.get("src/test/resources/" + TEST_FILE_NAME), StandardCharsets.UTF_8)); - optOutProcessor.process(); - assertEquals(4, optOutProcessor.optOutResultMap.size()); - verify(optOutProcessor, times(4)).createOptOutInformation(anyString(), anyLong()); - verify(optOutProcessor, times(2)).updateOptOut(any(OptOutInformation.class), any(Connection.class)); - verify(optOutProcessor, times(1)).createResponseContent(); - //Because map contains records with insertion error - Assertions.assertTrue(S3MockAPIExtension.isObjectExists(TEST_FILE_NAME)); + optOutProcessing = spy(new OptOutProcessor(TEST_FILE_NAME, TEST_ENDPOINT, mock(LambdaLogger.class))); + optOutProcessing.isRejected = false; } - @Test - void createOptOutInformationValidTest1() { - Optional optOutInformation = optOutProcessor.createOptOutInformation(VALID_LINE, 1L); - assertTrue(optOutInformation.isPresent()); - assertEquals(1L, optOutInformation.get().getLineNumber()); - assertEquals(VALID_LINE, optOutInformation.get().getText()); - assertEquals(MBI, optOutInformation.get().getMbi()); - assertTrue(optOutInformation.get().isOptOut()); + @AfterEach + void afterEach() { + S3MockAPIExtension.deleteFile(TEST_FILE_NAME); } @Test - void createOptOutInformationValidTest2() { - Optional optOutInformation = optOutProcessor.createOptOutInformation(VALID_LINE.substring(0, VALID_LINE.length() - 1) + 'N', 1L); - assertTrue(optOutInformation.isPresent()); - assertFalse(optOutInformation.get().isOptOut()); + void processTest(){ + optOutProcessing.isRejected = false; + optOutProcessing.process(); + assertEquals(7, optOutProcessing.optOutInformationMap.size()); } @Test - void optOutResultMapValidTest() { - optOutProcessor.createOptOutInformation(VALID_LINE, 1L); - assertEquals(1, optOutProcessor.optOutResultMap.size()); - OptOutResult mapValue = optOutProcessor.optOutResultMap.get(1L); - assertEquals(ReasonCode.ACCEPTED, mapValue.getReasonCode()); - assertEquals(RecordStatus.ACCEPTED, mapValue.getRecordStatus()); + void createTrueOptOutInformationTest() { + var optOutInformation = optOutProcessing.createOptOutInformation(validLine('Y')); + assertEquals(MBI, optOutInformation.getMbi()); + assertTrue(optOutInformation.getOptOutFlag()); } @Test - void createOptOutInformationInvalidTest() { - var optOutInformation = optOutProcessor.createOptOutInformation(INVALID_LINE, 0L); - assertFalse(optOutInformation.isPresent()); + void createFalseOptOutInformationTest() { + var optOutInformation = optOutProcessing.createOptOutInformation(validLine('N')); + assertEquals(MBI, optOutInformation.getMbi()); + assertFalse(optOutInformation.getOptOutFlag()); } @Test - void optOutResultMapParseErrorTest() { - optOutProcessor.createOptOutInformation(INVALID_LINE, 0L); - assertEquals(1, optOutProcessor.optOutResultMap.size()); - var mapValue = optOutProcessor.optOutResultMap.get(0L); - assertEquals(ReasonCode.PARSE_ERROR, mapValue.getReasonCode()); - assertEquals(RecordStatus.REJECTED, mapValue.getRecordStatus()); + void createAcceptedResponseTest() { + optOutProcessing.optOutInformationMap.put(1L, new OptOutInformation(MBI, true)); + var expectedLine = MBI + DATE + "Y" + RecordStatus.ACCEPTED; + var expectedText = AB2D_HEADER_CONF + DATE + LINE_SEPARATOR + + expectedLine + LINE_SEPARATOR + + AB2D_TRAILER_CONF + DATE + TRAILER_COUNT; + assertEquals(expectedText, optOutProcessing.createResponseContent()); } @Test - void createResponseOptOutContentTest() { - optOutProcessor.createOptOutInformation(VALID_LINE, 1L); - assertEquals(EXPECTED_ACCEPTED_LINE, optOutProcessor.createResponseContent()); + void createRejectedResponseTest() { + optOutProcessing.isRejected = true; + optOutProcessing.optOutInformationMap.put(1L, new OptOutInformation(MBI, false)); + var expectedLine = MBI + " " + "N" + RecordStatus.REJECTED; + var expectedText = AB2D_HEADER_CONF + DATE + LINE_SEPARATOR + + expectedLine + LINE_SEPARATOR + + AB2D_TRAILER_CONF + DATE + TRAILER_COUNT; + assertEquals(expectedText, optOutProcessing.createResponseContent()); } @Test - void createMultipleResponseOptOutContentTest() throws ParseException { - optOutProcessor.optOutResultMap.put(0L, new OptOutResult(new OptOutInformation(0L, INVALID_LINE), RecordStatus.REJECTED, ReasonCode.PARSE_ERROR)); - optOutProcessor.optOutResultMap.put(1L, new OptOutResult(new OptOutInformation(MBI, getTestTimestamp(), true, 1L, VALID_LINE), RecordStatus.ACCEPTED, ReasonCode.ACCEPTED)); - optOutProcessor.optOutResultMap.put(2L, new OptOutResult(new OptOutInformation(2L, INVALID_LINE), RecordStatus.REJECTED, ReasonCode.PARSE_ERROR)); - var expectedText = INVALID_LINE + LINE_SEPARATOR - + EXPECTED_ACCEPTED_LINE + LINE_SEPARATOR - + INVALID_LINE; - assertEquals(expectedText, optOutProcessor.createResponseContent()); + void updateOptOutTest() { + var optOutInformation = optOutProcessing.createOptOutInformation(validLine('Y')); + optOutProcessing.updateOptOut(1L, optOutInformation, dbConnection); + assertFalse(optOutProcessing.isRejected); } @Test - void updateOptOutTest() { - Optional optOutInformation = optOutProcessor.createOptOutInformation(VALID_LINE, 1L); - assertTrue(optOutInformation.isPresent()); - optOutProcessor.updateOptOut(optOutInformation.get(), dbConnection); - assertEquals(1, optOutProcessor.optOutResultMap.size()); - OptOutResult mapValue = optOutProcessor.optOutResultMap.get(1L); - assertEquals(ReasonCode.ACCEPTED, mapValue.getReasonCode()); - assertEquals(RecordStatus.ACCEPTED, mapValue.getRecordStatus()); + void updateOptOutExceptionTest() throws SQLException { + var optOutInformation = optOutProcessing.createOptOutInformation(validLine('Y')); + when(dbConnection.prepareStatement(anyString())).thenThrow(SQLException.class); + optOutProcessing.updateOptOut(1L, optOutInformation, dbConnection); + // Insertion error exists + assertTrue(optOutProcessing.isRejected); + assertTrue(S3MockAPIExtension.isObjectExists(TEST_FILE_NAME)); } @Test - void updateOptOutInvalidTest() throws SQLException { - Optional optOutInformation = optOutProcessor.createOptOutInformation(VALID_LINE, 1L); - when(dbConnection.prepareStatement(anyString())).thenThrow(SQLException.class); - assertTrue(optOutInformation.isPresent()); - optOutProcessor.updateOptOut(optOutInformation.get(), dbConnection); - assertEquals(1, optOutProcessor.optOutResultMap.size()); - OptOutResult mapValue = optOutProcessor.optOutResultMap.get(1L); - assertEquals(ReasonCode.INSERT_ERROR, mapValue.getReasonCode()); - assertEquals(RecordStatus.REJECTED, mapValue.getRecordStatus()); + void getEffectiveDateTest() { + optOutProcessing.isRejected = false; + assertEquals(DATE, optOutProcessing.getEffectiveDate(DATE)); + optOutProcessing.isRejected = true; + assertEquals(" ", optOutProcessing.getEffectiveDate(DATE)); } - private Timestamp getTestTimestamp() throws ParseException { - var dateFormat = new SimpleDateFormat(EFFECTIVE_DATE_PATTERN); - var date = dateFormat.parse("20230726"); - var time = date.getTime(); - return new Timestamp(time); + @Test + void getRecordStatusTest() { + optOutProcessing.isRejected = false; + assertEquals(RecordStatus.ACCEPTED.toString(), optOutProcessing.getRecordStatus()); + optOutProcessing.isRejected = true; + assertEquals(RecordStatus.REJECTED.toString(), optOutProcessing.getRecordStatus()); } } \ No newline at end of file diff --git a/optout/src/test/resources/optOutDummy.txt b/optout/src/test/resources/optOutDummy.txt index 25db240..0f866ab 100644 --- a/optout/src/test/resources/optOutDummy.txt +++ b/optout/src/test/resources/optOutDummy.txt @@ -1,4 +1,9 @@ -HDR_BENEDATARSP20230726 -DUMMY000001 NAME LASTNAME 111 DUMMY ADDRESS TESTDATA DUMMY11DUMMY20230726202307261-800TY -DUMMY000002 NAME A LASTNAME 222 DUMMY ADDRESS TESTDATA DUMMY22DUMMY20230726202307261-800TN -TRL_BENEDATARSP202307260000000002 \ No newline at end of file +HDR_BENEDATARSP20240123 +DUMMY000001N +DUMMY000002N +DUMMY000003Y +DUMMY000004N +DUMMY000005N +DUMMY000006Y +DUMMY000007N +TLR_BENEDATARSP202401230000000007 \ No newline at end of file