How can load a request in a method of groovy bean? - groovy

I want to create a groovy's bean which takes the parameters of a request and return an appropriate response when the sum of parameter is positive. I want to know how I can load my request in a method of groovy bean
import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.support.GroovyUtils
import com.eviware.soapui.model.mock.*
import org.apache.commons.lang.StringUtils
import groovy.util.*
import groovy.lang.*
public class Test
{
public int somme ()
{
def holder = new XmlHolder(mockRequest.getRequestContent())
def a = holder["//firstInt:a"]
def b = holder["//secondInt:b"]
return(a + b)
}
public String getResponse ()
{
Test t = new Test ()
if (t.somme() > 0)
{
return "response1"
}
else
{
return "response2"
}
}
}
When I run this code I have error below:
com.eviware.soapui.impl.wsdl.mock.DispatchException: Failed to
dispatch using script; groovy.lang.MissingPropertyException: No such
property: mockRequest for class:Test

Your class is supposed to appear like this.
Note the changes:
Declared mockRequest member variable, pass it as argument to constructor of the class.
Object creation is not required in getResponse() method, instead you may do it in another script as you desired. Like in Mock response's script where mockRequest is available.
import com.eviware.soapui.model.mock.MockRequest
import com.eviware.soapui.support.XmlHolder
public class Test {
MockRequest mockRequest
public Test(MockRequest mockRequest) {
this.mockRequest=mockRequest
}
public int somme () {
def holder=new XmlHolder(mockRequest.getRequestContent())
def a=holder["//firstInt:a"]
def b=holder["//secondInt:b"]
(a + b)
}
public String getResponse () {
somme() ? "response1" : "response2"
}
}

Related

Reflect metadata design:paramtypes return an array with unfedined element

I'm trying to obtain the constructor arguments for some class but the <> get an array with a undefined element.
For the test I work with services Example2Services and ExampleService
Example2Service.ts
import { ExampleService } from './example.service';
export class Example2Service {
constructor(private es1: ExampleService) {}
getValue() {
return "some value2";
}
}
ExampleService.ts
import { Example2Service } from './example2.service';
export class ExampleService {
constructor(private es2: Example2Service) { }
getValue() {
return this.es2.getValue();
}
getString() {
return "1"
}
}
In this function I send a Example2Service
private resolve(target: Type): Object | Function {
let tokens = Reflect.getMetadata('design:paramtypes', target)
...
}
debugging the code, I get this for the token variable
trying to understand the behavior, I try to obtain the args directly from some classes
import {Example2Service} from '../../exaple2.service.ts';
import {ExampleService} from '../../exaple.service.ts';
private resolve(target: Type): Object | Function {
let tokensA = Reflect.getMetadata('design:paramtypes', Example2Service)
let tokensB = Reflect.getMetadata('design:paramtypes', ExampleService);
let tokens = Reflect.getMetadata('design:paramtypes', target);
...
}
debuging this case I get this, sending a Example2Service to resolve function. Now tokens is an array with an Class inside. Note that when importing the Example2Service and ExampleService this happened, but when changing the import order the behavior changes
EDIT
I found out that this occurs when there is circularity between dependencies

How do you read existing method #annotations in groovy?

I'm trying to get the methods annotated with #XmlElement in the Top myxml but it the `myxml.methods[1]
import javax.xml.bind.annotation.*
import groovy.transform.*
import javax.xml.bind.*
#XmlRootElement
#XmlAccessorType(XmlAccessType.NONE)
#TupleConstructor
class Top {
String myBackingField
#XmlElement
public String getChildElement() {
return myBackingField
}
}
def myxml = new Top("child_one")
So far I got:
def mymethod = parsed.metaClass.methods.find { it.name == 'getChildElement' }
But the CachedMethod API doesn't give access to annotations.
From CachedMethod you can get to the java.lang.reflect.Method using CachedMethod.getCachedMethod() and from there you can use the Method.getAnnotations() or Method.isAnnotationPresent():
def mymethod = myxml.metaClass.methods.find { it.name == 'getChildElement' }
// mymethod.class is org.codehaus.groovy.reflection.CachedMethod
mymethod.cachedMethod.annotations // [#XmlElement(name=##default, namespace=##default,
// type=class XmlElement$DEFAULT, defaultValue=,
// required=false, nillable=false)]
To get only the methods with that annotation you can
def mymethods = myxml.metaClass.methods.findAll {
it.cachedMethod.isAnnotationPresent(XmlElement)
}
mymethods[0].class // CachedMethod backed by Method with XmlElement annotation

No signature of method: is applicable for argument types error in Groovy

