3 Replies Latest reply on Apr 17, 2013 3:12 PM by Bartosz Majsak

    End to End tests ReST with APE.

    Eduardo de Vera Toquero Newbie

      I want to be able to do an end to end test, starting with the ReST web services and ending with Database verification. I want to be able to seed the database with Arquillian Persistence Extension based on DbUnit, call the Web Service and do a match afterwards again with APE. For some unknown reason, Arquillian does not seem to be seeding the database correctly.

       

      The tests looks like:

      package com.edevera.iexchange.ws;
      
      
      import java.net.URISyntaxException;
      import java.net.URL;
      import java.util.UUID;
      
      
      import com.edevera.iexchange.entities.User;
      import org.jboss.arquillian.container.test.api.Deployment;
      import org.jboss.arquillian.container.test.api.RunAsClient;
      import org.jboss.arquillian.junit.Arquillian;
      import org.jboss.arquillian.persistence.UsingDataSet;
      import org.jboss.arquillian.test.api.ArquillianResource;
      import org.jboss.resteasy.client.ProxyFactory;
      import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
      import org.jboss.resteasy.spi.ResteasyProviderFactory;
      import org.jboss.shrinkwrap.api.Archive;
      import org.jboss.shrinkwrap.api.ShrinkWrap;
      import org.jboss.shrinkwrap.api.formatter.Formatters;
      import org.jboss.shrinkwrap.api.spec.WebArchive;
      import org.jboss.shrinkwrap.resolver.api.maven.Maven;
      import org.junit.Before;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      
      
      import static org.hamcrest.CoreMatchers.equalTo;
      import static org.hamcrest.CoreMatchers.is;
      import static org.hamcrest.CoreMatchers.notNullValue;
      import static org.hamcrest.CoreMatchers.nullValue;
      import static org.junit.Assert.assertThat;
      
      
      /**
       * @author <a href="mailto:eduardo.devera@gmail.com">Eduardo de Vera</a>
       */
      @RunWith(Arquillian.class)
      @UsingDataSet
      public class ReStUserServiceIT {
      
      
          @Deployment
          public static Archive<?> createDeployment() {
              final WebArchive result =  ShrinkWrap.create(WebArchive.class, "iexchange.war")
                      .addPackage("com.edevera.iexchange.ws")
                      .addAsLibrary(
                              Maven.resolver()
                                      .loadPomFromFile("pom.xml").resolve("com.edevera.iexchange:com.edevera.iexchange.model:1.0-SNAPSHOT")
                                      .withoutTransitivity().asSingleFile()
                      )
                      .addAsLibrary(
                              Maven.resolver()
                                      .loadPomFromFile("pom.xml").resolve("com.edevera.iexchange:com.edevera.iexchange.core:1.0-SNAPSHOT")
                                      .withoutTransitivity().asSingleFile()
                      )
                      .addAsWebInfResource("jboss-ds.xml")
                      .addAsWebInfResource("beans.xml")
                      //.addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml")
                      ;
              result.writeTo(System.out, Formatters.VERBOSE);
              System.out.println();
              return result;
          }
      
      
          @Before
          public  void setUp() throws Exception {
              RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
          }
      
      
          @Test
          @RunAsClient
          public void testViewUser(@ArquillianResource final URL url) throws URISyntaxException {
              final ReStUserService client = ProxyFactory.create(ReStUserService.class, url.toString());
              final String username = "existingUsername";
              final User user = client.viewUser(username);
              assertThat("The user must exist", user, is(notNullValue()));
              assertThat("The user name must be the expected", user.getName(), is(equalTo("User Name")));
          }
      
      
          @Test
          @RunAsClient
          public void testCreateUser(@ArquillianResource final URL url) throws Exception {
              final ReStUserService client = ProxyFactory.create(ReStUserService.class, url.toString());
      
      
              final String email = "user@domain.com";
              final String nameOfTheUser = "User Name";
              final String businessKey = UUID.randomUUID().toString();
      
              final User user = new User();
              user.setEmail(email);
              user.setName(nameOfTheUser);
              user.setBusinessKey(businessKey);
      
              client.registerUser(user);
              assertThat(user.getBusinessKey(), is(equalTo(user.getBusinessKey())));
      
              final User result = client.viewUser(user.getBusinessKey());
              assertThat("The user must exist", result, notNullValue());
              assertThat("The business key of the user ", result.getBusinessKey(), is(equalTo(businessKey)));
              assertThat("The email must be the expected", result.getEmail(), is(equalTo(email)));
              assertThat("The name must be the expected", result.getName(), is(equalTo(nameOfTheUser)));
          }
      
      
          @Test
          @RunAsClient
          public void testUpdateUser(@ArquillianResource final URL url) throws Exception {
              final ReStUserService client = ProxyFactory.create(ReStUserService.class, url.toString());
      
              final User user = client.viewUser("existingUsername");
              assertThat("The user must exist in order to be able to update it.", user, is(notNullValue()));
      
              user.setName("New Name"); 
      
              final User result = client.updateUser(user);
      
              assertThat("The business key of the user must not have changed.", result.getBusinessKey(), is(equalTo(user.getBusinessKey())));
              assertThat("The name of the user must have changed.", result.getName(), is(equalTo("New Name")));
          }
      
      
          @Test
          @RunAsClient
          public void testBanUser(@ArquillianResource final URL url) throws Exception {
              final ReStUserService client = ProxyFactory.create(ReStUserService.class, url.toString());
              final String existingUsername = "existingUsername";
      
              client.banUser(existingUsername);
      
              final User unexistingUser = client.viewUser(existingUsername);
              assertThat("The user should have been deleted and null should be returned.", unexistingUser, is(nullValue()));
          }
      }
      
      

       

      Has someone taken the same approach for testing their ReST services? Am I doing something wrong?

       

      Edited: just saw ticket https://issues.jboss.org/browse/ARQ-1077 and realized this is a know problem. The question now is, does Arquillian want to enable such testing?