package ru.stqa.jft.addressbook.tests;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import ru.stqa.jft.addressbook.model.ContactData;
import ru.stqa.jft.addressbook.model.Contacts;
import ru.stqa.jft.addressbook.model.GroupData;
import ru.stqa.jft.addressbook.model.Groups;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Created by Anna on 2017-01-25.
 */
public class ContactAddingToGroupTests extends TestBase{

    ContactData contactAdded;
    int id;

    @Test
    public void testContactAddToGroup() {

        if (app.db().groups().size() == 0) {
            GroupData group = new GroupData().withName("group").withHeader("header").withFooter("footer");
            app.group().create(group);
        }

        Groups groups = app.db().groups();
        GroupData group = groups.iterator().next();
        String groupName = group.getName();
        Contacts contacts = app.db().contacts();
        Contacts contactsInGroup = group.getContacts();
        if (contactsInGroup.size() >= 1) {
            contacts.retainAll(contactsInGroup);
        }

        for (ContactData contact: contacts) {
            contactAdded = new ContactData();
            id = contact.getId();
        }

        app.contact().selectContactById(id);
        app.contact().selectGroup(groupName);
        app.contact().addToGroup();

        contactsInGroup.add(contactAdded);
        Contacts contactsInGroupAfterAdding = group.getContacts();

        assertThat(contactsInGroup, equalTo(contactsInGroupAfterAdding));

        }

}

@@ -607,20 +607,29 @@
 ;%0A%0A    @
-Test
+BeforeMethod%0A
 %0A    pub
@@ -641,34 +641,364 @@
 oid 
-testContactAddToGroup() %7B%0A
+ensurePreconditions() %7B%0A        if (app.db().contacts().size() == 0) %7B%0A            app.goTo().newContact();%0A            app.contact().create(new ContactData().withName(%22Jan%22).withSurname(%22Kowalski%22).withAddress(%22Zielona 7%22).withHomeNumber(%22675-76-16%22).withMobileNumber(%22123 43 23%22).withWorkNumber(%22(44)-67-678%22).withEmail1(%22kowalski@poczta.pl%22));%0A        %7D
 %0A   
@@ -1186,32 +1186,90 @@
 roup);%0A        %7D
+%0A    %7D%0A    @Test%0A    public void testContactAddToGroup() %7B
 %0A%0A        Groups
@@ -1490,32 +1490,33 @@
 .getContacts();%0A
+%0A
         if (cont

add to group, try 6