Kohei Nozaki's blog 

Entries tagged [test]

Testing build.xml itself with AntUnit


Posted on Wednesday Jan 28, 2015 at 03:27PM in Technology


In my prevous entry I used AntUnit as a custom task unit testing framework. this time I’d try it for testing build.xml file itself (no custom tasks). it enables with antcallback task which is part of ant-contrib.

Test target is following. simply loads a properties file, then set a property named result which will be asserted in the caller (test case).

<project name="mybuild" basedir=".">

    <target name="main">
        <loadproperties srcFile="mybuild.properties"/>
        <property name="result" value="Hello from mybuild | ${file.var}"/>
    </target>

</project>

Test case is fairly simple as follows. it imports the test target and invoke a target with antcallback, then acquire a property in the calling target. then assert whether the property is set as expected. additionally, it puts a parameter named basedir to enable loading of mybuild.properties which is placed in the same directory to mybuild.xml.

<project name="mybuildTest" xmlns:au="antlib:org.apache.ant.antunit" xmlns="antlib:org.apache.tools.ant">

    <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

    <import file="${project.build.outputDirectory}/mybuild.xml"/>

    <target name="setUp">
        <echo>setup</echo>
    </target>

    <target name="test1">
        <antcallback target="mybuild.main" return="result">
            <!-- intended to enable loading mybuild.properties -->
            <param name="basedir" value="${project.build.outputDirectory}"/>
        </antcallback>

        <au:assertTrue>
            <equals arg1="Hello from mybuild | Hello from mybuild.properties" arg2="${result}"/>
        </au:assertTrue>
    </target>

    <target name="tearDown">
        <echo>tearDown</echo>
    </target>
</project>

Entire the project is available in my GitHub repository. you can test it on your environment easily with mvn clean test because Maven collects all of dependencies.

References:


Running AntUnit in Maven


Posted on Tuesday Jan 27, 2015 at 11:11PM in Technology


These days I’m writing a build.xml and some custom tasks which manipulates OS X’s tmutil (Time Machine utility) and vmrun (VMware utility). testing of custom tasks are bit annoying to me, so I’ve had some googling around and I found AntUnit which is unit test framework for custom Ant tasks. it enables us to test custom tasks in more realistic way than plain JUnit testing. it actually runs the custom tasks in test case which wrote as build.xml. it looks little similar to JUnit test case as follows:

<project xmlns:au="antlib:org.apache.ant.antunit" xmlns="antlib:org.apache.tools.ant">
    <taskdef name="mytask" classname="org.nailedtothex.antunitpractice.MyTask">
        <classpath>
            <pathelement location="${project.build.outputDirectory}"/>
        </classpath>
    </taskdef>

    <target name="setUp">
        <echo>setup</echo>
    </target>

    <target name="test1">
        <mytask property="prop"/>
        <au:assertTrue>
            <equals arg1="Hello from MyTask" arg2="${prop}"/>
        </au:assertTrue>
    </target>

    <target name="tearDown">
        <echo>tearDown</echo>
    </target>
</project>

Targets that beginning their name with test will be automatically executed. we can do assertion with some elements (e.g. au:assertTrue) that provided by AntUnit. target named setUp will be executed before executing every test cases. tearDown also the same as JUnit.

The function of a custom task which I wrote is very simple. it simply set the constant string Hello from MyTask to specified property (prop).

package org.nailedtothex.antunitpractice;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class MyTask extends Task {

    private String property;

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    @Override
    public void execute() throws BuildException {
        getProject().setNewProperty(property, "Hello from MyTask");
    }
}

So, how to run the test? I used Maven’s maven-antrun-plugin. it doesn’t need annoying preparation such as standalone installation of Ant and AntUnit because it collects annoying dependencies automatically. we only need to run mvn clean test as follows:

kyle-no-MacBook:antunitpractice kyle$ mvn clean test
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building antunitpractice 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ antunitpractice ---
[INFO] Deleting /Users/kyle/src/antunitpractice/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ antunitpractice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ antunitpractice ---
[INFO] Compiling 1 source file to /Users/kyle/src/antunitpractice/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ antunitpractice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ antunitpractice ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ antunitpractice ---
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (default-cli) @ antunitpractice ---
[INFO] Executing tasks

main:
[au:antunit] Build File: /Users/kyle/src/antunitpractice/target/test-classes/myTaskTest.xml
[au:antunit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.018 sec
[au:antunit] Target: test1 took 0.016 sec
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.413s
[INFO] Finished at: Wed Jan 28 09:38:19 JST 2015
[INFO] Final Memory: 16M/212M
[INFO] ------------------------------------------------------------------------

I haven’t apply this method to actual projects yet but looks useful for some occasions.

More information can be found via the official site of AntUnit. the entire project using in this entry can be found in my GitHub repository.


Comparison of 2 ways of integration testing: Arquillian and Remote EJB


Posted on Tuesday Mar 04, 2014 at 06:51AM in Technology


Arquillian

Pros

  • It enables us to testing of all kind of beans or methods.
    • CDI Managed Beans
    • EJBs that have only no interface view or local interface
    • Methods what returns/receives object which not serializable
    • everything
  • There are many various ways of manipulating testing package.
    • e.g. adding data source definition or persistence descriptor (persistence.xml) for testing purpose is easy because of useful ShrinkWrap.
  • We can keep the package for testing deployment smaller.
    • We can create smaller deployment package which contains only necessary resources for each test case.

Cons

  • It needs deployment for each test execution.
    • It brings longer deployment time for some cases. if you can keep test packages smaller or you can use faster embedded containers, you might think that it's not problem at all.
    • When I want to execute all of test classes, It will execute deployment many times (same as numbers of test classes). I guess it's inefficient.
    • Also there are some ways to avoid it such as creating delegate test classes, but I think it's annoying.
  • It takes some times for making test package.
    • It gives us great ability to manipulate test package with ShrinkWrap, but it takes some time for making test package. it needs to make WAR or EAR through scanning all necessary classes and dependencies (jar files), for each test execution.
  • Dependency resolving is annoying.
    • We can manipulate what testing package contains completely with Arquillian, but I guess it's annoying for some cases.
    • I have tried to resolve dependencies through Arquillian's Maven Plug-in API, but sometimes I got unexpected result which different with standalone Maven (such as exclusion, etc), and resolving is need to be executed for each test execution.
  • We have to include testing dependencies such as Mockito or DBUnit on the deployment.
    • I know it's not a problem for many cases, I just feel it is not likely to me. I guess that these are not required on the server-side.

Remote EJB

Pros

  • We can control the time of deployment completely.
    • We can execute many test classes for deployment only once.
    • It reduces many deployment time and test execution time for some cases.
  • We can do testing against pure production resources.
  • We don't need to another preparation of testing packages through resolving resources and dependencies.
  • It depends on only more simply and standardized technologies.
    • We don't need to mark @RunWith(Arquillian.class) on test classes.
    • All we need is just lookup the remote bean on the method marked @Before, and invoke target method.

Cons

  • Testable resources are limited on EJBs that have remote interface.
    • We can't test CDI Managed Beans on this way.
  • All of method parameters and return object are need to be serializable.
    • Also larger serializable objects in the test would make testing performance worse.
  • We have to create remote interfaces for beans that needs to be tested.
    • It's annoying, and remote interfaces are unnecessary for production environment at some occasions.
  • EJBs are considered as heavier than CDI Managed Beans.
  • Performance of remote EJB lookups may vary between environments.
    • I experienced that it takes 1 second or more with GlassFish3.
    • Now I'm using WildFly 8.0.0Final, it takes only some milli-seconds.
  • We have to deploy the whole application package for every testing.
    • Larger application needs more time for deployment.
    • So, we have to keep the size of projects at appropriate size.

Conclusion and how to overcome cons

  • I prefer Remote EJB at present time.
  • Annoying preparation of remote interfaces are not pain as much to me.
    • we can make remote interfaces this way, it needs only small amount of additional code:
public interface BusinessDayUtil {

    boolean isBusinessDay(Date date);

    Date addDays(Date date, int days);

    @javax.ejb.Remote
    interface Remote extends BusinessDayUtil {
    }

    @javax.ejb.Local
    interface Local extends BusinessDayUtil {
    }
}
  • Implementation class:
@Stateless
public class BusinessDayUtilImpl implements BusinessDayUtil.Local, BusinessDayUtil.Remote {
...
  • And we can inject the bean through local-view easily:
public class MyJobOperatorImpl implements JobOperator {

    @Inject
    BusinessDayUtil.Local businessDayUtil;
...
  • We don't want to EJBs to wrapping Exceptions with EJBException at some occasions.
    • It can avoid on application classes with annotate @ApplicationException
    • Also can achieve on API classes through placement of ejb-jar.xml.
    • I know it's a sidetracking topic, but I wrote for just a note for me.
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
    <assembly-descriptor>
        <application-exception>
            <exception-class>javax.persistence.NoResultException</exception-class>
            <rollback>true</rollback>
        </application-exception>
    </assembly-descriptor>
</ejb-jar>
  • Making all of arguments and return object serializable is impossible at some occasions.
    • Such as use of InputStream or OutputStream
    • But I guess we can take the old way which stick with plain JUnit testing with mocking frameworks, at such occasions.
CAUTION: invoking EJBs via local interface will call by reference but invoking via remote interface will call by value. you will get serious problems that different behavior between invoking local and remote interfaces. consider keep using remote interface on production (with accepting some performance degradation) or using modern testing framework such as Arquillian if necessary.


Integration testing on an application server with Maven


Posted on Thursday Feb 27, 2014 at 11:50AM in Technology


For a example of integration testing, I will try to test a Stateless Session Bean through Remote Interface.

Environment

  • WildFly 8.0.0.Final
  • Apache Maven 3.1.1
  • Oracle JDK7u51

How does the test case work?

  1. Run Unit Testing JUnit classes
  2. Start the application server
  3. Deploy the application to the application server
  4. Run Integration Testing JUnit classes
    • Remote EJB invocation testing
  5. Undeploy the application
  6. Stop the application server

Resources of the sample project

  • Whole resources are available in GitHub

pom.xml

  • 2 profiles are declared.

production

  • It's just a skeleton. additional configuration are required here if you want to deploy to production environment using this pom.xml.

integration-test

  • We can fire the integration-test by command “mvn -P integration-test verify”
  • WildFly installation at “/Users/kyle/apps2/wildfly-8.0.0.Final” is assumed.
  • It has a declaration of port-offset 30000 because I'm already using 8080 or 9990 ports for development.
    • Consequently, this pom.xml fires WildFly at port 38080 and 39990.

Test target classes

Hoge.java

  • Just a POJO class.
  • Supposed to be tested at unit test phase.

HigeImpl.java

  • Stateless Session Bean class which implements Remote Interface.
  • Supposed to be tested at integration-test phase.

JUnit Test classes

HogeTest.java

  • Just a plain JUnit class.
  • Supposed to be excluded at integration-test phase.

HigeTestIT.java

  • There are some EJB lookup codes, and JNDI reference name declared here.
  • Supposed to be excluded at unit-test phase.

jndi.properties

  • Some JNDI remote lookup configurations here.
  • If you want to run this project at a application server except WildFly, then you need to edit this.
  • Port number 8080 is supposed to be used with the application server which integrated with IDE.
    • When kicked by Maven, it will be override by system property (related logics are available at HigeTestIT.java and pom.xml).
    • Consequently, both IDE and Maven can run the test case.

Log

kyle-no-MacBook:it kyle$ mvn -P integration-test verify
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building it 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ it ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ it ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ it ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ it ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.16:test (default-test) @ it ---
[INFO] Surefire report directory: /Users/kyle/Documents/workspace/it/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
Running org.nailedtothex.it.HogeTest
***THIS IS A UNIT TEST***
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.04 sec - in org.nailedtothex.it.HogeTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ it ---
[INFO] Packaging webapp
[INFO] Assembling webapp [it] in [/Users/kyle/Documents/workspace/it/target/it-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/kyle/Documents/workspace/it/src/main/webapp]
[INFO] Webapp assembled in [23 msecs]
[INFO] Building war: /Users/kyle/Documents/workspace/it/target/it-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] --- wildfly-maven-plugin:1.0.1.Final:start (wildfly-run) @ it ---
[INFO] JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre
[INFO] JBOSS_HOME=/Users/kyle/apps2/wildfly-8.0.0.Final

[INFO] Server is starting up.
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
2 27, 2014 4:07:33 午後 org.xnio.Xnio <clinit>
INFO: XNIO version 3.2.0.Final
2 27, 2014 4:07:34 午後 org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.2.0.Final
2 27, 2014 4:07:34 午後 org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.0.Final
16:07:34,277 INFO  [org.jboss.modules] (main) JBoss Modules version 1.3.0.Final
16:07:34,482 INFO  [org.jboss.msc] (main) JBoss MSC version 1.2.0.Final
16:07:34,542 INFO  [org.jboss.as] (MSC service thread 1-6) JBAS015899: WildFly 8.0.0.Final "WildFly" starting
16:07:35,326 INFO  [org.jboss.as.server] (Controller Boot Thread) JBAS015888: Creating http management service using socket-binding (management-http)
16:07:35,340 INFO  [org.xnio] (MSC service thread 1-15) XNIO version 3.2.0.Final
16:07:35,346 INFO  [org.xnio.nio] (MSC service thread 1-15) XNIO NIO Implementation Version 3.2.0.Final
16:07:35,366 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 46) JBAS013171: Activating Security Subsystem
16:07:35,369 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 33) JBAS010280: Activating Infinispan subsystem.
16:07:35,378 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 41) JBAS011800: Activating Naming Subsystem
16:07:35,382 INFO  [org.jboss.as.security] (MSC service thread 1-16) JBAS013170: Current PicketBox version=4.0.20.Final
16:07:35,383 INFO  [org.jboss.as.webservices] (ServerService Thread Pool -- 50) JBAS015537: Activating WebServices Extension
16:07:35,384 INFO  [org.jboss.as.jsf] (ServerService Thread Pool -- 39) JBAS012615: Activated the following JSF Implementations: [main]
16:07:35,409 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 49) JBAS017502: Undertow 1.0.0.Final starting
16:07:35,409 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) JBAS017502: Undertow 1.0.0.Final starting
16:07:35,419 INFO  [org.jboss.remoting] (MSC service thread 1-15) JBoss Remoting version 4.0.0.Final
16:07:35,420 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 28) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
16:07:35,430 INFO  [org.jboss.as.connector.logging] (MSC service thread 1-12) JBAS010408: Starting JCA Subsystem (IronJacamar 1.1.3.Final)
16:07:35,436 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-14) JBAS010417: Started Driver service with driver-name = h2
16:07:35,449 INFO  [org.jboss.as.naming] (MSC service thread 1-8) JBAS011802: Starting Naming Service
16:07:35,449 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-12) JBAS015400: Bound mail session [java:jboss/mail/Default]
16:07:35,498 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 49) JBAS017527: Creating file handler for path /Users/kyle/apps2/wildfly-8.0.0.Final/welcome-content
16:07:35,518 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-4) JBAS017525: Started server default-server.
16:07:35,532 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-4) JBAS017531: Host default-host starting
16:07:35,588 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-9) JBAS017519: Undertow HTTP listener default listening on /127.0.0.1:38080
16:07:35,786 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-5) JBAS015012: Started FileSystemDeploymentService for directory /Users/kyle/apps2/wildfly-8.0.0.Final/standalone/deployments
16:07:35,795 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-11) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
16:07:36,025 INFO  [org.jboss.ws.common.management] (MSC service thread 1-10) JBWS022052: Starting JBoss Web Services - Stack CXF Server 4.2.3.Final
16:07:36,065 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:39990/management
16:07:36,066 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:39990
16:07:36,066 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.0.0.Final "WildFly" started in 2067ms - Started 183 of 232 services (80 services are lazy, passive or on-demand)
[INFO] 
[INFO] >>> wildfly-maven-plugin:1.0.1.Final:deploy (wildfly-run) @ it >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ it ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ it ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ it ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ it ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.16:test (default-test) @ it ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ it ---
[INFO] Packaging webapp
[INFO] Assembling webapp [it] in [/Users/kyle/Documents/workspace/it/target/it-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/kyle/Documents/workspace/it/src/main/webapp]
[INFO] Webapp assembled in [5 msecs]
[INFO] Building war: /Users/kyle/Documents/workspace/it/target/it-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] <<< wildfly-maven-plugin:1.0.1.Final:deploy (wildfly-run) @ it <<<
[INFO] 
[INFO] --- wildfly-maven-plugin:1.0.1.Final:deploy (wildfly-run) @ it ---
16:07:39,415 INFO  [org.jboss.as.repository] (management-handler-thread - 3) JBAS014900: Content added at location /Users/kyle/apps2/wildfly-8.0.0.Final/standalone/data/content/63/45e9720d69aeb3604f94f608a25f9d036229a2/content
16:07:39,427 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015876: Starting deployment of "it.war" (runtime-name: "it.war")
16:07:39,564 INFO  [org.jboss.weld.deployer] (MSC service thread 1-15) JBAS016002: Processing weld deployment it.war
16:07:39,604 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-15) HV000001: Hibernate Validator 5.0.3.Final
16:07:39,654 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-15) JNDI bindings for session bean named HigeImpl in deployment unit deployment "it.war" are as follows:

    java:global/it/HigeImpl!org.nailedtothex.it.Hige
    java:app/it/HigeImpl!org.nailedtothex.it.Hige
    java:module/HigeImpl!org.nailedtothex.it.Hige
    java:jboss/exported/it/HigeImpl!org.nailedtothex.it.Hige
    java:global/it/HigeImpl
    java:app/it/HigeImpl
    java:module/HigeImpl

