package ru.stqa.pft.addressbook.tests;

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

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

public class GroupDeletionTests extends TestBase {

    @BeforeMethod
    public void ensurePrecondition() {
        app.navigateTo().groupPage();
        if (! app.group().doesExist()) {
            app.group().create(new GroupData().withName("test"));
        }
    }

    @Test
    public void testGroupDeletion() {

        Groups groupsBefore = app.group().all();
        GroupData deletedGroup = groupsBefore.getRandom();

        app.group().deleteById(deletedGroup.getId());

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

}

@@ -431,78 +431,86 @@
     
-app.navigateTo().groupPage();%0A        if (! app.group().doesExist()) %7B
+if (app.db().groups().size() == 0) %7B%0A            app.navigateTo().groupPage();
 %0A   
@@ -637,16 +637,53 @@
 ion() %7B%0A
+        app.navigateTo().groupPage();
 %0A       
@@ -705,35 +705,35 @@
 efore = app.
-group().all
+db().groups
 ();%0A        
@@ -981,19 +981,19 @@
 app.
-group().all
+db().groups
 ();%0A

add db checking to group deletion tests