Java solution with JUnit tests