Technical Debt - Identifying and Managing

by GarciaPL on Monday, 20 November 2017

Client Backend - Project for interview

by GarciaPL on Sunday, 1 October 2017

Recently I was creating small distributed application called ClientBackend just for interview purpose. Solution consists of thee microservices :

  • Client (received request from API Gateway and sends it to Backend)
  • Backend (performs operation defined in request)
  • Discovery (Spring Eureka)
  • API Gateway (Spring Zuul)
Technology stack :
  • Java 8
  • Spring Boot
  • Hystrix
  • Eureka
  • Feign
  • Zuul
  • TypeOf

More info about the solution

Hamcrest Custom Exception Matcher

by GarciaPL on Friday, 11 August 2017

Sometimes asserting custom exception is difficult. Sometimes it's not, for instance when you are using AssertJ, it's very quite easy to achieve. In this post I would like to highlight how to achieve that using Hamcrest matcher.

Below you might find a BaseExceptionMatcher which can be easily extended for our needs. Let's assume that we have an exception with four properties like errorType, errorCode, status and description.

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;


public abstract class BaseExceptionMatcher extends TypeSafeMatcher<BaseException> {
    protected ErrorType expectedErrorType;
    protected ErrorCode expectedErrorCode;
    protected Response.Status expectedStatus;
    protected String expectedDescription;

    public BaseExceptionMatcher(ErrorCode expectedErrorCode, Response.Status expectedStatus, ErrorType expectedErrorType, String expectedDescription) {
        this.expectedErrorCode = expectedErrorCode;
        this.expectedStatus = expectedStatus;
        this.expectedErrorType = expectedErrorType;
        this.expectedDescription = expectedDescription;

    protected boolean matchesSafely(BaseException item) {
        return item.getStatus().equals(expectedStatus) &&
                item.getError().stream().allMatch(p ->
                        p.getType().equals( && p.getCode().equals( &&
                item.getError().stream().allMatch(p -> p.getDescription().contains(expectedDescription));

    public void describeTo(Description description) {
        description.appendText("Response status : ")
                .appendValue(expectedStatus.getReasonPhrase() + " (" + expectedStatus.getStatusCode() + ")")
                .appendText(" | ErrorType : ").appendValue(
                .appendText(" | ErrorCode : ").appendValue(
                .appendText(" | Description : ").appendValue(expectedDescription);

    protected void describeMismatchSafely(BaseException item, Description mismatchDescription) {
        mismatchDescription.appendText("Exception contains response status : ")
                .appendValue(item.getStatus().getReasonPhrase() + "(" + item.getStatus().getStatusCode() + ")")
                .appendText(" | ErrorType : ").appendValue(getMessageType(item).getType())
                .appendText(" | ErrorCode : ").appendValue(getMessageType(item).getCode())
                .appendText(" | Description : ").appendValue(getMessageType(item).getDescription());

    private MessageType getMessageType(BaseException item) {
        return item.getError().stream().findFirst().orElseGet(() -> {
            MessageType messageType = new MessageType();
            messageType.setDescription("Matcher Error");
            return messageType;
Now we can create a concrete exception class which might reuse that above abstract class.
import my.package.ErrorCode;
import my.package.ErrorType;
import my.package.BaseExceptionMatcher;


public class MyExceptionMatcher extends BaseExceptionMatcher {

    public MyExceptionMatcher(ErrorType expectedErrorType,
                                           ErrorCode expectedErrorCode,
                                           Response.Status expectedStatus,
                                           String expectedDescription) {
        super(expectedErrorCode, expectedStatus, expectedErrorType, expectedDescription);

So now you are ready to reuse that matcher in JUnit test. Only what you need to do is define special rule in your test as below.
public ExpectedException expectedException = ExpectedException.none();
Last thing is just write a test which might throw an exception and reuse exception matcher written above.
public void testFailure() throws MyException {
    expectedException.expect(new MyExceptionMatcher(ErrorType.APPLICATION, ErrorCode.DOCUMENT_NOT_FOUND, Response.Status.NOT_FOUND, "Document not found"));
    //do smth to fail

IntelliJ with Tomcat and Gradle - Smart Tomcat

by GarciaPL on Sunday, 9 July 2017

Last time I was describing how to enable Tomcat for projects maintained by Gradle in post with title called "IntelliJ with Tomcat and Gradle using Gretty plugin". The thing that I found recently even a better way to achieve that! Moreover this way is less invasive in terms of that you do not need to append any configuration to your gradle scripts! This better way is known as a Smart Tomcat! It is a plugin for IntelliJ which allow you to configure literally everything around your app if it should be run on Tomcat.

Smart Tomcat - IntelliJ IDEA Plugins

Convert Map into Map of List - Java 8

by GarciaPL

Last time I was looking for a way to convert a Map into a Map of List. In otherwise direction, I mean backward from value up to key. Below you might find an solution for that problem using of course main Java 8 components like stream, filter, grouping and mapping.

        Map<String, String> input = new HashMap<>();
        input.put("SHARK", null);
        input.put("DOG", "HOME");
        input.put("CAT", "HOME");
        input.put("TURTLE", "HOME");
        input.put("RABBIT", "HOME");
        input.put("LION", "NOT_HOME");
        input.put("HIPPO", "NOT_HOME");
        input.put("TIGER", "NOT_HOME");
        input.put("ZEBRA", "NOT_HOME");

        Map<String, List<String>> result = input.entrySet().stream()
                .filter(i -> i.getValue() != null)
                .collect(Collectors.groupingBy(i -> i.getValue(),
                        Collectors.mapping(i -> i.getKey(), Collectors.toList())));

Key : HOME with List of DOG, CAT, TURTLE and RABBIT

IntelliJ with Tomcat and Gradle using Gretty plugin

by GarciaPL on Thursday, 27 April 2017

This time I would like to describe briefly how to enable Tomcat in debug & class reload mode on IntelliJ when project is built using Gradle using this time Gretty plugin [1].

1) Add below line to build.gradle after applied war plugin

apply from: ''

2) At the end of build.gradle add also below statement which allows us to reload classes in container in the fly after change made by you in IntelliJ

gretty {
    managedClassReload = true

3) Add Remote item in IntelliJ [3]

4) Run project using below command line

gradle tomcatStartDebug --info

5) Pickup Remote item in IntelliJ and run it in debug mode

6) Place breakpoints and run the code to catch them

Reference :
[2] Hot deployment in Gretty
[3] Adding Remote item in IntelliJ