16:07:39,741 INFO  [org.jboss.weld.deployer] (MSC service thread 1-11) JBAS016005: Starting Services for CDI deployment: it.war
16:07:39,762 INFO  [org.jboss.weld.Version] (MSC service thread 1-11) WELD-000900: 2.1.2 (Final)
16:07:39,788 INFO  [org.jboss.weld.deployer] (MSC service thread 1-9) JBAS016008: Starting weld service for deployment it.war
16:07:40,560 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-11) JBAS017534: Registered web context: /it
16:07:40,596 INFO  [org.jboss.as.server] (management-handler-thread - 3) JBAS018559: Deployed "it.war" (runtime-name : "it.war")
[INFO] 
[INFO] --- maven-failsafe-plugin:2.16:integration-test (default) @ it ---
[INFO] Failsafe report directory: /Users/kyle/Documents/workspace/it/target/failsafe-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
Running org.nailedtothex.it.HigeTestIT
2 27, 2014 4:07:41 午後 org.xnio.Xnio <clinit>
INFO: XNIO version 3.2.0.Final
2 27, 2014 4:07:41 午後 org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.2.0.Final
2 27, 2014 4:07:41 午後 org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.0.Final
2 27, 2014 4:07:41 午後 org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
2 27, 2014 4:07:41 午後 org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@6d3a3c8e, receiver=Remoting connection EJB receiver [connection=org.jboss.ejb.client.remoting.ConnectionPool$PooledConnection@a5dc6a8,channel=jboss.ejb,nodename=kyle-no-macbook]} on channel Channel ID b8296d49 (outbound) of Remoting connection 09445fbe to localhost/127.0.0.1:38080
2 27, 2014 4:07:41 午後 org.jboss.ejb.client.EJBClient <clinit>
INFO: JBoss EJB Client version 2.0.0.Final
***THIS IS A INTEGRATION TEST***
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.574 sec - in org.nailedtothex.it.HigeTestIT

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- wildfly-maven-plugin:1.0.1.Final:undeploy (wildfly-stop) @ it ---
16:07:41,535 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-14) JBAS017535: Unregistered web context: /it
16:07:41,547 INFO  [org.jboss.weld.deployer] (MSC service thread 1-10) JBAS016009: Stopping weld service for deployment it.war
16:07:41,562 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015877: Stopped deployment it.war (runtime-name: it.war) in 30ms
16:07:41,578 INFO  [org.jboss.as.repository] (management-handler-thread - 4) JBAS014901: Content removed from location /Users/kyle/apps2/wildfly-8.0.0.Final/standalone/data/content/63/45e9720d69aeb3604f94f608a25f9d036229a2/content
16:07:41,578 INFO  [org.jboss.as.server] (management-handler-thread - 4) JBAS018558: Undeployed "it.war" (runtime-name: "it.war")
[INFO] 
[INFO] --- wildfly-maven-plugin:1.0.1.Final:shutdown (wildfly-stop) @ it ---
16:07:41,603 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-16) JBAS017532: Host default-host stopping
16:07:41,605 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-12) JBAS010409: Unbound data source [java:jboss/datasources/ExampleDS]
16:07:41,611 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-10) JBAS017521: Undertow HTTP listener default suspending
16:07:41,612 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-10) JBAS017520: Undertow HTTP listener default stopped, was bound to /127.0.0.1:38080
16:07:41,616 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) JBAS010418: Stopped Driver service with driver-name = h2
16:07:41,617 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) JBAS017506: Undertow 1.0.0.Final stopping
16:07:41,620 INFO  [org.jboss.as] (MSC service thread 1-7) JBAS015950: WildFly 8.0.0.Final "WildFly" stopped in 10ms
[INFO] 
[INFO] --- maven-failsafe-plugin:2.16:verify (default) @ it ---
[INFO] Failsafe report directory: /Users/kyle/Documents/workspace/it/target/failsafe-reports
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.129s
[INFO] Finished at: Thu Feb 27 16:07:42 JST 2014
[INFO] Final Memory: 13M/245M
[INFO] ------------------------------------------------------------------------
kyle-no-MacBook:it kyle$ 

