Pages

Thursday, September 24, 2015

First class functions Vs Higher order functions

First class functions means the ability to treat a function as a value, assigning it to a variable, or passing it as an argument to another function.

 //example 1 assignment  
   Function<String,String> postfixFunction = s -> s + "-postfix";  
 //example 2 passing as parameter  
   public void someMethod() {  
     stringManipulator(postfixFunction, asList("A","B"));  
   }  

A higher order function is a function that accepts other functions as arguments, or returns another function.

  //example 1 accepting function as arguments  
   public List<String> stringManipulator(Function<String, String> manipulator, List<String> data) {  
     return data.stream().map(manipulator).collect(toList());  
   }  
   //example 2 returning a function  
   public Function<String, String> postfixFunction() {  
     return s -> s + "-postfix";  
   }   

Friday, September 18, 2015

Don't be afraid of chinese restaurants ;)

How many times in your life have you shout to a non english speaking Chinese waitress at a restaurant in Soho, something like: "HEY!!! Why are you swapping the card 4 times?!?!?!!"
In this post I will prove that Chinese waitress are honest and often people panic, because they don't understand what Idempotency is.

The debit cards payment gateways are the classic example of idempotency.

In distributed systems terminology, we say that a service is Idempotent, when its clients can repeatedly send the same requests while the service produces the same result.
In other words, multiple identical requests are treaded by the service as a single request.

Important to note that this doesn't mean that the response is identical, it is just the service result that will be identical.
While the state of the response may change between requests, the result will always be the same, for identical requests.
In other words, it is save to call the service multiple times.

Let's have a look at a simple example of an idempotent function in java:

Receipt process(DebitCard debitCard, Amount amount) { throws NoPatienceException 
      if(not(transactionSentFor(debitCard,amount))) {  
           return cardProcessor.process(debitCard, amount);  
      }  
      else {           
           return blankReceiptFor(debitCard, amount);  
      }  
 }  

In the example above, we can clearly see that the conditional statement is safeguarding us from starting probably a distributed transaction across multiple parts of a distributed system. Instead of that, if we try to process again,  a blank/identical receipt with the returned



Come on, stop being affraid of chinese restaurants. 


Sunday, July 12, 2015

Why big corporations don't like retrospectives?

Not too long ago, I used to facilitate retrospectives for some of the teams in the department.
But for some strange reason their interest in retrospectives just disappeared.
I thought a lot about it, but I couldn't come up with an answer. Why?

Is it that they don't understand the advantages of retrospectives?
Is it that they have no time for retrospectives?
Or is it that they feel powerless when they do a retrospective?

In this brief text I want to just share my opinion on why I think corporations, have no interest in retrospectives(A bit of an speculative opinion, but just an opinion).

A retrospective is well known Agile practice that aims to improve any aspect of work. During a retrospective a team debates about things that matter for them and for the company: ideas, solutions, process, collaboration, communication, quality, recruitment, salaries, etc...
This are just some topics.

Unfortunately retrospectives are not being taken serious in many companies:
- in some, retrospectives are discouraged and seen as a waste of time
- in others shyly implemented within a constraint environment without visibility or power to
take any action. Just as a way of ventilating frustrations. 

So why is this that they are so unpopular?

The Answer is simple:
Companies fear change but specially, they fear decentralisation and lose of control.

The fact is that well organised regular retrospectives, could easily challenge the stablish organisational structure by making evident and visible its inefficiencies.

Many young technical companies quickly gain terrain to the prehistoric corporates not just because their decentralized and autonomous nature but also because of their eagerness and necessity to communicate and improve. Most of the successful ones often practice retrospectives.

The old school of building software companies has its days counted. What employees have to say about process inefficiencies, technical challenges, etc ... Is as important, if not more than the absolutely false illusion of rush that the corporation sees in the market.

Big corporations are organisations with huge amounts of resources and great potential for making significant impact in industry. Helping them getting rid of their fears is key for building not just good companies but a
healthy industry.

Software is continuous change, so embrace change, embrace retrospectives, embrace the future

Thursday, July 9, 2015

Maven build in colours

