diff --git a/README.md b/README.md index 53b0976..da0609f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -[![CircleCI](https://circleci.com/gh/moip/mockkid.svg?style=shield)](https://circleci.com/gh/moip/mockkid) +[![CircleCI](https://circleci.com/gh/moip/mockkid.svg?style=shield)](https://circleci.com/gh/moip/mockkid) [![codecov](https://codecov.io/gh/moip/mockkid/branch/master/graph/badge.svg)](https://codecov.io/gh/moip/mockkid) # Mockkid -Mockkid is a webserver for mocking http responses. +Mock Kid is a web server for mocking http responses through file-based configurations. ## Pre-requisites - Maven @@ -20,7 +20,7 @@ Mockkid is a webserver for mocking http responses. Running with docker-compose will point mockkid to the configurations in the *samples* folder. -To use other configurations, edit docker-compose.yml to change the volume or change the samples folder. +To use other configurations, edit `docker-compose.yml` to change the volume or change the samples folder. ## Samples diff --git a/src/main/java/br/com/moip/mockkid/service/ConditionalSolver.java b/src/main/java/br/com/moip/mockkid/service/ConditionalSolver.java index 3a51f43..30657b8 100644 --- a/src/main/java/br/com/moip/mockkid/service/ConditionalSolver.java +++ b/src/main/java/br/com/moip/mockkid/service/ConditionalSolver.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -22,10 +23,13 @@ public class ConditionalSolver { private Conditionals conditionals; public ResponseConfiguration solve(Configuration configuration, HttpServletRequest request) { - Map variables = variableResolver.resolve(configuration, request); - for (ResponseConfiguration r : configuration.getResponseConfigurations()) { - if (r.getConditional() != null && solve(r.getConditional(), variables)) { - return r; + for (ResponseConfiguration rc : configuration.getResponseConfigurations()) { + Conditional conditional = rc.getConditional(); + if (conditional == null) continue; + + Map variables = variableResolver.resolve(rc, conditional, request); + if (solve(rc.getConditional(), variables)) { + return rc; } } diff --git a/src/main/java/br/com/moip/mockkid/service/VariableResolver.java b/src/main/java/br/com/moip/mockkid/service/VariableResolver.java index 7817e13..1edc9c3 100644 --- a/src/main/java/br/com/moip/mockkid/service/VariableResolver.java +++ b/src/main/java/br/com/moip/mockkid/service/VariableResolver.java @@ -1,7 +1,6 @@ package br.com.moip.mockkid.service; import br.com.moip.mockkid.model.Conditional; -import br.com.moip.mockkid.model.Configuration; import br.com.moip.mockkid.model.ResponseConfiguration; import br.com.moip.mockkid.model.VariableResolvers; import org.slf4j.Logger; @@ -29,17 +28,10 @@ public class VariableResolver { @Autowired private VariableResolvers variableResolvers; - public Map resolve(Configuration config, HttpServletRequest request) { + public Map resolve(ResponseConfiguration responseConfiguration, Conditional conditional, HttpServletRequest request) { Map resolvedVariables = new HashMap<>(); - - for (ResponseConfiguration responseConfiguration: config.getResponseConfigurations()) { - Conditional conditional = responseConfiguration.getConditional(); - - resolvedVariables.putAll(handleConditionals(request, responseConfiguration, conditional)); - } - + resolvedVariables.putAll(handleConditionals(request, responseConfiguration, conditional)); logger.info("Variables = " + resolvedVariables); - return resolvedVariables; } @@ -101,7 +93,7 @@ private Map resolveVariables(Set variableNames, Response private String resolveVariable(String variableName, ResponseConfiguration responseConfiguration, HttpServletRequest request) { for (br.com.moip.mockkid.variable.VariableResolver resolver : variableResolvers) { - if (resolver.handles(variableName)) { + if (resolver.canHandle(variableName, request)) { String value = resolver.extract(variableName, responseConfiguration, request); if (value != null) { return value; diff --git a/src/main/java/br/com/moip/mockkid/variable/VariableResolver.java b/src/main/java/br/com/moip/mockkid/variable/VariableResolver.java index b906d64..3e2325e 100644 --- a/src/main/java/br/com/moip/mockkid/variable/VariableResolver.java +++ b/src/main/java/br/com/moip/mockkid/variable/VariableResolver.java @@ -6,7 +6,7 @@ public interface VariableResolver { - boolean handles(String variable); + boolean canHandle(String variable, HttpServletRequest request); String extract(String variable, ResponseConfiguration responseConfiguration, HttpServletRequest request); diff --git a/src/main/java/br/com/moip/mockkid/variable/resolver/BodyVariableResolver.java b/src/main/java/br/com/moip/mockkid/variable/resolver/BodyVariableResolver.java deleted file mode 100644 index 14547f8..0000000 --- a/src/main/java/br/com/moip/mockkid/variable/resolver/BodyVariableResolver.java +++ /dev/null @@ -1,32 +0,0 @@ -package br.com.moip.mockkid.variable.resolver; - -import br.com.moip.mockkid.model.ResponseConfiguration; -import br.com.moip.mockkid.variable.VariableResolver; -import br.com.moip.mockkid.variable.resolver.body.JSONBodyVariableResolver; -import br.com.moip.mockkid.variable.resolver.body.XMLBodyVariableResolver; - -import javax.servlet.http.HttpServletRequest; - -public class BodyVariableResolver implements VariableResolver { - - @Override - public boolean handles(String variable) { - return variable.startsWith("body."); - } - - @Override - public String extract(String name, ResponseConfiguration responseConfiguration, HttpServletRequest request) { - String header = request.getHeader("content-type"); - boolean isJson = header != null && (header.contains("application/json") || header.contains("text/json")); - boolean isXml = header != null && (header.contains("application/xml") || header.contains("text/xml")); - - if (isJson) { - return JSONBodyVariableResolver.extractValueFromJson(name, request); - } else if (isXml) { - return XMLBodyVariableResolver.extractValueFromXml(name, request); - } - - return null; - } - -} diff --git a/src/main/java/br/com/moip/mockkid/variable/resolver/HeaderVariableResolver.java b/src/main/java/br/com/moip/mockkid/variable/resolver/HeaderVariableResolver.java index 90a1e96..4e9ca2b 100644 --- a/src/main/java/br/com/moip/mockkid/variable/resolver/HeaderVariableResolver.java +++ b/src/main/java/br/com/moip/mockkid/variable/resolver/HeaderVariableResolver.java @@ -8,7 +8,7 @@ public class HeaderVariableResolver implements VariableResolver { @Override - public boolean handles(String variable) { + public boolean canHandle(String variable, HttpServletRequest request) { return variable.startsWith("headers."); } diff --git a/src/main/java/br/com/moip/mockkid/variable/resolver/body/JSONBodyVariableResolver.java b/src/main/java/br/com/moip/mockkid/variable/resolver/JSONBodyVariableResolver.java similarity index 63% rename from src/main/java/br/com/moip/mockkid/variable/resolver/body/JSONBodyVariableResolver.java rename to src/main/java/br/com/moip/mockkid/variable/resolver/JSONBodyVariableResolver.java index de773ca..7d72204 100644 --- a/src/main/java/br/com/moip/mockkid/variable/resolver/body/JSONBodyVariableResolver.java +++ b/src/main/java/br/com/moip/mockkid/variable/resolver/JSONBodyVariableResolver.java @@ -1,6 +1,8 @@ -package br.com.moip.mockkid.variable.resolver.body; +package br.com.moip.mockkid.variable.resolver; import br.com.moip.mockkid.model.MockkidRequest; +import br.com.moip.mockkid.model.ResponseConfiguration; +import br.com.moip.mockkid.variable.VariableResolver; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -11,11 +13,26 @@ import java.io.BufferedReader; import java.io.InputStreamReader; -public class JSONBodyVariableResolver { +public class JSONBodyVariableResolver implements VariableResolver { private static final Logger logger = LoggerFactory.getLogger(JSONBodyVariableResolver.class); - public static String extractValueFromJson(String name, HttpServletRequest request) { + @Override + public boolean canHandle(String variable, HttpServletRequest request) { + return variable.startsWith("body.") && isJson(request); + } + + @Override + public String extract(String variable, ResponseConfiguration responseConfiguration, HttpServletRequest request) { + return extractValueFromJson(variable, request); + } + + private boolean isJson(HttpServletRequest request) { + String header = request.getHeader("content-type"); + return header != null && (header.contains("application/json") || header.contains("text/json")); + } + + private String extractValueFromJson(String name, HttpServletRequest request) { try { BufferedReader reader = new BufferedReader( new InputStreamReader(((MockkidRequest) request).getSafeInputStream())); @@ -39,6 +56,7 @@ public static String extractValueFromJson(String name, HttpServletRequest reques } catch (Exception e) { logger.warn("Couldn't extract variable", e); } + return null; } } diff --git a/src/main/java/br/com/moip/mockkid/variable/resolver/RawBodyVariableResolver.java b/src/main/java/br/com/moip/mockkid/variable/resolver/RawBodyVariableResolver.java index fb78c38..a6ee06d 100644 --- a/src/main/java/br/com/moip/mockkid/variable/resolver/RawBodyVariableResolver.java +++ b/src/main/java/br/com/moip/mockkid/variable/resolver/RawBodyVariableResolver.java @@ -14,7 +14,7 @@ public class RawBodyVariableResolver implements VariableResolver { private static final Logger logger = LoggerFactory.getLogger(RawBodyVariableResolver.class); @Override - public boolean handles(String variable) { + public boolean canHandle(String variable, HttpServletRequest request) { return variable.equals("body"); } diff --git a/src/main/java/br/com/moip/mockkid/variable/resolver/RegexVariableResolver.java b/src/main/java/br/com/moip/mockkid/variable/resolver/RegexVariableResolver.java index 03dcc27..a191b33 100644 --- a/src/main/java/br/com/moip/mockkid/variable/resolver/RegexVariableResolver.java +++ b/src/main/java/br/com/moip/mockkid/variable/resolver/RegexVariableResolver.java @@ -19,7 +19,7 @@ public class RegexVariableResolver implements VariableResolver { private static final Logger logger = LoggerFactory.getLogger(RegexVariableResolver.class); @Override - public boolean handles(String variable) { + public boolean canHandle(String variable, HttpServletRequest request) { return variable.startsWith("regex."); } diff --git a/src/main/java/br/com/moip/mockkid/variable/resolver/URLQueryParameterVariableResolver.java b/src/main/java/br/com/moip/mockkid/variable/resolver/URLQueryParameterVariableResolver.java index 20e3583..b66e0cc 100644 --- a/src/main/java/br/com/moip/mockkid/variable/resolver/URLQueryParameterVariableResolver.java +++ b/src/main/java/br/com/moip/mockkid/variable/resolver/URLQueryParameterVariableResolver.java @@ -8,7 +8,7 @@ public class URLQueryParameterVariableResolver implements VariableResolver { @Override - public boolean handles(String variable) { + public boolean canHandle(String variable, HttpServletRequest request) { return variable.startsWith("url."); } diff --git a/src/main/java/br/com/moip/mockkid/variable/resolver/body/XMLBodyVariableResolver.java b/src/main/java/br/com/moip/mockkid/variable/resolver/XMLBodyVariableResolver.java similarity index 53% rename from src/main/java/br/com/moip/mockkid/variable/resolver/body/XMLBodyVariableResolver.java rename to src/main/java/br/com/moip/mockkid/variable/resolver/XMLBodyVariableResolver.java index 874c3e7..8db790b 100644 --- a/src/main/java/br/com/moip/mockkid/variable/resolver/body/XMLBodyVariableResolver.java +++ b/src/main/java/br/com/moip/mockkid/variable/resolver/XMLBodyVariableResolver.java @@ -1,6 +1,8 @@ -package br.com.moip.mockkid.variable.resolver.body; +package br.com.moip.mockkid.variable.resolver; import br.com.moip.mockkid.model.MockkidRequest; +import br.com.moip.mockkid.model.ResponseConfiguration; +import br.com.moip.mockkid.variable.VariableResolver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; @@ -11,14 +13,28 @@ import javax.xml.xpath.XPath; import javax.xml.xpath.XPathFactory; -public class XMLBodyVariableResolver { +public class XMLBodyVariableResolver implements VariableResolver { private static final Logger logger = LoggerFactory.getLogger(XMLBodyVariableResolver.class); - public static String extractValueFromXml(String name, HttpServletRequest request) { + @Override + public boolean canHandle(String variable, HttpServletRequest request) { + return variable.startsWith("body.") && isXml(request); + } + + @Override + public String extract(String variable, ResponseConfiguration responseConfiguration, HttpServletRequest request) { + return extractValueFromXml(variable, request); + } + + private boolean isXml(HttpServletRequest request) { + String header = request.getHeader("content-type"); + return header != null && (header.contains("application/xml") || header.contains("text/xml")); + } + + private String extractValueFromXml(String name, HttpServletRequest request) { try { - DocumentBuilderFactory builderFactory = - DocumentBuilderFactory.newInstance(); + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document document = builder.parse(((MockkidRequest) request).getSafeInputStream()); @@ -30,6 +46,7 @@ public static String extractValueFromXml(String name, HttpServletRequest request } catch (Exception e) { logger.warn("Couldn't extract variable", e); } + return null; } } diff --git a/src/main/resources/configuration/teste5.yaml b/src/main/resources/configuration/teste5.yaml new file mode 100644 index 0000000..881d98a --- /dev/null +++ b/src/main/resources/configuration/teste5.yaml @@ -0,0 +1,273 @@ +configuration: + endpoint: + url: /creditcardproxies/api/json + method: POST + responseConfigurations: + - responseConfiguration: + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "aprovar" + response: + status: 200 + headers: + content-type: application/json + body: | + { "status": "A", + "errCode": "00", + "subErrorString": "Transacao autorizada", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + - responseConfiguration: + name: "Transacao nao autorizada" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "transacao_nao_autorizada" + response: + status: 200 + headers: + content-type: application/json + body: | + + { "status": "F", + "errCode": "02", + "subErrorString": "Transacao não autorizada", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + - responseConfiguration: + name: "Timeout" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "Timeout" + response: + status: 500 + headers: + content-type: application/json + body: | + {"statusCode": "F", "status": "timeout"} + + - responseConfiguration: + name: "Nao aprovar" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "nao_aprovar" + response: + status: 200 + headers: + content-type: application/json + body: | + { "status": "F", + "errCode": "02", + "subErrorString": "nao aprovar", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + - responseConfiguration: + name: "Cartao invalido" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "cartao_invalido" + response: + status: 200 + headers: + content-type: application/json + body: | + + { "status": "F", + "errCode": "14", + "subErrorString": "Cartao invalido", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + + + - responseConfiguration: + name: "Cartao vencido - cartao retido" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "cartao_vencido_cartao_retido" + response: + status: 200 + headers: + content-type: application/json + body: | + { "status": "F", + "errCode": "14", + "subErrorString": "Cartao vencido - cartao retido", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + - responseConfiguration: + name: "Mau funcionamento do sistema" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "mau_funcionamento_sistema" + response: + status: 200 + headers: + content-type: application/json + body: | + + { "status": "F", + "errCode": "96", + "subErrorString": "Mau funcionamento do sistema", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + + - responseConfiguration: + name: "Cartao roubado" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "cartao_roubado" + response: + status: 200 + headers: + content-type: application/json + body: | + { "status": "F", + "errCode": "43", + "subErrorString": "Cartao roubado", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + + - responseConfiguration: + name: "Transacao nao aprovada" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "transacao_nao_aprovada" + response: + status: 200 + headers: + content-type: application/json + body: | + { "status": "F", + "errCode": "05", + "subErrorString": "Transacao nao aprovada", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + + + + - responseConfiguration: + name: "Nao autorizado - cartao vencido" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "nao_autorizado_cartao_vencido" + response: + status: 200 + headers: + content-type: application/json + body: | + { "status": "F", + "errCode": "54", + "subErrorString": "Nao autorizado - cartao vencido", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + + - responseConfiguration: + name: "Codigo secreto incorreto" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "codigo_secreto_incorreto" + response: + status: 200 + headers: + content-type: application/json + body: | + { "status": "F", + "errCode": "55", + "subErrorString": "Codigo secreto incorreto", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + - responseConfiguration: + name: "Cartao bloqueado" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "cartao_bloqueado" + response: + status: 200 + headers: + content-type: application/json + body: | + + { "status": "F", + "errCode": "76", + "subErrorString": "Cartao bloqueado", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } + + - responseConfiguration: + name: "unknown host" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "unknown_host" + response: + status: 500 + headers: + content-type: application/json + body: | + {"statusCode": "F", "status": "unknown host"} + + + - responseConfiguration: + name: "Cartao suspenso" + conditional: + type: EQUALS + element: body.endCustomer.firstName + value: "cartao_suspenso" + response: + status: 500 + headers: + content-type: application/json + body: | + + { "status": "F", + "errCode": "S3", + "subErrorString": "Cartao suspenso", + "responseData": { + "cvdResponseCode": "P", + "externalRrn": "145678" + } + } \ No newline at end of file diff --git a/src/test/java/br/com/moip/mockkid/service/VariableResolverTest.java b/src/test/java/br/com/moip/mockkid/service/VariableResolverTest.java index ef4f157..4982a15 100644 --- a/src/test/java/br/com/moip/mockkid/service/VariableResolverTest.java +++ b/src/test/java/br/com/moip/mockkid/service/VariableResolverTest.java @@ -20,6 +20,7 @@ import javax.servlet.http.HttpServletRequest; import java.io.IOException; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -45,7 +46,13 @@ public class VariableResolverTest { @Test public void shouldResolveVariables() { - Map variables = variableResolver.resolve(getConfiguration(), null); + Map variables = new HashMap<>(); + Configuration configuration = getConfiguration(); + + for (ResponseConfiguration rc : configuration.getResponseConfigurations()) { + variables.putAll(variableResolver.resolve(rc, rc.getConditional(), null)); + } + assertEquals("expression_resolved", variables.get("expression")); assertEquals("var_resolved", variables.get("var")); assertEquals("with.dot_resolved", variables.get("with.dot")); @@ -89,7 +96,7 @@ private Configuration getConfiguration() { private br.com.moip.mockkid.variable.VariableResolver getMockVariableResolver() { return new br.com.moip.mockkid.variable.VariableResolver() { @Override - public boolean handles(String variable) { + public boolean canHandle(String variable, HttpServletRequest request) { return !variable.startsWith("regex."); } @Override diff --git a/src/test/java/br/com/moip/mockkid/variable/resolver/BodyVariableResolverTest.java b/src/test/java/br/com/moip/mockkid/variable/resolver/BodyVariableResolverTest.java deleted file mode 100644 index 0fa1c8d..0000000 --- a/src/test/java/br/com/moip/mockkid/variable/resolver/BodyVariableResolverTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package br.com.moip.mockkid.variable.resolver; - -import br.com.moip.mockkid.model.MockkidRequest; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.springframework.mock.web.DelegatingServletInputStream; - -import java.io.ByteArrayInputStream; -import java.io.IOException; - -import static org.junit.Assert.*; - -public class BodyVariableResolverTest { - - private BodyVariableResolver resolver = new BodyVariableResolver(); - - @Mock - private MockkidRequest request; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void shouldHandle() { - assertTrue(resolver.handles("body.name")); - } - - @Test - public void shouldNotHandle() { - assertFalse(resolver.handles("url.url")); - assertFalse(resolver.handles("headers.authorization")); - } - - @Test - public void shouldResolveVariablesInJSON() throws IOException { - Mockito.when(request.getHeader("content-type")).thenReturn("application/json"); - configureRequestWithBody("{ \"name\":\"JOSE\" }"); - assertEquals("JOSE", resolver.extract("body.name", null, request)); - } - - @Test - public void shouldResolveVariablesInXML() throws IOException { - Mockito.when(request.getHeader("content-type")).thenReturn("application/xml"); - configureRequestWithBody("JOSE"); - assertEquals("JOSE", resolver.extract("body.name", null, request)); - } - - @Test - public void shouldNotResolveVariablesWithoutContentType() throws IOException { - configureRequestWithBody("{ \"name\":\"JOSE\" }"); - assertNull(resolver.extract("body.name", null, request)); - } - - protected void configureRequestWithBody(String body) throws IOException { - DelegatingServletInputStream stream = new DelegatingServletInputStream(new ByteArrayInputStream(body.getBytes())); - Mockito.when(request.getSafeInputStream()).thenReturn(stream); - } -} \ No newline at end of file diff --git a/src/test/java/br/com/moip/mockkid/variable/resolver/HeaderVariableResolverTest.java b/src/test/java/br/com/moip/mockkid/variable/resolver/HeaderVariableResolverTest.java index 30d21cc..3b4f023 100644 --- a/src/test/java/br/com/moip/mockkid/variable/resolver/HeaderVariableResolverTest.java +++ b/src/test/java/br/com/moip/mockkid/variable/resolver/HeaderVariableResolverTest.java @@ -26,14 +26,14 @@ public void setUp() { @Test public void shouldHandle() { - assertTrue(resolver.handles("headers.authorization")); - assertTrue(resolver.handles("headers.name")); + assertTrue(resolver.canHandle("headers.authorization", request)); + assertTrue(resolver.canHandle("headers.name", request)); } @Test public void shouldNotHandle() { - assertFalse(resolver.handles("body.authorization")); - assertFalse(resolver.handles("url.page")); + assertFalse(resolver.canHandle("body.authorization", request)); + assertFalse(resolver.canHandle("url.page", request)); } @Test diff --git a/src/test/java/br/com/moip/mockkid/variable/resolver/body/JsonBodyVariableResolverTest.java b/src/test/java/br/com/moip/mockkid/variable/resolver/JsonBodyVariableResolverTest.java similarity index 55% rename from src/test/java/br/com/moip/mockkid/variable/resolver/body/JsonBodyVariableResolverTest.java rename to src/test/java/br/com/moip/mockkid/variable/resolver/JsonBodyVariableResolverTest.java index 2eeb02c..95f3271 100644 --- a/src/test/java/br/com/moip/mockkid/variable/resolver/body/JsonBodyVariableResolverTest.java +++ b/src/test/java/br/com/moip/mockkid/variable/resolver/JsonBodyVariableResolverTest.java @@ -1,6 +1,7 @@ -package br.com.moip.mockkid.variable.resolver.body; +package br.com.moip.mockkid.variable.resolver; import br.com.moip.mockkid.model.MockkidRequest; +import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -11,34 +12,52 @@ import java.io.ByteArrayInputStream; import java.io.IOException; -import static org.junit.Assert.assertEquals; - public class JsonBodyVariableResolverTest { @Mock private MockkidRequest request; + private JSONBodyVariableResolver resolver = new JSONBodyVariableResolver(); + @Before public void setUp() { MockitoAnnotations.initMocks(this); + Mockito.when(request.getHeader("content-type")).thenReturn("application/json"); + } + + @Test + public void shouldHandle() { + assertTrue(resolver.canHandle("body.name", request)); + } + + @Test + public void shouldNotHandleVariableName() { + assertFalse(resolver.canHandle("url.url", request)); + assertFalse(resolver.canHandle("headers.authorization", request)); + } + + @Test + public void shouldNotHandleContentType() { + Mockito.when(request.getHeader("content-type")).thenReturn("text/plain"); + assertFalse(resolver.canHandle("body.name", request)); } @Test public void shouldExtractSimpleVariable() throws IOException { configureRequestWithBody("{ \"name\":\"JOSE\" }"); - assertEquals("JOSE", JSONBodyVariableResolver.extractValueFromJson("body.name", request)); + assertEquals("JOSE", resolver.extract("body.name", null, request)); } @Test public void shouldExtractVariable() throws IOException { configureRequestWithBody("{\"person\": { \"address\": { \"number\": 666 } } }"); - assertEquals("666", JSONBodyVariableResolver.extractValueFromJson("body.person.address.number", request)); + assertEquals("666", resolver.extract("body.person.address.number", null, request)); } @Test public void shouldReturnNullOnUnknownVariable() throws IOException { configureRequestWithBody("{\"person\": { \"address\": { \"street\": \"Av Paulista\" } } }"); - assertEquals(null, JSONBodyVariableResolver.extractValueFromJson("body.person.address.number", request)); + assertEquals(null, resolver.extract("body.person.address.number", null, request)); } private void configureRequestWithBody(String body) throws IOException { diff --git a/src/test/java/br/com/moip/mockkid/variable/resolver/URLQueryParameterVariableResolverTest.java b/src/test/java/br/com/moip/mockkid/variable/resolver/URLQueryParameterVariableResolverTest.java index 729c26b..eda09b5 100644 --- a/src/test/java/br/com/moip/mockkid/variable/resolver/URLQueryParameterVariableResolverTest.java +++ b/src/test/java/br/com/moip/mockkid/variable/resolver/URLQueryParameterVariableResolverTest.java @@ -26,14 +26,14 @@ public void setUp() { @Test public void shouldHandle() { - assertTrue(resolver.handles("url.authorization")); - assertTrue(resolver.handles("url.name")); + assertTrue(resolver.canHandle("url.authorization", request)); + assertTrue(resolver.canHandle("url.name", request)); } @Test public void shouldNotHandle() { - assertFalse(resolver.handles("body.authorization")); - assertFalse(resolver.handles("headers.page")); + assertFalse(resolver.canHandle("body.authorization", request)); + assertFalse(resolver.canHandle("headers.page", request)); } @Test diff --git a/src/test/java/br/com/moip/mockkid/variable/resolver/body/XMLBodyVariableResolverTest.java b/src/test/java/br/com/moip/mockkid/variable/resolver/XMLBodyVariableResolverTest.java similarity index 55% rename from src/test/java/br/com/moip/mockkid/variable/resolver/body/XMLBodyVariableResolverTest.java rename to src/test/java/br/com/moip/mockkid/variable/resolver/XMLBodyVariableResolverTest.java index 9915b6f..b260461 100644 --- a/src/test/java/br/com/moip/mockkid/variable/resolver/body/XMLBodyVariableResolverTest.java +++ b/src/test/java/br/com/moip/mockkid/variable/resolver/XMLBodyVariableResolverTest.java @@ -1,6 +1,7 @@ -package br.com.moip.mockkid.variable.resolver.body; +package br.com.moip.mockkid.variable.resolver; import br.com.moip.mockkid.model.MockkidRequest; +import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -11,34 +12,52 @@ import java.io.ByteArrayInputStream; import java.io.IOException; -import static org.junit.Assert.assertEquals; - public class XMLBodyVariableResolverTest { @Mock private MockkidRequest request; + private XMLBodyVariableResolver resolver = new XMLBodyVariableResolver(); + @Before public void setUp() { MockitoAnnotations.initMocks(this); + Mockito.when(request.getHeader("content-type")).thenReturn("application/xml"); + } + + @Test + public void shouldHandle() { + assertTrue(resolver.canHandle("body.name", request)); + } + + @Test + public void shouldNotHandleVariableName() { + assertFalse(resolver.canHandle("url.url", request)); + assertFalse(resolver.canHandle("headers.authorization", request)); + } + + @Test + public void shouldNotHandleContentType() { + Mockito.when(request.getHeader("content-type")).thenReturn("text/plain"); + assertFalse(resolver.canHandle("body.name", request)); } @Test public void shouldExtractSimpleVariable() throws IOException { configureRequestWithBody("JOSE"); - assertEquals("JOSE", XMLBodyVariableResolver.extractValueFromXml("body.name", request)); + assertEquals("JOSE", resolver.extract("body.name", null, request)); } @Test public void shouldExtractVariable() throws IOException { configureRequestWithBody("
666
"); - assertEquals("666", XMLBodyVariableResolver.extractValueFromXml("body.person.address.number", request)); + assertEquals("666", resolver.extract("body.person.address.number", null, request)); } @Test public void shouldReturnNullOnUnknownVariable() throws IOException { configureRequestWithBody("
Av Paulista
"); - assertEquals(null, XMLBodyVariableResolver.extractValueFromXml("body.person.address.number", request)); + assertEquals(null, resolver.extract("body.person.address.number", null, request)); } private void configureRequestWithBody(String body) throws IOException {