Forum Discussion

casp8's avatar
casp8
Occasional Contributor
5 years ago
Solved

Adding a keystore/groovy script to single request through Java

Hi all, 

I have a testsituation in which I want to have requests with and without the use of a keystore in a single testcase. I'm generating soapuiprojects through the Java package com.eviware.soapui.

 

I found a way to add a certificate in java using this code;

SoapUI.getSettings().setString(SSLSettings.KEYSTORE,
"pathToKeystore");
SoapUI.getSettings().setString(SSLSettings.KEYSTORE_PASSWORD,
"xxxxxxx");
SoapUI.getSettings().setBoolean(SSLSettings.CLIENT_AUTHENTICATION,
true);
SoapUI.saveSettings();

However, this adds the credentials to the whole project (logically), and therefore isn't usefull to me.

 

In the ReadyAPI user interface I can add credentials to a single request (with a request property), and a way to do this using the library would help me most!

 

An other option would be adding a groovy script to a testcase, and perform all the teststeps with a certificate after running this script. I did this in the user interface with this groovy script;

import com.eviware.soapui.settings.SSLSettings
import com.eviware.soapui.SoapUI

SoapUI.settings.setString( SSLSettings.KEYSTORE, "pathToKeyStore" )
SoapUI.settings.setString( SSLSettings.KEYSTORE_PASSWORD, "xxxxxxx" )

 

However, I do not know how to add this groovy script to a testcase using the package in Java Code. So any help with this would be appriaciated as well!

 

Thanks in advance!

 

  • Try writing the above code inside TestCase level TearDown script. Hope it helps.

3 Replies

  • avidCoder's avatar
    avidCoder
    Super Contributor

    Try writing the above code inside TestCase level TearDown script. Hope it helps.

    • CrazyFrog's avatar
      CrazyFrog
      Contributor

      Hello casp8 

       

      First you need to add a keystore to your project, you can do this with the following code:

       

      DefaultWssContainer wssContainer = wsdlOperation.getProject().getWssContainer();
      wssContainer.addCrypto([KEYSTORE_PATH] + [certificateName],[password], CryptoType.KEYSTORE);

       

      With this way you can add multiple certificates to your keystore.

      After this you can select for every teststep a different keystore.

      I use the following code therefore:

       

      public static void addCertificateToTeststep(WsdlTestRequestStep testStep, String certificateName){
      testStep.getTestRequest().setSslKeystore(certificateName);
      }

        

      regards,

       

      CrazyFrog :)