Forum Discussion

ashutosh's avatar
ashutosh
Occasional Contributor
5 years ago
Solved

Set SSL Keystore using Groovy on Test Step level

On the Project Level i have setup the Keystore.

 

Now i want to be able to Select this on Test Step Level using Groovy.

 

In the Step : Select SSL on Test Step i wrote a groovy to do the same but it is failing.

 

import com.eviware.soapui.impl.rest.RestMethod
import com.eviware.soapui.impl.rest.RestRequestInterface.HttpMethod
import com.eviware.soapui.impl.rest.RestResource
import com.eviware.soapui.impl.wsdl.support.wss.WssCrypto
import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
import com.eviware.soapui.impl.wsdl.support.wss.crypto.CryptoType

 

def step = testRunner.testCase.testSteps['GET - TEST'].testRequest

log.info step.getSslKeystore() // This step works fine and reads the Keystore Selected on the Test Step (if any)


step.setSslKeystore( 'keystore.jks') // This step does not work and gives error message

 

 

Error : 


groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.teststeps.RestTestRequest.setSslKeystore() is applicable for argument types: (java.lang.String) values: [keystore.jks] Possible solutions: setSslKeystore(com.eviware.soapui.impl.wsdl.support.wss.WssCrypto), getSslKeystore() error at line: 13

 

 

 

 

 

Please note that my tests require for me to be able to select & deselect the SSL Keystore for a test step dynamically (through groovy) based on the environment i am running my tests on .. (for ex : QA , UAT etc)

 

  • Hi ashutosh,

     

    Looks like the below code line works for unsetting the keystore. You can give it a try.

    testRunner.testCase.testSteps['Step Name'].testRequest.setSslKeystore(new EmptyWssCrypto())

     

