package cz.muni.fi.pv243.test;

import static org.junit.Assert.*;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.extension.rest.client.ArquillianResteasyResource;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import cz.muni.fi.pv243.model.Author;
import cz.muni.fi.pv243.model.Book;
import cz.muni.fi.pv243.rest.BookEndpoint;
import cz.muni.fi.pv243.service.BookService;

@RunWith(Arquillian.class)
public class RestTest {
    @Deployment(testable = false)
    public static Archive<?> createDeployment() {
        return ShrinkWrap.create(WebArchive.class, "test.war")
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addPackage(Book.class.getPackage())
                .addPackage(BookService.class.getPackage())
                .addPackage(BookEndpoint.class.getPackage())
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }

    @Test
    @RunAsClient
    public void testCreateBookWithAuthor(@ArquillianResteasyResource("") WebTarget webTarget) throws Exception {
        Author author = new Author();
        author.setName("Foo");
        author.setSurname("Bar");
        Response authorResponse = webTarget
                .path("authors")
                .request(MediaType.APPLICATION_JSON)
                .post(Entity.json(author));
        assertEquals(Response.Status.CREATED.getStatusCode(), authorResponse.getStatus());
        Author outAuthor = authorResponse.readEntity(Author.class);
        Book book = new Book();
        book.setTitle("Book");
        book.setISBN("1234");
        book.getAuthors().add(outAuthor);
        Response bookResponse = webTarget
                .path("books")
                .request(MediaType.APPLICATION_JSON)
                .post(Entity.json(book));
        assertEquals(Response.Status.CREATED.getStatusCode(), bookResponse.getStatus());
        Book outBook = bookResponse.readEntity(Book.class);
        assertNotNull(outBook.getId());

        bookResponse = webTarget
                .path("books/" + outBook.getId())
                .request(MediaType.APPLICATION_JSON)
                .get();
        assertEquals(Response.Status.OK.getStatusCode(), bookResponse.getStatus());
        outBook = bookResponse.readEntity(Book.class);
        assertEquals("Book", outBook.getTitle());
        assertEquals(1, outBook.getAuthors().size());
        assertEquals("Foo", outBook.getAuthors().get(0).getName());
    }
}

@@ -60,16 +60,40 @@
 ert.*;%0A%0A
+import java.util.List;%0A%0A
 import j
@@ -783,16 +783,54 @@
 l.Book;%0A
+import cz.muni.fi.pv243.model.Volume;%0A
 import c
@@ -2989,11 +2989,1260 @@
 );%0A    %7D
+%0A%0A    @Test%0A    @RunAsClient%0A    public void testCreateBookWithVolumes(@ArquillianResteasyResource(%22%22) WebTarget webTarget) throws Exception %7B%0A        Volume volume = new Volume();%0A        volume.setBarcodeId(123);%0A        Book book = new Book();%0A        book.setTitle(%22Book%22);%0A        book.setISBN(%221234%22);%0A        book.getVolumes().add(volume);%0A        Response bookResponse = webTarget%0A                .path(%22books%22)%0A                .request(MediaType.APPLICATION_JSON)%0A                .post(Entity.json(book));%0A        assertEquals(Response.Status.CREATED.getStatusCode(), bookResponse.getStatus());%0A        Book outBook = bookResponse.readEntity(Book.class);%0A        assertNotNull(outBook.getId());%0A%0A        bookResponse = webTarget%0A                .path(%22books/%22 + outBook.getId())%0A                .request(MediaType.APPLICATION_JSON)%0A                .get();%0A        assertEquals(Response.Status.OK.getStatusCode(), bookResponse.getStatus());%0A        outBook = bookResponse.readEntity(Book.class);%0A        assertEquals(%22Book%22, outBook.getTitle());%0A        List%3CVolume%3E volumes = outBook.getVolumes();%0A        assertEquals(1, volumes.size());%0A        Volume outVolume = volumes.get(0);%0A        assertEquals(123, outVolume.getBarcodeId());%0A    %7D
 %0A%7D%0A

Test for creating book with volumes