Generic method to Compare two array of response

ParasofttoudayaParasofttoudaya Posts: 195

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

}

Sign In or Register to comment.