HARSH JAIN
HARSH JAIN

Reputation: 31

SOAPUI: unable to resolve class TokenType while Automating Token Retrieval

I am trying to perform automatic OAuth retrieval given at link https://support.smartbear.com/readyapi/docs/projects/requests/auth/types/oauth2/automate/sample.html using the following Groovy code:

// Import the required classes
import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade;
import com.eviware.soapui.support.editor.inspectors.auth.TokenType;
import com.eviware.soapui.model.support.ModelSupport;

// Set up variables
def project = ModelSupport.getModelItemProject(context.getModelItem());
def authProfile = project.getAuthRepository().getEntry("OAuth 2");
def oldToken = authProfile.getAccessToken();
def tokenType = TokenType.ACCESS;

// Create a facade object
def oAuthFacade = new OltuOAuth2ClientFacade(tokenType);

// Request an access token in headless mode
oAuthFacade.requestAccessToken(authProfile, true, true);

// Wait until the access token gets updated
while(oldToken == authProfile.getAccessToken()) {
}
//The sleep method can be used instead of a while loop
//sleep(3000);

// Post the info to the log
log.info("Set new token: " + authProfile.getAccessToken());

But I am getting following error that it is unable to resolve class TokenType:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script15.groovy: 5: unable to resolve class com.eviware.soapui.support.editor.inspectors.auth.TokenType @ line 5, column 1. import com.eviware.soapui.support.editor.inspectors.auth.TokenType; ^ org.codehaus.groovy.syntax.SyntaxException: unable to resolve class com.eviware.soapui.support.editor.inspectors.auth.TokenType @ line 5, column 1. at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:149) at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1225) at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:178) at org.codehaus.groovy.control.CompilationUnit$11.call(CompilationUnit.java:651) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:694) at groovy.lang.GroovyShell.parse(GroovyShell.java:706) at groovy.lang.GroovyShell.parse(GroovyShell.java:742) at groovy.lang.GroovyShell.parse(GroovyShell.java:733) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:136) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:87) at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:141) at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:250) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) 1 error

If I do not import TokenType, then I am getting the error that No Such Property TokenType. How can I import TokenType class OR is there any other way I can request a new Access Token without using TokenType?

Upvotes: 0

Views: 954

Answers (1)

HARSH JAIN
HARSH JAIN

Reputation: 31

I was able to automate the OAuth token retrieval by removing TokenType from the code. OltuOAuth2ClientFacade can be instantiated without specifying TokenType. Following is the updated code:

// Import the required classes
import com.eviware.soapui.impl.rest.actions.oauth.*;
import com.eviware.soapui.model.support.ModelSupport;

//// Set up variables

def authContainer = testRunner.testCase.testSuite.project.OAuth2ProfileContainer
def authProfile = authContainer.getProfileByName("Profile 1")
def oldToken = authProfile.getAccessToken();

// Create a facade object
def oAuthFacade = new OltuOAuth2ClientFacade();

// Request an access token in headless mode
oAuthFacade.requestAccessToken(authProfile);

// Wait until the access token gets updated
while(oldToken == authProfile.getAccessToken()) {
}

// Post the info to the log
log.info("Set new token: " + authProfile.getAccessToken());

Upvotes: 3

Related Questions