Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
royteeuwen opened this issue Jul 16, 2020 · 10 comments
Closed

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

royteeuwen opened this issue Jul 16, 2020 · 10 comments

Comments

@royteeuwen
Copy link
Contributor

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
Copy link
Collaborator

pun-ky commented Jul 16, 2020

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

@royteeuwen
Copy link
Contributor Author

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

@pun-ky
Copy link
Collaborator

pun-ky commented Jul 18, 2020

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

@royteeuwen
Copy link
Contributor Author

royteeuwen commented Jul 22, 2020

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
Copy link
Collaborator

pun-ky commented Jul 23, 2020

FYI I already have requested these problems somehow on Moco side, but I am afraid no simplifications are done till now.
See dreamhead/moco#278 (comment)

@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
Copy link
Contributor Author

I have started with a branch ;)

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
Copy link
Collaborator

pun-ky commented Jul 23, 2020

Cool 👍

@royteeuwen
Copy link
Contributor Author

#36 added an initial implementation that works

@pun-ky
Copy link
Collaborator

pun-ky commented Aug 1, 2020

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
Copy link
Collaborator

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

marcinkp pushed a commit that referenced this issue Nov 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants