Program: 25744
Project: 25744
Buggy source:
@Test public void test() throws Exception {
  String dataJsonText="String_Node_Str" + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str";
  JsonNode dataJson=BridgeObjectMapper.get().readTree(dataJsonText);
  HealthDataRecord intermediateRecord=createValidRecordBuilder(dataJson).withId(TEST_RECORD_ID).build();
  HealthDataService mockHealthDataService=mock(HealthDataService.class);
  ArgumentCaptor<HealthDataRecord> createRecordArgCaptor=ArgumentCaptor.forClass(HealthDataRecord.class);
  when(mockHealthDataService.createOrUpdateRecord(createRecordArgCaptor.capture())).thenReturn(TEST_RECORD_ID);
  ArgumentCaptor<HealthDataAttachment> createAttachmentArgCaptor=ArgumentCaptor.forClass(HealthDataAttachment.class);
  when(mockHealthDataService.createOrUpdateAttachment(createAttachmentArgCaptor.capture())).thenReturn(ATTACHMENT_ID_BAR,ATTACHMENT_ID_FOO);
  when(mockHealthDataService.getRecordById(TEST_RECORD_ID)).thenReturn(intermediateRecord);
  when(mockHealthDataService.getAttachmentBuilder()).thenAnswer(new Answer<HealthDataAttachmentBuilder>(){
    @Override public HealthDataAttachmentBuilder answer(    InvocationOnMock invocation){
      return new DynamoHealthDataAttachment.Builder();
    }
  }
);
  when(mockHealthDataService.getRecordBuilder()).thenAnswer(new Answer<HealthDataRecordBuilder>(){
    @Override public HealthDataRecordBuilder answer(    InvocationOnMock invocation){
      return new DynamoHealthDataRecord.Builder();
    }
  }
);
  S3Helper mockS3Helper=mock(S3Helper.class);
  UploadArtifactsHandler handler=new UploadArtifactsHandler();
  handler.setHealthDataService(mockHealthDataService);
  handler.setS3Helper(mockS3Helper);
  Map<String,byte[]> attachmentMap=new TreeMap<>();
  attachmentMap.put("String_Node_Str","String_Node_Str".getBytes(Charsets.UTF_8));
  attachmentMap.put("String_Node_Str","String_Node_Str".getBytes(Charsets.UTF_8));
  HealthDataRecordBuilder recordBuilder=createValidRecordBuilder(dataJson);
  DynamoUpload2 upload=new DynamoUpload2();
  upload.setUploadId(TEST_UPLOAD_ID);
  UploadValidationContext context=new UploadValidationContext();
  context.setAttachmentsByFieldName(attachmentMap);
  context.setHealthDataRecordBuilder(recordBuilder);
  context.setUpload(upload);
  handler.handle(context);
  List<HealthDataRecord> createRecordArgList=createRecordArgCaptor.getAllValues();
  assertEquals(2,createRecordArgList.size());
  HealthDataRecord createIntermediateRecordArg=createRecordArgList.get(0);
  JsonNode createIntermediateRecordDataJson=createIntermediateRecordArg.getData();
  assertEquals(2,createIntermediateRecordDataJson.size());
  assertEquals("String_Node_Str",createIntermediateRecordDataJson.get("String_Node_Str").textValue());
  assertEquals(42,createIntermediateRecordDataJson.get("String_Node_Str").intValue());
  HealthDataRecord createFinalRecordArg=createRecordArgList.get(1);
  assertEquals(TEST_RECORD_ID,createFinalRecordArg.getId());
  JsonNode createFinalRecordDataJson=createFinalRecordArg.getData();
  assertEquals(4,createFinalRecordDataJson.size());
  assertEquals("String_Node_Str",createFinalRecordDataJson.get("String_Node_Str").textValue());
  assertEquals(42,createFinalRecordDataJson.get("String_Node_Str").intValue());
  assertEquals(ATTACHMENT_ID_BAR,createFinalRecordDataJson.get("String_Node_Str").textValue());
  assertEquals(ATTACHMENT_ID_FOO,createFinalRecordDataJson.get("String_Node_Str").textValue());
  List<HealthDataAttachment> createAttachmentArgList=createAttachmentArgCaptor.getAllValues();
  assertEquals(2,createAttachmentArgList.size());
  for (  HealthDataAttachment oneAttachment : createAttachmentArgList) {
    assertEquals(TEST_RECORD_ID,oneAttachment.getRecordId());
  }
  verify(mockS3Helper).writeBytesToS3(TestConstants.ATTACHMENT_BUCKET,ATTACHMENT_ID_BAR,BYTES_BAR);
  verify(mockS3Helper).writeBytesToS3(TestConstants.ATTACHMENT_BUCKET,ATTACHMENT_ID_FOO,BYTES_FOO);
  assertEquals(TEST_RECORD_ID,context.getRecordId());
  assertTrue(context.getMessageList().isEmpty());
}

