Guide [1.0.x]

How it works?

 

The project will read a config file containing a Route. A Route must have a Request and a Response.

When you start the project a listener will be created in the port described in the config file.

All the Requests must be mapped in the config file.

Download – Project

You can download the Standalone version here: Download uaiMockServer

You can download the project to use with your tests adding the code below to your pom:

<dependency>
    <groupId>uaihebert.com</groupId>
    <artifactId>uaiMockServer</artifactId>
    <version>1.0.1</version>
    <scope>test</scope>
</dependency>

Download – CONFIG

You can download a sample config file here: Download uaiMockServer

Below you will find more details about each property.

Running the JAR

When you run the project using the JAR, we say that you are using the Standalone version.

To run the Standalone you need to have the java JDK installed (version 1.6 or higher). 

Search for in Goole: “install jdk”.

OR

You can use this link: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html to download the latest version.

Type in the console java -version and make sure that a friendly message will appear with the installed java version.

Type in the console java -jar uaiMockServer-{VERSION}.Standalone.jar to start the server. The config file must be in the same fold of the jar file. 

You can rename the config file and pass it as argument. Just type: java -jar uaiMockServer.java yourConfigFile.config

With linux you can execute CURL to invoke the URL. With windows you could use your browser or plugins like POSTMAN.

Running with JUnit

You can use it like:

public class YourTest {
    private static UaiMockServer uaiMockServer;

    @BeforeClass
    public static void beforeClass() {
        uaiMockServer = UaiMockServer.start();
    }

    @AfterClass
    public static void afterClass() {
        uaiMockServer.shutdown();
    }

    @Test
    public void aTest() {
        final Customer customer = // invoke the URL
        assertTrue(customer != null);
    }
}

The config file must be in the src/test/resources folder. If you want to use an config file with a different name, you have to pass the full path, like:

final URL resource = UaiMockServerConfig.class.getResource("/yourConfigurationFile.config");

uaiMockServer = UaiMockServer.start(resource.getFile());

The file (“yourConfigurationFile.config“) must be inside the resources folder.

How the configuration works?

The project will read a config file containing a Route. A Route must have a Request and a Response.

When you start the project a listener will be created in the port described in the config file.

All the Requests must be mapped in the config file.

Root Configurations

  • port —> the port that will be mapped the requests. You can set it to 8080, 7070 or any other value that you want. In the config sample file you will see the 1234 value
  • host —> The hostname that the will be invoked. In the config sample file you will see the localhost value
  • fileLog —> If you want a file log to be active. If the value is true, a file named uaiMockServer.log will be created
  • consoleLog —> If you want the console log to be displayed. If you disable both logs you will get a better performance when running hundreds of tests by seconds. 
  • context —> Context that will be added to the URL. If you map a context with the value “/uaiMockServer” and map an URL with the path “/hello” you will need to invoke /uaiMockServer/hello
  • defaultContentTypeResponse —> Set this to define a “Global” contentType for every response. If you do not overwrite this value in a Response mapping, this value will be used. In the config file you will find the default value of: “text/html;charset=UTF-8″.

Mapping a Route

A route is composed of a Request and a Response.

You can add a list of Routes like below:

routes = [
    {
        request {
            path = "/one"
            method = "GET"
        }
        response {
            status-code = 200
            body = """{"mockBody":{"title":"Hello World 01"}}"""
        }
    }
    {
        request {
            path = "/two"
            method = "GET"
        }
        response {
            content-type = "application/json;charset=UTF-8"
            status-code = 204
        }
    }
]

Mapping Routes in other files

If you map all the Routes in your main config file, this file will be huge and hard to read. You can create other file to hold only the requests. First of all, in your main config file add the root configuration:

	mappingRoutesFileList = [
			"customerRequests.config"
			"paymentsRequests.config"
	]

Now you will just need to create two files with the names described in the attribute. A sample of a extra config (customerRequests.config) file would be:

com.uaihebert.uaimockserver {
	routes = [
	    {
			request {
				path = "/customer"
				method = "PUT"
				isBodyRequired = true
			}
			response {
				content-type = "application/json;charset=UTF-8"
				status-code = 201
				body = """{"mockCustomerBody":{"name":"Joseph of Arimathea"}}"""
			}
	    }
	]
}

Mapping a Request

A request must be inside a Route. You will need to set the path, method and contentType:

  • path —> The invoked URL must match all the path. If you set the path with “/hello/” the invoked URL must be “context/hello/”. If you invoke with “context/hello” (without the last slash) an error message will be displayed.
  • method —> You can define the HTTP method that will be invoked: GET/POST/DELETE/ etc…
  • contentType —> The request content type: application/json, text/html/ etc…

There are other allowed attributes:

  •  isBodyRequired —> define if a body is required for the request. If the attribute is set to true and the body is not found in the request, an Exception will be thrown
  • requiredHeaderList –> define if a required header list for the request. If the attribute is set to true and the value is not found / or it has a different value an error will be thrown.
  • requiredQueryParamList –> define if a query param list for the request. If the attribute is set to true and the value is not found / or it has a different value an error will be thrown.

A sample of configuration would be:

com.uaihebert.uaimockserver {
	port = 1234
	host = localhost
	fileLog = false
	consoleLog = true
	context = /uaiMockServer
	defaultContentTypeResponse = "text/html;charset=UTF-8"
	routes = [
	    {
            request {
                path = "/customer"
                method = "PUT"
                isBodyRequired = true
                requiredHeaderList = [
                    {
                        header {
                            name = "X-UAI-LOGIN-HEADER"
                            valueList = [
                                "X_KPYW_Z33!"
                            ]
                        }
                    }
                ]
                requiredQueryParamList = [
                    {
                        queryParam {
                            name = "uaiQueryParam01"
                            valueList = [
                                "VALUE_01"
                                "VALUE_02"
                            ]
                        }
                    }
                ]
            }
            response {
                content-type = "application/json;charset=UTF-8"
                status-code = 201
                body = """{"mockCustomerBody":{"name":"Joseph of Arimathea"}}"""
            }
        }
    ]
}

WildCard for Headers and QueryParam

Sometimes you will need to send a value in the Header/QueryParam but this value can change. You will be able to use the UAI_* wildcard. All you need to do is set the value with the UAI_* wildcard and the framework will only validate if a value is present, but will not validate its content.

You could do like:

requiredHeaderList = [
    {
        header {
            name = "X-UAI-LOGIN-HEADER"
            valueList = [
                "UAI_*"
            ]
        }
    }
]
requiredQueryParamList = [
    {
        queryParam {
            name = "uaiQueryParam01"
            valueList = [
                "UAI_*"
            ]
        }
    }
]

What about performance?

In the project GIT you will find a test that executes 300 requests in 2 seconds (in average).