Automated tests for SOAP API in Java

Don’t think the story will be short, but it’s definitely interesting. So, I got a new project where I need to cover a SOAP API with automated tests written on Java. As a starting point I have a URL to a WSDL file to work with. The “tricky” part is in the environment configuration. Our test environment is hidden behind a SOCKS proxy server. So, if you want to work with a test environment, you need to make your requests through that proxy, otherwise you’ll “talk” to another environment that we don’t need right now. Not a usual configuration, but it had it’s own reasons for doing so. My first problem was to decide how to work with WSDL and in what direction my future framework would go. After some Googling and trials, I stopped on the JAX-WS library. This is handy because with the “maven wsimport” task, you can easily generate all needed classes and objects where your stubs will be held. Here’s how it looks like in pom.xml:

                <plugin>
                    <groupid>org.jvnet.jax-ws-commons</groupid>
                    <artifactid>jaxws-maven-plugin</artifactid>
                    <version>${jaxws.plugin.version}</version>
                    <executions>
                        <execution>
                            <id>{put_some_id}</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>wsimport</goal>
                            </goals>
                            <configuration>
                                <!--<wsdldirectory>${project.basedir}\src\wsdl</wsdlDirectory>-->
                                <wsdllocation>{your_wsdl_location}</wsdllocation>
                                <destdir>${project.basedir}\src\main\java\Stubs\oop</destdir>
                                <wsdlfiles>
                                    <wsdlfile>${project.basedir}\src\main\resources\wsdl\myWSDL.wsdl</wsdlfile>
                                </wsdlfiles>
                                <packagename>Stubs.oop</packagename>
                                <!-- Without this, multiple WSDLs won't be processed -->
                                <stalefile>${project.build.directory}/jaxws/stale/wsdl.done</stalefile>
                            </configuration>
                        </execution>
                        <execution>
                            <id>{put_some_id}</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>wsimport</goal>
                            </goals>
                            <configuration>
                                <!--<wsdldirectory>${project.basedir}\src\wsdl</wsdlDirectory>-->
                                <wsdllocation>{your_wsdl_location}</wsdllocation>
                                <destdir>${project.basedir}\src\main\java\Stubs\wsdl2</destdir>
                                <wsdlfiles>
                                    <wsdlfile>${project.basedir}\src\main\resources\wsdl\your_wsld2.wsdl</wsdlfile>
                                </wsdlfiles>
                                <packagename>your_package_name</packagename>
                                <!-- Without this, multiple WSDLs won't be processed -->
                                <stalefile>${project.build.directory}/jaxws/stale/wsdl.done</stalefile>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

It was a bit hard for me to get the WSDL file from the URL, so I decided to save it locally and read it from there then. Since changes on the API are not frequent, I can easily accept this. This configuration allows to add as many WSDL sources as I want and to store stubs for each of them in project. First problem was solved. Now, I had to figure out how to make my framework to use proxy for it’s calls. My first trial was basic and most common in the Java world:

System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", getProxyHost());
System.getProperties().put("proxyPort", getProxyPort());

But I kept getting errors and no successful connection. My next guess was that problem could be in the SSL, since we are using HTTPS. So, I started the browser, imported the security certificates (they were self-signed since it’s a test environment), added them to java “cacerts” keystore, and added next to my code:

System.setProperty("javax.net.ssl.keyStore", "keystore.jks");
System.setProperty("javax.net.ssl.trustStrore", "cacerts.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");

I tried to debug and to make sure that the JVM is using my proxy settings and it was true – everything was there. What would be the next step in debugging for me? I decided to install a local proxy and try to put my requests through it, so I could get more details. I installed Charles – a fantastic proxy server that helped me a lot in the past with REST API automation. It has a life changing ( for me in this case) “External Proxy” option . I set it to proxy all requests to our SOCKS proxy and I put my Java API calls through Charles as well. And it worked! You can’t even believe how happy I was because of it. But now I got another problem – Charles cost $50 for one license. So, there were two questions that I needed to solve:

  • Why my Java calls works fine with Charles and without it they fail?
  • Where to find free and easy to setup HTTP proxy?

With help from one of our developers we could find out why Java kept getting authentication errors and didn’t want to connect to test environment properly. The problem was that Java was connecting to the SOCKS proxy properly, but it was resolving remote DNS to another environment to which I didn’t have proper certificates and credentials for authorization! I found only one article on StackOverflow regarding this issue and from that point I new that I need to have local HTTP proxy that is:

  • free
  • can forward my requests to SOCKS proxy

I’ve spent whole day trying to find an easy-to-use and adequate proxy server for Windows ( it’s my Workstation that I need to use), I looked over 15 different apps and failed. In the end, I installed VirtualBox with Ubuntu and install there Squid3 proxy server. It’s the most popular and common proxy server for Unix, from what I saw on Google. But this crazy application has a configuration file with more then 3000 lines! It’s not that easy to make it run as I want, it’s not even that easy to restart it. So, after couple hours I gave up on it and started looking for more solutions. Luckily, I found Polipo – tiny, easy to install and setup proxy server. It only has a few main options that I had to setup and make everything work as a charm:

  • proxyAddress – set it to IP address of your local virtual box, or your local machine, if you use Linux or Mac
  • allowedClients – list of IP addresses from which Polipo will allow to access it and forward requests towards another proxy or web directly
  • socksParentProxy = “host name and port of your proxy to which I needed to forward my requests”
  • socksProxyType = socks5

Save changes and restart – that’s it! After that I pointed my Java framework to the local proxy and got green tests! To set the proxy for my tests I used custom MyProxySelector class:

                      package Base;

                      import java.net.*;
                      import java.util.ArrayList;
                      import java.util.HashMap;
                      import java.io.IOException;

                      public  class MyProxySelector extends ProxySelector {
                          // Keep a reference on the previous default
                          public ProxySelector defsel = null;

                          /**
                           * Inner class representing a Proxy and a few extra data
                           */
                          class InnerProxy {
                              Proxy proxy;
                              SocketAddress addr;
                              // How many times did we fail to reach this proxy?
                              int failedCount = 0;

                              InnerProxy(InetSocketAddress a) {
                                  addr = a;
                                  proxy = new Proxy(Proxy.Type.HTTP, a);
                              }

                              SocketAddress address() {
                                  return addr;
                              }

                              Proxy toProxy() {
                                  return proxy;
                              }

                              int failed() {
                                  return ++failedCount;
                              }
                          }

                          /**
                           * A list of proxies, indexed by their address.
                           */
                          HashMap<SocketAddress, InnerProxy> proxies = new HashMap<SocketAddress, InnerProxy>();

                          public MyProxySelector(ProxySelector def, String host, int port) {
                              // Save the previous default
                              defsel = def;

                              // Populate the HashMap (List of proxies)
                              InnerProxy I = new InnerProxy(new InetSocketAddress(host, port));
                              proxies.put(i.address(), i);
                          }

                          /**
                           * This is the method that the handlers will call.
                           * Returns a List of proxy.
                           */
                          public java.util.List select(URI uri) {
                              // Let's stick to the specs.
                              if (uri == null) {
                                  throw new IllegalArgumentException("URI can't be null.");
                              }
                              /**
                               * If it's a http (or https) URL, then we use our own
                               * list.
                               */
                              String protocol = uri.getScheme();
                              if ("http".equalsIgnoreCase(protocol) ||
                                      "https".equalsIgnoreCase(protocol)) {
                                  ArrayList l = new ArrayList();
                                  for (InnerProxy p: proxies.values()) {
                                      l.add(p.toProxy());
                                  }
                                  return l;
                              }

                              /**
                               * Not HTTP or HTTPS (could be SOCKS or FTP)
                               * defer to the default selector.
                               */
                              if (defsel != null) {
                                  return defsel.select(uri);
                              } else {
                                  ArrayList l = new ArrayList();
                                  l.add(Proxy.NO_PROXY);
                                  return l;
                              }
                          }

                          /**
                           * Method called by the handlers when it failed to connect
                           * to one of the proxies returned by select().
                           */
                          public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
                              // Let's stick to the specs again.
                              if (uri == null || sa == null || ioe == null) {
                                  throw new IllegalArgumentException("Arguments can't be null.");
                              }

                              /**
                               * Let's lookup for the proxy
                               */
                              InnerProxy p = proxies.get(sa);
                              if (p != null) {
                                      /**
                                       * It's one of ours, if it failed more than 3 times
                                       * let's remove it from the list.
                                       */
                                  if (p.failed() >= 3)
                                      proxies.remove(sa);
                              } else {
                                      /**
                                       * Not one of ours, let's delegate to the default.
                                       */
                                  if (defsel != null)
                                      defsel.connectFailed(uri, sa, ioe);
                              }
                          }
                      }

And to turn the proxy on and off I wrote next switch methods:

            private ProxySelector defaultProxy = ProxySelector.getDefault();

            public void setLocalProxy(){
                MyProxySelector ps = new MyProxySelector(ProxySelector.getDefault(),
                                                         localProxyName,localProxyPort);
                ProxySelector.setDefault(ps);
            }

            public void disableProxy(){
                ProxySelector.setDefault(defaultProxy);
            }

That’s it. Now I can run my tests easily with control over when to the use the proxy and when not to. In the future I’ll move my tests to the CI server (Jenkins most probably) and will setup Polipo HTTP proxy on that environment in two minutes. It’s always nice to solve such non-ordinary problems. Most probably my solution is not very elegant and “right”, but it works right now and for this moment this is all that matters to me, since I can start writing automated tests rather then fighting with configuration issues.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *