Typecasting in Groovy - groovy

I am trying to parse an yaml file in Groovy. However I am facing issue while typecasting the result to Map object.
Here is my logic
import org.yaml.snakeyaml.Yaml
import java.util.Map
Reader reader = null
int tokenCount = 0
def Map map = null
StringTokenizer st = new java.util.StringTokenizer("Country.State.City", ".")
reader = new FileReader("filepath")
String val = null
Yaml yaml = new Yaml()
map = (Map) yaml.load(reader)
tokenCount = st.countTokens()
for (i=1; i < tokenCount; i++) {
String token = st.nextToken()
map = (Map) map.get(token)
}
val = map.get(st.nextToken()).toString()
However I am getting error at line:
map = (Map) map.get(token)
where it says:
"org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'some value' with class 'java.lang.String' to class 'java.util.Map' error at line: 15"..
Where I am going wrong?

your provided yaml file is syntactically incorrect. This is a fixed version:
location: C:\\Users\\amah11\\Desktop\\New folder
type: hi
Header:
Code:
Start: 0
End: 2
value: H00
Owner:
Start: 3
End: 5
value: AIM
User:
Start: 6
End: 8
Value: AIM
number: 1
Note that Code: **Static** in the original messes things up. And all the keys on the final level need a space after the : (e.g. Start:3 is wrong).
The actual error message is:
Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Static Start:0 End:2 value:H00' with class 'java.lang.String' to class 'java.util.Map'
which is rather clear in showing, that there is something wrong with the original file.
You might want to consider using an editor, that detects errors like this right away.
An alternative to the original code, would be the use of inject on the tokenizer:
def map = new Yaml().load(new FileReader("cardconfig.yml"))
println new StringTokenizer('Header.Code.End', '.').inject(map) { r, s -> r.get(s) }
BTW: you don't need to import java.util in groovy.

Related

Nim: Can't access fields of object returned by a procedure

I was trying to write a lexer in Nim. Sorry, if this sounds a bit idiotic because I started using nim yesterday, so my problem is that I created a type such as what follows
import position
type
Error* = object
pos_start : position.Position
pos_end : position.Position
name: string
details: string
then I went on to create a procedure that returns an instance of this type,
proc IllegalCharacterError*(pos_start : position.Position, pos_end : position.Position, details : string) : Error =
return Error(pos_start: pos_start, pos_end: pos_end, name: "IllegalCharacterError", details: details)
Now, everything works fine except when I, from another module, try to access fields of this returned instance I get error
from errors import nil
from position import nil
var current_char = "2"
let pos = position.Position(idx: -1, ln: 0, col: -1, fn: fn, ftxt: text)
let error = errors.IllegalCharacterError(pos, pos, current_char)
echo error.name
The last line is the one that throws the error and following is the error that appeared during compilation
Error: undeclared field: 'name' for type errors.Error [declared in C:\Users\Mlogix\Desktop\testNim\errors.nim(4, 3)]
Thanks, any help would be really appreciated.
Ok, finally after an hour I realized that my fields weren't public. For anyone from the future, I changed my type code to
import position
type
Error* = object
pos_start* : position.Position
pos_end* : position.Position
name*: string
details*: string
and it worked. Hooray.

ODI 12c Groovy - Automatically generate scenarios of a mapping with two physical layers

I want to create a groovy script that will generate two scenarios of a mapping that have two physical layers.
I have the code below, but it seems that it's not correct. I try to pass as value for "generateSecnario" method, the physical layer. Don't know if it's ok.
import oracle.odi.domain.project.finder.IOdiProjectFinder;
import oracle.odi.domain.model.finder.IOdiDataStoreFinder;
import oracle.odi.domain.project.finder.IOdiFolderFinder;
import oracle.odi.domain.mapping.finder.IMappingFinder;
import oracle.odi.domain.model.OdiDataStore;
import oracle.odi.domain.model.OdiModel;
import oracle.odi.domain.model.finder.IOdiModelFinder;
import oracle.odi.core.persistence.transaction.support.DefaultTransactionDefinition;
import oracle.odi.generation.support.OdiScenarioGeneratorImpl;
import oracle.odi.generation.IOdiScenarioGenerator;
import oracle.odi.domain.runtime.scenario.OdiScenario;
import oracle.odi.domain.mapping.Mapping;
import oracle.odi.domain.mapping.finder.IMappingFinder;
import oracle.odi.domain.runtime.scenario.finder.IOdiScenarioFinder;
import oracle.odi.domain.project.OdiProject;
txnDef = new DefaultTransactionDefinition()
tm = odiInstance.getTransactionManager()
tme = odiInstance.getTransactionalEntityManager()
txnStatus = tm.getTransaction(txnDef)
def fm = ((IMappingFinder) tme.getFinder(Mapping.class)) // shorcut to Find Mapping
def mappingList = fm.findAll().findAll {w-> w.getProject().getCode() == 'DL_GENERATE_MAPPINGS'
}
if (mappingList == null) {
println "Map is null"
}
ms = mappingList.iterator()
while (ms.hasNext()) {
ms_i = ms.next()
println ms_i.getName()
scenName = ms_i.getName();
stxnDef = new DefaultTransactionDefinition()
stm = odiInstance.getTransactionManager()
stme = odiInstance.getTransactionalEntityManager()
stxnStatus = stm.getTransaction(stxnDef)
OdiScenario sc = ((IOdiScenarioFinder) stme.getFinder(OdiScenario.class)).findLatestByName(scenName)
if (sc != null) {
println "Scenario already exist"
println sc
}
println("test");
odiInstance.getTransactionalEntityManager().persist(ms_i);
PhysicalDesignList = ms_i.getExistingPhysicalDesigns();
println("ceva" + PhysicalDesignList);
for (pd in PhysicalDesignList) {
if (pd.getName() == "DailyLayer") {
println("test1");
IOdiScenarioGenerator gene = new OdiScenarioGeneratorImpl(odiInstance);
OdiScenario newScen = gene.generateScenario(ms_i, scenName, "100");
} else if (pd.getName() == "CorrectionLayer") {
println("test2");
IOdiScenarioGenerator gene = new OdiScenarioGeneratorImpl(odiInstance);
OdiScenario newScen = gene.generateScenario(ms_i, scenName, "200");
}
}
println newScen
//tme.persist(newScen)
stm.commit(stxnStatus)
println "Created"
//odiInstance.close()
}
tm.commit(txnStatus)
Do you know how to do this ?
Thank you,
UPDATE 1
If I change "ms_i" in generateScenario method with "pd" (generating scenario for each physical layer instead of each mapping), I god this error:
Hi, I forgot to mention that I already replaced with pd and tried. When I run this first, I got this error:
No such property: newScen for class: Generate_scenarios_v1 (Subtract
18 from the error line number to account for the standard imports)
groovy.lang.MissingPropertyException: No such property: newScen for
class: Generate_scenarios_v1 at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at
org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
at Generate_scenarios_v1.run(Generate_scenarios_v1.groovy:80) at
groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518) at
groovy.lang.GroovyShell.run(GroovyShell.java:497) at
groovy.lang.GroovyShell.run(GroovyShell.java:170) at
oracle.di.studio.groovy.GroovyScriptRunInstance.run(GroovyScriptRunInstance.java:222)
After this, I i'll try to run it again, it goes into loop or something like that .. it doesn't do anything, like it's blocked by something. Maybe I need to close some connection and I don't do it ...
The first parameter of the generateScenario method you are invoking is of type IOdiScenarioSource. One of the implementation of this interface is MapPhysicalDesign, so you could pass that instead of your mapping.
OdiScenario newScen = gene.generateScenario(pd, scenName, "100");
I see you are using the same name for the two scenarii with a different version number. This might lead to some confusion in the long run, especially because executing version -1 of a scenario will take the latest version (so the correction in your case). I would recommend to use 2 different names (e.g. scenName+'_DAILY' and scenName+'_CORR')

How to evaluate a String which one like a classname.methodname in SoapUI with Groovy?

I have groovy code as below:
def randomInt = RandomUtil.getRandomInt(1,200);
log.info randomInt
def chars = (("1".."9") + ("A".."Z") + ("a".."z")).join()
def randomString = RandomUtil.getRandomString(chars, randomInt) //works well with this code
log.info randomString
evaluate("log.info new Date()")
evaluate('RandomUtil.getRandomString(chars, randomInt)') //got error with this code
I want to evaluate a String which one like a {classname}.{methodname} in SoapUI with Groovy, just like above, but got error here, how to handle this and make it works well as I expect?
I have tried as blew:
evaluate('RandomUtil.getRandomString(chars, randomInt)') //got error with this code
Error As below:
Thu May 23 22:26:30 CST 2019:ERROR:An error occurred [No such property: getRandomString(chars, randomInt) for class: com.hypers.test.apitest.util.RandomUtil], see error log for details
The following code:
log = [info: { println(it) }]
class RandomUtil {
static def random = new Random()
static int getRandomInt(int from, int to) {
from + random.nextInt(to - from)
}
static String getRandomString(alphabet, len) {
def s = alphabet.size()
(1..len).collect { alphabet[random.nextInt(s)] }.join()
}
}
randomInt = RandomUtil.getRandomInt(1, 200)
log.info randomInt
chars = ('a'..'z') + ('A'..'Z') + ('0'..'9')
def randomString = RandomUtil.getRandomString(chars, 10) //works well with this code
log.info randomString
evaluate("log.info new Date()")
evaluate('RandomUtil.getRandomString(chars, randomInt)') //got error with this code
emulates your code, works, and produces the following output when run:
~> groovy solution.groovy
70
DDSQi27PYG
Thu May 23 20:51:58 CEST 2019
~>
I made up the RandomUtil class as you did not include the code for it.
I think the reason you are seeing the error you are seeing is that you define your variables char and randomInt using:
def chars = ...
and
def randomInt = ...
this puts the variables in local script scope. Please see this stackoverflow answer for an explanation with links to documentation of different ways of putting things in the script global scope and an explanation of how this works.
Essentially your groovy script code is implicitly an instance of the groovy Script class which in turn has an implicit Binding instance associated with it. When you write def x = ..., your variable is locally scoped, when you write x = ... or binding.x = ... the variable is defined in the script binding.
The evaluate method uses the same binding as the implicit script object. So the reason my example above works is that I omitted the def and just typed chars = and randomInt = which puts the variables in the script binding thus making them available for the code in the evaluate expression.
Though I have to say that even with all that, the phrasing No such property: getRandomString(chars, randomInt) seems really strange to me...I would have expected No such method or No such property: chars etc.
Sharing the code for for RandomUtil might help here.