I am quite new to groovy and getting following error when running the below method. I am trying to pass xml file name and Map
RD.groovy
Given(~'^input currency "([^"]*)"$') { String baseCurr ->
fromCurr = baseCurr
}
When(~'^insert end Currency "([^"]*)"$') { String tragetCurr ->
toCurr = tragetCurr
}
Then(~'^get the expected end currency value "([^"]*)"$') { String result ->
assert result == currCon(fromCurr, toCurr)
}
private currCon(fromCurr, toCurr)
{
def binding = ["fromCurr": fromCurr, "toCurr": toCurr]
response = Consumer.currConvert("request/CurrencyConvert.xml",binding) --> This is line 119
assert 200 == response.status
return response.data.ConversionRateResult.toString()
}
ClassA.groovy
package abc.api.member
import abc.util.Log
import abc.util.TemplateUtil
import groovyx.net.http.ContentType
import abc.api.RestClient
class ClassA extends ClassB{
ClassA(RestClient restClient) {
super(restClient)
}
def currConvert(String xmlFilename, Map binding) {
return currencyConvertRequest(TemplateUtil.xmlFromTemplate(xmlFilename, binding))
}
def currencyConvertRequest(xmlString) {
def params = [path : 'CurrencyConvertor.asmx',
headers: globeHeaders(),
body: xmlString]
return restClient.post(params)
}
Consumer.Groovy
package abc.api.member
import geb.Browser
import org.apache.http.client.utils.URIBuilder
import abc.api.RestClient
import abc.browser.member.Admin
class Consumer {
Browser browser
String token
String userId
#Delegate
private ClassA classA
Consumer(url) {
browser = new Browser()
browser.baseUrl = baseUrl(url)
restClient = new RestClient(url)
classA = new ClassA(restClient)
}
private baseUrl(url) {
URI uri = URI.create(url)
URIBuilder builder = new URIBuilder()
URI result =builder.setHost(uri.host). //
setPath(uri.path). //
setPort(uri.port). //
setScheme(uri.scheme).
setUserInfo("Cons", "pbiCons").build()
return result.toURL().toString()
}
Error:
groovy.lang.MissingMethodException: No signature of method: abc.api.consumer.Consumer.currConvert() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl, java.util.LinkedHashMap) values: [request/globe/CurrencyConvert.xml, [fromCurr:AUD, ...]]
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at RD.currCon(RD.groovy:119)
After searching the issue it turned out its a common issue. Couldn't figure out though. Because all solutions are subjective.
Just curious where I am doing wrong
Thanks
currConvert is an instance method, but it's being called as if it was a static method.
I had a similar problem like this :
class Example {
static void main (String [] args) {
printMessage(obj)
}
def printMessage(obj) {
}
}
I was getting the same exception at printMessage(obj).
It got fixed after changing it to like this :
class Example {
static void main (String [] args) {
new Example().printMessage(obj)
}
def printMessage(obj) {
}
}

NullPointerException when calling a method on another Groovy class

I have the following two Groovy classes:
Buzz.groovy:
import widgets.Fizz
class Buzz {
def WhistleFeather
def init = { servletContext ->
WhistleFeather.load()
println "WhistleFeather loaded."
}
def destroy = { }
}
WhistleFeather.groovy:
package net.me.myapp
import widgets.Fizz
public class WhistleFeather {
def navMenu
def load() {
println "Loading!"
}
}
When execution gets to the WhistleFeather.load() method call I'm getting a NullPointerException. Can anyone see why?
WhistleFeather#load is an instance method, not a static method. Either call it with new WhistleFeather().load(), or turn it into a static method (static load() { ... }).

Unit test Groovy2.0 with Spock : setup( )

I am writing unit test using Spock for groovy-2.0 , and using gradle to run. If I write following the test pass.
import spock.lang.Specification
class MyTest extends Specification {
def "test if myMethod returns true"() {
expect:
Result == true;
where:
Result = new DSLValidator().myMethod()
}
}
myMethod() is a simple method in DSLValidator class, that simply returns true.
But if I write a setup() function and create the object in setup(), my test fails: Gradel says: FAILED: java.lang.NullPointerException: Cannot invoke method myMethod() on null object
Following is what it looks like with setup(),
import spock.lang.Specification
class MyTest extends Specification {
def obj
def setup(){
obj = new DSLValidator()
}
def "test if myMethod returns true"() {
expect:
Result == true;
where:
Result = obj.myMethod()
}
}
Can somebody help?
Here is the solution I got to the problem:
import spock.lang.Specification
class DSLValidatorTest extends Specification {
def validator
def setup() {
validator = new DSLValidator()
}
def "test if DSL is valid"() {
expect:
true == validator.isValid()
}
}
In Spock objects stored into instance fields are not shared between feature methods. Instead, every feature method gets its own object.
If you need to share an object between feature methods, declare a #Shared field.
class MyTest extends Specification {
#Shared obj = new DSLValidator()
def "test if myMethod returns true"() {
expect:
Result == true
where:
Result = obj.myMethod()
}
}
class MyTest extends Specification {
#Shared obj
def setupSpec() {
obj = new DSLValidator()
}
def "test if myMethod returns true"() {
expect:
Result == true
where:
Result = obj.myMethod()
}
}
There are 2 fixture methods for setting up the environment:
def setup() {} // run before every feature method
def setupSpec() {} // run before the first feature method
I don't understand why the second example with setupSpec() works and fails with setup() because in documentation says otherwise:
Note: The setupSpec() and cleanupSpec() methods may not reference
instance fields.

Resources