package ru.stqa.pft.addressbook.tests;

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

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

/**
 * Created by Alesia on 23.05.17.
 */
public class GroupRemoveContactTests extends TestBase {
  @BeforeMethod
  public void ensurePreconditions() {
    Groups groups = app.db().groups();
    if (app.db().groups().size() == 0) {
      app.goTo().groupPage();
      app.group().create(new GroupData().withName("GROUP1"));
    }

//    if (app.db().contacts().size() == 0) {
//      app.goTo().homePage();
//      app.contact().create(new ContactData().
//              withFirstname("Anna").withLastname("Ivanova").withAddress("Address")
//              .inGroup(groups.iterator().next()), true);
//    }

    app.goTo().homePage();
  }

  @Test
  public void testGroupRemoveContact() {
    Contacts contacts = app.db().contacts();
    Groups before = app.db().groups();
    GroupData groupInTopList = before.iterator().next();
    GroupData group = new GroupData().withName(groupInTopList.getName());

    if (groupInTopList.getContacts().size() == 0) {
      app.goTo().homePage();
      app.contact().create(new ContactData().
              withFirstname("Kate").withLastname("Smirnova").withAddress("Minsk")
              .inGroup(group), true);
    }

    app.group().deleteFromGroup(group);


    Groups after = app.db().groups();

    assertThat(after, equalTo(before.without(groupInTopList).withAdded(group.withContacts(contacts).withId(after.stream().mapToInt((c) -> c.getId()).max().getAsInt()))));
  }
}



@@ -797,286 +797,8 @@
   %7D%0A
-%0A//    if (app.db().contacts().size() == 0) %7B%0A//      app.goTo().homePage();%0A//      app.contact().create(new ContactData().%0A//              withFirstname(%22Anna%22).withLastname(%22Ivanova%22).withAddress(%22Address%22)%0A//              .inGroup(groups.iterator().next()), true);%0A//    %7D%0A%0A
     
@@ -1061,35 +1061,170 @@
 with
-Name(groupInTopList.getName
+Id(groupInTopList.getId())%0A            .withName(groupInTopList.getName()).withHeader(groupInTopList.getHeader()).%0A            withFooter(groupInTopList.getFooter
 ());
@@ -1478,17 +1478,16 @@
 ;%0A    %7D%0A
-%0A
     app.
@@ -1518,18 +1518,16 @@
 group);%0A
-%0A%0A
     Grou
@@ -1633,16 +1633,28 @@
 ed(group
+)));%0A%0A//    
 .withCon
@@ -1668,16 +1668,31 @@
 ontacts)
+%0A//            
 .withId(
@@ -1753,17 +1753,16 @@
 nt()))))
-;
 %0A  %7D%0A%7D%0A%0A

Fix for testGroupRemoveContact.