Kafka – Command Line Consumer

If you have an application deployed on Docker and the application send messages to Kafka and if you want to verify the messages without running a Junit test case or Flume agent, then you can use the below command


docker exec -it KAFKA_CONTAINER_ID /opt/kafka_2.11-0.8.2.1/bin/kafka-console-consumer.sh --zookeeper ZOOKEEPER_DOMAIN_NAME:2181 --topic TOPIC_NAME --from-beginning

Docker File to Deploy a WAR file on Tomcat

In this post, I am going to show how to deploy a war file on Tomcat Server.

Instead of using Default Tomcat image from DockerHub, I have tried to download the tomcat server bundle and used it.

For this exercise, I have taken the sample.war file from Tomcat web site. Please refer https://tomcat.apache.org/tomcat-6.0-doc/appdev/sample/

Once you have downloaded the sample.war file, then it’s time to create the Dockerfile.

Create Dockerfile and copy the below content in it.



FROM java
MAINTAINER Bala

RUN curl -O http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.55/bin/apache-tomcat-7.0.55.tar.gz
RUN tar xzf apache-tomcat-7.0.55.tar.gz
ADD sample.war apache-tomcat-7.0.55/webapps/
CMD apache-tomcat-7.0.55/bin/startup.sh && tail -f apache-tomcat-7.0.55/logs/catalina.out
EXPOSE 8080


Then it’s time to build and run the docker container.

Build the container:


docker build -t tomcat .

Run the container:


docker run -p 8080:8080 tomcat

Then access the browser and check http://localhost:8080/hello. You will be able to see the welcome message.

Please refer Deploy war file on Tomcat to know on how to use Tomcat image directly.

Docker compose

Docker compose provides a nice way to link multiple containers that are running on a machine.

Take the below example,

We have an Angular JS web application and the back end is a Rest App. Both are running on different port number. Angular App is running on 80 and REST is on 8081. So we have to hard code the domain and other information in web application config file to consume the Rest services or else we have to pass an env variable in the start up script of web application.

So if you want to update REST service URL, you would have to update the code or start up script. But if we use Docker compose, then it will be pretty simple to do.

Refer the below docker compose file.


rest:
   image: rest_image:1.0
   log_driver: "json-file"
   ports:
     - 8081:8081
   log_opt:
      max-size: "10m"
      max-file: "5"    
web:
    image: web_image:1.0
    environment:
     - REST_URL=http://rest:8081
    log_driver: "json-file"
    ports:
      - 80:80
    log_opt:
       max-size: "10m"
       max-file: "5"
    links:
         - rest:rest

web_layer has an env variable which holds the Rest service url location. Assume that you have two env’s. Dev and QA and want to point QA env’s Rest service from Dev env Web app. Then it can be done nicely in docker compose script as below.


rest:
   image: rest_image:1.0
   log_driver: "json-file"
   ports:
     - 8081:8081
log_opt:
     max-size: "10m"
     max-file: "5"

web:
    image: web_image:1.0
    environment:
    - REST_URL=http://qa.companyinfo.com:8081
    log_driver: "json-file"
    ports:
     - 80:80
    log_opt:
      max-size: "10m"
      max-file: "5"

Rest Assured – Test Secure web services

I have recently faced an issue while integrating the Rest assured Test cases with Maven build.

All my web services are protected by SAML implemented tool. So if we access any web service via Rest Assured test case then it will return an error page and asking to provide the user name and password. Hence all those test cases are getting failed.

There are two ways we can resolve the issue.

1. Expose the Restful web services via an another port on which the authentication is not enabled. So we can access all the services through that port.


import com.jayway.restassured.RestAssured;
import static org.hamcrest.Matchers.containsString;
import org.junit.Test;

public class HelloServiceTest  {

    @Test
    public void testPing() {
        RestAssured.baseURI = "http://localhost";
        //Different port number
        RestAssured.port = 8080;
        RestAssured.basePath = "/ctx/";
        RestAssured.when().expect().statusCode(200).body(containsString("Hello World")).get("/hello");
        }
    }

2. Create an unexpired Test cookie and pass that cookie for each Rest Service call like below. This cookie needs to be processed and validated at service provider end.



import com.jayway.restassured.RestAssured;
import static org.hamcrest.Matchers.containsString;
import org.junit.Test;

public class HelloServiceTest  {

    @Test
    public void testPing() {
        RestAssured.baseURI = "http://localhost";
        RestAssured.port = 80;
        RestAssured.basePath = "/ctx/";
        RestAssured.given().cookies("TEST_USER_COOKIE", "Bala-UID").when().expect().statusCode(200).body(containsString("Hello World")).get("/hello");
        }
    }

String handling – Interview question

Find if two strings only differ by n characters. True if it does, false if it doesn’t

Krishna ,Krishnn, 1 =>true
Expanion, XEpanion, 2 => true
Expanion, APppanion, 1 => false


import java.util.Scanner;

public class StringMain {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        int diff = scanner.nextInt();
        if (str1 == null || str2 == null) {
            System.out.println("One of the string is null");
        }
        int actualDiff = compare(str1, str2);
        if (diff == actualDiff) {
            System.out.println("True");
        } else {
            System.out.println("False");
        }
    }

    private static int compare(String str1, String str2) {
        int diff = 0;
        char[] smallArray;
        char[] largeArray;
        if (str1.length() < str2.length()) {
            smallArray = str1.toCharArray();
            largeArray = str2.toCharArray();
        } else {
            smallArray = str2.toCharArray();
            largeArray = str1.toCharArray();
        }
        int i = 0;
        while (i < smallArray.length) {
            if (smallArray[i] != largeArray[i])
                diff++;
            i++;
        }
        diff += largeArray.length - i;
        return diff;
    }
}