References

  1. Maven Failsafe Plugin - Introduction
  2. JUnitのCategoryさんとMavenのintegration-testでの実行 - 日々常々


Mocking a unstable HTTP server with WireMock


Posted on Wednesday Feb 26, 2014 at 10:12AM in Technology


Environment

  • WireMock 1.4.3
  • HttpClient 4.3.2
  • Oracle JDK7u51

Why need it?

  • Sometimes website scraping stops at unexpected error like socket timeout or internal server error.
  • We prefer to retry several times at such occasion.
  • So we have to develop some retrying mechanism, and we have to create a test and create the mock of server.
    • So in this article, I'm going to try to create the mock with WireMock.

Requirements for client

  • Retry if processing fails, for 3 times.
  • Retry when timeout occurred.
    • SocketTimeoutException
  • Retry when server returned status code 5xx

Test cases

  1. Server returns code 200 without any problems.
  2. Server returns code 500 at first, then client will retry, server returns code 200.
  3. Server returns code 500 forever, then client will retry 3 times and give up.
  4. Server delays the response, then client will regard as timeout and retry, then server returns code 200.
  5. Server delays the response forever, then client will retry 3 times and give up.

Resources

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.nailedtothex</groupId>
    <artifactId>wiremock</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-core</artifactId>
          <version>1.3</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.tomakehurst</groupId>
            <artifactId>wiremock</artifactId>
            <version>1.43</version>
            <classifier>standalone</classifier>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>fluent-hc</artifactId>
            <version>4.3.2</version>
        </dependency>
    </dependencies>

</project>

RetryableHttpFetcher.java

package org.nailedtothex.wiremock;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ServiceUnavailableRetryStrategy;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class RetryableHttpFetcher {

    private static final Logger log = Logger.getLogger(RetryableHttpFetcher.class.getName());

    // these parameters would be better to retrieve through JNDI or any other mechanism
    private int MAX_RETRIES = 3;
    private int RETRY_INTERVAL = 1000;
    private int TIMEOUT = 500;

    private final ServiceUnavailableRetryStrategy MY_SERVICE_UNAVAILABLE_RETRY_STRATEGY = new ServiceUnavailableRetryStrategy() {
        @Override
        public boolean retryRequest(HttpResponse response, int executionCount,
                org.apache.http.protocol.HttpContext context) {

            boolean rc = response.getStatusLine().getStatusCode() >= 500 && executionCount <= MAX_RETRIES;

            log.log(Level.INFO,
                    "retryRequest(): returning={0}, statusCode={1}, executionCount={2}, maxRetries={3}, interval={4}",
                    new Object[] { rc, response.getStatusLine().getStatusCode(), executionCount, MAX_RETRIES,
                            RETRY_INTERVAL });

            return rc;
        }

        @Override
        public long getRetryInterval() {
            return RETRY_INTERVAL;
        }
    };

    private final HttpRequestRetryHandler MY_HTTP_REQUEST_RETRY_HANDLER = new HttpRequestRetryHandler() {

        @Override
        public boolean retryRequest(IOException e, int executionCount, HttpContext context) {
            log.log(Level.INFO, "retryRequest(): exception={0}, executionCount={1}, maxRetries={2}",
                    new Object[] { e.getClass(), executionCount, MAX_RETRIES });

            if (executionCount > MAX_RETRIES) {
                log.log(Level.INFO, "give up: {0}", executionCount);
                return false;
            }

            if (e instanceof java.net.SocketTimeoutException) {
                log.log(Level.INFO, "retry: {0}", e.getMessage());
                return true;
            }

            log.log(Level.INFO, "not retry: {0}", e.getMessage());
            return false;
        }
    };

    private final RequestConfig MY_REQUEST_CONFIG = RequestConfig.custom()
            .setConnectionRequestTimeout(TIMEOUT)
            .setConnectTimeout(TIMEOUT)
            .setSocketTimeout(TIMEOUT)
            .build();

    public String fetchAsString(String url) throws ClientProtocolException, IOException {

        try (CloseableHttpClient client = HttpClientBuilder.create()
                .setDefaultRequestConfig(MY_REQUEST_CONFIG)
                .setRetryHandler(MY_HTTP_REQUEST_RETRY_HANDLER)
                .setServiceUnavailableRetryStrategy(MY_SERVICE_UNAVAILABLE_RETRY_STRATEGY)
                .build()) {

            try (CloseableHttpResponse res = client.execute(new HttpGet(url))) {
                if (res.getStatusLine().getStatusCode() >= 400) {
                    throw new HttpResponseException(res.getStatusLine().getStatusCode(), res.getStatusLine()
                            .getReasonPhrase());
                }
                return EntityUtils.toString(res.getEntity());
            }
        }

    }
}

