Sunday, November 27, 2016

A Web Service Client for a soap - service

Currently, I can test my web service with a set of soap requests that I call with Soap-ui.

I have to find a way to automatize the system tests for my service, and to integrate them into the build chain.

My intention is to move the soap requests I have in my soap-UI project into a set of java classes, that I can then integrate into my build process.

I started by trying to use Apache CXF for writing my test classes.

This is how I proceeded:

- I created a new IntelliJ project (Facet : WebService Client : JavaEE, WebServices, JAX-WS)
- Separately, I installed the Apache CXF Distribution under my tools - folder (version 3.1.6)
- I downloaded and saved a local copy of the wsdl of the Lossration Service. I edited the pointers to the *.xsd that are included so that they point to a local copy. I did this because wsdl2java requires access to the *.wsdl and related files, and LossratioService is protected (locally: basic authentication, in the diverses environments : https).
- Generate the client classes with the command-line tool of the CXF distribution:

$C:\ieu\tools\apache-cxf-3.1.6\bin\wsdl2java -client -d generated LossratioService.wsdl

- Copy the classes that have been generated to %PROJECT_ROOT%/src/
- Implement the client. Beware the particular problem of the authentication:

public final class Client {
    private static final String WSDL_URL = 
    "http://localhost:8080/LossratioService_v3_0/LossratioService_v3_0/LossratioService?wsdl";
    private static final QName SERVICE_NAME =
            new QName("http://xml.mobi.ch/service/ch/mobi/contractmanagement/LossratioService/v3_0"
            , "LossratioService");

    private static final String USERNAME = "username";
    private static final char[] PASSWORD = "password".toCharArray();

    private Client() {
    }

    public static void main(String args[]) throws Exception {

        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(USERNAME, PASSWORD);
            }
        });

        URL wsdlURL = new URL(WSDL_URL);
        LossratioService ss = new LossratioService(wsdlURL, SERVICE_NAME);
        LossratioPortType port = ss.getLossratioPort();

        CallContext ctx = new CallContext();
            ctx.setCaller("ctx-caller");
            ctx.setUser("ctx-user");
            ctx.setUuid("ctx-uuid");

        ShakedownResult resp = port.shakedownTest(ctx);
        port.ping();
 }
}

I found useful hints about access to a protected wsdl on this blog 


How to rename a remote branch in GIT

There is no functionality to rename a remote branch in GIT. In order to do this, you checkout the remote branch as a new local branch with a new name, push it as a new remote branch, and finally delete the old remote branch.

Here's the cookbook:

1. Checkout the remote branch with a new name:
git branch new-branch-name origin/old-branch-name
2. Push the newly named local branch to remote:
git push origin --set-upstream new-branch-name
3. Delete the old remote branch:
git push origin :old-branch-name

Thursday, November 17, 2016

What is a detached HEAD in Git, and how to deal with it

Checkout providing a branch name:

$ git checkout development

Checkout providing a SHA1 hash of a specific commit:

$ git checkout 56a4e5c08
Note: checking out '56a4e5c08'.

You are in 'detached HEAD' state...

A "detached HEAD" describes this exact state - when a specific commit is checked out instead of a branch.

"Detached HEAD" are common

Submodules are checked out at specific commits instead of branches
Rebase creates a temporary detached HEAD

Where a detached HEAD should not show up


When going back in time to try out an older version of your project, for example in the context of a bug, when you want to see how things worked in an older revision.

This is a perfectly valid and common use case. However, you don't have to maneuver yourself into a detached HEAD state to deal with it. Instead, remember how simple and cheap the whole concept of branching is in Git: you can simply create a (temporary) branch and delete it once you're done.

$ git checkout -b test-branch 56a4e5c08
...do your thing...
$ git checkout master
$ git branch -d test-branch

How to re-attach a detached HEAD

To recover from the situation where you have a detached HEAD, you should create a branch that points to the commit currently pointed to by your detached HEAD:

$git branch temp
$git checkout temp
(these two commands can be abbreviated as git checkout -b temp)

This will reattach your HEAD to the new temp branch.

Next, you should compare the current commit (and its history) with the normal branch on which you expected to be working:

$git log --graph --decorate --pretty=oneline --abbrev-commit master $origin/master temp
$git diff master temp
$git diff origin/master temp

(You will probably want to experiment with the log options: add -p, leave off --pretty=… to see the whole log message, etc.)

If your new temp branch looks good, you may want to update (e.g.) master to point to it:

$git branch -f master temp
$git checkout master
(these two commands can be abbreviated as git checkout -B master temp)

You can then delete the temporary branch:

$git branch -d temp

Finally, you will probably want to push the reestablished history:

$git push origin master

References

[1] See an excellent explanation on the concept of detached HEAD
[2] Stackoverflow account on how to deal with a detached HEAD

Friday, November 4, 2016

How to override the hashCode, equals, toString and clone methods in Java


    @Override
    public int hashCode() {
        return new HashCodeBuilder()
                .append(contract)
                .append(periodStart)
                .append(periodEnd)
                .append(reportingDate)
                .toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof DefaKey)) {
            return false;
        }
        DefaKey other = (DefaKey) obj;
        return new EqualsBuilder()
                .append(contract, other.contract)
                .append(periodStart, other.periodStart)
                .append(periodEnd, other.periodEnd)
                .append(reportingDate, other.reportingDate)
                .isEquals();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append(getAmount().toString())
                .append(getDataType().toString())
                .toString();
    }