Posts

Showing posts from 2017

RestTemplate Custom Interceptors

Spring Framework provides a very handy Http connection class specialized on REST communication named RestTemplate. The RestTemplate has a wealth of REST call methods that can be easily used to quickly invoke external / remote REST API endpoints with no mess of opening and closing sockets, or even marshal and unmarshaling java objects. However such ease of use comes with a caveat, whenever we need greater control with the connection, or the marshal and unmarshal process of java objects we can't do so easily as when we are using HttpConnection class. Nevertheless, one way we can gain some control to the Request and Response is through ClientHttpRequestInterceptor interface. When registered to the RestTemplate object, this interface grants us access to the Request object, request body data and also Response object. The example below shows how we can utilize ClientHttpRequestInterceptor interface to log Request and Response during a typical communication with external API: im

Mocking JNDI calls for Unit Test

Mocking JNDI calls for Unit Test Sometimes, on the rarer cases the properties data that usually drive a server based application resides in the JNDI calls instead of something.properties file. In such scenario, mocking the JNDI calls using Mockito or Powermock are inherently difficult. Luckily the Spring Framework provides a way to "mock" the context by using SimpleNamngContextBuilder. By declaring the JNDI calls in a static method (because the Builder needs to be activated before any other autowiring could take place): @BeforeClass     public static void oneTimeSetUp() throws Exception {         SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();                 builder.bind("sampleJndiKey", "sampleJndiiValue");         builder.activate();     } Here's a full sample: @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration public class TestIT {     @Autowired         Environment env

Sniffing Request and Response on Wiremock

Sniffing Request and Response on Wiremock Often we use Wiremock on our test classes without knowing how the entire request or response looks like, well, mostly the requests since we can construct the response body from plain string. For that apparently Wiremock has a nice little request listener that can be utilised to print out all requests received by the Wiremock port: Syntax:   @Rule   WireMockRule wmr = new WireMockRule(8888);   @Before   public void setUp() {     wmr.addMockServiceRequestListener(new RequestListener() {       @Override       public void requestReceived(Request req, Response resp) {         System.out.println("URL: " + req.getAbsoluteUrl());         System.out.println("Req Body: " + req.getBodyAsString());         System.out.println("Resp Code: " + resp.getStatus());         System.out.println("Resp Body: " + resp.getBodyAsString());       }     });   } or the Java 8 way:

The Rise of Lift Tailing

Image
The Rise of Lift Tailing Recently there has been a rising number of talks about CBD apartments, even the secured ones, getting robbed. Intriguingly a search on google found no news about such robberies ever happened. So one would be left to ponder whether the talks we have been hearing about are all just rumours or they are true but being covered up in an attempt to avoid getting bad publicity and potentially lowering the sale price of the apartments. Whether it is just a rumour or the truth, in this blog I am analysing the possibilities and what we can do to prevent such events. The list below is formatted in the way of what could happen and what measure could be taken to prevent it from happening . I have always been an advocate of preventive is better than a cure. And I am sure you would agree if in an extreme example: if you left your daughter alone in the apartment and the robber managed to sneak in to the unit and have his way with your daughter for a good five minutes be

Scamming The Good Samaritans

Image
Lately I had been receiving several calls from people who claimed that they were from the not for profit organisations that I had donated to in the past. The intention of the calls were quite clear: they thanked me for my past donations and wondered if I would like to donate more. One thing that I found odd was the smell of desperation that I sensed by just chatting with them. Here is a grossly summarised conversation between me and a particular caller claiming to be from UNICEF: Caller: Hi this is XXXXX from UNICEF, may I speak with Irwan? Me: This is Irwan, how are you? ... ... Caller: We would like to thank you for your very generous past contribution ( as far as I recalled, I only ever donated $50 to UNICEF ) ... ... ... Caller: Do you know that XXXXX country is having XXXXX issue at the moment and require urgent supports, would you be able to do once off donation of $100 ? Me: Sorry I would like to donate but I am not in the position to commit at the moment ...