RetryableHttpFetcherTest.java

package org.nailedtothex.wiremock;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.net.SocketTimeoutException;

import org.apache.http.client.HttpResponseException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.stubbing.Scenario;

public class RetryableHttpFetcherTest {

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(18089);

    private RetryableHttpFetcher instance;

    @Before
    public void init() {
        instance = new RetryableHttpFetcher();
    }

    @Test
    public void test1_ok() throws Exception {
        stubFor(get(urlEqualTo("/hoge.txt")).willReturn(
                aResponse().withStatus(200).withHeader("Content-Type", "text/plain").withBody("OK")));
        String expected = "OK";

        String actual = instance.fetchAsString("http://localhost:18089/hoge.txt");

        assertThat(actual, is(expected));
    }

    @Test
    public void test2_retryAt500() throws Exception {
        stubFor(get(urlEqualTo("/500")).inScenario("retry at 500")
                .whenScenarioStateIs(Scenario.STARTED)
                .willSetStateTo("one time requested")
                .willReturn(aResponse().withBody("error").withStatus(500)));

        stubFor(get(urlEqualTo("/500")).inScenario("retry at 500")
                .whenScenarioStateIs("one time requested")
                .willReturn(aResponse().withBody("OK").withStatus(200)));

        String actual = instance.fetchAsString("http://localhost:18089/500");

        assertThat(actual, is("OK"));
    }

