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

How to control scenario names? #16

Open
adrian-augustyn opened this issue Mar 11, 2013 · 17 comments
Open

How to control scenario names? #16

adrian-augustyn opened this issue Mar 11, 2013 · 17 comments

Comments

@adrian-augustyn
Copy link

Hi,

started using wiremock couple of months ago, really speeds up development. Thank you very much for the effort!

Now that I feel more comfortable with it, I have started using it in a bit more complicated cases ... and some of them are just not solvable without the state machine.

http://wiremock.org/stateful-behaviour.html -> this is helpful, but none of the examples states how to modify the scenario you're in ... and I just cannot get this to work.

thanks in advance for any tips!

regards,
Adrian

@tomakehurst
Copy link
Member

Hi,

Nice to hear someone other than me is using scenarios!

When you say you want to modify the scenario you're in, do you mean move it to a different state?

If so, then the willSetStateTo() method in the Java DSL will do the trick. The "newScenarioState" attribute in the JSON API does the same.

Perhaps I'm missing what you're getting at?

Cheers,
Tom

@adrian-augustyn
Copy link
Author

Hi, wow - really quick response. Thanks for that.

Let me shed more light on what I am after.

Firstly - I am using JSON to set up my mappings.

I have a facade service that is accepting a request and then calls bunch of other services fetching and updating a lot of entities.
I use wiremock to stub responses from those services.

Let's say one of those calls is defined like that :

{
"request": {
"method": "GET",
"url": "/service/A/entity"
},
"response": {
"status": 200,
"bodyFileName": "entity_success.xml",
"headers": {
"Content-Type": "application/xml"
}
}
}

This works fine. Now I wanted to add error scenarios. One error scenario per service.
I have added one extra mapping to initiate the state machine - to say - my facade will now work in this scenario :

{
"scenarioName" : "error scenario A",
"newScenarioState": "Return 404 on entity request",
"request": {
"method": "POST",
"url": "/scenario/error_404"
},
"response": {
"status": 201
}
}

NOW. the previous mapping (I thought) would change to something like this:

{
"scenarioName" : "normal",
"requiredScenarioState": "Started",
"request": {
"method": "GET",
"url": "/service/A/entity"
},
"response": {
"status": 200,
"bodyFileName": "entity_success.xml",
"headers": {
"Content-Type": "application/xml"
}
}
}

and the error case would be something like this:

{
"scenarioName" : "error scenario A",
"requiredScenarioState": "Return 404 on entity request",
"request": {
"method": "GET",
"url": "/service/A/entity"
},
"response": {
"status": 404
}
}

But that doesn't work. In fact I am now getting 404 everytime I hit the apis...

Does that make things clearer? :)

Thanks
adrian

@tomakehurst
Copy link
Member

Let me take a look at that. I have a run of meetings all afternoon unfortunately, but I'll try and take a look after that.

@adrian-augustyn
Copy link
Author

Thanks, again. Really appreciate it!
What I was thinking ... but wasn't able to find it - was a call similar to __admin/reset to check what the current scenario details are ... as a step to verify if I am setting everything correctly.

But maybe you will be able to spot some incorrect usage of the json configuration, which would be even better ;-)

@tomakehurst
Copy link
Member

Ah, I see now. You're right, I've completely forgotten to document this. To reset all the scenarios' state, you need to POST to __admin/scenarios/reset.

@adrian-augustyn
Copy link
Author

No, no. Sorry I didn't write that clear enough.
You did document this. http://wiremock.org/stateful-behaviour.html#scenarios-reset

I was just saying that I thought that maybe there was something like __admin/scenarios/details to check if I have actually correctly managed to change the status.

As in:
call to __admin/scenarios/details -> all scenarios on "started"
call to my /scenario/error_404 -> scenario updated, provided the configuration is correct
another call to __admin/scenarios/details -> returns all scenarios on 'started', but my "error scenario A" on "Return 404 on entity request"
call to the existing __admin/scenarios/reset and then all is back to 'started'.

@tomakehurst
Copy link
Member

Doh, so I did. Should read my own docs a bit more thoroughly...

There isn't currently a way to interrogate a scenario's state directly. Obviously you could do it indirectly by creating stub mappings dependent on specific states and nothing else and check against those, although I admit that's a pretty messy solution.

Another solution in the short term would be to run it with a debugger attached. The InMemoryStubMappings class contains an instance variable called scenarioMap with all the current states contained.

Longer term, I'm happy to support for this to the backlog, or accept a pull request if you fancy creating one.

@adrian-augustyn
Copy link
Author

That sounds like an interesting proposal - something I might come back to you at some point.

Would it be too much to ask you, for now, to test that basic JSON configuration of mine to work with scenarios and either post fixed version of it here or add to to documentation? If of course there is something wrong with those mappings I posted ...

cheers

@tomakehurst
Copy link
Member

Apologies for not having got to this yet. Day job not leaving me with much time at the moment.

@karkaladeepak
Copy link

Hi,

I am using Wiremock to send various success and failure responses for job requests to my application. The responses are in the form of JSONs which I have stored in 'mappings' folder.

