No signature of method: is applicable for argument types error in Groovy - 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) {
}
}

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

groovy.lang.MissingMethodException: No signature of method exception when using Groovy default Builder annotation

I have the following enum, trait and a class.
enum FileFormat {
V2, V3
}
trait FileSet {
int fileSetId
List<DataFile> srcFiles = Collections.emptyList()
boolean header = false
boolean mixedPack = false
FileFormat format
List<String> getSrcFileNames() {
srcFiles.collect { it -> it.getSrcFileName() }
}
int getFileCount() {
srcFiles.size()
}
abstract boolean isValid()
def addFile(HeaderFileType hdrType) {
def f = DataFile()
}
}
#Builder(builderMethodName = "builder", buildMethodName = "build", prefix = "with", excludes = "srcFileNames, valid, fileCount")
class VolumeFileSet implements FileSet {
#Override
boolean isValid() {
//TODO implement based on VolumeFileSet validation rules
return true
}
}
When I try to use builder to set the format enum, I am getting the error
groovy.lang.MissingMethodException: No signature of method: static com.tccc.bia.testdrive.nsr.VolumeFileSet.witFormat() is applicable for argument types: (com.tccc.bia.testdrive.nsr.FileFormat) values: [V3]
Possible solutions: setFormat(com.tccc.bia.testdrive.nsr.FileFormat), getFormat()
Here is the test
class TestSpec extends Specification {
def setupSpec() {
def volumeFileSet = VolumeFileSet
.builder()
.withHeader(true)
.withMixedPack(true)
.witFormat(FileFormat.V3) //ERROR here
.build()
}
}
You misspelled the method name.
It should be withFormat(FileFormat.V3), not witFormat.
When corrected, the code compiles and runs just fine.

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

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

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"
}
}

groovy generic fluent builder

I'd like to create a simple wrapper, which would allow calling objects methods as a fluent interface. I've been thinking about rewriting methods of a class upon creation, but this doesn't seem to work. Is this possible in some way with groovy metaprograming?
I have this kind of code snippet so far:
class FluentWrapper {
def delegate
FluentWrapper(wrapped) {
delegate = wrapped
delegate.class.getMethods().each { method ->
def name = method.getName()
FluentWrapper.metaClass."$name" = { Object[] varArgs ->
method.invoke(wrapped, name, varArgs)
return this
}
}
}
def methodMissing(String name, args) {
def method = delegate.getClass().getDeclaredMethods().find { it.match(name) }
if(method) {
method.invoke(delegate,name, args)
return FluentWrapper(delegate)
}
else throw new MissingMethodException(name, delegate, args)
}
}
Assuming example Java class:
class Person {
void setAge()
void setName()
}
I'd like to be able to execute the following piece of code:
def wrappedPerson = new FluentWrapper(new Person())
wrappedPerson.setAge().setName()
I'm using Groovy 1.6.7 for this.
This is all Groovy, and I'm using 1.8.6 (the current latest), but given this Person Class:
class Person {
int age
String name
public void setAge( int age ) { this.age = age }
public void setName( String name ) { this.name = name }
public String toString() { "$name $age" }
}
And this FluentWrapper class:
class FluentWrapper {
def delegate
FluentWrapper(wrapped) {
delegate = wrapped
}
def methodMissing(String name, args) {
def method = delegate.getClass().declaredMethods.find { it.name == name }
if(method) {
method.invoke( delegate, args )
return this
}
else throw new MissingMethodException(name, delegate, args)
}
}
Then, you should be able to do:
def wrappedPerson = new FluentWrapper(new Person())
Person person = wrappedPerson.setAge( 85 ).setName( 'tim' ).delegate
And person should have the age and name specified
I find #tim_yates' answer nice, but you couldn't access delegate methods' return values (something one usually likes doing, even for Builders in the case of build() :)
Moreover, if this wasn't intended for a Builder but for an object with a chainable interface (like that of jQuery wrapped objects in JS), it would be a serious issue.
So I'd put the wrapper like this:
class FluentWrapper {
def delegate
FluentWrapper(wrapped) {
delegate = wrapped
}
def methodMissing(String name, args) {
def method = delegate.getClass().declaredMethods.find { it.name == name }
if(method) {
def result = method.invoke(delegate, args)
return result != null ? result : this
}
else throw new MissingMethodException(name, delegate, args)
}
}
Note the elvis operator is unsuitable since a falsy value would never get returned.
Of course, it's up to the invoker to know wether a method is chainable or not, but that could be overcome with method annotations if neccesary.

Resources