package ru.stqa.jft.addressbook.tests;


import org.apache.xpath.SourceTree;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import ru.stqa.jft.addressbook.model.ContactData;
import ru.stqa.jft.addressbook.model.Contacts;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

public class ContactCreationTests extends TestBase {

    @DataProvider
    public Iterator<Object[]> validContacts() throws IOException {
        List<Object[]> list = new ArrayList<Object[]>();
        BufferedReader reader = new BufferedReader(new FileReader(new File("src/test/resources/contacts.csv")));
        String line = reader.readLine();
        while (line != null) {
            String[] split = line.split(";");
            list.add(new Object[] {new ContactData().withName(split[0]).withSurname(split[1]).withAddress(split[2])
                    .withHomeNumber(split[3]).withMobileNumber(split[4]).withWorkNumber(split[5]).withEmail1(split[6])
                    .withGroup(split[7])});
            line = reader.readLine();
        }
        return list.iterator();
    }

    @Test (dataProvider = "validContacts")
    public void ContactCreationTests(ContactData contact) {
        Contacts before = app.contact().all();
        app.goTo().newContact();
        // File photo = new File("src/test/resources/02.jpg");
        app.contact().create(contact);
        app.contact().returnToHomepage();
        assertThat(app.contact().count(), equalTo(before.size()+1));
        Contacts after = app.contact().all();
        assertThat(after, equalTo(
                before.withAdded(contact.withId(after.stream().mapToInt((c) -> c.getId()).max().getAsInt()))));
    }

}

@@ -34,16 +34,57 @@
 ests;%0A%0A%0A
+import com.thoughtworks.xstream.XStream;%0A
 import o
@@ -384,16 +384,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -823,17 +823,42 @@
 cts.
-csv
+xml
 %22)));%0A
+        String xml = %22%22;%0A
     
@@ -941,157 +941,58 @@
     
-String%5B%5D split = line.split(%22;%22);%0A            list.add(new Object%5B%5D %7Bnew ContactData().withName(split%5B0%5D).withSurname(split%5B1%5D).withAddress(split%5B2%5D)
+xml += line;%0A            line = reader.readLine();
 %0A   
@@ -992,32 +992,34 @@
 ();%0A        
+%7D%0A
         
     .withHom
@@ -1010,222 +1010,265 @@
     
-    .withHomeNumber(split%5B3%5D).withMobileNumber(split%5B4%5D).withWorkNumber(split%5B5%5D).withEmail1(split%5B6%5D)%0A                    .withGroup(split%5B7%5D)%7D);%0A            line = reader.readLine(
+XStream xstream = new XStream();%0A        xstream.processAnnotations(ContactData.class);%0A        List%3CContactData%3E contacts = (List%3CContactData%3E) xstream.fromXML(xml
 );%0A  
- 
-     %7D%0A
       
-  
 return 
-list
+contacts.stream().map((c) -%3E new Object%5B%5D %7Bc%7D).collect(Collectors.toList())
 .ite

read data from xml file (contacts)