• Slider Banner

    Prepare for

    Technical Interview

    Over .1 Million+ Interview Questions for different profiles

    Read More
  • Slider Banner

    Join the

    Programming World

    Learn about top programming languages, tools and frameworks.

    Read More
  • Slider Banner

    Prepare with

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read More

How to do JSON Schema Validation using Rest Assured?

Use below steps for JSON schema validation using Rest Assured

Step 1: Copy Latest Maven dependency from below link

https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator

Step 2: Get JSON Schema from developer or generate using some tool (Click Here for online Tool) from your api. Use below sample for test (save it with Sample.json):

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    },
    {
      "type": "object",
      "properties": {
        "postId": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "body": {
          "type": "string"
        }
      },
      "required": [
        "postId",
        "id",
        "name",
        "email",
        "body"
      ]
    }
  ]
}

Step 3: Create Sample.java file and paste below code

import org.testng.annotations.Test;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;

import io.restassured.RestAssured;

public class Sample
{
	@Test
	
	public void testJsonSchema()
	{
		RestAssured.given()
			.when()
			.get("https://jsonplaceholder.typicode.com/comments?postId=1")
			.then()
			.assertThat()
			.body(matchesJsonSchemaInClasspath("com/mycompany/api/bl/Sample.json"));
	}
}

Here in get you have to specify your request and in body you have to specify path where your json schema file is.

What is the group index in XPath?

The group index is nothing but writing the XPath as a sequence. For example, XPath provides numbers from 1,2,3... for the element under every parent.

When we use an index, we might get end up having multiple elements when we wanted a particular element.

In such cases, the Group index will be more helpful to the user. Now the Group index numbers the element from 1..., even though element under the different parent.

The index numbers will never be repeated.

How to Select a Checkbox using Protractor?

We need to follow a few steps to make sure that the element is selected or not, Normally when we have this kind of scenario, what we do is, we will use click() method to select the checkbox. After this, we might move on to the next step of the scenario.

Sometimes not selecting will help you; you got to make sure that the checkbox is selected. To verify it we need to use the isSelected() method and get check whether it is really selected.

How do you run headless mode browsers in Protractor?

Headless browser doesn’t require a graphics driver at all, They just get commands and execute them directly in memory. To achieve this, you need to specify headless in configuration.js under capabilities.

Chrome

capabilities: {
  browserName: 'chrome',

  chromeOptions: {
	 args: [ "--headless", "--disable-gpu", "--window-size=800,600" ]
   }
}

Firefox

capabilities: {
  browserName: 'firefox',

  'moz:firefoxOptions': {
	 args: [ "--headless" ]
   }
}

How to implement Read Write Lock in Java?

Use below code:

package com.test.filehandling;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ThreadSafeArrayList<E>
{
	private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
	private final Lock readLock = readWriteLock.readLock();
	private final Lock writeLock = readWriteLock.writeLock();
	
	private final List<E> list = new ArrayList<E>();
	
	public void set(E o)
	{
        writeLock.lock();
        try
        {
            list.add(o);
            System.out.println("Adding element by thread: "+Thread.currentThread().getName());
        }
        finally
        {
            writeLock.unlock();
        }
	}
	
	public E get(int i)
	{
		readLock.lock();
        try
        {
            System.out.println("Printing elements by thread: "+Thread.currentThread().getName());
            return list.get(i);
        }
        finally
        {
            readLock.unlock();
        }
    }
	
    public static void main(String[] args)
    {
        ThreadSafeArrayList<String> threadSafeArrayList = new ThreadSafeArrayList<String>();
        threadSafeArrayList.set("1");
        threadSafeArrayList.set("2");
        threadSafeArrayList.set("3");
 
        System.out.println("Printing the First Element: "+threadSafeArrayList.get(0));
    }
}

What are the differences between HashMap and TreeMap?

Below are the difference:

Point of Distinction HashMap TreeMap
Time Complexity (Big O notation) Get, Put, ContainsKey and Remove method O(1) O(1)
Definition Java HashMap is a hashtable based implementation of Map interface. Java TreeMap is a Tree structure-based implementation of Map interface.
Iterator Order Random Sorted according to either natural order of keys or comparator (as specified during construction) 
Null Keys / Value HashMap allows a single null key and multiple null values. TreeMap does not allow null keys but can have multiple null values.
Homogeneous/ Heterogeneous HashMap allows heterogeneous elements because it does not perform sorting on keys. TreeMap allows homogeneous values as a key because of sorting.
Performance HashMap is faster than TreeMap because it provides constant-time performance that is O(1) for the basic operations like get() and put(). TreeMap is slow in comparison to HashMap because it provides the performance of O(log(n)) for most operations like add(), remove() and contains().
Interface HashMap implements Map, Cloneable, and Serializable interface. TreeMap implements NavigableMap, Cloneable, and Serializable interface.
Data Structure The HashMap class uses the hash table. TreeMap internally uses a Red-Black tree, which is a self-balancing Binary Search Tree.
Comparison Methods It uses equals() method of the Object class to compare keys. The equals() method of Map class overrides it. It uses the compareTo() method to compare keys.
Functionality HashMap class contains only basic functions like get(), put(), KeySet(), etc.  TreeMap class is rich in functionality, because it contains functions like: tailMap(), firstKey(), lastKey(), pollFirstEntry(), pollLastEntry().
Requirement for Keys Equals() and hashCode() needs to be overwritten. comparator needs to be supplied for keys implementation, otherwise natural order will be used to sort the keys.
Application General purpose, fast retrieval, non-synchronized. ConcurrentHashMap can be used where concurrency is involved.  Algorithm where sorted or Navigable feature are required. For example, find among the list of employees whose salary is next to given employee, range search, etc.

What will be the output of below Java code?

Example Code 1: 

public interface  A  
{	
    public void m1();
    public void m2();
}

class B implements A
{
    public void m1()
    {	
    }
}

Output: compile-time error:- The type B must implement the inherited abstract method A.m2()

Example Code 2: 

public interface A  
{
    public void m1();
    public void m2();
}

abstract class B implements A
{
    public void m1()
    {
        System.out.println("Vinay");
    }
}

class C extends B
{
    public void m2()
    {
        System.out.println("Method2 value from the class C");
    }
    public void m1()
    {
        System.out.println("Method1 value from the class C");
    }

    public static void main(String[] args)
    {
        C c=new C();
        c.m1();
        c.m2();

        B b=new C();
        b.m1();
        b.m2();

        A a=new C();
        a.m1();
        a.m2();
    }
}

Output:

Method1 value from the class C

Method2 value from the class C

Method1 value from the class C

Method2 value from the class C

Method1 value from the class C

Method2 value from the class C

Subscribe to