    @Test(expected = HttpResponseException.class)
    public void test3_retryAt500GiveUp() throws Exception {
        stubFor(get(urlEqualTo("/500"))
                .willReturn(aResponse().withBody("500").withStatus(500)));

        instance.fetchAsString("http://localhost:18089/500");
    }

    @Test
    public void test4_retryAtTimeout() throws Exception {
        stubFor(get(urlEqualTo("/timeout")).inScenario("retrying")
                .whenScenarioStateIs(Scenario.STARTED)
                .willSetStateTo("one time requested")
                .willReturn(aResponse().withBody("error").withStatus(500).withFixedDelay(3000)));
        stubFor(get(urlEqualTo("/timeout")).inScenario("retrying")
                .whenScenarioStateIs("one time requested")
                .willReturn(aResponse().withBody("OK").withStatus(200)));

        String actual = instance.fetchAsString("http://localhost:18089/timeout");
        assertThat(actual, is("OK"));
    }

    @Test(expected = SocketTimeoutException.class)
    public void test5_retryAtTimeoutGiveUp() throws Exception {
        stubFor(get(urlEqualTo("/timeout"))
                .willReturn(aResponse().withBody("timeout").withStatus(500).withFixedDelay(Integer.MAX_VALUE)));
        instance.fetchAsString("http://localhost:18089/timeout");
    }

