private static void addModifiedUserFlags(
     Flags oldFlags, Flags newFlags, String[] userflags, Flags modifiedFlags) {
   for (String userFlag : userflags) {
     if (isChanged(oldFlags, newFlags, userFlag)) {
       modifiedFlags.add(userFlag);
     }
   }
 }
  public Flags applyToState(boolean isSeen, boolean isAnswered, boolean isFlagged) {
    Flags newStateFlags = new Flags();

    boolean shouldMessageBeFlagged =
        isFlagged().isPresent() && isFlagged().get() || (!isFlagged().isPresent() && isFlagged);
    if (shouldMessageBeFlagged) {
      newStateFlags.add(Flags.Flag.FLAGGED);
    }
    boolean shouldMessageBeMarkAnswered =
        isAnswered().isPresent() && isAnswered().get() || (!isAnswered().isPresent() && isAnswered);
    if (shouldMessageBeMarkAnswered) {
      newStateFlags.add(Flags.Flag.ANSWERED);
    }
    boolean shouldMessageBeMarkSeen =
        isUnread().isPresent() && !isUnread().get() || (!isUnread().isPresent() && isSeen);
    if (shouldMessageBeMarkSeen) {
      newStateFlags.add(Flags.Flag.SEEN);
    }
    return newStateFlags;
  }
 @Test
 public void flagsShouldBeSetIntoMessage() throws Exception {
   Flags flags = new Flags();
   flags.add(Flag.ANSWERED);
   flags.add(Flag.FLAGGED);
   flags.add(Flag.DRAFT);
   MetaDataWithContent testMail =
       MetaDataWithContent.builder()
           .uid(MessageUid.of(2))
           .flags(flags)
           .size(0)
           .internalDate(INTERNAL_DATE)
           .content(new ByteArrayInputStream("".getBytes(Charsets.UTF_8)))
           .attachments(ImmutableList.of())
           .mailboxId(MAILBOX_ID)
           .messageId(MessageId.of("test|test|2"))
           .build();
   Message testee = messageFactory.fromMetaDataWithContent(testMail);
   assertThat(testee)
       .extracting(
           Message::isIsUnread, Message::isIsFlagged, Message::isIsAnswered, Message::isIsDraft)
       .containsExactly(true, true, true, true);
 }
 private static void addModifiedSystemFlags(Flags oldFlags, Flags newFlags, Flags modifiedFlags) {
   if (isChanged(oldFlags, newFlags, Flags.Flag.ANSWERED)) {
     modifiedFlags.add(Flags.Flag.ANSWERED);
   }
   if (isChanged(oldFlags, newFlags, Flags.Flag.DELETED)) {
     modifiedFlags.add(Flags.Flag.DELETED);
   }
   if (isChanged(oldFlags, newFlags, Flags.Flag.DRAFT)) {
     modifiedFlags.add(Flags.Flag.DRAFT);
   }
   if (isChanged(oldFlags, newFlags, Flags.Flag.FLAGGED)) {
     modifiedFlags.add(Flags.Flag.FLAGGED);
   }
   if (isChanged(oldFlags, newFlags, Flags.Flag.RECENT)) {
     modifiedFlags.add(Flags.Flag.RECENT);
   }
   if (isChanged(oldFlags, newFlags, Flags.Flag.SEEN)) {
     modifiedFlags.add(Flags.Flag.SEEN);
   }
 }
  @Override
  public void setUp() throws Exception {
    ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean("ServiceRegistry");
    transactionService = serviceRegistry.getTransactionService();
    nodeService = serviceRegistry.getNodeService();
    importerService = serviceRegistry.getImporterService();
    personService = serviceRegistry.getPersonService();
    authenticationService = serviceRegistry.getAuthenticationService();
    permissionService = serviceRegistry.getPermissionService();
    imapService = serviceRegistry.getImapService();
    searchService = serviceRegistry.getSearchService();
    namespaceService = serviceRegistry.getNamespaceService();
    fileFolderService = serviceRegistry.getFileFolderService();

    flags = new Flags();
    flags.add(Flags.Flag.SEEN);
    flags.add(Flags.Flag.FLAGGED);
    flags.add(Flags.Flag.ANSWERED);
    flags.add(Flags.Flag.DELETED);

    // start the transaction
    txn = transactionService.getUserTransaction();
    txn.begin();
    authenticationService.authenticate(USER_NAME, USER_PASSWORD.toCharArray());

    // downgrade integrity
    IntegrityChecker.setWarnInTransaction();

    anotherUserName = "******" + System.currentTimeMillis();

    PropertyMap testUser = new PropertyMap();
    testUser.put(ContentModel.PROP_USERNAME, anotherUserName);
    testUser.put(ContentModel.PROP_FIRSTNAME, anotherUserName);
    testUser.put(ContentModel.PROP_LASTNAME, anotherUserName);
    testUser.put(ContentModel.PROP_EMAIL, anotherUserName + "@alfresco.com");
    testUser.put(ContentModel.PROP_JOBTITLE, "jobTitle");

    personService.createPerson(testUser);

    // create the ACEGI Authentication instance for the new user
    authenticationService.createAuthentication(anotherUserName, anotherUserName.toCharArray());

    user =
        new AlfrescoImapUser(anotherUserName + "@alfresco.com", anotherUserName, anotherUserName);

    String storePath = "workspace://SpacesStore";
    String companyHomePathInStore = "/app:company_home";

    StoreRef storeRef = new StoreRef(storePath);

    NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);

    List<NodeRef> nodeRefs =
        searchService.selectNodes(
            storeRootNodeRef, companyHomePathInStore, null, namespaceService, false);
    NodeRef companyHomeNodeRef = nodeRefs.get(0);

    ChildApplicationContextFactory imap = (ChildApplicationContextFactory) ctx.getBean("imap");
    ApplicationContext imapCtx = imap.getApplicationContext();
    ImapServiceImpl imapServiceImpl = (ImapServiceImpl) imapCtx.getBean("imapService");

    // Creating IMAP test folder for IMAP root
    LinkedList<String> folders = new LinkedList<String>();
    folders.add(TEST_IMAP_FOLDER_NAME);
    FileFolderServiceImpl.makeFolders(
        fileFolderService, companyHomeNodeRef, folders, ContentModel.TYPE_FOLDER);

    // Setting IMAP root
    RepositoryFolderConfigBean imapHome = new RepositoryFolderConfigBean();
    imapHome.setStore(storePath);
    imapHome.setRootPath(companyHomePathInStore);
    imapHome.setFolderPath(TEST_IMAP_FOLDER_NAME);
    imapServiceImpl.setImapHome(imapHome);

    // Starting IMAP
    imapServiceImpl.startup();

    nodeRefs =
        searchService.selectNodes(
            storeRootNodeRef,
            companyHomePathInStore
                + "/"
                + NamespaceService.CONTENT_MODEL_PREFIX
                + ":"
                + TEST_IMAP_FOLDER_NAME,
            null,
            namespaceService,
            false);
    testImapFolderNodeRef = nodeRefs.get(0);

    /*
     * Importing test folders:
     *
     * Test folder contains: "___-___folder_a"
     *
     * "___-___folder_a" contains: "___-___folder_a_a",
     *                             "___-___file_a",
     *                             "Message_485.eml" (this is IMAP Message)
     *
     * "___-___folder_a_a" contains: "____-____file_a_a"
     *
     */
    importInternal("imap/imapservice_test_folder_a.acp", testImapFolderNodeRef);

    reauthenticate(anotherUserName, anotherUserName);
  }