wttech / aem-stubs

Tool for providing sample data for AEM applications in a simple and flexible way. Stubbing server on AEM, no separate needed.
Apache License 2.0
41 stars 4 forks source link

Can't seem to use the "req" object in moco groovy scripts #34

Closed royteeuwen closed 3 years ago

royteeuwen commented 4 years ago

I'm trying to use an example of the api docs of moco:

stubs.server.request(by(uri("/template"))).response(template("${req.version}"));

But I get following response:

16.07.2020 14:54:51.178 *ERROR* [sling-oak-observation-14] com.cognifide.aem.stubs.core.ConfigurableStubManager Cannot execute AEM Stubs script at path '/conf/stubs/moco/websites/example/get.stub.groovy'!
groovy.lang.MissingPropertyException: No such property: req for class: Script1

What I actually want to achieve is the following:

stubs.server
    .get(by(uri("/some-api")))
.response(repository.readText(template("./file_${req.queries['id']}.json")))
pun-ky commented 4 years ago

To be clarified. Thanks for reporting. I am on vacation now so investigation will take a little bit more time

royteeuwen commented 4 years ago

No worries! There is a workaround by making a rule per id hardcoded, but it would be cleaner if that wasnt needed !

pun-ky commented 4 years ago

If you know how to fix it don't hesitate to create a PR 😁

royteeuwen commented 4 years ago

I had a look, and it is harder than I thought it would be. I think we would have to make a new ContentResourceReader in moco, like the following ones:

https://github.com/dreamhead/moco/blob/master/moco-core/src/main/java/com/github/dreamhead/moco/resource/reader/AbstractFileResourceReader.java

https://github.com/dreamhead/moco/blob/master/moco-core/src/main/java/com/github/dreamhead/moco/resource/reader/FileResourceReader.java

We could make a JcrResourceReader, that can then be used in the template resource reader to find the correct location in jcr, instead of having the repository.readText, which can't be used in the template logic.

You could then do

 response(jcr(template("my_file_${req.queries['id']}.json"))

After further investigation I also found that the req object is available, but because we are using groovy, if you use $ it is already interpreted by groovy, so you would have to type

response("\${req.version}")
pun-ky commented 4 years ago

FYI I already have requested these problems somehow on Moco side, but I am afraid no simplifications are done till now. See https://github.com/dreamhead/moco/issues/278#issuecomment-623253746

@royteeuwen about '$' ... or simply wrap your string into ' not " :)

you wrote:

stubs.server.request(by(uri("/template"))).response(template("${req.version}"));

consider:

stubs.server.request(by(uri('/template'))).response(template('${req.version}'));

about file content readers... if you have sth working, I mean code changes, don't hesitate to share it via PR. Even if it will be not completed, could be a base for further changes, when I will have some time I could complete them or sb else

royteeuwen commented 4 years ago

I have started with a branch ;)

https://github.com/Cognifide/aem-stubs/compare/master...royteeuwen:feature/add-jcr-resource-reader

It already comes into the correct classes, you can do response(jcr('/content/my-file.json')) but still have work to of course read the actual resource :D

pun-ky commented 4 years ago

Cool 👍

royteeuwen commented 4 years ago

36 added an initial implementation that works

pun-ky commented 4 years ago

I had a look, and it is harder

Yep. I was trying sth similar to what you did. The moco API looks nice but actually it is tricky and not straightforward.

marcinkp commented 3 years ago

Thanks @royteeuwen, I am closing as it is merged to master. I added integration test for this functionality