package hu.rbr.sfinapp.account;

import hu.rbr.sfinapp.IntegrationTestBase;
import org.junit.Test;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.GenericType;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

public class AccountIT extends IntegrationTestBase {

    private static class AccountListType extends GenericType<List<Account>> {}

    @Test
    public void addAndQuery() throws Exception {
        List<Account> accounts = webTarget("accounts")
                .request()
                .get(new AccountListType());

        assertThat(accounts.size(), equalTo(0));

        Account acc1 = createAccount("1-name", "1-desc", false);
        Account acc2 = createAccount("2-name", "2-desc", true);

        accounts = webTarget("accounts")
                .request()
                .get(new AccountListType());

        assertThat(accounts.size(), equalTo(2));
        assertThat(accounts.get(0), new AccountMatcher(acc1));
        assertThat(accounts.get(1), new AccountMatcher(acc2));


        Integer acc1Id = accounts.get(0).id;

        Account account = webTarget("accounts")
                .path(acc1Id.toString())
                .request()
                .get(Account.class);

        assertThat(account.id, equalTo(acc1Id));
        assertThat(account, new AccountMatcher(acc1));
    }

    private Account createAccount(String name, String description, boolean technical) {
        Account newAccount = new Account();
        newAccount.name = name;
        newAccount.description = description;
        newAccount.technical = technical;

        webTarget("accounts")
            .request()
            .post(Entity.json(newAccount));

        return newAccount;
    }

}

@@ -151,48 +151,231 @@
 rs.c
-ore.GenericType;%0Aimport java.util.List;%0A
+lient.WebTarget;%0Aimport javax.ws.rs.core.GenericType;%0Aimport javax.ws.rs.core.Response;%0Aimport java.util.List;%0A%0Aimport static javax.ws.rs.core.Response.Status.NOT_MODIFIED;%0Aimport static javax.ws.rs.core.Response.Status.OK;
 %0Aimp
@@ -682,33 +682,40 @@
 unt%3E accounts = 
-w
+accountW
 ebTarget(%22accoun
@@ -703,34 +703,24 @@
 ntWebTarget(
-%22accounts%22
 )%0A          
@@ -981,17 +981,24 @@
 ounts = 
-w
+accountW
 ebTarget
@@ -994,34 +994,24 @@
 ntWebTarget(
-%22accounts%22
 )%0A          
@@ -1322,17 +1322,24 @@
 count = 
-w
+accountW
 ebTarget
@@ -1335,34 +1335,24 @@
 ntWebTarget(
-%22accounts%22
 )%0A          
@@ -1558,16 +1558,969 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void etagVersion() throws Exception %7B%0A        createAccount(%221-name%22, %221-desc%22, false);%0A%0A        Response firstResponse = accountWebTarget()%0A                .request()%0A                .get();%0A%0A        assertThat(firstResponse.getStatus(), equalTo(OK.getStatusCode()));%0A%0A%0A        Response secondResponse = accountWebTarget()%0A                .request()%0A                .header(%22If-None-Match%22, firstResponse.getEntityTag())%0A                .get();%0A%0A        assertThat(secondResponse.getStatus(), equalTo(NOT_MODIFIED.getStatusCode()));%0A%0A%0A        createAccount(%222-name%22, %222-desc%22, true);%0A%0A        Response thirdResponse = accountWebTarget()%0A                .request()%0A                .header(%22If-None-Match%22, firstResponse.getEntityTag())%0A                .get();%0A%0A        assertThat(thirdResponse.getStatus(), equalTo(OK.getStatusCode()));%0A    %7D%0A%0A    private WebTarget accountWebTarget() %7B%0A        return webTarget(%22accounts%22);%0A    %7D%0A%0A
     priv
@@ -2772,17 +2772,24 @@
         
-w
+accountW
 ebTarget
@@ -2789,26 +2789,16 @@
 bTarget(
-%22accounts%22
 )%0A      

Test account ETag versioning