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 static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Created by gis on 09.11.2016.
 */
public class ContactModificationTests extends TestBase {

    @BeforeMethod
    public void ensurePreconditions() {
        if (app.db().contacts().size() == 0) {
            app.navigateTo().home();
            ContactData contactData = ContactData.getWithRandomData();
            app.contact().create(contactData);
        }
    }

    @Test
    public void testContactModification() {
        Contacts contactsBefore = app.db().contacts();

        ContactData contactOld = contactsBefore.getRandom();
        ContactData contactNew = ContactData.getWithRandomData()
                .withId(contactOld.getId());

        app.navigateTo().home();
        app.contact().modifyById(contactOld.getId(), contactNew);

        // assertions
        assertThat(app.contact().getContactCount(), equalTo(contactsBefore.size()));
        Contacts contactsAfter = app.db().contacts();
        assertThat(contactsAfter, equalTo(contactsBefore.withModified(contactOld, contactNew)));
        verifyContactListInUI();
    }

}

@@ -209,16 +209,64 @@
 ontacts;
+%0Aimport ru.stqa.pft.addressbook.model.GroupData;
 %0A%0Aimport
@@ -1277,24 +1277,130 @@
 contacts();%0A
+        for (GroupData group : contactOld.getGroups()) %7B%0A            contactNew.inGroup(group);%0A        %7D%0A
         asse

fix ContactModificationTests - add groups of old contact to new one