3 Replies Latest reply on Jul 22, 2016 8:07 AM by Giuseppe D'Elia

    Arquillian tests on Glassfish 4.1 using Apache POI

    Giuseppe D'Elia Newbie

      Hi,

      I'm new with Arquillian so I don't know if I making some stupid errors anyway what I'm doing is to load some data from Excel. Each row in Excel will be a test for me. I get this behaviour using @Rule (I know that Arquillian doesn't support @Parameterized annotation). Of course I'm using Apache POI to read from Excel and build by objects.

      After I created my objects I will call a REST service PUT running in a remote container (Glassfish 4.1) which is in my local machine actually.

       

      FIrst issue I noticed is that the glassfish-adapter embedded provides Jersey 2.x instead if I use glassfish-adapter remote I have Jersey 1.x. Except for that once I run my test I get this error:

      "

      org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException

       

       

        at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:65)

        at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:601)

        at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:174)

        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:279)

        at com.bmw.ca.wisp.data.ExcelData.loadExcel(ExcelData.java:50)

        at com.bmw.ca.wisp.data.ExcelData.<init>(ExcelData.java:41)

        at com.bmw.ca.wisp.test.ExcelHelper.loadData(ExcelHelper.java:19)

        at com.bmw.ca.wisp.test.TestWispREST.<init>(TestWispREST.java:120)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)

        at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)

        at org.jboss.arquillian.junit.Arquillian.access$300(Arquillian.java:54)

        at org.jboss.arquillian.junit.Arquillian$6.runReflectiveCall(Arquillian.java:281)

        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

        at org.jboss.arquillian.junit.Arquillian.methodBlock(Arquillian.java:283)

        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

        at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:204)

        at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:426)

        at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54)

        at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:218)

        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

        at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:166)

        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

        at org.junit.runner.JUnitCore.run(JUnitCore.java:115)

        at org.jboss.arquillian.junit.container.JUnitTestRunner.execute(JUnitTestRunner.java:66)

        at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.executeTest(ServletTestRunner.java:170)

        at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.execute(ServletTestRunner.java:135)

        at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.doGet(ServletTestRunner.java:98)

        at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)

        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)

        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)

        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)

        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)

        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)

        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)

        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)

        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)

        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)

        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)

        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)

        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)

        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)

        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)

        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)

        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)

        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)

        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)

        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)

        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)

        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)

        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:526)

        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)

        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)

        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)

        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)

        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)

        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)

        at java.lang.Thread.run(Thread.java:745)

      Caused by: java.lang.reflect.InvocationTargetException

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)

        at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)

        at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:62)

        ... 65 more

      Caused by: java.lang.ExceptionInInitializerError

        at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument.<clinit>(Unknown Source)

        at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)

        at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:85)

        ... 71 more

      Caused by: java.lang.IllegalStateException: Cannot load getContextTypeLoader: verify that xbean.jar is on the classpath

        at org.apache.xmlbeans.XmlBeans.buildMethod(XmlBeans.java:181)

        at org.apache.xmlbeans.XmlBeans.buildNoArgMethod(XmlBeans.java:190)

        at org.apache.xmlbeans.XmlBeans.buildGetContextTypeLoaderMethod(XmlBeans.java:200)

        at org.apache.xmlbeans.XmlBeans.<clinit>(XmlBeans.java:126)

        ... 74 more

      "

       

      This is the code I'm using:

       

      @RunWith(Arquillian.class)

      public class TestWispREST extends RESTClient {

       

         private static final Logger logger = Logger.getLogger(TestWispREST.class.getName());

       

         private static final String URI = "http://localhost:" + Configuration.getPort() + "/wisp-crm-0.0.1-SNAPSHOT/pdiservice/api/v1";

         private WispResponse responseString;

         private ClientResponse clientResponse;

         private Object[] args;

         private MessageFormat mf = new MessageFormat(

         "\n Test: {0} - {1}\n\n Field: \"{2}\".\n Expected result: {3}.\n Actual error code: {4}.\n Actual error message: {5}\n");

       

         @Deployment
         public static JavaArchive createDeployment() {

         final JavaArchive war = ShrinkWrap.create(JavaArchive.class)

        .addPackage(ClientResponse.class.getPackage())

        .addPackage(RESTClient.class.getPackage())

        .addPackage(ExcelData.class.getPackage())

        .addPackage(GeneratorFactory.class.getPackage())

        .addPackage(PdiRequest.class.getPackage())

        .addPackage(ExcelHelper.class.getPackage())

        .addPackage(PdiResource.class.getPackage())

        .addPackage(Filterable.class.getPackage())

        .addPackage(XmlObject.class.getPackage())

        .addPackage(EncryptionDocument.class.getPackage())

        .addPackage(XmlBeans.class.getPackage())

        .addClass(SystemCache.class)

        .addClass(PdiDomainBrandType.class)

        .addClass(PdiDomainGenderType.class)

        .addClass(PdiDomainGenderType.class)

        .addClass(PdiDomainInterfaceParamType.class)

        .addClass(PdiDomainVehicleSalesType.class)

        .addClass(DomainDataEntityBase.class)

        .addClass(DomainDataEntity.class)

        .addClass(PdiPayloadTransaction.class)

        .addClass(PdiPayloadTransactionCustomer.class)

        .addClass(PdiPayloadTransactionDPA.class)

        .addClass(PdiPayloadTransactionMainVehicle.class)

        .addClass(PdiPayloadTransactionModelOfInterest.class)

        .addClass(PdiRequestEnquiresObject.class)

        .addClass(PdiRequestObject.class)

        .addClass(WispConsumerRequestBaseObject.class)

        .addClass(WispRequestHeader.class)

        .addPackages(true, "org.apache.poi", "org.openxmlformats.schemas")

        .addAsResource("test-persistence.xml")

        .addAsResource("test-data/wisp-pdi-testdata.xlsx");

      // .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

         logger.log(Level.INFO, war.toString());

         return war;

        }

       

         @Rule
         public Generator<PdiRequest> request = GeneratorFactory.list(ExcelHelper.loadData());

       

         @Before
        @Override
         public void initializeClient() {

        System.setProperty("javax.xml.bind.context.factory","org.eclipse.persistence.jaxb.JAXBContextFactory");

         super.initializeClient(URI);

        }

       

         @Test
         public void restPdiTest() {

        StringWriter writer = new StringWriter();

         try {

        JAXBContext jc = JAXBContext.newInstance(PdiRequestObject.class);

        Marshaller marshaller = jc.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);

        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");

       

        marshaller.marshal(request.value().getPayload(), writer);

        } catch (PropertyException e) {

         logger.log(Level.SEVERE, e.getMessage());

        } catch (JAXBException e) {

         logger.log(Level.SEVERE, e.getMessage());

        }

       

         logger.log(Level.CONFIG, writer.toString());

       

         if (!writer.toString().isEmpty()) {

         clientResponse = contextURI
         .accept(MediaType.APPLICATION_JSON)

        .header("X-Apisec-Ctxid", request.value().getPayload().getHeader().getAgency())

        .header(HttpHeaders.CONTENT_TYPE, "application/json")

        .put(ClientResponse.class, writer.toString());

       

         responseString = clientResponse.getEntity(WispResponse.class);

        }

       

         // check if not null
         checkResponseNotNull();

       

         // check if valid http status
         checkHttpStatus();

       

         // check if expected error codes
         checkErrorCodes();

        }

       

         private void checkResponseNotNull() {

         assertNotNull("No Response from the server!", responseString);

        }

       

         private void checkHttpStatus() {

       

         int expectedStatus = 200;

         try {

        expectedStatus = Integer.parseInt(request.value().getExpectedResults().getHttpStatus());

        } catch (NumberFormatException ex) {

         args = new Object[]{request.value().getTestId(), request.value().getTestDescription(),

         "HTTP Status", "", expectedStatus, "HTTP Status not valid"};

         fail(mf.format(args));

        }

       

         // check if expected http status
         args = new Object[]{request.value().getTestId(), request.value().getTestDescription(),

         "HTTP Status", clientResponse.getStatus(), expectedStatus, ""};

         assertThat(mf.format(args), clientResponse.getStatus(), is(expectedStatus));

        }

       

         private void checkErrorCodes() {

       

        Map<String, String> responseErrors = new HashMap<>();

         for (WispResponseTransactionDetails details: responseString.getResponse().getTransactions()) {

         for (WispTransactionError wispTransactionError: details.getErrors()) {

        responseErrors.put(wispTransactionError.getCode(), wispTransactionError.getDesc());

        }

        }

       

      // assertThat(request.getExpectedResults().getErrorsCode(), IsIterableContainingInOrder.contains(responseErrors.keySet().toArray()));

         responseErrors.keySet().stream().forEach(actualError -> {

         assertTrue(mf.format(new Object[]{request.value().getTestId(), request.value().getTestDescription(),

         "Error Code", request.value().getExpectedResults().getErrorsCode(), responseErrors.keySet(), responseErrors.values()}),

         request.value().getExpectedResults().getErrorsCode().contains(actualError));

        });

        }

       

         @After
         public void closeResponse() {

         clientResponse.close();

        }