Do we have a provision in wiremock so that I can include/exclude the files I want on scenario basis?
In the sense, if in a particular scenario, I am mocking success, I should be able to change the settings file to include the json files with success responses and exclude others. Similarly when I am mocking failure, I should be able to change the settings file.

Currently I am changing the whole folder structure for every scenario that I am running and then I need to restart the server.

Please let me know if we have a solution for this.

Thanks,
Deepak

@tomakehurst
Copy link
Member

Unfortunately swapping out whole mappings based on scenario isn't possible.

Could you give me some examples of the problem you're trying to solve? I suspect there'll be a way to make the current features work in the way you need them to.

@MaharanaPratap
Copy link

Hi,
First of all great thanks for creating this tool, I have just one word for it "awesome".

I started using it 2 weeks back, trying to use stateful behaviour.
In your documentation, you have not changed the state for the POST method, is it because if the method type is same then only state needs to be changed ?
If we use stateful behaviour, do we need to specify states for all the mappings loaded in the environment?

While is record mode, is there a way to capture stateful behaviour ? I read in the documentation that is same URL is called again, it will be ignored.

@jzk
Copy link

jzk commented Feb 23, 2016

There are lots of tools like this one, but scenario make this one unique, thanks for creating it.

@macscripter
Copy link

Hello!

First of all, THANK YOU VERY MUCH FOR THIS AMAZING SOFTWARE!

I have a problem with a stateful execution.

I copied directly from http://wiremock.org/stateful-behaviour.html

this code in the mappings dir:

`{
"scenarioName": "To do list",
"requiredScenarioState": "Started",
"request": {
"method": "GET",
"url": "/todo/items"
},
"response": {
"status": 200,
"body" : "Buy milk"
}
}

{
"scenarioName": "To do list",
"requiredScenarioState": "Started",
"newScenarioState": "Cancel newspaper item added",
"request": {
"method": "POST",
"url": "/todo/items",
"bodyPatterns": [
{ "contains": "Cancel newspaper subscription" }
]
},
"response": {
"status": 201
}
}

{
"scenarioName": "To do list",
"requiredScenarioState": "Cancel newspaper item added",
"request": {
"method": "GET",
"url": "/todo/items"
},
"response": {
"status": 200,
"body" : "Buy milkCancel newspaper subscription"
}
}`

After, I run wiremock server in standalone mode:
java -jar wiremock-1.57-standalone.jar

After I execute the first scenario (GET):

http://localhost:8080/todo/items

I get the

<items> <item>Buy milk</item> </items>

Fine.

After I execute http://localhost:8080/todo/items (POST) with the json object (application/json):

{"value":"Cancel newspaper subscription"}

And I get:

<title>Error 404 NOT_FOUND</title>

And when I try to execute http://localhost:8080/todo/items (GET)
I get the the same as in the first scenario.....

<items> <item>Buy milk</item> </items>

Could someone explain me where am I wrong?

Thank you!!!!

@ghost
Copy link

ghost commented May 6, 2016

Hi

@macscripter,
I checked and for me it turns out you need to put each scenario step into separate valid json file. Presented example for scenarios is not a valid json file and it seems WireMock just reads first "entry" that handles GET /todo/items and "skips" definitions of following two entries.

BTW, I just discovered that inside mappings directory you can have sub-directories and they are also scanned for mapping definitions. That allows managing scenario files easier.

@muhrynov
Copy link

muhrynov commented Mar 25, 2018

Hi!
I slightly don't understand one moment. In example i see sequence of methods: Get -> Post -> Get
I have question: Can I change state without POST method?
I have one method GET, whose state i want to change after check him again.
For example:

  1. First scenario

`{
"scenarioName": "check status event",
"requiredScenarioState": "Started",
"request": {
"method": "GET",
"urlPathPattern": "/cashboxes/[0-9]+/registration/[0-9]+"
},

"response": {
"status": 200,
"jsonBody": {
"status": "status one",
},
"headers": {
"Content-Type": "application/json"
}
}
}`

  1. Second scenario

"scenarioName": "check status event",
"requiredScenarioState": "Started",
"newScenarioState": "status two",
"request": {
"method": "GET",
"urlPathPattern": "/cashboxes/[0-9]+/registration/[0-9]+"
},

"response": {
"status": 200,
"jsonBody": {
"status": "status two",
},
"headers": {
"Content-Type": "application/json"
}
}
}`

  1. Third scenario

"scenarioName": "check status event",
"requiredScenarioState": "status two",
"newScenarioState": "status three",
"request": {
"method": "GET",
"urlPathPattern": "/cashboxes/[0-9]+/registration/[0-9]+"
},

"response": {
"status": 200,
"jsonBody": {
"status": "status three",
},
"headers": {
"Content-Type": "application/json"
}
}
}`

@rbaker-ps
Copy link

Hi tomakehurst,

Thank you so much for all of your hard work, the Wiremock tool is awesome and it's saved us countless hours of testing!

I was wondering if there is a document that shows all of the different variations of the JSON stubs. I was trying to do scenarios and didn't find what I needed until I came across this ticket.

Thanks so much and keep up the awesome work!

Russ Baker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants