package ru.sfwt.mt.addressbook.tests;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import ru.sfwt.mt.addressbook.model.GroupData;
import ru.sfwt.mt.addressbook.model.Groups;

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

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

public class GroupCreationTests extends TestBase{

  @DataProvider
  public Iterator<Object[]> validGroups() throws IOException {
    List<Object[]> list = new ArrayList<Object[]>();
    BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/groups.csv"));
    String line = reader.readLine();
    while (line != null) {
      String[] split = line.split(";");
      list.add(new Object[] {new GroupData().withName(split[0]).withHeader(split[1]).withFooter(split[2])});
      line = reader.readLine();
    }
    return list.iterator();
  }

  @Test(dataProvider = "validGroups")
  public void testGroupCreation(GroupData group) {
    app.goTo().groupPage();
    Groups before = app.group().all();
    app.group().create(group);
    assertThat(app.group().count(), equalTo(before.size() +1));
    Groups after = app.group().all();
    assertThat(after, equalTo(
            before.withAdded(group.withId(after.stream().mapToInt((g) -> g.getId()).max().getAsInt()))));
  }


  @Test
  public void testBadGroupCreation() {
    app.goTo().groupPage();
    Groups before = app.group().all();
    GroupData group = new GroupData().withName("test2'");
    app.group().create(group);
    assertThat(app.group().count(), equalTo(before.size()));
    Groups after = app.group().all();
    assertThat(after, equalTo(before));
  }
}

@@ -28,24 +28,65 @@
 ook.tests;%0A%0A
+import com.thoughtworks.xstream.XStream;%0A
 import org.t
@@ -381,16 +381,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -656,61 +656,8 @@
 n %7B%0A
-    List%3CObject%5B%5D%3E list = new ArrayList%3CObject%5B%5D%3E();%0A
     
@@ -745,16 +745,37 @@
 ups.
-csv
+xml
 %22));%0A
+    String xml = %22%22;%0A
     
@@ -844,204 +844,297 @@
     
-String%5B%5D split = line.split(%22;%22);%0A      list.add(new Object%5B%5D %7Bnew GroupData().withName(split%5B0%5D).withHeader(split%5B1%5D).withFooter(split%5B2%5D)%7D);%0A      line = reader.readLine();%0A    %7D%0A    return list
+xml += line;%0A      line = reader.readLine();%0A    %7D%0A    XStream xstream = new XStream();%0A    xstream.processAnnotations(GroupData.class);%0A    List%3CGroupData%3E groups = (List%3CGroupData%3E)xstream.fromXML(xml);%0A    return groups.stream().map((g) -%3E new Object%5B%5D %7Bg%7D).collect(Collectors.toList())
 .ite

Read test data from xml file. GroupCreationTests