protected void completeValidateAndDelete(TicketBookingRequest request)
     throws CvqException, IOException {
   // add a document to the request
   ///////////////////////////////
   Document doc = new Document();
   doc.setEcitizenNote("Ma carte d'identité !");
   doc.setDepositOrigin(DepositOrigin.ECITIZEN);
   doc.setDepositType(DepositType.PC);
   doc.setHomeFolderId(request.getHomeFolderId());
   doc.setIndividualId(request.getRequesterId());
   doc.setDocumentType(
       documentTypeService.getDocumentTypeByType(IDocumentTypeService.IDENTITY_RECEIPT_TYPE));
   Long documentId = documentService.create(doc);
   requestDocumentService.addDocument(request.getId(), documentId);
   Set<RequestDocument> documentsSet =
       requestDocumentService.getAssociatedDocuments(request.getId());
   assertEquals(documentsSet.size(), 1);
   // FIXME : test list of pending / in-progress registrations
   Critere testCrit = new Critere();
   testCrit.setAttribut(Request.SEARCH_BY_HOME_FOLDER_ID);
   testCrit.setComparatif(Critere.EQUALS);
   testCrit.setValue(request.getHomeFolderId());
   Set<Critere> testCritSet = new HashSet<Critere>();
   testCritSet.add(testCrit);
   List<Request> allRequests = requestSearchService.get(testCritSet, null, null, -1, 0, false);
   assertNotNull(allRequests);
   // close current session and re-open a new one
   continueWithNewTransaction();
   SecurityContext.setCurrentSite(localAuthorityName, SecurityContext.BACK_OFFICE_CONTEXT);
   SecurityContext.setCurrentAgent(agentNameWithCategoriesRoles);
   requestWorkflowService.updateRequestState(request.getId(), RequestState.COMPLETE, null);
   requestWorkflowService.updateRequestState(request.getId(), RequestState.VALIDATED, null);
   // close current session and re-open a new one
   continueWithNewTransaction();
   byte[] generatedCertificate =
       requestSearchService.getCertificate(request.getId(), RequestState.PENDING);
   if (generatedCertificate == null) fail("No certificate found");
   //     Write tele-service xml data file
   File xmlFile = File.createTempFile("tmp" + request.getId(), ".xml");
   FileOutputStream xmlFos = new FileOutputStream(xmlFile);
   xmlFos.write(requestSearchService.getById(request.getId(), true).modelToXmlString().getBytes());
   File file = File.createTempFile("tmp" + request.getId(), ".pdf");
   FileOutputStream fos = new FileOutputStream(file);
   fos.write(generatedCertificate);
   // close current session and re-open a new one
   continueWithNewTransaction();
   // delete request
   requestWorkflowService.delete(request.getId());
 }
  @Test
  public void testCreate() throws CvqException {

    SecurityContext.setCurrentSite(localAuthorityName, SecurityContext.FRONT_OFFICE_CONTEXT);
    SecurityContext.setCurrentEcitizen(fake.responsibleId);

    continueWithNewTransaction();

    Individual individual = userSearchService.getHomeFolderResponsible(fake.id);
    DocumentType documentType =
        documentTypeService.getDocumentTypeByType(IDocumentTypeService.ADOPTION_JUDGMENT_TYPE);

    Document document = BusinessObjectsFactory.gimmeDocument("", null, null, documentType);
    document.setHomeFolderId(fake.id);
    document.setIndividualId(new Long(individual.getId().longValue()));
    documentService.create(document);
    Long documentId = document.getId();

    try {
      documentService.check(documentId, null);
      fail("should have thrown an exception");
    } catch (PermissionException pe) {
      // that was expected
    }

    document = BusinessObjectsFactory.gimmeDocument("", null, null, documentType);
    document.setHomeFolderId(Long.valueOf("0"));
    try {
      documentService.create(document);
      fail("should have thrown an exception");
    } catch (PermissionException pe) {
      // that was expected
    }

    continueWithNewTransaction();

    SecurityContext.setCurrentSite(localAuthorityName, SecurityContext.BACK_OFFICE_CONTEXT);
    SecurityContext.setCurrentAgent(agentNameWithCategoriesRoles);

    documentService.check(documentId, null);
    documentService.getById(documentId);
  }
  @Test
  public void testIndividualDeleteEvent() throws CvqException {
    SecurityContext.setCurrentSite(localAuthorityName, SecurityContext.FRONT_OFFICE_CONTEXT);
    SecurityContext.setCurrentEcitizen(fake.responsibleId);

    DocumentType documentType =
        documentTypeService.getDocumentTypeByType(IDocumentTypeService.ADOPTION_JUDGMENT_TYPE);

    Document document = BusinessObjectsFactory.gimmeDocument("", null, null, documentType);
    document.setHomeFolderId(fake.id);
    document.setIndividualId(fake.womanId);
    documentService.create(document);

    continueWithNewTransaction();

    SecurityContext.setCurrentSite(localAuthorityName, SecurityContext.BACK_OFFICE_CONTEXT);
    SecurityContext.setCurrentAgent(agentNameWithCategoriesRoles);

    userWorkflowService.delete(userSearchService.getById(fake.womanId));

    continueWithNewTransaction();

    assertTrue(documentService.getIndividualDocuments(fake.womanId).isEmpty());
  }
  @Test
  public void testAll() throws CvqException, java.io.IOException, java.io.FileNotFoundException {

    SecurityContext.setCurrentSite(localAuthorityName, SecurityContext.FRONT_OFFICE_CONTEXT);

    // ensure document digitalization is enabled
    assertTrue(SecurityContext.getCurrentSite().isDocumentDigitalizationEnabled());

    // ensure document types have been bootstrapped
    List<DocumentType> allDocumentTypes = documentTypeService.getAllDocumentTypes();
    assertTrue(allDocumentTypes.size() > 0);

    SecurityContext.setCurrentEcitizen(fake.responsibleId);

    Individual anIndividual = userSearchService.getAdults(fake.id).get(0);

    // create a document
    Document doc =
        BusinessObjectsFactory.gimmeDocument(
            "",
            DepositOrigin.ECITIZEN,
            DepositType.PC,
            documentTypeService.getDocumentTypeByType(IDocumentTypeService.IDENTITY_RECEIPT_TYPE));
    doc.setDepositId(anIndividual.getId());
    doc.setHomeFolderId(fake.id);
    doc.setIndividualId(anIndividual.getId());
    Long docId = documentService.create(doc);

    // add binary data
    try {
      documentService.addPage(docId, getImageDocumentBinary());
    } catch (CvqModelException cme) {
      fail("thrown cvq model exception : " + cme.getI18nKey());
    }

    // and another one ...
    try {
      documentService.addPage(docId, getImageDocumentBinary());
    } catch (CvqModelException cme) {
      fail("thrown cvq model exception : " + cme.getI18nKey());
    }

    continueWithNewTransaction();

    // check the document and its two binary have been successfully added ...
    // ... to the home folder
    List<Document> documentsList = documentService.getHomeFolderDocuments(fake.id, -1);
    assertEquals("Bad number of associated documents on home folder", 1, documentsList.size());
    List<DocumentBinary> docBinarys = documentService.getById(docId).getDatas();
    assertEquals("Bad number of associated data on document", 2, doc.getDatas().size());

    // ... and to the individual
    documentsList = documentService.getIndividualDocuments(anIndividual.getId());
    assertEquals("Bad number of associated documents on individual", 1, documentsList.size());
    try {
      documentsList = documentService.getIndividualDocuments(new Long(0));
      fail("should have thrown an exception");
    } catch (PermissionException pe) {
      // that was expected
    }

    // modify a page
    try {
      documentService.modifyPage(docId, 0, getImageDocumentBinary());
    } catch (CvqModelException cme) {
      fail("thrown cvq model exception : " + cme.getI18nKey());
    }

    // remove a page
    doc.getDatas().remove(1);
    assertEquals("Bad number of associated data on document", 1, doc.getDatas().size());
    assertNotNull("Could find page", doc.getDatas().get(0));

    // try to retrieve the list of identity pieces for home folder
    DocumentType docType =
        documentTypeService.getDocumentTypeByType(IDocumentTypeService.IDENTITY_RECEIPT_TYPE);
    documentsList = documentService.getProvidedDocuments(docType, fake.id, null);
    assertEquals("Bad number of docs for home folder (1)", 1, documentsList.size());
    // and try other successful and unsuccessful searches among provided documents
    documentsList = documentService.getProvidedDocuments(docType, fake.id, anIndividual.getId());
    assertEquals("Bad number of docs for home folder and individual", 1, documentsList.size());
    docType =
        documentTypeService.getDocumentTypeByType(IDocumentTypeService.MEDICAL_CERTIFICATE_TYPE);
    documentsList = documentService.getProvidedDocuments(docType, fake.id, null);
    assertEquals("Bad number of docs for home folder (2)", 0, documentsList.size());

    // test end validity durations by creating different sort of doc types
    // based on example data from $BASE_DIR/db/init_ref_data.sql

    // ... a permanently durable
    doc =
        BusinessObjectsFactory.gimmeDocument(
            "",
            DepositOrigin.ECITIZEN,
            DepositType.PC,
            documentTypeService.getDocumentTypeByType(IDocumentTypeService.IDENTITY_RECEIPT_TYPE));
    doc.setDepositId(anIndividual.getId());
    doc.setHomeFolderId(fake.id);
    doc.setIndividualId(anIndividual.getId());
    documentService.create(doc);

    // ... a 3-year valid
    doc =
        BusinessObjectsFactory.gimmeDocument(
            "",
            DepositOrigin.ECITIZEN,
            DepositType.PC,
            documentTypeService.getDocumentTypeByType(IDocumentTypeService.DOMICILE_RECEIPT_TYPE));
    doc.setDepositId(anIndividual.getId());
    doc.setHomeFolderId(fake.id);
    documentService.create(doc);

    // ... a 2-month valid
    doc =
        BusinessObjectsFactory.gimmeDocument(
            "",
            DepositOrigin.ECITIZEN,
            DepositType.PC,
            documentTypeService.getDocumentTypeByType(
                IDocumentTypeService.ID_CARD_LOSS_DECLARATION_TYPE));
    doc.setDepositId(anIndividual.getId());
    doc.setHomeFolderId(fake.id);
    Long docId3 = documentService.create(doc);

    // ... an end-of-the-year valid
    doc = new Document();
    doc.setDepositId(anIndividual.getId());
    doc.setDepositOrigin(DepositOrigin.ECITIZEN);
    doc.setDepositType(DepositType.PC);
    doc.setDocumentType(
        documentTypeService.getDocumentTypeByType(IDocumentTypeService.TAXES_NOTIFICATION_TYPE));
    doc.setHomeFolderId(fake.id);
    Long docId4 = documentService.create(doc);

    // ... an end-of-the-school-year valid
    doc =
        BusinessObjectsFactory.gimmeDocument(
            "",
            DepositOrigin.ECITIZEN,
            DepositType.PC,
            documentTypeService.getDocumentTypeByType(
                IDocumentTypeService.VACATING_CERTIFICATE_TYPE));
    doc.setDepositId(anIndividual.getId());
    doc.setHomeFolderId(fake.id);
    documentService.create(doc);

    // delete a document
    documentService.delete(docId3);

    continueWithNewTransaction();

    // test modifications on a document
    Document docToModify = documentService.getById(docId4);
    doc.setDepositType(DepositType.TERMINAL);
    doc.setAgentNote("Quelle belle PJ");
    Calendar calendar = new GregorianCalendar();
    Date currentDate = new Date();
    calendar.setTime(currentDate);
    calendar.add(Calendar.MONTH, 3);
    doc.setEndValidityDate(calendar.getTime());
    documentService.modify(docToModify);
    docToModify = documentService.getById(docId4);
    assertNotNull("Argh, where my f****** document has gone ??!");
    assertEquals(doc.getAgentNote(), "Quelle belle PJ");

    // hmm ? just a test :-)
    try {
      documentService.modify(null);
      fail("should have thrown an exception");
    } catch (PermissionException pe) {
      // that was expected
    }

    // retrieve all known document types
    allDocumentTypes = documentTypeService.getAllDocumentTypes();
    assertNotNull(allDocumentTypes);

    SecurityContext.setCurrentEcitizen(fake.responsibleId);
    int count = documentService.searchCount(null);
    assertNotSame(count, 0);

    List<Document> docs = new ArrayList<Document>();
    Hashtable<String, Object> params = new Hashtable<String, Object>();
    params.put(
        "documentType",
        documentTypeService.getDocumentTypeByType(IDocumentTypeService.TAXES_NOTIFICATION_TYPE));

    docs = documentService.search(params, -1, -1);
    assertNotSame(docs.size(), 0);

    params = new Hashtable<String, Object>();
    params.put("homeFolderId", fake.id);

    count = documentService.searchCount(params);
    docs = documentService.search(params, -1, -1);
    assertEquals(docs.size(), count);
  }