Did you ever wonder, how you can make your maven build display colours?
In this quick post I will show you what you need to do to show colours in your maven build.

  1. Navigate to wherever you have installed maven and go into the lib folder ~/apache-maven-3.3.1/lib
  2. Delete slf4j-simple-1.7.5.jar
  3. Find on the internet the following jars and add them to the lib folder
    log4j-api-2.2.jar
    log4j-core-2.2.jar
    log4j-slf4j-impl-2.2.jar
    slf4j-ext-1.7.5.jar

  4. Get out of the lib folder and navigate into the conf folder once in there create a file called log4j2.xml and add the following content:

    <?xml version="1.0" encoding="UTF-8" ?>
    <Configuration>
      <Properties>
        <Property name="maven.logging.root.level">INFO</Property>
      </Properties>
      <Appenders>
        <Console name="console" target="SYSTEM_OUT">
          <PatternLayout pattern="%highlight{[%p] %msg%n%throwable}" />
        </Console>
      </Appenders>
      <Loggers>
        <Root level="${sys:maven.logging.root.level}">
          <Appender-ref ref="console"/>
        </Root>
      </Loggers>
    </Configuration>
     
  5. Go to the terminal and try building a maven project

Monday, June 8, 2015

Stefan Birkner's system-rules library

Just a super quick post about an interesting library I just discovered :)

Its very rare the case when you will have to test things such as an String being printed in the console or a System property being set from the program... But in occasions it happens that.

Last week I discovered a library called system-rules, from Stefan Birkner. http://stefanbirkner.github.io/system-rules/
It is really interesting and easy to use, it can help you easily do tests that involve java.lang.System

Lets just quickly saw an example.
Imagine that for some reason you want to test that the console prints some message... I don't know you, but the only way I know to do that, is to redirect the stream that goes to the console, to something that you can control and read from(e.g a file, a log...). This kind of test would have lots of boiler plate. It would look somehow like this:

 @Test  
   public void consoleOutputOldSchoolTest() throws FileNotFoundException {  
     //Create some text file where the output will be redirected, so you can make an assertion later.  
     File testingFile = new File("testingFile.txt");  
     //Create a testing stream and give the file to it  
     PrintStream testingStream = new PrintStream(testingFile);  
     //Keep a copy of the old console output stream  
     PrintStream consoleStream = new PrintStream(System.out);  
     //Reset the object out to the new stream  
     System.setOut(testingStream);  
     //Write something to the "console"  
     System.out.print("test");  
     //Rewire back to the original console output  
     System.setOut(consoleStream);  
     //Just an informative message  
     System.out.println("all back to normal");  
     //Read the output that we are testing  
     String output = new Scanner(testingFile).nextLine();  
     //Do your assertion  
     assertThat(output, is("test"));  
     //Delete the test file  
     testingFile.delete();  
   }  

With Stefan's library you can test the messages that go the terminal in the blink of an eye:

   @Rule  
   public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();  
   @Test  
   public void systemRulesLibraryConsoleOutputTest() {  
     System.out.print("test");  
     assertThat(systemOutRule.getLog(), is("test"));  
   }  

Ok, now back to work now ;)

Sunday, April 12, 2015

Install and configure Java in ubuntu easy

Whenever I change laptop or format my hard drive, I end up making a mess with the soft links to configure java...

This post is just a reminder too myself, for the future. I found a clean way I am comfortable with for setting my system to use java, via the update-alternatives tool that ubuntu has. This is how I do it:

1- Download the JDK from oracle and copy it to your user directory.
I like having it under ~/Java/jdk1.8.0_40

2- I often like installing maven, so I do the same, just I create another directory for it and I put in there the downloaded maven. ~/maven/apache-maven3.3.1

3- Then I set the necessary environmental variables for my user in the ~/.bashrc file.

export JAVA_HOME=/home/djordje/Java/jdk1.8.0_40
export M2_HOME=/home/djordje/maven/apache-maven-3.3.1
export M2=$M2_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin:$M2  


4- I use update-alternatives to install in the system the java interpreter and the compiler.

