/*
 * Copyright (C) 2015 Karumi.
 *
 * 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.karumi.katagenda.ui;

import com.karumi.katagenda.domain.Contact;
import com.karumi.katagenda.usecase.AddContact;
import com.karumi.katagenda.usecase.GetContacts;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ContactsListPresenterTest {

  private static final int ANY_NUMBER_OF_CONTACTS = 7;
  private static final String ANY_FIRST_NAME = "Pedro Vicente";
  private static final String ANY_LAST_NAME = "Gomez Sanchez";
  private static final String ANY_PHONE_NUMBER = "666666666";

  @Mock private ContactsListPresenter.View view;
  @Mock private GetContacts getContacts;
  @Mock private AddContact addContact;

  @Before public void setUp() {
    MockitoAnnotations.initMocks(this);
  }

  @Test public void shouldShowWelcomeMessageOnInitialize() {
    ContactsListPresenter presenter = givenAContactsListPresenter();

    presenter.onInitialize();

    verify(view).showWelcomeMessage();
  }

  @Test public void shouldShowEmptyCaseIfTheAgendaIsEmpty() {
    ContactsListPresenter presenter = givenAContactsListPresenter();
    givenTheAgendaIsEmpty();

    presenter.onInitialize();

    verify(view).showEmptyCase();
  }

  @Test public void shouldShowContactsFromTheAgendaOnInitialize() {
    ContactsListPresenter presenter = givenAContactsListPresenter();
    List<Contact> contacts = givenTheAgendaIsNotEmpty();

    presenter.onInitialize();

    verify(view).showContacts(contacts);
  }

  @Test public void shouldShowGoodbyeMessageOnStop() {
    ContactsListPresenter presenter = givenAContactsListPresenter();

    presenter.onStop();

    verify(view).showGoodbyeMessage();
  }


  private void givenTheAgendaIsEmpty() {
    when(getContacts.execute()).thenReturn(Collections.<Contact>emptyList());
  }

  private List<Contact> givenTheAgendaIsNotEmpty() {
    List<Contact> contacts = new LinkedList<>();
    for (int i = 0; i < ANY_NUMBER_OF_CONTACTS; i++) {
      Contact contact = new Contact(ANY_FIRST_NAME, ANY_LAST_NAME, ANY_PHONE_NUMBER);
      contacts.add(contact);
    }
    when(getContacts.execute()).thenReturn(contacts);
    return contacts;
  }

  private ContactsListPresenter givenAContactsListPresenter() {
    return new ContactsListPresenter(view, getContacts, addContact);
  }
}
@@ -913,53 +913,199 @@
 ito.
-Mock;%0Aimport org.mockito.MockitoAnnotations;%0A
+ArgumentCaptor;%0Aimport org.mockito.Captor;%0Aimport org.mockito.Mock;%0Aimport org.mockito.MockitoAnnotations;%0A%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.mockito.Mockito.times;
 %0Aimp
@@ -1599,16 +1599,80 @@
 Contact;
+%0A  @Captor private ArgumentCaptor%3CList%3CContact%3E%3E contactsCaptor;
 %0A%0A  @Bef
@@ -2645,16 +2645,825 @@
 );%0A  %7D%0A%0A
+  @Test public void shouldShowTheContactsListWithTheNewContactOnContactAdded() %7B%0A    ContactsListPresenter presenter = givenAContactsListPresenter();%0A    Contact contactToCreate = givenTheUserAddsAContact();%0A    givenTheContactIsAddedCorrectly(contactToCreate);%0A%0A    presenter.onInitialize();%0A    presenter.onAddContactOptionSelected();%0A%0A    verify(view, times(2)).showContacts(contactsCaptor.capture());%0A    List%3CContact%3E newContacts = contactsCaptor.getAllValues().get(1);%0A    assertTrue(newContacts.contains(contactToCreate));%0A  %7D%0A%0A  private void givenTheContactIsAddedCorrectly(Contact contact) %7B%0A    when(addContact.execute(contact)).thenReturn(contact);%0A    LinkedList%3CContact%3E newContacts = new LinkedList%3C%3E();%0A    newContacts.add(contact);%0A    when(getContacts.execute()).thenReturn(newContacts);%0A  %7D%0A
 %0A  priva
@@ -4073,10 +4073,396 @@
 ct);%0A  %7D
+%0A%0A  private Contact givenTheUserAddsAContact() %7B%0A    Contact contact = new Contact(ANY_FIRST_NAME, ANY_LAST_NAME, ANY_PHONE_NUMBER);%0A    when(view.getNewContactFirstName()).thenReturn(contact.getFirstName());%0A    when(view.getNewContactLastName()).thenReturn(contact.getLastName());%0A    when(view.getNewContactPhoneNumber()).thenReturn(contact.getPhoneNumber());%0A    return contact;%0A  %7D
 %0A%7D

Add a new test to check if the add user process is working as should