Я написал тестовый пример для проверки класса StateMachineExecutorManager
, но он выдает следующие ошибки:
Требуется, но не вызывается: com.amazon.heimdalltttingestionservicelambda.utils.StepFunctionUtility.startExecution (StepFunctionUtility. java: 43) На самом деле с этим макетом не было взаимодействий.
Код юнит-теста:
@RunWith(MockitoJUnitRunner.class)
public class StateMachineExecutorTest extends BaseTester {
private static final String EVENTS = "{\"Records\":[{\"eventVersion\":\"2.0\",\"eventSource\":\"aws:s3\",\"awsRegion\":\"us-east-1\",\"eventTime\":\"2018-11-02T14:02:43.283Z\",\"eventName\":\"ObjectCreated:Put\",\"userIdentity\":{\"principalId\":\"AWS:AIDAIMIQPLFXDFSTWXCGM\"},\"requestParameters\":{\"sourceIPAddress\":\"10.4.50.122\"},\"responseElements\":{\"x-amz-request-id\":\"7EEC615C11B5C373\",\"x-amz-id-2\":\"GCI+e/xr+vKZjnCy3IpUJIn9XrIEzDdiZOTBKXzsqgbwqFDlZ91gGKWj8d0W/UbUdqoWYCuf1lw=\"},\"s3\":{\"s3SchemaVersion\":\"1.0\",\"configurationId\":\"InboundManifestSQSNotify\",\"bucket\":{\"name\":\"test-bucket\",\"ownerIdentity\":{\"principalId\":\"A1JUPHJ1SEYN6S\"},\"arn\":\"arn:aws:s3:::ftpayroll-test\"},\"object\":{\"key\":\"inbound/lenel_badge/testfile.csv\",\"size\":4960,\"eTag\":\"df3a71421cc268356754c9069ca9d517\",\"sequencer\":\"005BDC59032AAAD3EE\"}}}]}\n" +
" ";
StateMachineExecutor stateMachineExecutor;
StepFunctionUtility stepFunctionUtility;
@Before
public void setup() {
super.setup();
this.stateMachineExecutor = new StateMachineExecutor(testServiceComponent.stateMachineExecutorManager());
this.stepFunctionUtility = mock(StepFunctionUtility.class);
}
@Test
public void testSuccess(){
S3Event s3Event = getS3Event();
try{
stateMachineExecutor.handleRequest(s3Event, mockContext);
verify(this.stepFunctionUtility, times(1)).startExecution(any(IngestionWorkflowMetadata.class));
}catch(Exception exception){
failTheTest();
}
}
private S3Event getS3Event(){
S3EventNotification s3EventNotification = S3EventNotification.parseJson(EVENTS);
S3Event s3Event = new S3Event(s3EventNotification.getRecords());
return s3Event;
}
}
Класс для тестирования:
@AllArgsConstructor
public class StateMachineExecutorManager{
StepFunctionUtility stepFunctionUtility;
public Void handleRequest(final S3Event s3Event) {
final S3Entity s3Entity = S3Utility.getS3Entity(s3Event);
IngestionWorkflowMetadata executionInputs = generateStepFunctionExecutionInput(s3Entity);
Environment.log(String.format("Received trigger for - Bucket: %s, Key: %s",
s3Entity.getBucket().getName(), s3Entity.getObject().getKey()));
try{
stepFunctionUtility.startExecution(executionInputs);
}catch (JsonProcessingException jsonProcessingException){
Environment.log(String.format("[%s]: Error in JSON Parsing", LogMetricKeys.JSON_PARSING_FAILED));
throw new RuntimeException(jsonProcessingException);
}
return null;
}
private IngestionWorkflowMetadata generateStepFunctionExecutionInput(final S3Entity s3Entity){
final IngestionWorkflowMetadata executionInput = new IngestionWorkflowMetadata();
try{
executionInput.setBucketName(s3Entity.getBucket().getName());
// S3 keys use URL encoding, e.g. '=' becomes '%3D'
executionInput.setFileKey(S3Utility.decodeS3Key(s3Entity));
executionInput.setExecutionId(generateStepFunctionExecutionId());
} catch (UnsupportedEncodingException unSupportedEncodingException){
Environment.log("[%s]: Error in decoding S3 file key: [%s]",
LogMetricKeys.S3_FILE_KEY_DECODING_FAILURE_, s3Entity.getObject().getKey());
throw new RuntimeException(unSupportedEncodingException);
}
return executionInput;
}
private String generateStepFunctionExecutionId() {
final String uuid = UUID.randomUUID().toString();
return String.format("%s_%s", uuid, Instant.now().toEpochMilli());
}
}