Fixed version (ground truth):
@Test public void test() throws Exception {
  String dataJsonText="String_Node_Str" + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str";
  JsonNode dataJson=BridgeObjectMapper.get().readTree(dataJsonText);
  HealthDataRecord intermediateRecord=createValidRecordBuilder(dataJson).withId(TEST_RECORD_ID).build();
  HealthDataService mockHealthDataService=mock(HealthDataService.class);
  ArgumentCaptor<HealthDataRecord> createRecordArgCaptor=ArgumentCaptor.forClass(HealthDataRecord.class);
  when(mockHealthDataService.createOrUpdateRecord(createRecordArgCaptor.capture())).thenReturn(TEST_RECORD_ID);
  ArgumentCaptor<HealthDataAttachment> createAttachmentArgCaptor=ArgumentCaptor.forClass(HealthDataAttachment.class);
  when(mockHealthDataService.createOrUpdateAttachment(createAttachmentArgCaptor.capture())).thenReturn(ATTACHMENT_ID_BAR,ATTACHMENT_ID_FOO);
  when(mockHealthDataService.getRecordById(TEST_RECORD_ID)).thenReturn(intermediateRecord);
  when(mockHealthDataService.getAttachmentBuilder()).thenAnswer(invocation -> new DynamoHealthDataAttachment.Builder());
  when(mockHealthDataService.getRecordBuilder()).thenAnswer(invocation -> new DynamoHealthDataRecord.Builder());
  S3Helper mockS3Helper=mock(S3Helper.class);
  UploadArtifactsHandler handler=new UploadArtifactsHandler();
  handler.setHealthDataService(mockHealthDataService);
  handler.setS3Helper(mockS3Helper);
  Map<String,byte[]> attachmentMap=new TreeMap<>();
  attachmentMap.put("String_Node_Str","String_Node_Str".getBytes(Charsets.UTF_8));
  attachmentMap.put("String_Node_Str","String_Node_Str".getBytes(Charsets.UTF_8));
  HealthDataRecordBuilder recordBuilder=createValidRecordBuilder(dataJson);
  DynamoUpload2 upload=new DynamoUpload2();
  upload.setUploadId(TEST_UPLOAD_ID);
  UploadValidationContext context=new UploadValidationContext();
  context.setAttachmentsByFieldName(attachmentMap);
  context.setHealthDataRecordBuilder(recordBuilder);
  context.setUpload(upload);
  handler.handle(context);
  List<HealthDataRecord> createRecordArgList=createRecordArgCaptor.getAllValues();
  assertEquals(2,createRecordArgList.size());
  HealthDataRecord createIntermediateRecordArg=createRecordArgList.get(0);
  assertEquals("String_Node_Str",createIntermediateRecordArg.getHealthCode());
  assertNull(createIntermediateRecordArg.getId());
  assertEquals(ARBITRARY_TIMESTAMP,createIntermediateRecordArg.getCreatedOn().longValue());
  assertEquals("String_Node_Str",createIntermediateRecordArg.getSchemaId());
  assertEquals(1,createIntermediateRecordArg.getSchemaRevision());
  assertEquals("String_Node_Str",createIntermediateRecordArg.getStudyId());
  assertEquals("String_Node_Str",createIntermediateRecordArg.getUploadDate().toString(ISODateTimeFormat.date()));
  assertEquals(TEST_UPLOAD_ID,createIntermediateRecordArg.getUploadId());
  assertEquals("String_Node_Str",createIntermediateRecordArg.getUserExternalId());
  assertEquals(ParticipantOption.SharingScope.SPONSORS_AND_PARTNERS,createIntermediateRecordArg.getUserSharingScope());
  assertEquals(42,createIntermediateRecordArg.getVersion().longValue());
  assertTrue(createIntermediateRecordArg.getMetadata().isObject());
  assertEquals(0,createIntermediateRecordArg.getMetadata().size());
  JsonNode createIntermediateRecordDataJson=createIntermediateRecordArg.getData();
  assertEquals(2,createIntermediateRecordDataJson.size());
  assertEquals("String_Node_Str",createIntermediateRecordDataJson.get("String_Node_Str").textValue());
  assertEquals(42,createIntermediateRecordDataJson.get("String_Node_Str").intValue());
  HealthDataRecord createFinalRecordArg=createRecordArgList.get(1);
  assertEquals("String_Node_Str",createFinalRecordArg.getHealthCode());
  assertEquals(TEST_RECORD_ID,createFinalRecordArg.getId());
  assertEquals(ARBITRARY_TIMESTAMP,createFinalRecordArg.getCreatedOn().longValue());
  assertEquals("String_Node_Str",createFinalRecordArg.getSchemaId());
  assertEquals(1,createFinalRecordArg.getSchemaRevision());
  assertEquals("String_Node_Str",createFinalRecordArg.getStudyId());
  assertEquals("String_Node_Str",createFinalRecordArg.getUploadDate().toString(ISODateTimeFormat.date()));
  assertEquals(TEST_UPLOAD_ID,createFinalRecordArg.getUploadId());
  assertEquals("String_Node_Str",createFinalRecordArg.getUserExternalId());
  assertEquals(ParticipantOption.SharingScope.SPONSORS_AND_PARTNERS,createFinalRecordArg.getUserSharingScope());
  assertEquals(42,createFinalRecordArg.getVersion().longValue());
  assertTrue(createFinalRecordArg.getMetadata().isObject());
  assertEquals(0,createFinalRecordArg.getMetadata().size());
  JsonNode createFinalRecordDataJson=createFinalRecordArg.getData();
  assertEquals(4,createFinalRecordDataJson.size());
  assertEquals("String_Node_Str",createFinalRecordDataJson.get("String_Node_Str").textValue());
  assertEquals(42,createFinalRecordDataJson.get("String_Node_Str").intValue());
  assertEquals(ATTACHMENT_ID_BAR,createFinalRecordDataJson.get("String_Node_Str").textValue());
  assertEquals(ATTACHMENT_ID_FOO,createFinalRecordDataJson.get("String_Node_Str").textValue());
  List<HealthDataAttachment> createAttachmentArgList=createAttachmentArgCaptor.getAllValues();
  assertEquals(2,createAttachmentArgList.size());
  for (  HealthDataAttachment oneAttachment : createAttachmentArgList) {
    assertEquals(TEST_RECORD_ID,oneAttachment.getRecordId());
  }
  verify(mockS3Helper).writeBytesToS3(TestConstants.ATTACHMENT_BUCKET,ATTACHMENT_ID_BAR,BYTES_BAR);
  verify(mockS3Helper).writeBytesToS3(TestConstants.ATTACHMENT_BUCKET,ATTACHMENT_ID_FOO,BYTES_FOO);
  assertEquals(TEST_RECORD_ID,context.getRecordId());
  assertTrue(context.getMessageList().isEmpty());
}