package ru.stqa.pft.addressbook.tests;

import org.testng.annotations.BeforeClass;
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 java.io.File;

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

public class ContactCreationTests extends TestBase {
    private String groupName = "test1";

    @BeforeClass
    public void ensurePreconditions() {
        app.navigateTo().groupPage();
        if (! app.group().doesExist(groupName)) {
            GroupData groupData = new GroupData().withName(groupName);
            app.group().create(groupData);
        }
    }
    
    @Test
    public void testContactCreation() {
        app.navigateTo().home();
        Contacts contactsBefore = app.contact().all();
        File photo = new File("src/test/resources/vinni.jpeg");
        ContactData contact = ContactData.getWithRandomData()
                .withGroup(groupName)
                .withPhoto(photo);
        app.contact().create(contact);

        // assertions
        assertThat(app.contact().getContactCount(), equalTo(contactsBefore.size() + 1));
        Contacts contactsAfter = app.contact().all();
        contact.withId(contactsAfter.stream().mapToInt((c) -> c.getId()).max().getAsInt());
        assertThat(contactsAfter, equalTo(contactsBefore.withAdded(contact)));
    }

}

@@ -44,42 +44,127 @@
 ort 
-org.testng.annotations.BeforeClass
+com.thoughtworks.xstream.XStream;%0Aimport org.testng.annotations.BeforeClass;%0Aimport org.testng.annotations.DataProvider
 ;%0Aim
@@ -362,210 +362,1128 @@
 .io.
-File;%0A%0Aimport static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0A%0Apublic class ContactCreationTests extends TestBase %7B%0A    private String groupName = %22test1%22;
+BufferedReader;%0Aimport java.io.File;%0Aimport java.io.FileReader;%0Aimport java.io.IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.stream.Collectors;%0A%0Aimport static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0A%0Apublic class ContactCreationTests extends TestBase %7B%0A    private String groupName = %22test1%22;%0A%0A    @DataProvider%0A    public Iterator%3CObject%5B%5D%3E validContactsFromXml() throws IOException %7B%0A        List%3CObject%5B%5D%3E list = new ArrayList%3CObject%5B%5D%3E();%0A        BufferedReader reader = new BufferedReader(new FileReader(new File(%22src/test/resources/contacts.xml%22)));%0A        String xml = %22%22;%0A        String line = reader.readLine();%0A        while (line != null) %7B%0A            xml += line;%0A            line = reader.readLine();%0A        %7D%0A%0A        XStream xStream = new XStream();%0A        xStream.processAnnotations(ContactData.class);%0A        List%3CContactData%3E contacts = (List%3CContactData%3E) xStream.fromXML(xml);%0A        return contacts.stream().map((c) -%3E new Object%5B%5D %7Bc%7D).collect(Collectors.toList()).iterator();%0A    %7D
 %0A%0A  
@@ -1769,16 +1769,55 @@
    @Test
+(dataProvider = %22validContactsFromXml%22)
 %0A    pub
@@ -1845,16 +1845,35 @@
 reation(
+ContactData contact
 ) %7B%0A    
@@ -2028,28 +2028,16 @@
         
-ContactData 
 contact 
@@ -2042,39 +2042,15 @@
 t = 
-C
+c
 ontact
-Data.getWithRandomData()
 %0A   

load contacts data from xml file in ContactCreationTests