@Test
  public void deleteLogsByVersion() {
    KieSession kieSession = createKSession(HELLO_WORLD_PROCESS);
    startProcess(kieSession, HELLO_WORLD_PROCESS_ID, 7);
    disposeRuntimeManager();
    kieSession = createKSession(HELLO_WORLD_PROCESS2);
    startProcess(kieSession, HELLO_WORLD_PROCESS2_ID, 2);

    // Delete all logs of version 1.1
    ProcessInstanceLogDeleteBuilder deleteBuilder =
        auditService.processInstanceLogDelete().processVersion("1.0");
    int deleteResult = deleteBuilder.build().execute();
    Assertions.assertThat(deleteResult).isEqualTo(7);

    // Make sure that the 1.1 version logs are gone
    List<ProcessInstanceLog> resultList =
        auditService.processInstanceLogQuery().processVersion("1.0").build().getResultList();
    Assertions.assertThat(resultList).hasSize(0);

    // Now check that 1.0 version logs are present
    resultList =
        auditService.processInstanceLogQuery().processVersion("1.1").build().getResultList();
    Assertions.assertThat(resultList).hasSize(2);
    Assertions.assertThat(resultList).extracting("processVersion").containsExactly("1.1", "1.1");
  }
  @Test
  public void deleteLogsByProcessName() {
    KieSession kieSession = createKSession(HELLO_WORLD_PROCESS);

    startProcess(kieSession, HELLO_WORLD_PROCESS_ID, 2);

    // Check that all records are created.
    List<ProcessInstanceLog> resultList =
        auditService
            .processInstanceLogQuery()
            .processId(HELLO_WORLD_PROCESS_ID)
            .processVersion("1.0")
            .build()
            .getResultList();
    Assertions.assertThat(resultList).hasSize(2);
    Assertions.assertThat(resultList)
        .extracting("processName")
        .containsExactly(HELLO_WORLD_P1NAME, HELLO_WORLD_P1NAME);

    // Perform delete
    int resultCount =
        auditService.processInstanceLogDelete().processName(HELLO_WORLD_P1NAME).build().execute();
    Assertions.assertThat(resultCount).isEqualTo(2);

    // Check that all records are gone
    resultList =
        auditService
            .processInstanceLogQuery()
            .processId(HELLO_WORLD_PROCESS_ID)
            .processVersion("1.0")
            .build()
            .getResultList();
    Assertions.assertThat(resultList).hasSize(0);
  }
 @Override
 public void tearDown() throws Exception {
   try {
     auditService.clear();
     auditService.dispose();
   } finally {
     super.tearDown();
   }
 }
  @Test
  @BZ("1188702")
  public void deleteLogsByDate() {
    Date testStartDate = new Date();

    KieSession kieSession = createKSession(HELLO_WORLD_PROCESS);

    startProcess(kieSession, HELLO_WORLD_PROCESS_ID, 4);

    List<ProcessInstanceLog> resultList =
        auditService
            .processInstanceLogQuery()
            .startDateRangeStart(testStartDate)
            .build()
            .getResultList();
    Assertions.assertThat(resultList)
        .hasSize(4)
        .extracting("processId")
        .containsExactly(
            HELLO_WORLD_PROCESS_ID,
            HELLO_WORLD_PROCESS_ID,
            HELLO_WORLD_PROCESS_ID,
            HELLO_WORLD_PROCESS_ID);

    // Delete the last 3 logs in the list
    for (int i = resultList.size() - 1; i > 0; i--) {
      int resultCount =
          auditService
              .processInstanceLogDelete()
              .startDate(resultList.get(i).getStart())
              .build()
              .execute();
      Assertions.assertThat(resultCount).isEqualTo(1);
    }

    // Attempt to delete with a date later than end of all the instances
    int resultCount =
        auditService.processInstanceLogDelete().startDate(new Date()).build().execute();
    Assertions.assertThat(resultCount).isEqualTo(0);

    // Check the last instance
    List<ProcessInstanceLog> resultList2 =
        auditService
            .processInstanceLogQuery()
            .startDateRangeStart(testStartDate)
            .build()
            .getResultList();
    Assertions.assertThat(resultList2).hasSize(1);
    Assertions.assertThat(resultList2.get(0)).isEqualTo(resultList.get(0));
  }
 private int getProcessInstanceLogSize(Date startDateRangeStart, Date startDateRangeEnd) {
   return auditService
       .processInstanceLogQuery()
       .startDateRangeStart(startDateRangeStart)
       .startDateRangeEnd(startDateRangeEnd)
       .build()
       .getResultList()
       .size();
 }
  @Test
  public void deleteLogsWithStatusActive() {
    KieSession kieSession = null;
    List<ProcessInstance> instanceList1 = null;
    List<ProcessInstance> instanceList2 = null;

    try {
      kieSession = createKSession(HELLO_WORLD_PROCESS, HUMAN_TASK);
      instanceList1 = startProcess(kieSession, HELLO_WORLD_PROCESS_ID, 3);
      instanceList2 = startProcess(kieSession, HUMAN_TASK_ID, 5);

      ProcessInstanceLogDeleteBuilder deleteBuilder =
          auditService.processInstanceLogDelete().status(ProcessInstance.STATE_ACTIVE);
      int deleteResult = deleteBuilder.build().execute();
      Assertions.assertThat(deleteResult).isEqualTo(5);

      ProcessInstanceLogQueryBuilder queryBuilder =
          auditService.processInstanceLogQuery().status(ProcessInstance.STATE_COMPLETED);
      List<ProcessInstanceLog> queryList = queryBuilder.build().getResultList();

      Assertions.assertThat(queryList).hasSize(3);
      Assertions.assertThat(queryList)
          .extracting("processId")
          .containsExactly(HELLO_WORLD_PROCESS_ID, HELLO_WORLD_PROCESS_ID, HELLO_WORLD_PROCESS_ID);
      Assertions.assertThat(queryList)
          .extracting("processVersion")
          .containsExactly("1.0", "1.0", "1.0");
      Assertions.assertThat(queryList)
          .extracting("status")
          .containsExactly(
              ProcessInstance.STATE_COMPLETED,
              ProcessInstance.STATE_COMPLETED,
              ProcessInstance.STATE_COMPLETED);
    } finally {
      if (instanceList2 != null) {
        abortProcess(kieSession, instanceList2);
      }
      if (kieSession != null) {
        kieSession.dispose();
      }
    }
  }
  @Test
  @BZ("1192498")
  public void deleteLogsByDateRange() throws InterruptedException {
    KieSession kieSession =
        createKSession(PARENT_PROCESS_CALLER, PARENT_PROCESS_INFO, HELLO_WORLD_PROCESS);

    Date date1 = new Date();
    startProcess(kieSession, PARENT_PROCESS_CALLER_ID, 4);
    Date date2 = new Date();
    Thread.sleep(1000);
    startProcess(kieSession, HELLO_WORLD_PROCESS_ID, 2);
    Date date3 = new Date();

    int beforeSize = getProcessInstanceLogSize(getYesterday(), getTomorrow());
    Assertions.assertThat(beforeSize).isEqualTo(10);

    int resultCount =
        auditService
            .processInstanceLogDelete()
            .startDateRangeStart(date1)
            .startDateRangeEnd(date2)
            .build()
            .execute();
    // 1 for ReussableSubprocess and 1 for ParentProcessInfo called by it
    Assertions.assertThat(resultCount).isEqualTo(8);

    List<ProcessInstanceLog> resultList =
        auditService.processInstanceLogQuery().startDateRangeEnd(date3).build().getResultList();
    Assertions.assertThat(resultList)
        .hasSize(2)
        .extracting("processId")
        .containsExactly(HELLO_WORLD_PROCESS_ID, HELLO_WORLD_PROCESS_ID);

    int afterSize = getProcessInstanceLogSize(date1, date3);
    Assertions.assertThat(afterSize).isEqualTo(2);
  }
  private void deleteLogsByDateRange(Date startDate, Date endDate, boolean expectRemoval) {
    KieSession kieSession = createKSession(HELLO_WORLD_PROCESS);

    startProcess(kieSession, HELLO_WORLD_PROCESS_ID, 2);

    int beforeSize = getProcessInstanceLogSize(getYesterday(), getTomorrow());
    Assertions.assertThat(beforeSize).isEqualTo(2);

    int resultCount =
        auditService
            .processInstanceLogDelete()
            .startDateRangeStart(startDate)
            .startDateRangeEnd(endDate)
            .build()
            .execute();
    // 1 for ReussableSubprocess and 1 for ParentProcessInfo called by it
    Assertions.assertThat(resultCount).isEqualTo(expectRemoval ? 2 : 0);

    int afterSize = getProcessInstanceLogSize(getYesterday(), getTomorrow());
    Assertions.assertThat(afterSize).isEqualTo(expectRemoval ? 0 : 2);
  }
 @Override
 public void setUp() throws Exception {
   super.setUp();
   auditService = new JPAAuditLogService(getEmf());
   auditService.clear();
 }