Blog

Share Cookies between Step definitions of RestAssured Cucumber Example

BDD, Microservices
Recently I had to provide a framework to test a bunch of APIs in Cucumber RestAssured which has a login step definition. The login returned a set of cookies which should be used in subsequent requests. My first instinct was to use getDetailedCookies() and pass the cookies on to subsequent requests and set it using cookies(detailedCookies) method. But unfortunately there were cookies missing in the returned Cookies and subsequent API test requests failed. //Authentication Step definition String body="userId=1212&&password=232323"; String url = "http://mywebsitelogin.com"; Cookies detailedCookies =RestAssured.given().body(body).post(url).andReturn().getDetailedCookies(); return detailedCookies; //Actual API request RestAssured.given().cookies(detailedCookies).body(body).post(url); Finally CookieFilter came to rescue. The CookieFilter object will be filled with cookies and you can use it in other step definitions. Pretty simple but works like a charm. //Authentication Step CookieFilter filter = new CookieFilter(); String body="userId=1212&&password=232323"; String…
Read More

Streaming With Reactive Java and Spring JDBCTemplate

Reactive Java, RxJava
I started learning about RxJava while working on implementing Hystrix Circuit Breaker for the microservices that I was developing. Recently I had a use case where I had to load a lot of data from the backend database. I ended up not implementing the streaming that I am about to describe at my work, but I still think this might be useful for anyone trying to load data from database but does not need the entire data set. I am not saying this is the case all the time but this code could come in handy in some cases. You can find the entire code at : GIT Let us start with the code that calls the Streaming interface. QueryStreamerJdbcTemplate template = new QueryStreamerJdbcTemplate(dataSource); Semaphore semaphore = new Semaphore(1); semaphore.acquire(1);…
Read More

Java 8 Parallel Streams

Java8
Using Parallel Stream Java 8 streams comes with some convenience to do parallel processing by just calling the method parallel() or parallelStream() and all the magic happens behind the scene. But it comes with a price. If you are not careful about a few things it can actually backfire and cause problems with performance or may even cause concurrency problems or race conditions. A few things to take into consideration: Are the operations in the stream stateful or stateless? Consider the following example: words.stream() .filter( word->word.startsWith("a")||word.startsWith("e")||word.startsWith("i") ) .forEach(System.out::println); Here the filter operation is stateless operation because it only operates on one element at a time. It does not depend on any other elements to complete the operation. Consider the following. Inorder for the elements of the stream to be sorted…
Read More

Java 8 Optionals pattern

Java8
What is an optional? An optional is a concept that was introduced to represent the case when no result is produced. An optional wraps a value that is returned from a function and now your function can return a <no value> wrapped in an optional. This means that the optional can be empty. For example consider the following stream which finds the user with max age with name "Jacob". What if the list of users is empty. In that case we have nothing to return . This is where optionals come handy. It gives a way to specify that the function can return a no value. List<User> users = new ArrayList<>(); Optional<User> jacob = users .stream() .filter(user -> user.getName().equals("Jacob")) .reduce( MaxAgeHelper::maxAgeUser); public class MaxAgeHelper { public static User maxAgeUser(User user1,…
Read More

Shiro Security with Spring in WebApplication

Security
Things to know before starting with SHIRO security. Get familiar with the following terms Subject : A subject is a security specific view of a user. User usually denotes a human being. But a subject could be human or something else. Subject is associated with a Session. Subject contains methods like login/logout. Session: This is Shiro specific instance of Session. In a web environment it is HTTPSession. Biggest take away here is you don't need an HTTP environment  or EJB environment to use Shiro as the session is Shiro specific. Session object gives you all the facilities of HTTPSession plus some extra goodies. Principal : Principal is the identifier used to identify the user. It could be user name. Credential: Credential is used to prove the identity of the user. Example is the…
Read More