package ru.stqa.pft.addressbook.tests;

import org.testng.Assert;
import org.testng.annotations.Test;
import ru.stqa.pft.addressbook.appmanager.HelperBase.FormAction;
import ru.stqa.pft.addressbook.model.ContactData;
import ru.stqa.pft.addressbook.model.GroupData;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

public class ContactCreationTests extends TestBase {
    
    @Test
    public void testContactCreation() {
        String groupName = "test1";

        app.getNavigationHelper().gotoGroupPage();
        app.getNavigationHelper().isOnGroupPage();
        if (! app.getGroupHelper().isThereAGroup(groupName)) {
            GroupData groupData = new GroupData(groupName, null, null);
            app.getGroupHelper().createGroup(groupData);
        }

        app.getNavigationHelper().gotoHome();
        List<ContactData> contactsBefore = app.getContactHelper().getContactList();

        ContactData contact = new ContactData();
        contact
                .setId(Integer.MAX_VALUE)
                .setFirstname("Yan")
                .setMiddlename("E.")
                .setLastname("Doe")
                .setTitle("Dr.")
                .setAddress("221B Baker Street London")
                .setHomePhone("9(2131)324-33-33")
                .setEmail("test@test.ts")
                .setGroup(groupName);
        app.getContactHelper().createContact(contact);

        List<ContactData> contactsAfter = app.getContactHelper().getContactList();
        Assert.assertEquals(contactsAfter.size(), contactsBefore.size() + 1);

        contactsBefore.add(contact);
        Comparator<? super ContactData> byId = (g1, g2) -> Integer.compare(g1.getId(), g2.getId());
        contactsBefore.sort(byId);
        contactsAfter.sort(byId);
        Assert.assertEquals(contactsAfter, contactsBefore);
    }

}

@@ -59,16 +59,60 @@
 Assert;%0A
+import org.testng.annotations.BeforeMethod;%0A
 import o
@@ -443,18 +443,62 @@
     
-%0A    @Test
+private String groupName = %22test1%22;%0A%0A    @BeforeMethod
 %0A   
@@ -514,69 +514,32 @@
 oid 
-testContactCrea
+ensurePrecondi
 tion
+s
 () %7B%0A
-        String groupName = %22test1%22;%0A%0A
     
@@ -881,24 +881,85 @@
 gotoHome();%0A
+    %7D%0A    %0A    @Test%0A    public void testContactCreation() %7B%0A
         List

add method ensurePreconditions() to ContactCreationTests class