How to do decode and encode base 64 operation in parasoft SOA test using value from Json data bank

RajkumarRajkumar Posts: 2

We want to implement below steps in SOA test:
1. get the raw token string value from Json data bank
2. decode the token value to base 64
3. extract a part of decoded token
4. encode the extracted part to base 64 and get the value in json data bank

Can someone share detailed step by step document of this solution implementation.

Best Answers

Answers

  • benken_parasoftbenken_parasoft Posts: 664 ✭✭✭
    edited March 8

    I only have time to provide quick responses on the forum but I can explain things at a high level. If I were you I would use an Extension tool to run a script to do this.

    get the raw token string value from Json data bank

    This section from the docs has examples for how to get the value of a data bank from a script: Extensibility and Scripting Basics. Code would be something like String encoded = context.getValue("Generated Data Source", "Column Name")

    decode the token value to base 64

    There is Java API to do this, easy to invoke from a Groovy script, for example. Code would be something like byte[] decoded = java.util.Base64.getDecoder().decode(encoded);

    extract a part of decoded token

    How to code that depends on your criteria or requirements. A substring at a particular index? Extract a portion based on a particular regular expression? Something else?

    encode the extracted part to base 64 and get the value in json data bank

    Again, use Java API like String encoded = java.util.Base64.getEncoder().encodeToString(bytes);.

    If you want to store the newly encoded value back into a JSON data bank then your script could perhaps return a JSON string containing the newly encoded value and then you could chain another JSON data bank to your Extension Tool. Otherwise, you can also write the value to a test suite variable using context.setValue("name", "value")

  • RajkumarRajkumar Posts: 2

    Hi Benken,

    We are using below code in Extension tool and we see that the code is not working. We need help on below points:

    1. We see that decoding and encoding is not happening as we see that same token value after these operations.
    2. Also, please suggest if we need to make Extension tool as Add new test or as Add output within the REST API test case.

    Note: We have token containing more than 1000 characters.

    Code:
    import com.parasoft.api.*
    import java.lang.*
    import javax.xml.bind.DatatypeConverter;

    {

    //Retrieve the value from a databank with column name "item_var"
    c1 = context.getValue("Generated Data Source","Databank_Variable_Name");

    //Decode c1 and store it in c2
    
       byte[] c2 = java.util.Base64.getDecoder().decode(c1);
       context.setValue("Databank_Variable_Name", c2);
    
    return c2
    

    }

  • benken_parasoftbenken_parasoft Posts: 664 ✭✭✭
    edited March 11

    Are you sure this script is syntactically correct? I'm not seeing a method name in the script before the curly brace, for example. Were you able to select a method in the "Method" combo box? Does clicking "Evaluate" button show an error? Did the Extension Tool report an error to Quality Tasks view when executed?

    Please also note what I mentioned earlier about how to data bank columns. Only data banks can set a data bank columns. So, if you want to set a data bank column then you must chain a Data Bank to your Extension Tool. Otherwise, if you want to set a test suite variable then you can call "context.setValue" method to do that.

Sign In or Register to comment.