@Test
 public void testDeleteLastAttendanceEvent() {
   String attendanceRecordId = insertAttendanceEventData(true);
   Entity deleteAssessment =
       createDeleteAttendanceEntity(
           "Missed school bus", "Tardy", "2011-10-26", attendanceRecordId);
   CascadeResult deleteResult2 =
       repository.safeDelete(
           deleteAssessment, attendanceRecordId, false, false, false, false, 1, null);
   Entity attendanceRecord = getAttendanceRecord(attendanceRecordId);
   Assert.assertEquals(
       "Delete result should be success", CascadeResult.Status.SUCCESS, deleteResult2.getStatus());
   Assert.assertNull("Attendance record should not exist in DB", attendanceRecord);
 }
 @Test
 public void testDeleteNextToLastAttendanceEvent() {
   String attendanceRecordId = insertAttendanceEventData(false);
   Entity deleteAssessment =
       createDeleteAttendanceEntity(
           "Excused: sick", "Excused Absence", "2012-04-18", attendanceRecordId);
   CascadeResult deleteResult1 =
       repository.safeDelete(
           deleteAssessment, attendanceRecordId, false, false, false, false, 1, null);
   List<Map<String, String>> attendanceEvents = getAttendanceEvents(true);
   Entity attendanceRecord = getAttendanceRecord(attendanceRecordId);
   Assert.assertEquals(
       "Delete result should be success", CascadeResult.Status.SUCCESS, deleteResult1.getStatus());
   Assert.assertNotNull("Attendance record should exist in DB", attendanceRecord);
   Assert.assertEquals(
       "Field attendanceEvent mismatch",
       attendanceEvents,
       attendanceRecord.getBody().get("attendanceEvent"));
 }
 @Test
 public void testDeleteAttendanceEventWithBogusId() {
   // Result is error, and Attendance record should remain unchanged in DB.
   String attendanceRecordId = insertAttendanceEventData(false);
   String bogusAttendanceRecordId = "123-abc-789-def-e1e10";
   Entity bogusDeleteAssessment =
       createDeleteAttendanceEntity(
           "Excused: sick", "Excused Absence", "2012-04-18", bogusAttendanceRecordId);
   CascadeResult bogusDeleteResult =
       repository.safeDelete(
           bogusDeleteAssessment, bogusAttendanceRecordId, false, false, false, false, 1, null);
   List<Map<String, String>> attendanceEvents = getAttendanceEvents(false);
   Entity attendanceRecord = getAttendanceRecord(attendanceRecordId);
   Assert.assertEquals(
       "Delete result should be error", CascadeResult.Status.ERROR, bogusDeleteResult.getStatus());
   Assert.assertNotNull("Attendance record should exist in DB", attendanceRecord);
   Assert.assertEquals(
       "Field attendanceEvent mismatch",
       attendanceEvents,
       attendanceRecord.getBody().get("attendanceEvent"));
 }
 @Test
 public void testDeleteAttendanceEventWithDiffNonKeyField() {
   // Result is success, Attendance record should have natural key match attendanceEvent deleted in
   // DB.
   String attendanceRecordId = insertAttendanceEventData(false);
   Entity bogusDeleteAttendance =
       createDeleteAttendanceEntity(
           "Excused: dead", "Excused Absence", "2012-04-18", attendanceRecordId);
   CascadeResult bogusDeleteResult =
       repository.safeDelete(
           bogusDeleteAttendance, attendanceRecordId, false, false, false, false, 1, null);
   List<Map<String, String>> attendanceEvents = getAttendanceEvents(true);
   Entity attendanceRecord = getAttendanceRecord(attendanceRecordId);
   Assert.assertEquals(
       "Delete result should be success",
       CascadeResult.Status.SUCCESS,
       bogusDeleteResult.getStatus());
   Assert.assertNotNull("Attendance record should exist in DB", attendanceRecord);
   Assert.assertEquals(
       "Field attendanceEvent mismatch",
       attendanceEvents,
       attendanceRecord.getBody().get("attendanceEvent"));
 }
  private void testSafeDeleteHelper(
      String entityType,
      String overridingId,
      boolean cascade,
      boolean dryrun,
      boolean forced,
      boolean logViolations,
      Integer maxObjects,
      AccessibilityCheck access,
      boolean leafDataOnly,
      int expectedNObjects,
      int expectedDepth,
      CascadeResult.Status expectedStatus,
      CascadeResultError.ErrorType expectedErrorType,
      CascadeResultError.ErrorType expectedWarningType) {
    //        System.out.println("Testing safeDelete: ");
    //        System.out.println("   entity type             : " + entityType);
    //        System.out.println("   override id             : " + overridingId);
    //        System.out.println("   cascade                 : " + cascade);
    //        System.out.println("   dryrun                  : " + dryrun);
    //        System.out.println("   maxObjects              : " + maxObjects);
    //        System.out.println("   leaf data only          : " + leafDataOnly);
    //        System.out.println("   expected affected count : " + expectedNObjects);
    //        System.out.println("   expected depth          : " + expectedDepth);

    CascadeResult result = null;
    String idToDelete = prepareSafeDeleteGradingPeriodData(leafDataOnly);

    // used to test bad id scenario
    if (overridingId != null) {
      idToDelete = overridingId;
    }

    result =
        repository.safeDelete(
            entityType, idToDelete, cascade, dryrun, forced, logViolations, maxObjects, access);

    // check for at least one instance of the expected error type
    boolean errorMatchFound = false;
    List<CascadeResultError> errors = result.getErrors();
    if (expectedErrorType == null && errors != null && errors.size() == 0) {
      errorMatchFound = true;
    } else {
      for (CascadeResultError error : errors) {
        if (error.getErrorType() == expectedErrorType) {
          errorMatchFound = true;
          break;
        }
      }
    }

    //        for (CascadeResultError error : result.getErrors()) {
    //            System.out.println(error);
    //        }

    // check for at least one instance of the expected warning type
    boolean warningMatchFound = false;
    List<CascadeResultError> warnings = result.getWarnings();
    if (expectedWarningType == null && warnings != null && warnings.size() == 0) {
      warningMatchFound = true;
    } else {
      for (CascadeResultError warning : warnings) {
        if (warning.getErrorType() == expectedWarningType) {
          warningMatchFound = true;
          break;
        }
      }
    }

    //        for(CascadeResultError warning : result.getWarnings()) {
    //            System.out.println(warning);
    //        }

    //   verify expected results
    assertEquals(expectedNObjects, result.getnObjects());
    assertEquals(expectedDepth, result.getDepth());
    assertEquals(expectedStatus, result.getStatus());
    assertTrue(errorMatchFound);
    assertTrue(warningMatchFound);
  }