Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Generic method to Compare two array of response

Options
Parasofttoudaya
Parasofttoudaya Posts: 232 ✭✭

Lets take a situation where you need to compare two array of response:

Extract the source --> alter and append with '|' (Pipe) symbol --> source_orderIds
Extract the target--> alter and append with '|' (Pipe) symbol -->target_orderIds

Add below code as extension tool to compare:
Using this code unordered array also gets validated

import com.parasoft.api.*;
import java.util.Arrays;
import java.util.*;
import java.util.regex.Pattern;
public boolean CompareResponse(Object input, ScriptingContext context){

String Source, Target;
Source = context.getValue("Generated Data Source", "source_orderIds");
Target = context.getValue("Generated Data Source", "target_orderIds");
Application.showMessage("Source: " + Source)
Application.showMessage("Target: " + Target)
return compareTwoArrayString(Source,Target);

}

public boolean compareTwoArrayString(String Source, String Target){

  Source = Source.trim();
  Target = Target.trim();
        String[] ArrayStringSource,ArrayStringTarget;
        ArrayStringSource = Source.split(Pattern.quote("|"));
        ArrayStringTarget = Target.split(Pattern.quote("|"));
        Application.showMessage("value : " +ArrayStringTarget[0])
        Application.showMessage("Length : " +ArrayStringTarget.length)
        if (ArrayStringSource.length == ArrayStringTarget.length ){
              Arrays.sort(ArrayStringSource);
              Arrays.sort(ArrayStringTarget);

              return Arrays.equals(ArrayStringSource, ArrayStringTarget);
        }else
        return false

}