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");
        }
    }
Advertisements

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;
    }
}