/*
 * StoreCrawlerText.java
 *
 * Created on 2019-08-20, 6:46
 *
 * Copyright 2019 Marc Nuri San Felix
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.marcnuri.mnimapsync.index;

import static com.marcnuri.mnimapsync.index.StoreCrawler.populateFromStore;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import com.sun.mail.imap.IMAPStore;
import javax.mail.Folder;
import javax.mail.MessagingException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

/**
 * Created by Marc Nuri <marc@marcnuri.com> on 2019-08-20.
 */
class StoreCrawlerText {

  private IMAPFolder defaultFolder;
  private IMAPStore imapStore;

  @BeforeEach
  void setUp() throws Exception {
    defaultFolder = mockFolder("INBOX");
    doReturn(new IMAPFolder[]{
        mockFolder("Folder 1"), mockFolder("Folder 2")
    }).when(defaultFolder).list();
    imapStore = Mockito.mock(IMAPStore.class);
    doReturn(defaultFolder).when(imapStore).getDefaultFolder();
    doAnswer(invocation -> mockFolder(invocation.getArgument(0)))
        .when(imapStore).getFolder(anyString());
  }

  @AfterEach
  void tearDown() {
    imapStore = null;
    defaultFolder = null;
  }

  @Test
  void populateFromStore_storeHasFolders_ShouldPopulateIndex() throws Exception {
    // Given
    final Index index = new Index();
    doReturn(1).when(defaultFolder).getMessageCount();
    // When
    populateFromStore(index, imapStore, 1);
    // Then
    verify(defaultFolder, times(1)).expunge();
    assertThat(index.containsFolder("INBOX"), equalTo(true));
    assertThat(index.containsFolder("Folder 1"), equalTo(true));
    assertThat(index.containsFolder("Folder 2"), equalTo(true));
    assertThat(index.getCrawlExceptions(), empty());
    assertThat(index.hasCrawlException(), equalTo(false));
  }

  private static IMAPFolder mockFolder(String name) throws MessagingException {
    final IMAPFolder mockFolder = Mockito.mock(IMAPFolder.class);
    doReturn(name).when(mockFolder).getFullName();
    doReturn(Folder.HOLDS_MESSAGES | Folder.HOLDS_FOLDERS).when(mockFolder).getType();
    doReturn(Folder.READ_WRITE).when(mockFolder).getMode();
    doReturn(new IMAPFolder[0]).when(mockFolder).list();
    doReturn(new IMAPMessage[0]).when(mockFolder).getMessages(anyInt(), anyInt());
    return mockFolder;
  }
}

@@ -918,16 +918,130 @@
 qualTo;%0A
+import static org.junit.jupiter.api.Assertions.assertThrows;%0Aimport static org.junit.jupiter.api.Assertions.fail;%0A
 import s
@@ -1083,16 +1083,16 @@
 anyInt;%0A
-
 import s
@@ -2366,17 +2366,17 @@
 Folders_
-S
+s
 houldPop
@@ -2868,24 +2868,24 @@
 , empty());%0A
-
     assertTh
@@ -2936,16 +2936,897 @@
 );%0A  %7D%0A%0A
+  @Test%0A  void populateFromStore_indexHasExceptionsAndStoreHasFolders_shouldThrowException() throws Exception %7B%0A    // Given%0A    final Index index = new Index();%0A    index.addCrawlException(new MessagingException(%22Indexing tasks went wrong at some point%22));%0A    doReturn(1).when(defaultFolder).getMessageCount();%0A    // When%0A    final MessagingException result = assertThrows(MessagingException.class, () -%3E %7B%0A      populateFromStore(index, imapStore, 1);%0A      fail();%0A    %7D);%0A    // Then%0A    verify(defaultFolder, times(1)).expunge();%0A    assertThat(index.containsFolder(%22INBOX%22), equalTo(true));%0A    assertThat(index.containsFolder(%22Folder 1%22), equalTo(true));%0A    assertThat(index.containsFolder(%22Folder 2%22), equalTo(true));%0A    assertThat(index.hasCrawlException(), equalTo(true));%0A    assertThat(result.getMessage(), equalTo(%22Indexing tasks went wrong at some point%22));%0A  %7D%0A%0A
   privat

Increment coverage of affected classes