package ru.stqa.pft.addressbook.tests;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import ru.stqa.pft.addressbook.model.GroupData;
import ru.stqa.pft.addressbook.model.Groups;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

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

public class GroupCreationTests extends TestBase {

    @DataProvider
    public Iterator<Object[]> validGroups() {
        List<Object[]> list = new ArrayList<Object[]>();
        list.add(new Object[] {new GroupData().withName("t1").withHeader("h1").withFooter("f1")});
        list.add(new Object[] {new GroupData().withName("t2").withHeader("h1").withFooter("f1")});
        list.add(new Object[] {new GroupData().withName("t3").withHeader("h1").withFooter("f1")});
        return list.iterator();
    }

    @Test(dataProvider = "validGroups")
    public void testGroupCreation(GroupData group) {
        app.navigateTo().groupPage();
        Groups groupsBefore = app.group().all();

        app.group().create(group);

        // assertions
        assertThat(app.group().getGroupCount(), equalTo(groupsBefore.size() + 1));
        Groups groupsAfter = app.group().all();
        group.withId(groupsAfter.stream().mapToInt((g) -> g.getId()).max().getAsInt());
        assertThat(groupsAfter, equalTo(groupsBefore.withAdded(group)));
    }

    @Test
    public void testBadGroupCreation() {
        app.navigateTo().groupPage();
        Groups groupsBefore = app.group().all();

        GroupData group = new GroupData()
                .withName("test'" + rnd.getInt(0, 1000));
        app.group().create(group);

        // assertions
        assertThat(app.group().getGroupCount(), equalTo(groupsBefore.size()));
        Groups groupsAfter = app.group().all();
        assertThat(groupsAfter, equalTo(groupsBefore));
    }

}

@@ -203,24 +203,42 @@
 el.Groups;%0A%0A
+import java.io.*;%0A
 import java.
@@ -546,16 +546,35 @@
 Groups()
+ throws IOException
  %7B%0A     
@@ -637,96 +637,108 @@
     
-list.add(new Object%5B%5D %7Bnew GroupData().withName(%22t1%22).withHeader(%22h1%22).withFooter(%22f1%22)%7D
+BufferedReader reader = new BufferedReader(new FileReader(new File(%22src/test/resources/groups.csv%22))
 );%0A 
@@ -748,99 +748,122 @@
     
-list.add(new Object%5B%5D %7Bnew GroupData().withName(%22t2%22).withHeader(%22h1%22).withFooter(%22f1%22)%7D);%0A
+String line = reader.readLine();%0A        while (line != null) %7B%0A            String%5B%5D split = line.split(%22;%22);%0A    
     
@@ -918,12 +918,16 @@
 ame(
-%22t3%22
+split%5B0%5D
 ).wi
@@ -939,12 +939,16 @@
 der(
-%22h1%22
+split%5B1%5D
 ).wi
@@ -960,16 +960,68 @@
 ter(
-%22f1%22)%7D);
+split%5B2%5D)%7D);%0A            line = reader.readLine();%0A        %7D
 %0A   

load groups from file