How to do decode and encode base 64 operation in parasoft SOA test using value from Json data bank
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
-
Here is a groovy script we use to decode and encode
import com.parasoft.api.*
import java.lang.*
import javax.xml.bind.DatatypeConverter;public String Decode(input, context) {
//Pull the encoded base64 string into the script. a = context.getValue("Generated Data Source", "Variable_Name"); Application.showMessage("Variable_Name: " + a) //Decode the string. decoded = new String(DatatypeConverter.parseBase64Binary(a)); Application.showMessage("Decoded: " + decoded) assert decoded.contains('SUCCESS') //Encode the string. This should match the original string you pulled in to the script. encoded = new String(DatatypeConverter.printBase64Binary(decoded.getBytes())); Application.showMessage("Encoded: " + encoded) return
}
6 -
I'd recommend using java.util.Base64 over DatatypeConverter. A couple reasons:
- The Base64 class is the "new" way of doing this, introduced in Java 8 whereas DatatypeConverter is what you had to use in Java 7 and earlier.
- DatatypeConverter is also part of the JAXB API which was removed from the core Java API since Java 11. Using "Base64" is safer if you want this code to also work with newer versions of Java.
5
Answers
-
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")
0 -
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:
- We see that decoding and encoding is not happening as we see that same token value after these operations.
- 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
}
0 -
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.
0