sudo update-alternatives --install "/usr/bin/java" "java" "/home/djordje/Java/jdk1.8.0_40/bin/java" 1

sudo update-alternatives --install "/usr/bin/javac" "javac" "/home/djordje/Java/jdk1.8.0_40/bin/javac" 1

5- Again i use update-alternatives, but for this time to configure the default version of the interpreter and compiler to be used by default.

sudo update-alternatives --config java
sudo update-alternatives --config javac

When running the command, it shows all versions of java installed, to pick one

6- At the end I just run java -version to see that the system is using the version of java I want.

I like this way, it is easy to do and you can quickly switch to a different installed version by just using the --config option









Wednesday, March 18, 2015

Contract testing

Let's imagine the following scenario...
We are working in distributed system with lots of applications.
The developers understand the importance of avoiding coupling amoung componets, so they decide to create restful applications to communicate via xml and json,
instead of building applications that are binary dependant with other applications.

During the development of a feature, the development team, did a change to the API, and unconciously they broke one of the consummer apps.
Unfortunately, this bug was really expensive, since the company just managed to discover it in its replica, pre-production environment by a long running
end to end functional test, after determining that what was broken was actually a marshaller of xml, there was no quick fix and they had to roll back.

In the root cause analysis meeting, developers from each of the teams, that own the apps that failed realised that the API change was the reason for the bug
and that there was no aditional work done in one of the unmarshallers.
The developers were told to fix the bug and also to come up with a solution that would avoid this from happening again.

After fixing the bug the developers toke some time to think how they could catch this kind of bugs before the pre-production environment where the expensive
integration tests run. One of them said, "What we need is consummer contract testing!"...

Consumer contract testing, allows consumers and providers of an API knowing if their latest changes on their marshallers or unmarshallers, could potentially be
harmful for the other party, without the necessity of performing an integration test. This is how it works:


1- The provider of the API, publishes an example of the API somewhere where he knows the consumer can access it(e.g publish it in a repo, sending it via email...).
2- The consummer takes the API example and writes a test that tolerantly accesses the values of interest.
   This in-document path(e.g xpath,jsonpath...) used to retrieve the values from the API example, is known as the contract.
3- The consummer publishes the contract in a place where knows the provider has access to it(e.g publish it in a repo, sending it via email...).
4- The provider will take the contract, and will use it in a test, against the generated output of the application. If the test fails when being run, the provider will know that they could potentially be breaking the the consumer, if they were to release the current version under test(a negotiation can take place).

Let's now have a look at a practical example of each of the steps above.

1- The developers that own the provider app, take from their passing acceptance test the output that the application is sending back to the consumer and they save
it into a file called "apiexample.xml", which looks like this:

 <output>  
      <content>  
           <partA>A</partA>  
           <partB>B</partB>  
      </content>  
 </output>  

They send this file over email to the team that owns the consumer application.

2- The developers that own the consumer app, will take the exampe and will write queries to it, to determine the contract they need. A unit test against the example, could be fine.

 @Test  
    public void apiExampleGeneratesValidatesToContract() throws Exception {  
     XPath xPath = XPathFactory.newInstance().newXPath();  
     String value = xPath.evaluate("/output/content/partB", getSource(readExample("apiexample.xml")));  
     assertThat(value,is(notNullValue()));  
    }  

3- Now that the developers know that the contract to access what they are interested in is:
 "/output/content/partB"
They can save it in a file called "contract.txt" and send it over email to the other team for they to make sure they will always be outputing according to the contract. Note that this tolerant
paths, allow to the provider to change any part of the API they want to change, as long as the contract is respected.

4- The provider will read the "contract.txt" file and will write a test where the contract will be applied to the applications output.

 @Test  
    public void apiExampleGeneratesValidatesToContract() throws Exception {  
     XPath xPath = XPathFactory.newInstance().newXPath();  
     String value = xPath.evaluate("/output/content/partB", getSource(readExample("apiexample.xml")));  
     assertThat(value,is(notNullValue()));  
    }  

Now when any of the teams run their builds, they will know if they are in breaching the contract and they will avoid the bug going further than the development environment.

You can find the complete source code of this example here.

Share with your friends