Skip to content

Commit

Permalink
revamp mock listener and mock model (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sérgio Martins committed Apr 18, 2020
1 parent 1530e82 commit bd08e60
Show file tree
Hide file tree
Showing 12 changed files with 406 additions and 95 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
[![badge-jdk](https://img.shields.io/badge/jdk-8-green.svg)](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
[![License badge](https://img.shields.io/github/license/sergiomartins8/ui-automation-bootstrap)](http://www.apache.org/licenses/LICENSE-2.0)
[![Contributors badge](https://img.shields.io/github/contributors/sergiomartins8/ui-automation-bootstrap)](https://github.com/sergiomartins8/ui-automation-bootstrap/graphs/contributors)
[![Issues](https://img.shields.io/github/issues/sergiomartins8/ui-automation-bootstrap)](https://github.com/sergiomartins8/ui-automation-bootstrap/issues)
![Languages](https://img.shields.io/github/languages/top/sergiomartins8/ui-automation-bootstrap)

# ui-automation-bootstrap

A foundation for selenium based ui automation projects using _[selenide](https://github.com/selenide/selenide)_

[![badge-jdk](https://img.shields.io/badge/jdk-8-green.svg)](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
![Languages](https://img.shields.io/github/languages/top/sergiomartins8/ui-automation-bootstrap)
![Forks](https://img.shields.io/github/forks/sergiomartins8/ui-automation-bootstrap?style=social)
![Stars](https://img.shields.io/github/stars/sergiomartins8/ui-automation-bootstrap?style=social)
[![Contributors badge](https://img.shields.io/github/contributors/sergiomartins8/ui-automation-bootstrap)](https://github.com/sergiomartins8/ui-automation-bootstrap/graphs/contributors)
[![Issues](https://img.shields.io/github/issues/sergiomartins8/ui-automation-bootstrap)](https://github.com/sergiomartins8/ui-automation-bootstrap/issues)
[![License badge](https://img.shields.io/github/license/sergiomartins8/ui-automation-bootstrap)](http://www.apache.org/licenses/LICENSE-2.0)

* [About](#about)
* [Getting Started](#getting-started)
* [Documentation](#documentation)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sergiomartins8</groupId>
<artifactId>ui-automation-bootstrap</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
16 changes: 12 additions & 4 deletions src/test/java/utils/listeners/MockListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@ public class MockListener implements ITestListener, Loggable {
@Override
public void onTestStart(ITestResult result) {
extractMockAnnotation(result).ifPresent(mock -> {
for (String mockPath : mock.path()) {
MockDefinition mockDefinition = MockParser.toObject(mockPath);
for (String path : mock.path()) {
MockDefinition mockDefinition = MockParser.toObject(path);
MockContext.getMockServerClient()
.when(request()
.withMethod(requireNonNull(mockDefinition).getRequest().getMethod())
.withPath(mockDefinition.getRequest().getPath()))
.withPath(mockDefinition.getRequest().getPath())
.withQueryStringParameters(mockDefinition.getRequest().getQueryStringParameters())
.withCookies(mockDefinition.getRequest().getCookies())
.withBody(mockDefinition.getRequest().getBody())
.withHeaders(mockDefinition.getRequest().getHeaders())
)
.respond(response()
.withStatusCode(mockDefinition.getResponse().getStatusCode())
.withBody(mockDefinition.getResponse().getBody()));
.withBody(mockDefinition.getResponse().getBody())
.withHeaders(mockDefinition.getResponse().getHeaders())
.withCookies(mockDefinition.getResponse().getCookies())
);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/utils/mocks/Mock.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
/**
* Annotation to inject mocks during runtime.
* <p>
* Accepts a String array which contains the paths to the mocks.
* e.g. {@code @Mock({"path1", "path2", ...})}
* Mocks are required to be json files.
* <p>
* e.g. {@code @Mock({"path1", "path2", ...})}
* </p>
*/
@Target(ElementType.METHOD)
Expand Down
81 changes: 13 additions & 68 deletions src/test/java/utils/mocks/MockDefinition.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,40 @@
package utils.mocks;

import com.google.gson.Gson;
import utils.mocks.model.MockRequest;
import utils.mocks.model.MockResponse;

/***
* Concrete definition of a mock.
* To be used by a mapper; from json to {@link MockDefinition} object.
* <p>
* A mock is represented by a {@link MockRequest} and by a {@link MockResponse}
* </p>
*/
@SuppressWarnings("unused")
public final class MockDefinition {

/**
* Holds the mocked request params.
* Represents the mocked request.
*/
private Request request;
private MockRequest request;

/**
* Holds the mocked response params.
* Represents the mocked response.
*/
private Response response;
private MockResponse response;

public Request getRequest() {
public MockRequest getRequest() {
return request;
}

public Response getResponse() {
public MockResponse getResponse() {
return response;
}

public void setResponse(Response response) {
public void setResponse(MockResponse response) {
this.response = response;
}

public void setRequest(Request request) {
public void setRequest(MockRequest request) {
this.request = request;
}

public static class Request {

/**
* Contains the mapping request path.
*/
private String path;

/**
* Contains the type of request (e.g. GET, POST).
*/
private String method;

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}
}

public static class Response {

/**
* Holds the whole response body.
*/
private Object body;

/**
* Status code response.
*/
private Integer statusCode;

public String getBody() {
return new Gson().toJson(this.body);
}

public void setBody(Object body) {
this.body = body;
}

public Integer getStatusCode() {
return statusCode;
}

public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
}
}
18 changes: 5 additions & 13 deletions src/test/java/utils/mocks/MockParser.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package utils.mocks;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
* Single responsibility of parsing json files into objects.
* Single responsibility of parsing json files into {@link MockDefinition} objects.
*/
public final class MockParser {

public static MockDefinition toObject(String path) {
ObjectMapper mapper = new ObjectMapper();
InputStream inputStream = MockParser.class.getResourceAsStream(path);
try {
return mapper.readValue(inputStream, MockDefinition.class);
} catch (IOException e) {
e.printStackTrace();
}
return null;
public static MockDefinition toObject(String jsonPath) {
return new Gson().fromJson(new InputStreamReader(MockParser.class.getResourceAsStream(jsonPath)), MockDefinition.class);
}
}
34 changes: 34 additions & 0 deletions src/test/java/utils/mocks/model/MockCookie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package utils.mocks.model;

/**
* Concrete definition of a mocked cookie.
*/
@SuppressWarnings("unused")
public class MockCookie {

/**
* Cookie name.
*/
private String name;

/**
* Cookie value.
*/
private String value;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
34 changes: 34 additions & 0 deletions src/test/java/utils/mocks/model/MockHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package utils.mocks.model;

/**
* Concrete definition of a mocked header.
*/
@SuppressWarnings("unused")
public final class MockHeader {

/**
* Header name.
*/
private String name;

/**
* Header values.
*/
private String values;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValues() {
return values;
}

public void setValues(String values) {
this.values = values;
}
}
36 changes: 36 additions & 0 deletions src/test/java/utils/mocks/model/MockQueryStringParameter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils.mocks.model;

import java.util.List;

/**
* Concrete definition of a mocked query parameter.
*/
@SuppressWarnings("unused")
public final class MockQueryStringParameter {

/**
* Query parameter name.
*/
private String name;

/**
* Query parameter values.
*/
private List<String> values;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<String> getValues() {
return values;
}

public void setValues(List<String> values) {
this.values = values;
}
}
Loading

0 comments on commit bd08e60

Please sign in to comment.