package ru.sfwt.mt.addressbook.tests;

import com.thoughtworks.xstream.XStream;
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 java.util.stream.Collectors;

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

public class GroupCreationTests extends TestBase{

  @DataProvider
  public Iterator<Object[]> validGroups() throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/groups.xml"));
    String xml = "";
    String line = reader.readLine();
    while (line != null) {
      xml += line;
      line = reader.readLine();
    }
    XStream xstream = new XStream();
    xstream.processAnnotations(GroupData.class);
    List<GroupData> groups = (List<GroupData>)xstream.fromXML(xml);
    return groups.stream().map((g) -> new Object[] {g}).collect(Collectors.toList()).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,95 @@
 ook.tests;%0A%0A
+import com.google.gson.Gson;%0Aimport com.google.gson.reflect.TypeToken;%0A
 import com.t
@@ -144,16 +144,16 @@
 Stream;%0A
-
 import o
@@ -699,16 +699,23 @@
 idGroups
+FromXml
 () throw
@@ -1225,16 +1225,571 @@
 );%0A  %7D%0A%0A
+  @DataProvider%0A  public Iterator%3CObject%5B%5D%3E validGroupsFromJson() throws IOException %7B%0A    BufferedReader reader = new BufferedReader(new FileReader(%22src/test/resources/groups.json%22));%0A    String json = %22%22;%0A    String line = reader.readLine();%0A    while (line != null) %7B%0A      json += line;%0A      line = reader.readLine();%0A    %7D%0A    Gson gson = new Gson();%0A    List%3CGroupData%3E groups = gson.fromJson(json, new TypeToken%3CList%3CGroupData%3E%3E()%7B%7D.getType());%0A    return groups.stream().map((g) -%3E new Object%5B%5D %7Bg%7D).collect(Collectors.toList()).iterator();%0A  %7D%0A%0A
   @Test(
@@ -1815,16 +1815,24 @@
 idGroups
+FromJson
 %22)%0A  pub

Read test data from json. GroupCreationTests