package ru.sfwt.mt.addressbook.tests;

import com.thoughtworks.xstream.XStream;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import ru.sfwt.mt.addressbook.model.AddressData;
import ru.sfwt.mt.addressbook.model.Addresses;

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 AddressCreationTests extends TestBase{

  @DataProvider
  public Iterator<Object[]> validContact() throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/address.xml"));
    String xml ="";
    String line = reader.readLine();
    while (line != null) {
      xml += line;
      line = reader.readLine();
    }
    XStream xstream = new XStream();
    xstream.processAnnotations(AddressData.class);
    List<AddressData> addresses = (List<AddressData>)xstream.fromXML(xml);
    return addresses.stream().map((ad) -> new Object[] {ad}).collect(Collectors.toList()).iterator();
  }

  @DataProvider
  public Iterator<Object[]> validContactCsv() throws IOException {
    List<Object[]> list = new ArrayList<Object[]>();
    BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/address.csv"));
    String line = reader.readLine();
    while (line != null) {
      String[] split = line.split(";");
      list.add(new Object[] {new AddressData().withFirstname(split[0]).withLastname(split[1]).withGroup("test1")});
      line = reader.readLine();
    }

    return list.iterator();

  }
  @Test(dataProvider = "validContact")
  public void testAddressCreation(AddressData address) {
    app.goTo().homePage();
    Addresses before = app.contact().all();
    app.goTo().addressCreationPage();
    app.contact().create(address);
    app.goTo().homePage();
    assertThat(app.contact().count(), equalTo(before.size() +1));
    Addresses after = app.contact().all();
    assertThat(after, equalTo(before.withAdded(
            address.withId(after.stream().mapToInt((ad) -> ad.getId()).max().getAsInt()))));
  }
  @Test
  public void testAddressCreationPhoto() {
    app.goTo().homePage();
    Addresses before = app.contact().all();
    app.goTo().addressCreationPage();
    File photo = new File("/src/test/resources/images.jpg");
    AddressData address = new AddressData().withFirstname("name").withLastname("last").withPhoto(photo).withGroup("test1");
    app.contact().create(address);
    app.goTo().homePage();
    assertThat(app.contact().count(), equalTo(before.size() +1));
    Addresses after = app.contact().all();
    assertThat(after, equalTo(before.withAdded(
            address.withId(after.stream().mapToInt((ad) -> ad.getId()).max().getAsInt()))));
  }

  @Test (enabled = false)
  public void testBadAddressCreation() {
    app.goTo().homePage();
    Addresses before = app.contact().all();
    app.goTo().addressCreationPage();
    AddressData address = new AddressData().withFirstname("name'").withLastname("last").withGroup("test1");
    app.contact().create(address);
    app.goTo().homePage();
    assertThat(app.contact().count(), equalTo(before.size()));
    Addresses after = app.contact().all();
    assertThat(after, equalTo(before));
  }

  @Test (enabled = false)
  public void testCurrentDir() {
    File currentDir = new File(".");
    System.out.println(currentDir.getAbsolutePath());
    File photo = new File("/src/test/resources/images.jpg");
    System.out.println(photo.getAbsolutePath());
    System.out.println(photo.exists());
  }

}

@@ -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
@@ -319,16 +319,63 @@
 dresses;
+%0Aimport ru.sfwt.mt.addressbook.model.GroupData;
 %0A%0Aimport
@@ -713,16 +713,591 @@
 dContact
+FromJson() throws IOException %7B%0A    BufferedReader reader = new BufferedReader(new FileReader(%22src/test/resources/address.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%3CAddressData%3E addresses = gson.fromJson(json, new TypeToken%3CList%3CAddressData%3E%3E()%7B%7D.getType());%0A    return addresses.stream().map((ad) -%3E new Object%5B%5D %7Bad%7D).collect(Collectors.toList()).iterator();%0A  %7D%0A%0A  @DataProvider%0A  public Iterator%3CObject%5B%5D%3E validContactFromXml
 () throw
@@ -1877,16 +1877,20 @@
 dContact
+From
 Csv() th
@@ -2347,24 +2347,24 @@
 tor();%0A%0A  %7D%0A
-
   @Test(data
@@ -2387,16 +2387,24 @@
 dContact
+FromJson
 %22)%0A  pub

Read test data from json. AddressCreationTests