Error while doing Data-Driven Testing in JMeter using data in Excel

I am attempting to read xmls stored in one column of an Excel spreadsheet and fire them to a server using HTTP Sampler and then store the response xml in the same Excel.
This is the structure of my test plan in JMeter:
However I have encountered and error.
I am not able to pinpoint the exact place where the error is taking place but I have obtained the error message from the Results Tree are as follows:
for the JSR223 Sampler
Response message: javax.script.ScriptException: Sourced file: inline
evaluation of: import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.us . . . '' : Typed variable declaration :
Attempt to resolve method: parseInt() on undefined variable or class
name: INTEGER : at Line: 6 : in file: inline evaluation of:import
org.apache.poi.xssf.usermodel.XSSFWorkbook; import
org.apache.poi.xssf.us . . . '' : INTEGER .parseInt ( vars .get (
"counter" ) ) in inline evaluation of: ``import
org.apache.poi.xssf.usermodel.XSSFWorkbook; import
org.apache.poi.xssf.us . . . '' at line number 6
The error in the HTTP Request Sampler's Response Data tab reads as:
Exception occured: Parsing xml error, xml string is:${RQI}
BeanShell Assertion error is :
Assertion error: true Assertion failure: false Assertion failure
message: org.apache.jorphan.util.JMeterException: Error invoking bsh
method: eval In file: inline evaluation of: ``import
org.apache.poi.xssf.usermodel.XSSFWorkbook; import
org.apache.poi.xssf.us . . . '' Encountered ":" at line 6, column 65.
This is the code that I had used in the JSSR223 Sampler in the While Controller:
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import java.io.*;
int i = INTEGER.parseInt(vars.get("counter"));
XSSFRow row = vars.getObject("book").getSheetAt(0).getRow(i);
vars.putObject("row", row);
for (int j = 1; j <= vars.getObject("book").getSheetAt(0).getRow(0).getLastCellNum(); j++) {
if (row.getCell(j) == null) {
row.createCell(j).setCellValue("");
}
}
String payload = row.getCell(1).toString();
vars.put("RQI",payload);
//String password = row.getCell(2).toString();
// vars.put("password",password);
//String expectedResult = row.getCell(5).toString();
// vars.put("expectedResult",expectedResult);
Please assist. Also, feel free to ask for more information as I have left out the code for the other JSR223 Samplers in this post for brevity. Thank you in advance.
You must change this line:
INTEGER.parseInt(vars.get("counter"));
to this one
Integer.parseInt(vars.get("counter"));
check out Integer class JavaDoc - Integer.parseInt()
You should be using JSR223 Test Elements instead of Beanshell test elements and you should be using Groovy language in the JSR223 Test Elements
You might find How to Implement Data Driven Testing in your JMeter Test reference useful.

How to use Groovy script in soapUi to loop multiple time

I am new to SoapUi. I am exploring on how multiple request in soapUi is done using groovy script.
below is the example that im trying to do, based on example that i found through "googling"
import com.eviware.soapui.SoapUI;
import com.eviware.soapui.model.testsuite.*;
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
import java.util.Random
import com.eviware.soapui.model.testsuite.TestRunner.Status
// Define your testCase pointer
//def testcase = testRunner.testCase.testSuite.project.testSuites["TestSuite - User Management REST API"].getTestCaseByName ("Authenticate User")
def counterUser = testRunner.testCase.testSuite.getPropertyValue( "counter" )
int value = counterUser.toInteger()
String tester = ""
30.times {
value = value + 1
tester = "tester " + value.toString()
testRunner.testCase.testSuite.setPropertyValue( "userName", tester )
testRunner.runTestStepByName("POST - createUser - Create a User")
}
testRunner.testCase.testSuite.setPropertyValue( "counter", value.toString() )
I want to create a 30 users which start from Tester1...tester2.....tester30.
Is it possible to do this way? I keep getting an error such as NullPointerException at this line
int value = counterUser.toInteger()
I got what you say.
That is because, initially there is no value for counter which results to null and you are applying toInteger() over it.
Just change:
From:
int value = counterUser.toInteger()
To:
int value = counterUser?.toInteger() ?: 0

Resources