Rest Assured – Example

Rest Assured is one of the nice API for testing the Rest web services. Its easy to learn and simple to implement. I can say that its based on Given when then approach[Gherkin language]

Just include the below dependency in the maven pom.xml


Here is a simple example,

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class CustomerRestAssuredTest {

    private final static Logger LOGGER = LoggerFactory.getLogger(CustomerRestAssuredTest .class);

    public void setUp() {
       // Replace with the domain name of the service
        RestAssured.baseURI = "http://localhost";
        //Port number
        RestAssured.port = 8080;
        //Service Base Path
        RestAssured.basePath = "/service/";

    public void testGetCustomers() {
        Response response = RestAssured.when().get("customers");
        assertEquals(200, response.getStatusCode());
        assertEquals(true, response.getBody().asString().contains("CONTENT_TO_TEST_AGAINST"));
    public void testGetCustomerById() {
        Response response = RestAssured.when().get("customers").param("id","cust_123");
        assertEquals(200, response.getStatusCode());
        assertEquals(true, response.getBody().asString().contains("CONTENT_TO_TEST_AGAINST"));
    public void testSaveCustomerDetails() {
       //Create request builder
        RequestSpecBuilder builder = new RequestSpecBuilder();
       //Set the body content
        //Set the request content type
        builder.setContentType("application/json; charset=UTF-8");
        //Build the spec
        RequestSpecification requestSpec =;
        Response response = RestAssured.given().spec(requestSpec ).when().post("customers/save");
        assertEquals(200, response.getStatusCode());
        assertEquals(true, response.getBody().asString().contains("SUCCESS"));

Assume that we have a service called customers and its returns the list of customers. The service URL is http://localhost:8080/service/customers

Please refer the above code and inside the setUp method, we just have to initialize the Base URL, port and Base Path

After that in the actual test method, We call RestAssured.when() then followed by HTTP method and the service name. Here in this its a GET service and method name is customers. Here we dont pass any parameters to this service. Thats is.
It will return com.jayway.restassured.response.Response and we have to parse through it or get the response body content and validate it.

This is just a simple example. For more information and examples, please refer

Apache Spark Cluster Architecture

Please refer the below cluster diagram of Spark.



Here we are just going to see how the Spark cluster is working. Basically the Cluster manager is responsible for managing all the worker nodes and allocate resources upon the request from Driver program.

So the below is like this,
1. Driver submits the request to any one of the cluster manager to run the jobs. If in case of stand alone cluster it will submit the request to Master
2. Master/Cluster manager allocate the resources(Worker Nodes) to Driver
3. Then Driver program contacts each worker nodes directly and each node has Executor which is responsible for doing the tasks.
4. Driver sends the application code to each executors in the form of JAR
5. Finally the Spark Context sends tasks to each executors to run the tasks


Create a Fat JAR

To create a FAT jar contains all the dependent classes and Jars, Please use the below approach





Add the sbt-assembly plugin in the plugins.sbt as below

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.1")

Change the version appropriately and run sbt assembly to create the Fat Jar

Apache Spark

Apache spark is a cluster computing engine and its best fit for handling the iterative tasks

Apache Spark supports in memory data computation which means, the data will be moved to node’s memory, then the computation will be done. Because of this, Spark is more faster than Hadoop.

The fundamental data structure in spark is RDD. Resilent Distributed DataSet which is an immutable distributed collection of objects.

It does not have a specific storage as like Hadoop. So it may use any of the underlying data storage. It could be anything such as cloud storage, Hdfs, Nfs.

Spark is not a replacement for Hadoop but its a better replace for Hadoop Map reduce jobs.

Spark job is very easy to create compared to Hadoop Map reduce.

Spark streaming is a best alternative to Storm processing

MLib is also a best alternative to Mahout

Extract haState from the ResourceManager REST API’s response

I have used jq[] command line JSON processor to process the clusterInfo JSON response and extract the haState value from it.

If the JSON is stored in a file, then you could use the below script. Make sure that you install jq package first.

jq '.clusterInfo.haState' clusterInfo.json

If you want to access the service and parse the response on the fly, then use the below CURL command

curl 'http:///ws/v1/cluster/info' | jq -r '.clusterInfo.haState' 

Wordcount in Scala


object WordCount {
  def main(args: Array[String]) {
    val textContent = Source.fromFile("TEXT_FILE_PATH").mkString
    val countMap = textContent.split("\\s+").groupBy(x => x).mapValues(x => x.length)

Text File:

This is a test file



Map(test -> 2, this -> 2, is -> 1, a -> 1, file -> 1, one -> 1)

Scala Map, FlatMap

In scala, we can map a value to an another value without affecting the original value. For that we can use map method.

Please note that map is not a Map collection in Java.

There are two kinds of mapping can be done.

Map and FlatMap

Map => This maps the value to another value
FlatMap => This maps the value first and then flatten it

If a flatMap applied on a collection of string means, then it will return List[List[String]]

Please refer the below example to know more.

package com.scala

object MapTest {

  def main(args: Array[String]) {
    val fruits = List("apple","orange", "pineapple")
    //Print the contents
    //Convert into map
    //Convert into FlatMap


List(apple, orange, pineapple)
List(A, P, P, L, E, O, R, A, N, G, E, P, I, N, E, A, P, P, L, E)