public void assumePrintJobInstructions(final String expectedStatus) {
   for (final I_C_Print_Job_Instructions printJobInstructions :
       helper.getDB().getRecords(I_C_Print_Job_Instructions.class)) {
     Assert.assertEquals(
         "Invalid status for " + printJobInstructions,
         expectedStatus,
         printJobInstructions.getStatus());
   }
 }
  @Test
  public void test_TwoPrintJobs_SecondOneIsUsedBecauseFirstOneFails() {
    //
    // Setup routings
    final I_AD_PrinterRouting routing1 =
        helper.createPrinterRouting("printer01", "tray01", -1, -1, -1);
    routing1.setAD_Org_ID(1);
    helper.getDB().save(routing1);
    final I_AD_PrinterRouting routing2 =
        helper.createPrinterRouting("printer02", "tray01", -1, -1, -1);
    routing2.setAD_Org_ID(2);
    helper.getDB().save(routing2);

    //
    // Create printing queue
    helper.addToPrintQueue("01", 1, -1); // AD_Org_ID=1, C_DocType_ID=N/A
    helper.addToPrintQueue("02", 2, -1); // AD_Org_ID=2, C_DocType_ID=N/A

    //
    // Expect 2 print jobs to be created
    final int printJobsCountActual = helper.createAllPrintJobs();
    Assert.assertEquals("Invalid Print Jobs count", 2, printJobsCountActual);

    final List<I_C_Print_Job> printJobs = helper.getDB().getRecords(I_C_Print_Job.class);
    Assert.assertEquals("Invalid Print Jobs count in database", 2, printJobs.size());

    // Validate PrintJob 1
    final I_C_Print_Job printJob1 = printJobs.get(0);
    {
      final List<I_C_Print_Job_Line> lines =
          IteratorUtils.toList(helper.getDAO().retrievePrintJobLines(printJob1));
      Assert.assertEquals("Job1 - Only one line was expected for " + printJob1, 1, lines.size());

      final I_C_Print_Job_Line line = lines.get(0);
      final I_C_Print_Job_Detail detail = helper.getDAO().retrievePrintJobDetails(line).get(0);
      Assert.assertEquals("Job1 - Invalid routing used", routing1, detail.getAD_PrinterRouting());
    }
    // Validate PrintJob 2
    final I_C_Print_Job printJob2 = printJobs.get(1);
    {
      final List<I_C_Print_Job_Line> lines =
          IteratorUtils.toList(helper.getDAO().retrievePrintJobLines(printJob2));
      Assert.assertEquals("Job2 - Only one line was expected for " + printJob2, 1, lines.size());

      final I_C_Print_Job_Line line = lines.get(0);
      final I_C_Print_Job_Detail detail = helper.getDAO().retrievePrintJobDetails(line).get(0);
      Assert.assertEquals("Job2 - Invalid routing used", routing2, detail.getAD_PrinterRouting());
    }

    //
    // Everything is fine.... now drop the matching for printer01
    // ... this shall produce errors when trying to generate the package for first print job
    {
      final I_AD_Printer_Matching matching =
          helper.getDAO().retrievePrinterMatching(helper.getSessionHostKey(), routing1);
      helper.getDB().delete(matching);
    }

    //
    // Creating next package
    // Expectations: printJob2 is used
    helper.createNextPrintPackageAndTest(
        printJob2, new PdfCollator().addPages(helper.getPdf("02"), 1, 20).toByteArray());

    //
    // Validate PrintJob1 Instructions
    {
      final I_C_Print_Job_Instructions instructions1 =
          helper.getDAO().retrievePrintJobInstructionsForPrintJob(printJob1);
      // task 09028: don't check for the host key..the user shall be able to print this wherever
      // they are logged inAssert.assertEquals("Job1 instructions - Invalid HostKey",
      // helper.getSessionHostKey(), instructions1.getHostKey());
      // Assert.assertEquals("Job1 instructions - Invalid status",
      // X_C_Print_Job_Instructions.STATUS_Error, instructions1.getStatus());
      Assert.assertNotNull(
          "Job1 instructions - Missing error message", instructions1.getErrorMsg());
    }

    //
    // Validate PrintJob2 Instructions
    {
      final I_C_Print_Job_Instructions instructions2 =
          helper.getDAO().retrievePrintJobInstructionsForPrintJob(printJob2);
      // task 09028: don't check for the host key..the user shall be able to print this wherever
      // they are logged in
      // Assert.assertEquals("Job2 instructions - Invalid HostKey", helper.getSessionHostKey(),
      // instructions2.getHostKey());
      Assert.assertEquals(
          "Job2 instructions - Invalid status",
          X_C_Print_Job_Instructions.STATUS_Send,
          instructions2.getStatus());
      Assert.assertNull("Job2 instructions - Missing error message", instructions2.getErrorMsg());
    }

    //
    // Recreate back the HW matching for printer01
    helper.createPrinterConfigAndMatching(
        helper.getSessionHostKey(), "printer01-hw-again", "tray01-hw-again", "printer01", "tray01");

    //
    // Job1 instructions, prepare it again
    {
      final I_C_Print_Job_Instructions instructions1 =
          helper.getDAO().retrievePrintJobInstructionsForPrintJob(printJob1);
      instructions1.setStatus(X_C_Print_Job_Instructions.STATUS_Pending);
      instructions1.setErrorMsg(null);
      InterfaceWrapperHelper.save(instructions1);
    }

    //
    // Creating next package
    // Expectations: printJob1 is used
    {
      helper.createNextPrintPackageAndTest(
          printJob1, new PdfCollator().addPages(helper.getPdf("01"), 1, 20).toByteArray());
    }

    //
    // Validate PrintJob2 Instructions
    {
      final I_C_Print_Job_Instructions instructions1 =
          helper.getDAO().retrievePrintJobInstructionsForPrintJob(printJob1);
      Assert.assertEquals(
          "Job1 instructions - Invalid status",
          X_C_Print_Job_Instructions.STATUS_Send,
          instructions1.getStatus());
      Assert.assertNull("Job1 instructions - Missing error message", instructions1.getErrorMsg());
    }

    //
    // Final assumptions
    assumePrintQueueProcessed();
    assumePrintJobInstructions(X_C_Print_Job_Instructions.STATUS_Send); // everything is Send
    assumePrintJobsProcessed();
    helper.assumeNothingLocked();
  }