Ramiro Martinez admin

About

Username
Ramiro Martinez
Joined
Visits
454
Last Active
Roles
Admin, Staff
Points
40
Posts
46
Badges
5
  • Re: Encode / Decode base64 with javascript?

    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.