    @Test(expected = HttpResponseException.class)
    public void notFound() throws Exception {
        instance.fetchAsString("http://localhost:18089/NOT_FOUND");
    }
}

Test logs

  • All tests were passed.

test1_ok

2 26, 2014 11:34:22 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$1 retryRequest
情報: retryRequest(): returning=false, statusCode=200, executionCount=1, maxRetries=3, interval=1,000

test2_retryAt500

2 26, 2014 11:35:32 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$1 retryRequest
情報: retryRequest(): returning=true, statusCode=500, executionCount=1, maxRetries=3, interval=1,000
2 26, 2014 11:35:33 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$1 retryRequest
情報: retryRequest(): returning=false, statusCode=200, executionCount=2, maxRetries=3, interval=1,000

test3_retryAt500GiveUp

2 26, 2014 11:35:51 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$1 retryRequest
情報: retryRequest(): returning=true, statusCode=500, executionCount=1, maxRetries=3, interval=1,000
2 26, 2014 11:35:52 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$1 retryRequest
情報: retryRequest(): returning=true, statusCode=500, executionCount=2, maxRetries=3, interval=1,000
2 26, 2014 11:35:53 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$1 retryRequest
情報: retryRequest(): returning=true, statusCode=500, executionCount=3, maxRetries=3, interval=1,000
2 26, 2014 11:35:54 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$1 retryRequest
情報: retryRequest(): returning=false, statusCode=500, executionCount=4, maxRetries=3, interval=1,000

test4_retryAtTimeout

2 26, 2014 11:36:12 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: retryRequest(): exception=class java.net.SocketTimeoutException, executionCount=1, maxRetries=3
2 26, 2014 11:36:12 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: retry: Read timed out
2 26, 2014 11:36:12 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$1 retryRequest
情報: retryRequest(): returning=false, statusCode=200, executionCount=1, maxRetries=3, interval=1,000

test5_retryAtTimeoutGiveUp

2 26, 2014 11:36:39 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: retryRequest(): exception=class java.net.SocketTimeoutException, executionCount=1, maxRetries=3
2 26, 2014 11:36:39 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: retry: Read timed out
2 26, 2014 11:36:40 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: retryRequest(): exception=class java.net.SocketTimeoutException, executionCount=2, maxRetries=3
2 26, 2014 11:36:40 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: retry: Read timed out
2 26, 2014 11:36:40 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: retryRequest(): exception=class java.net.SocketTimeoutException, executionCount=3, maxRetries=3
2 26, 2014 11:36:40 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: retry: Read timed out
2 26, 2014 11:36:41 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: retryRequest(): exception=class java.net.SocketTimeoutException, executionCount=4, maxRetries=3
2 26, 2014 11:36:41 午前 org.nailedtothex.wiremock.RetryableHttpFetcher$2 retryRequest
情報: give up: 4

References

  1. HttpClient 4 Cookbook