Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Virtual Service Response Scripting

Hi, I have wrote a Java code to generate a response, when tested in the Java Perspective in Parasoft tool code is executed without any errors got a valid output. When i loaded the same to the "Virtual Service" ---> Response section and tested in local Server getting below error. Could someone correct the code. Attached the code that i used. Thanks in Advance.

Error Message: com.parasoft.api.UserMethodException: argument type mismatch

Comments

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,

    The issue is that the scripted mode for the "Response" section expects a method that takes either zero arguments, or a single argument of the type com.parasoft.api.ScriptingContext

    From your code the easier option would be to change to have a public method that takes zero arguments.

    For more information on scripting take a look at this documentations
    https://docs.parasoft.com/display/SOAVIRT9103CTP310/Extensibility+and+Scripting+Basics

  • benken_parasoft
    benken_parasoft Posts: 1,306 ✭✭✭
  • Thanks William for the response, As you said i have made changes to the code and it worked. Any Idea how to add the .class file of the Java code to the parasoft remote server. As i see when deployed the .class file is not getting added to the server.

    Thanks benken for your response as well.

  • AvinashPanditi
    AvinashPanditi Posts: 32
    edited January 2020

    HI William, I have added below code to extract the URL parameters but getting below error when tested the Virtual Service. If i remove the code and tested the Virtual Service its working fine. Attached the .java file for reference.

    Code: -
    Object x = context.get(CorrelationScriptingHookConstants.URL_PARAMS);
    String y = (String) ((Context) x).get("last_name");
    Error:-

    Exception in thread "main" java.lang.NullPointerException
    at CreateXMLScriptingContext.CreateXMLScriptingContextNew(CreateXMLScriptingContext.java:20)
    at CreateXMLScriptingContext.main(CreateXMLScriptingContext.java:84)

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,
    Is this script meant to be used in the "Responder Correlation" tab? It looks like from the txt file the error is saying the context is null? I also noticed that you are casting object x as a context when it should instead be a map. The script worked when I used it after fixing the class cast.

  • Thanks will, i will try this on Monday and will let you know the results.

  • Hi William, I am executing this code in "Response" tab. Scenario is to read the URL parameters and then form the response XML. Is there a way we can communicate directly via call or chat or any other media to meet and solve this issue.

    We fixed the casting but the Code is failing when tested the Virtual Service. Could you please attach the code that you have fixed.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,
    I think the issue is that in the "Response" tab the context may not have been populated with the correlation values that hook is meant for. I am attaching an alternative example where I use a REST URL Data bank to get the URL parameter then from the script I access the data banked parameter. I used groovy because it is easier to deploy and test, it is very close to java. I copied that main part of your method to create the xml document in as is.

  • Hi William, Thanks for sharing the code, now i am able understand how to script in Groovy. Thanks for all your help. :):)

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Happy to help!

  • Hi William, I have wrote a script similar to what we have discussed to generate a argument value in the JSON structure. I have went to Response --> Input Mode ---> Form JSON-->argument1---->Value is selected to "Script" and added the Groovy script here. When i press the evaluate button and i dont see any errros. The script is returning a value when tested in Java Perspective.

    When tested the Virtual Service from local machine i dont see the argument is populated with the value. Could you please share me a simple script with the following scenario.

    1. Script should get an URLParam value from the REST URL Data bank.
    2. Based on the URLParam value i am returning a new string value.

    It will be really great if you could share an example like you shared in our above conversation.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,
    It should work exactly the same as in the response tab. Are you trying to just return a simple string value that would be the text value of a json property? Or are you trying to replace the entire property with some json? Could you share more about what value you are trying to return?

  • HI William, I found out the issue and corrected that. Thanks.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Glad to hear you were able to get it to work!