8 Replies

  • SiKing's avatar
    SiKing
    Community Expert

    Please note that my tests require for me to be able to select & deselect the SSL Keystore for a test step dynamically (through groovy) based on the environment i am running my tests on .. (for ex : QA , UAT etc)

    If you use environments, then just define the keystore in a custom property.

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Great hint SiKing! Thank you.

      ashutosh were you able to solve the issue? Please share your progress :smileyhappy:

      • ashutosh's avatar
        ashutosh
        Occasional Contributor

        Hi All ,

         

        The original issue was that we have to test our API's for Swagger Endpoints and Apigee Endpoints 

        Swagger uses TLS

        Apigee uses Ping Fed tokens.

         

        Since i wanted to  write the tests only 1 time and  then reuse them for multiple environments (like Swagger QA , Swagger UAT , Apigee QA, Apigee UAT)  I wanted a capability to Programmatically (through Groovy) Setup the TLS and Assign it to  test Steps.

         

        So I was able to write the Script as i mentioned above.

        So now if my tests are running against Swagger URL , i am able to set the TLS Certificate (through groovy)

        However , when i want to run the same test for Apigee URLs (which only accept Pink Fed tokens and fails if TLS is supplied) my tests fail since i dont know how to Programatically Delete the TLS Setup.

         

         

        //SSL is only used for Swagger tests. Before running this test , make sure the jks certificate is saved on Local on Location : {ProjectDir}\CompositeFolderName\ZGV0Dependencies
        //Also make sure to define 3 Project Propertiesunder Default Environment as : a) KeystoreFolder b) KeystorePassword c)KeystoreFileName

        import com.eviware.soapui.impl.wsdl.support.wss.crypto.CryptoType
        import com.eviware.soapui.impl.rest.RestMethod
        import com.eviware.soapui.impl.rest.RestRequestInterface.HttpMethod
        import com.eviware.soapui.impl.rest.RestResource
        import com.eviware.soapui.impl.wsdl.support.wss.WssCrypto
        import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
        import com.eviware.soapui.impl.wsdl.support.wss.crypto.CryptoType

        def JenkinsEnv = testRunner.getTestCase().getTestSuite().getProject().getActiveEnvironmentName()
        log.info 'The Test Environment on which the Script is executing is : ' +JenkinsEnv

        def TestEnv

        if (JenkinsEnv.contains('Apigee'))
        TestEnv = "Apigee Env"
        else if (JenkinsEnv.contains('Default'))
        TestEnv = "Default Env"
        else if (!JenkinsEnv.contains('Apigee'))
        TestEnv = "Swagger Env"

        log.info 'Alias name of this env is : ' +TestEnv

        if (TestEnv.contains('Swagger'))
        {
        def GetCertificateList = testRunner.testCase.testSuite.project.wssContainer.getCryptoList()

        if (GetCertificateList.isEmpty()) //if no key store is defined on this project, create a keystore
        {
        log.info "No Keystore is Defined for this Project. Creating a Keystore First"

        //Flip the Environment to Default to get the Values below
        context.getTestCase().getTestSuite().getProject().setActiveEnvironment('Default environment')
        log.info 'Flipping the Env to : ' +testRunner.getTestCase().getTestSuite().getProject().getActiveEnvironmentName() + " to fetch some Custom Project Properties"

        //Get the Values of Certificate location and Certificate Password ( these values are Setup for Default environment)


        def RelativePath = testRunner.testCase.testSuite.project.getPath()
        def KeystoreFolder = context.expand( '${#Project#KeystoreFolder}' )
        def KeystoreFileName = context.expand( '${#Project#KeystoreFileName}' )
        def KeystorePassword= context.expand( '${#Project#KeystorePassword}' )
        def KeystorePath = RelativePath+"\\"+KeystoreFolder+"\\"+KeystoreFileName
        log.info "Keystore is Stored on Location " + KeystorePath

        //Re-Flip the Env back  to jenkins Env ( Jenkins sends a Trigger to run the Tests against diff env like Swagger QA or Apigee QA)


        context.getTestCase().getTestSuite().getProject().setActiveEnvironment(JenkinsEnv)
        log.info 'The Environment is flipped Back to : ' + testRunner.getTestCase().getTestSuite().getProject().getActiveEnvironmentName()

        //Set the TLS
        testRunner.testCase.testSuite.project.wssContainer.addCrypto(KeystorePath,KeystorePassword,CryptoType.KEYSTORE) //path, password, cryptoType
        }
        else
        {
        log.info "Certificate is already defined : " +GetCertificateList
        }

        GetCertificateList = testRunner.testCase.testSuite.project.wssContainer.getCryptoList()
        def step = testRunner.testCase.testSteps['GET - TEST'].testRequest
        step.setSslKeystore(GetCertificateList[0])
        log.info "Selecting the Keystore for the test before running the API as : " +GetCertificateList[0]
        }
        else
        {

        log.info "Since the Test is running on Apigee , no need to setup TLS"

        // I need a Script here to Delete the TLS Certificate since if i dont do this , my Test Fails as Apigee does not expect TLS cert when i am hitting their end point

        }

         

         

        I am almost there but i am just missing the last piece of the puzzle.

        Hope i can get some help

         

    • ashutosh's avatar
      ashutosh
      Occasional Contributor

      Hi , 

       

      Quick Update. 

      I got the solution after i put in 3 hours into this thing.

      Here it goes for the community.

       

       

      import com.eviware.soapui.impl.wsdl.support.wss.crypto.CryptoType
      import com.eviware.soapui.impl.rest.RestMethod
      import com.eviware.soapui.impl.rest.RestRequestInterface.HttpMethod
      import com.eviware.soapui.impl.rest.RestResource
      import com.eviware.soapui.impl.wsdl.support.wss.WssCrypto
      import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
      import com.eviware.soapui.impl.wsdl.support.wss.crypto.CryptoType

      def myList = testRunner.testCase.testSuite.project.wssContainer.getCryptoList()

      if (myList.isEmpty()) //if no key store is defined on this project, create a keystore
      {
      log.info "No Keystore is Defined for this Project. Creating a Keystore First"
      testRunner.testCase.testSuite.project.wssContainer.addCrypto(context.expand( '${#Project#KeystorePath}' ),context.expand( '${#Project#KeystorePassword}' ),CryptoType.KEYSTORE) //path, password, cryptoType
      }

      //Once the Keystore is Created , Apply it to whichever tests you want to apply it to
      myList = testRunner.testCase.testSuite.project.wssContainer.getCryptoList()
      log.info myList

      def step = testRunner.testCase.testSteps['GET - TEST'].testRequest
      step.setSslKeystore(myList[0])