Skip to content

Commit

Permalink
AB2D-6008 Incident cleanup (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
smirnovaae authored Mar 4, 2024
1 parent 11d190b commit cfcff70
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

@Testcontainers
@ExtendWith({S3MockAPIExtension.class})
public class AttributionDataShareHelperTest {
public class AttributionDataShareTest {
@SuppressWarnings({"rawtypes", "unused"})
@Container
private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER = new AB2DPostgresqlContainer();
Expand All @@ -37,7 +37,7 @@ public class AttributionDataShareHelperTest {
String FILE_NAME = FILE_PARTIAL_NAME + new SimpleDateFormat(PATTERN).format(new Date()) + FILE_FORMAT;
String FILE_FULL_PATH = FILE_PATH + FILE_NAME;

String MBI = "1AA2BB3CC45";
String MBI = "DUMMY000001";
AttributionDataShareHelper helper;

@BeforeEach
Expand Down
6 changes: 3 additions & 3 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public void processSQSMessage(SQSEvent.SQSMessage msg, Context context) {
try {
logger.log("OptOut Lambda started. Processing message from SQS " + msg.getBody());

var optOutProcessing = processingInit(msg.getBody(), logger);
var optOutProcessing = processorInit(msg.getBody(), logger);
optOutProcessing.process();
} catch (Exception ex) {
logger.log("An error occurred");
throw new OptOutException("An error occurred", ex);
}
}

public OptOutProcessing processingInit(String msg, LambdaLogger logger) throws URISyntaxException {
return new OptOutProcessing(msg, ENDPOINT, logger);
public OptOutProcessor processorInit(String msg, LambdaLogger logger) throws URISyntaxException {
return new OptOutProcessor(msg, ENDPOINT, logger);
//ToDo: uncomment when permanent credentials will be available
// var creds = SecretManager.getS3Credentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY, ACCESS_TOKEN, logger);
// if (creds.isPresent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

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

public class OptOutProcessing {
public class OptOutProcessor {
private final LambdaLogger logger;
public SortedMap<Long, OptOutResult> optOutResultMap;
private final OptOutS3 optOutS3;

public OptOutProcessing(String fileName, String endpoint, LambdaLogger logger) throws URISyntaxException {
public OptOutProcessor(String fileName, String endpoint, LambdaLogger logger) throws URISyntaxException {
this.logger = logger;
this.optOutResultMap = new TreeMap<>();
var s3Client = S3Client.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public class OptOutHandlerTest {
@Container
private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER = new AB2DPostgresqlContainer();
private final static OptOutHandler handler = spy(new OptOutHandler());
private final static OptOutProcessing optOutProcessing = mock(OptOutProcessing.class);
private final static OptOutProcessor OPT_OUT_PROCESSOR = mock(OptOutProcessor.class);
private final static SQSEvent sqsEvent = mock(SQSEvent.class);
private final static SQSEvent.SQSMessage sqsMessage = mock(SQSEvent.SQSMessage.class);

@BeforeAll
static void beforeAll() throws URISyntaxException {
when(sqsEvent.getRecords()).thenReturn(Collections.singletonList(sqsMessage));
when(sqsMessage.getBody()).thenReturn("optOutDummy.txt");
when(handler.processingInit(anyString(), any(LambdaLogger.class))).thenReturn(optOutProcessing);
when(handler.processorInit(anyString(), any(LambdaLogger.class))).thenReturn(OPT_OUT_PROCESSOR);
}

@Test
Expand All @@ -48,7 +48,7 @@ void optOutHandlerInvoke() {

@Test
void optOutHandlerException() {
doThrow(new OptOutException("errorMessage", new AmazonS3Exception("errorMessage"))).when(optOutProcessing).process();
assertThrows(OptOutException.class, optOutProcessing::process);
doThrow(new OptOutException("errorMessage", new AmazonS3Exception("errorMessage"))).when(OPT_OUT_PROCESSOR).process();
assertThrows(OptOutException.class, OPT_OUT_PROCESSOR::process);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
import static org.mockito.Mockito.*;

@ExtendWith({S3MockAPIExtension.class})
public class OptOutProcessingTest {
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 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 OptOutProcessing optOutProcessing;
static OptOutProcessor optOutProcessor;

@BeforeAll
static void beforeAll() throws SQLException {
Expand All @@ -47,24 +47,24 @@ static void beforeAll() throws SQLException {
@BeforeEach
void beforeEach() throws URISyntaxException {
// var creds = StaticCredentialsProvider.create(AwsSessionCredentials.create("test", "test", ""));
optOutProcessing = spy(new OptOutProcessing(TEST_FILE_NAME, TEST_ENDPOINT, logger));
optOutProcessor = spy(new OptOutProcessor(TEST_FILE_NAME, TEST_ENDPOINT, logger));
}

@Test
void processTest() throws IOException {
S3MockAPIExtension.createFile(Files.readString(Paths.get("src/test/resources/" + TEST_FILE_NAME), StandardCharsets.UTF_8));
optOutProcessing.process();
assertEquals(4, optOutProcessing.optOutResultMap.size());
verify(optOutProcessing, times(4)).createOptOutInformation(anyString(), anyLong());
verify(optOutProcessing, times(2)).updateOptOut(any(OptOutInformation.class), any(Connection.class));
verify(optOutProcessing, times(1)).createResponseContent();
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));
}

@Test
void createOptOutInformationValidTest1() {
Optional<OptOutInformation> optOutInformation = optOutProcessing.createOptOutInformation(VALID_LINE, 1L);
Optional<OptOutInformation> optOutInformation = optOutProcessor.createOptOutInformation(VALID_LINE, 1L);
assertTrue(optOutInformation.isPresent());
assertEquals(1L, optOutInformation.get().getLineNumber());
assertEquals(VALID_LINE, optOutInformation.get().getText());
Expand All @@ -74,71 +74,71 @@ void createOptOutInformationValidTest1() {

@Test
void createOptOutInformationValidTest2() {
Optional<OptOutInformation> optOutInformation = optOutProcessing.createOptOutInformation(VALID_LINE.substring(0, VALID_LINE.length() - 1) + 'N', 1L);
Optional<OptOutInformation> optOutInformation = optOutProcessor.createOptOutInformation(VALID_LINE.substring(0, VALID_LINE.length() - 1) + 'N', 1L);
assertTrue(optOutInformation.isPresent());
assertFalse(optOutInformation.get().isOptOut());
}

@Test
void optOutResultMapValidTest() {
optOutProcessing.createOptOutInformation(VALID_LINE, 1L);
assertEquals(1, optOutProcessing.optOutResultMap.size());
OptOutResult mapValue = optOutProcessing.optOutResultMap.get(1L);
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());
}

@Test
void createOptOutInformationInvalidTest() {
var optOutInformation = optOutProcessing.createOptOutInformation(INVALID_LINE, 0L);
var optOutInformation = optOutProcessor.createOptOutInformation(INVALID_LINE, 0L);
assertFalse(optOutInformation.isPresent());
}

@Test
void optOutResultMapParseErrorTest() {
optOutProcessing.createOptOutInformation(INVALID_LINE, 0L);
assertEquals(1, optOutProcessing.optOutResultMap.size());
var mapValue = optOutProcessing.optOutResultMap.get(0L);
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());
}

@Test
void createResponseOptOutContentTest() {
optOutProcessing.createOptOutInformation(VALID_LINE, 1L);
assertEquals(EXPECTED_ACCEPTED_LINE, optOutProcessing.createResponseContent());
optOutProcessor.createOptOutInformation(VALID_LINE, 1L);
assertEquals(EXPECTED_ACCEPTED_LINE, optOutProcessor.createResponseContent());
}

@Test
void createMultipleResponseOptOutContentTest() throws ParseException {
optOutProcessing.optOutResultMap.put(0L, new OptOutResult(new OptOutInformation(0L, INVALID_LINE), RecordStatus.REJECTED, ReasonCode.PARSE_ERROR));
optOutProcessing.optOutResultMap.put(1L, new OptOutResult(new OptOutInformation(MBI, getTestTimestamp(), true, 1L, VALID_LINE), RecordStatus.ACCEPTED, ReasonCode.ACCEPTED));
optOutProcessing.optOutResultMap.put(2L, new OptOutResult(new OptOutInformation(2L, INVALID_LINE), RecordStatus.REJECTED, ReasonCode.PARSE_ERROR));
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, optOutProcessing.createResponseContent());
assertEquals(expectedText, optOutProcessor.createResponseContent());
}

@Test
void updateOptOutTest() {
Optional<OptOutInformation> optOutInformation = optOutProcessing.createOptOutInformation(VALID_LINE, 1L);
Optional<OptOutInformation> optOutInformation = optOutProcessor.createOptOutInformation(VALID_LINE, 1L);
assertTrue(optOutInformation.isPresent());
optOutProcessing.updateOptOut(optOutInformation.get(), dbConnection);
assertEquals(1, optOutProcessing.optOutResultMap.size());
OptOutResult mapValue = optOutProcessing.optOutResultMap.get(1L);
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());
}

@Test
void updateOptOutInvalidTest() throws SQLException {
Optional<OptOutInformation> optOutInformation = optOutProcessing.createOptOutInformation(VALID_LINE, 1L);
Optional<OptOutInformation> optOutInformation = optOutProcessor.createOptOutInformation(VALID_LINE, 1L);
when(dbConnection.prepareStatement(anyString())).thenThrow(SQLException.class);
assertTrue(optOutInformation.isPresent());
optOutProcessing.updateOptOut(optOutInformation.get(), dbConnection);
assertEquals(1, optOutProcessing.optOutResultMap.size());
OptOutResult mapValue = optOutProcessing.optOutResultMap.get(1L);
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());
}
Expand Down

0 comments on commit cfcff70

Please sign in to comment.