Micro Services Simulator

Simulates RESTFUL End Points for Micro Services.

License

License

GroupId

GroupId

org.jsmart
ArtifactId

ArtifactId

micro-simulator
Last Version

Last Version

1.1.10
Release Date

Release Date

Type

Type

jar
Description

Description

Micro Services Simulator
Simulates RESTFUL End Points for Micro Services.
Project URL

Project URL

http://www.jsmart.org
Source Code Management

Source Code Management

https://github.com/authorjapps/simulator

Download micro-simulator

How to add to project

<!-- https://jarcasting.com/artifacts/org.jsmart/micro-simulator/ -->
<dependency>
    <groupId>org.jsmart</groupId>
    <artifactId>micro-simulator</artifactId>
    <version>1.1.10</version>
</dependency>
// https://jarcasting.com/artifacts/org.jsmart/micro-simulator/
implementation 'org.jsmart:micro-simulator:1.1.10'
// https://jarcasting.com/artifacts/org.jsmart/micro-simulator/
implementation ("org.jsmart:micro-simulator:1.1.10")
'org.jsmart:micro-simulator:jar:1.1.10'
<dependency org="org.jsmart" name="micro-simulator" rev="1.1.10">
  <artifact name="micro-simulator" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.jsmart', module='micro-simulator', version='1.1.10')
)
libraryDependencies += "org.jsmart" % "micro-simulator" % "1.1.10"
[org.jsmart/micro-simulator "1.1.10"]

Dependencies

compile (10)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.0.7
ch.qos.logback : logback-core jar 1.0.7
org.skyscreamer : jsonassert jar 1.2.3
com.jayway.jsonpath : json-path jar 0.5.3
com.fasterxml.jackson.core : jackson-databind jar 2.4.4
commons-lang : commons-lang jar 2.6
com.google.classpath-explorer : classpath-explorer jar 1.0
org.simpleframework : simple jar 5.1.6
commons-io : commons-io jar 2.1
org.apache.httpcomponents : httpclient jar 4.5

test (2)

Group / Artifact Type Version
org.hamcrest : hamcrest-all jar 1.3
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

Micro Services REST Simulator

How to use the simulator?

To run the predefined(in json files) simulator(s) as main.

new SimpleRestJsonSimulatorsMain(PORT).start();

How it works?

Put your simulation Request URL and Response here in the respective json file or create a new file if not present.

Put the following JSON into the simulator json file above.

Then run the "SimpleRestJsonSimulatorsMain" or new SimpleRestJsonSimulatorsMain(PORT).start();

That's it. The REST api is ready to use.

"ignoreBody":false : This means, the mock API will evaluate JSON match/compare with the POST body with {"status": "In Progress"}, if matches then you get a response 200(ok) with { "pid": 1001 }, otherwise a 40X will be returned to the caller.

"ignoreBody":true : Which is default even if you dont specify this. This means, the mock API will accept any POST JSON body and will not compare/match against the mocked body, then you get a response 200(ok) with { "pid": 1001 }.

Json structure is as below. Sample content of the customers-simulator.json

{
    "name" : "Customers-Simulator",
    "apis" : [
    {
      "name": "e.g. Create a Process Instance",
      "operation": "POST",
      "url": "/bpm-service/start/case",
      "ignoreBody": false,
      "body":{
        "status": "In Progress"
      },
      "response": {
        "status": 200,
        "body": {
          "pid": 1001
        }
      }
    },
    {
        "name": "Get Customer by Id",
        "operation": "GET",
        "url": "/customers/1",
        "response": {
          "status": 200,
          "body": {
              "id": 1,
              "name": "Kate",
              "sex": "Female"
          }
        }
    },
    {
        "name" : "Get Resources List",
        "operation" : "GET",
        "url" : "/customers",
        "response" : {
          "headers" : {
            "language" : "en_GB"
          },
          "status" : 200,
          "body" : [
            {
              "id": 1,
              "name": "Kate",
              "sex": "Female"
            },
            {
              "id": 2,
              "name": "Rowland",
              "sex": "Male"
            }
          ]
        }
    }
  ]
}

Now: Both end end points are ready to use. You can put as many end points as you need.

Using your browser or REST client invoke: http://localhost:9999/customers/1 and see the response.

During unit testing / feature testing :

(Note: See "maven-simulator-plugin" for Integration-Testing)

e.g.

GET

While you are in need of an interfacing REST end point but it's not yet developed, then you can simulate this REST end point and do your Unit/Feature testing as below.

    String endPoint = "/customers/1";
    String requiredResponse = "{" +
                "    \"id\": 1," +
                "    \"age\": 0," +
                "    \"isAdult\": false" +
                "}";

   RestApi api = new RestApi()
                .name("Get Customers By Id API")
                .operation(Method.GET)
                .url("/customers/1")
                .response(new RestResponse("{\"Locale\": \"en_gb\"}", 200, requiredResponse))
                .build();
                
    simulator = new SimpleRestSimulator(HTTP_PORT)
            .restApi(api)
            .run();
``````
> Now the REST end point "/customers/1" with method "GET" is ready to use.


e.g.
#### POST 
String requiredResponse = "{\"id\": 1}";
        RestApi api = new RestApi()
                .name("Create Customer")
                .operation(Method.POST)
                .url("/customers")
                .response(new RestResponse("{\"accept-language\": \"en_gb\"}", 201, requiredResponse))
                .build();

        simulator = new SimpleRestSimulator(HTTP_PORT)
                .withApi(api)
                .run();

* More examples here:
`````
- See: Test case: SimpleRestSimulatorTest
  #willSimulateGETAtRunTimeWithPortAndUrlUsingApiBuilder()
`````

> Sample log below after the above test was run:

#Simulator: RESTFUL-simulator #started. Listening at port: 9090 2015-05-31 16:51:22,406 [Dispatcher: Thread-9] 561 [Dispatcher: Thread-9] INFO o.j.s.impl.SimpleRestSimulator - #GET: Target URL: /customers/1 2015-05-31 16:51:22,407 [Dispatcher: Thread-9] 562 [Dispatcher: Thread-9] INFO o.j.s.impl.SimpleRestSimulator -

Response Status: 200

2015-05-31 16:51:22,408 [Dispatcher: Thread-9] 563 [Dispatcher: Thread-9] INFO o.j.s.impl.SimpleRestSimulator -

Response body:

{ "id": 1, "age": 0, "isAdult": false } 2015-05-31 16:51:22,507 [main] 662 [main] INFO o.j.simulator.base.BaseSimulator - #RESTFUL-simulator stopped.


### To simulate more than one REST end points see:
````` 
        String endPoint1 = "/customers/1";
        String customerResponse = "{\n" +
                "    \"id\": 1,\n" +
                "    \"age\": 0,\n" +
                "    \"isAdult\": false\n" +
                "}";

        String endPoint2 = "/orders/1";
        String orderResponse = "{\n" +
                "    \"id\": 1,\n" +
                "    \"customerId\": 1,\n" +
                "    \"quantity\": 60\n" +
                "}";

        Api apiCustomer = new Api(
                "Get Customer By Id API",
                Method.GET,
                endPoint1,
                new RestResponse("some-headers", 200, customerResponse)
        );
        Api apiOrder = new Api(
                "Get Order Details By Order Id",
                Method.GET,
                endPoint2,
                new RestResponse(null, 200, orderResponse)
        );
        simulator = new SimpleRestSimulator(HTTP_PORT)
                .withApi(apiCustomer)
                .withApi(apiOrder)
                .run();
`````

Versions

Version
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4