Skip to content

Commit

Permalink
update to count total opt ins and total opt outs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rwolfe-Nava committed Jun 11, 2024
1 parent 5c44bb1 commit d8f7edd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
8 changes: 5 additions & 3 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public class OptOutConstants {
public static final String UPDATE_STATEMENT = "UPDATE public.current_mbi\n" +
"SET opt_out_flag = ?, effective_date = current_date\n" +
"WHERE mbi = ?";
public static final String COUNT_STATEMENT = "SELECT \n"+
"COUNT(CASE WHEN opt_out_flag = 'true' THEN 1 END) AS optin, \n"+
"COUNT(CASE WHEN opt_out_flag = 'false' THEN 1 END) AS optout \n"+
"FROM current_mbi WHERE opt_out_flag IS NOT NULL";

public static final String COUNT_STATEMENT = "select count(*) AS total from current_mbi where opt_out_flag is not null";
private OptOutConstants() {
}
private OptOutConstants() {}
}
11 changes: 7 additions & 4 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ public void processSQSMessage(SQSEvent.SQSMessage msg, Context context) {
var optOutProcessing = processorInit(logger);
var optOutResults = optOutProcessing.process(getFileName(notification), getBucketName(notification), ENDPOINT);
if (optOutResults != null) {
logger.log("OptOut Lambda completed. Total records processed today=" + optOutResults.getTotalToday()
+ " Number of opt in=" + optOutResults.getOptInToday()
+ " Number of opt out=" + optOutResults.getOptOutToday()
+ " Total records processed to date=" + optOutResults.getTotalFromDB());
logger.log("OptOut Lambda completed. Total records processed today is: totaltoday=" + optOutResults.getTotalToday()
+ " Number of opt in today is: todayin=" + optOutResults.getOptInToday()
+ " Number of opt out today is: todayout=" + optOutResults.getOptOutToday()
+ " Total records processed to date is: totaltodate=" + optOutResults.getTotalAllTime()
+ " Total number of opt in is: totalin=" + optOutResults.getOptInTotal()
+ " Total number of opt out is: totalout=" + optOutResults.getOptOutTotal()
);
}
} catch (Exception ex) {
logger.log("An error occurred");
Expand Down
9 changes: 6 additions & 3 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,16 @@ public String createResponseContent() {
}

public OptOutResults getOptOutResults() {
int totalFromDb = 0;
int totalOptedIn = 0;
int totalOptedOut = 0;

try (var dbConnection = DriverManager.getConnection(parameterStore.getDbHost(), parameterStore.getDbUser(), parameterStore.getDbPassword());
var statement = dbConnection.createStatement();
ResultSet rs = statement.executeQuery(COUNT_STATEMENT)
) {
while (rs.next()) {
totalFromDb = rs.getInt("total");
totalOptedIn = rs.getInt("optin");
totalOptedOut = rs.getInt("optout");
}

int numberOptedIn = 0;
Expand All @@ -154,7 +157,7 @@ public OptOutResults getOptOutResults() {
numberOptedOut++;
}
}
return new OptOutResults(totalFromDb, numberOptedIn, numberOptedOut);
return new OptOutResults(numberOptedIn, numberOptedOut, totalOptedIn, totalOptedOut);
} catch (SQLException ex) {
logger.log("There is an error " + ex.getMessage());
}
Expand Down
24 changes: 17 additions & 7 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

public class OptOutResults {

private final int totalFromDB;
private final int optInToday;
private final int optOutToday;
private final int optInTotal;
private final int optOutTotal;

public OptOutResults(int totalFromDB, int optInToday, int optOutToday) {
this.totalFromDB = totalFromDB;
public OptOutResults(int optInToday, int optOutToday, int optInTotal, int optOutTotal) {
this.optInToday = optInToday;
this.optOutToday = optOutToday;
}

public int getTotalFromDB() {
return totalFromDB;
this.optInTotal = optInTotal;
this.optOutTotal = optOutTotal;
}

public int getOptInToday() {
Expand All @@ -28,4 +26,16 @@ public int getTotalToday() {
return optInToday + optOutToday;
}

public int getOptInTotal() {
return optInTotal;
}

public int getOptOutTotal() {
return optOutTotal;
}

public int getTotalAllTime() {
return optInTotal + optOutTotal;
}

}
30 changes: 21 additions & 9 deletions optout/src/test/java/gov/cms/ab2d/optout/OptOutProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class OptOutProcessorTest {
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";
private static final int TEST_TOTAL_RESULT_COUNT = 7;
private static String validLine(char isOptOut) {
return MBI + isOptOut;
}
Expand All @@ -51,8 +50,6 @@ static void beforeAll() throws SQLException {
when(dbConnection.prepareStatement(anyString())).thenReturn(preparedStatement);
when(dbConnection.createStatement()).thenReturn(preparedStatement);
when(preparedStatement.executeQuery(anyString())).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true).thenReturn(false);
when(resultSet.getInt(any())).thenReturn(TEST_TOTAL_RESULT_COUNT);
}

@BeforeEach
Expand All @@ -73,9 +70,10 @@ void processTest() throws URISyntaxException {
optOutProcessing.isRejected = false;
OptOutResults results = optOutProcessing.process(TEST_FILE_NAME, TEST_BFD_BUCKET_NAME, TEST_ENDPOINT);
assertEquals(7, optOutProcessing.optOutInformationList.size());
assertEquals(7, results.getTotalToday());
assertEquals(4, results.getOptOutToday());

assertEquals(3, results.getOptInToday());
assertEquals(4, results.getOptOutToday());
assertEquals(7, results.getTotalToday());
}

@Test
Expand Down Expand Up @@ -155,12 +153,26 @@ void getRecordStatusTest() {
}

@Test
void getOptOutResultsTest() {
void getOptOutResultsTest() throws SQLException {
final String OPT_IN_RESULTSET_STRING = "optin";
final String OPT_OUT_RESULTSET_STRING = "optout";

final int OPT_IN_TOTAL = 9;
final int OPT_OUT_TOTAL = 7;

when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
when(resultSet.getInt(OPT_IN_RESULTSET_STRING)).thenReturn(OPT_IN_TOTAL);
when(resultSet.getInt(OPT_OUT_RESULTSET_STRING)).thenReturn(OPT_OUT_TOTAL);

optOutProcessing.optOutInformationList.add(new OptOutInformation(MBI, true));
optOutProcessing.optOutInformationList.add(new OptOutInformation("DUMMY000002", false));

OptOutResults results = optOutProcessing.getOptOutResults();
assertNotNull(results);
System.out.println("Results = " + results);
System.out.println("ResultsFromDB = " + results.getTotalFromDB());
assertEquals(TEST_TOTAL_RESULT_COUNT, results.getTotalFromDB());;
assertEquals(1, results.getOptInToday());
assertEquals(1, results.getOptOutToday());
assertEquals(OPT_IN_TOTAL, results.getOptInTotal());
assertEquals(OPT_OUT_TOTAL, results.getOptOutTotal());
}

}

0 comments on commit d8f7edd

Please sign in to comment.