Encode / Decode base64 with javascript?

TestTest Posts: 3
edited February 2017 in SOAtest

Hi,

I need to decode base64 encoded output of a rest service and compare the decoded result with a value from a database, but I don't know how.
I understood that it is possible to decode with javascript, but how can this be done? This is the scenario:

  1. JSON call to a REST service returns a encoded value.
  2. I saved this value in the JSON databank.
  3. Now I need to base64 decode this value --> how? How can I use the stored value in Javascript?
  4. After decoding this value, I need to check if this is similar to the value in the database. --> How can I compare the result of a javascript with another value?

I have no idea how to use the stored value in javascript for decoding, and how to compare this output. Any advice is appreciated.

Comments

  • OmarROmarR Posts: 205 admin

    Hello Señor Test,

    Have you had a chance to look at the Extensibility (Scripting) section of your SOAtest Userguide? You can find this in your Installation Directory or if you navigate to the top toolbar in Parasoft>Help>Help Contents, you will find a plethora of information available to you!!


    Now, to encode/decode a value, you should be able to use the following method I found online:


    Please look over the Extensibility section of your SOAtest Userguide and try to implement the above! Let us know how it goes!

  • TestTest Posts: 3

    Hi Omar,

    I am now able to use a stored value from a data bank in Javascript, so thanks a lot for that!
    However, I'm stuck at the decoding part. I was hoping to use the 'atob' function within javascript in the extension tool, but it seems that it's not supported by parasoft. I'm getting the following error:
    Runtime error The function 'atob' is not defined for object '[object Object]'
    detected at line 7 of function 'getKeywords' in string starting with: 'var decoded =
    Packages.soaptest.api.SOAPUtil '...

    Do you know how we can make the atob function work in the extension tool?

    About the javax.xml.bind suggestion, I do not know how to make it work :sweat: I added the jar to the classpath in the preferences window and from there on I'm lost. I have no experience with java and have no clue what to do next :s So if the atob function cannot work in Parasoft, can you guide me through making the java.xml.bind work?

    Señorita Test ;)

  • OmarROmarR Posts: 205 admin

    Forgive me Señorita,

    I have not used the 'atob' function myself, but the following Groovy script should do the trick. No additional jars required. :)

    import com.parasoft.api.*
    import java.lang.*
    import javax.xml.bind.DatatypeConverter;
    
    public String Decode(input, context) {
    
       //Retrieve the value from a databank with column name "item_var"
        c1 = context.getValue("Generated Data Source", "item_var");
    
        //Decode c1 and store it in c2
        c2 = new String(DatatypeConverter.parseBase64Binary(c1));
    
        //Encode c2 back and store it in c3 to verify conversion
        //c3 = new String(DatatypeConverter.printBase64Binary(c2.getBytes()));
    
        return c2
    }
    
  • TestTest Posts: 3

    Thanks Omar,

    Unfortunately I am getting a nullpointerexception. Is there an easy way to fix this?

    Error Message:
    DataSource: Base64 (row 1): Error during script execution. View Details for more
    information.

    java.lang.NullPointerException.

  • Ramiro MartinezRamiro Martinez Posts: 47 admin

    Hi Test,

    This error normally indicates that you are attempting to "grab' an element that does not exist.
    In the line:
    c1 = context.getValue("Generated Data Source", "item_var");
    You are attempting to extract a value from a variable named "item_var", which would have been created using a data bank.
    If you see this error, please make certain that you are calling the exact same name as the Variable you created with your data bank, otherwise when attempting to pull the value from the context it will look at a null point.

    The name here:

    should match the name here:
    c1 = context.getValue("Generated Data Source", "item_var");

    Best regards,

    Ramiro

  • benken_parasoftbenken_parasoft Posts: 522 ✭✭✭

    As of Java 8, I'd recommend using java.util.Base64.
    DatatypeConverter from JAXB is only needed for Java 7.

  • Ramiro MartinezRamiro Martinez Posts: 47 admin
    edited February 2017

    Hey everyone,

    The script that Omar provided can also be used for encoding, with a few small modification:

    import com.parasoft.api.*
    import java.lang.*
    import javax.xml.bind.DatatypeConverter;
    
    public String Encode(input, context) {
    
       //Retrieve the value from a databank with column name "item_var"
        c1 = context.getValue("x");
    
        //Decode c1 and store it in c2
        //c2 = new String(DatatypeConverter.parseBase64Binary(c1));
    
        //Encode c2 back and store it in c3 to verify conversion
        c3 = new String(DatatypeConverter.printBase64Binary(c1.getBytes()));
        context.setValue("x", c3);
        return c3
    }
    

    The above script will take the take the value that is associated with the test suite variable 'x' and encode the value. It then returns the value to the next tool in the chain and sets a new value for test suite variable 'x'(this only last for this test's execution). The new 'x' value can then be used for the rest of the test in this execution.

    I agree with Benken that "java.util.Base64.DatatypeConverter" is preferred.
    If you are still using SOATest 9.9 and have not changed the Java version then the above will work.

  • benken_parasoftbenken_parasoft Posts: 522 ✭✭✭

    The name is "Benken" :)

  • Ramiro MartinezRamiro Martinez Posts: 47 admin

    You didn't see anything..... It never happened ;)

Sign In or Register to comment.