From 79438be5a4ff8d8c4ab48da715ce42c1c45cd735 Mon Sep 17 00:00:00 2001 From: osvaldjr Date: Sat, 27 Jul 2019 12:38:28 -0300 Subject: [PATCH] Add step definition clear all mocks and fix delete with null body --- pom.xml | 2 +- .../osvaldjr/gateways/TargetGateway.java | 17 +++++++++++++- .../osvaldjr/gateways/feign/TargetClient.java | 9 +++++--- .../stepdefinitions/steps/DefaultSteps.java | 22 +++++++++++++------ ...erUseCase.java => GetMockHitsUseCase.java} | 8 +++---- .../unit/gateways/TargetGatewayTest.java | 19 ++++++++++++++++ ...eTest.java => GetMockHitsUseCaseTest.java} | 10 ++++----- .../resources/features/integration.feature | 5 ++++- 8 files changed, 70 insertions(+), 22 deletions(-) rename src/main/java/io/github/osvaldjr/usecases/{HitsMatcherUseCase.java => GetMockHitsUseCase.java} (61%) rename src/test/java/io/github/osvaldjr/unit/usecases/{HitsMatcherUseCaseTest.java => GetMockHitsUseCaseTest.java} (74%) diff --git a/pom.xml b/pom.xml index da9aa10..f54182f 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ io.github.osvaldjr easy-cucumber jar - 0.0.8 + 0.0.9 io.github.osvaldjr:easy-cucumber Easy Cucumber JVM Testing diff --git a/src/main/java/io/github/osvaldjr/gateways/TargetGateway.java b/src/main/java/io/github/osvaldjr/gateways/TargetGateway.java index 886474e..2a944fd 100644 --- a/src/main/java/io/github/osvaldjr/gateways/TargetGateway.java +++ b/src/main/java/io/github/osvaldjr/gateways/TargetGateway.java @@ -1,5 +1,7 @@ package io.github.osvaldjr.gateways; +import static java.util.Optional.ofNullable; + import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; @@ -40,7 +42,20 @@ public ResponseEntity post( public ResponseEntity delete( String host, String uri, T body, Map headers) { - return buildClient(host).delete(uri, body, headers); + return requestDelete(host, uri, body, headers); + } + + private ResponseEntity requestDelete( + String host, String uri, T body, Map headers) { + TargetClient targetClient = buildClient(host); + ResponseEntity delete; + + if (ofNullable(body).isPresent()) { + delete = targetClient.delete(uri, body, headers); + } else { + delete = targetClient.delete(uri, headers); + } + return delete; } public ResponseEntity put( diff --git a/src/main/java/io/github/osvaldjr/gateways/feign/TargetClient.java b/src/main/java/io/github/osvaldjr/gateways/feign/TargetClient.java index b72e193..6ba9069 100644 --- a/src/main/java/io/github/osvaldjr/gateways/feign/TargetClient.java +++ b/src/main/java/io/github/osvaldjr/gateways/feign/TargetClient.java @@ -17,17 +17,20 @@ public interface TargetClient { ResponseEntity get(@Param("url") String url, @HeaderMap Map headers); @RequestLine("POST {url}") - @Body("{request}") + @Body("{body}") ResponseEntity post( @Param("url") String uri, R body, @HeaderMap Map headers); @RequestLine("DELETE {url}") - @Body("{request}") + @Body("{body}") ResponseEntity delete( @Param("url") String uri, R body, @HeaderMap Map headers); + @RequestLine("DELETE {url}") + ResponseEntity delete(@Param("url") String uri, @HeaderMap Map headers); + @RequestLine("PUT {url}") - @Body("{request}") + @Body("{body}") ResponseEntity put( @Param("url") String uri, R body, @HeaderMap Map headers); } diff --git a/src/main/java/io/github/osvaldjr/stepdefinitions/steps/DefaultSteps.java b/src/main/java/io/github/osvaldjr/stepdefinitions/steps/DefaultSteps.java index 12ae695..c9c3662 100644 --- a/src/main/java/io/github/osvaldjr/stepdefinitions/steps/DefaultSteps.java +++ b/src/main/java/io/github/osvaldjr/stepdefinitions/steps/DefaultSteps.java @@ -3,7 +3,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.HashMap; @@ -28,8 +27,9 @@ import io.github.osvaldjr.domains.TargetRequest; import io.github.osvaldjr.domains.exceptions.FeignException; import io.github.osvaldjr.gateways.FileGateway; +import io.github.osvaldjr.gateways.mock.MockGateway; import io.github.osvaldjr.usecases.CreateStubbyUseCase; -import io.github.osvaldjr.usecases.HitsMatcherUseCase; +import io.github.osvaldjr.usecases.GetMockHitsUseCase; import io.github.osvaldjr.usecases.RequestTargetUseCase; public class DefaultSteps extends Steps { @@ -41,20 +41,23 @@ public class DefaultSteps extends Steps { private final FileGateway fileGateway; private final RequestTargetUseCase requestTargetUseCase; private final CreateStubbyUseCase createStubbyUsecase; - private final HitsMatcherUseCase hitsMatcherUsecase; + private final GetMockHitsUseCase getMockHitsUsecase; private FeignException httpException; private Map stubbyIdMap; + private final MockGateway mockGateway; @Autowired public DefaultSteps( FileGateway fileGateway, RequestTargetUseCase requestTargetUseCase, CreateStubbyUseCase createStubbyUsecase, - HitsMatcherUseCase hitsMatcherUsecase) { + GetMockHitsUseCase getMockHitsUsecase, + MockGateway mockGateway) { this.fileGateway = fileGateway; this.requestTargetUseCase = requestTargetUseCase; this.createStubbyUsecase = createStubbyUsecase; - this.hitsMatcherUsecase = hitsMatcherUsecase; + this.getMockHitsUsecase = getMockHitsUsecase; + this.mockGateway = mockGateway; request = new TargetRequest<>(); stubbyIdMap = new HashMap<>(); httpException = null; @@ -100,11 +103,11 @@ public void aHaveAMockForDependency(String mockName, String serviceName) throws @Then("I expect mock ([^\"]*) for dependency ([^\"]*) to have been called (\\d+) times") public void iExpectMockForDependencyToHaveBeenCalledTimes( - String mockName, String serviceName, int times) { + String mockName, String serviceName, Integer times) { String mapKey = getStubbyKey(scenarioName, serviceName, mockName); Object stubbyId = stubbyIdMap.get(mapKey); - assertTrue(hitsMatcherUsecase.execute(stubbyId, times)); + assertThat(getMockHitsUsecase.execute(stubbyId), equalTo(times)); } @Given("I make a request defined in ([^\"]*)") @@ -173,6 +176,11 @@ public void responseDoesNotContainProperty(String jsonPath) { Assert.assertTrue(pathNotFound); } + @Given("I clear all mocks") + public void iClearAllMocks() { + mockGateway.deleteAllServices(); + } + private String getStubbyKey(String scenario, String serviceName, String mockName) { return scenario + serviceName + mockName; } diff --git a/src/main/java/io/github/osvaldjr/usecases/HitsMatcherUseCase.java b/src/main/java/io/github/osvaldjr/usecases/GetMockHitsUseCase.java similarity index 61% rename from src/main/java/io/github/osvaldjr/usecases/HitsMatcherUseCase.java rename to src/main/java/io/github/osvaldjr/usecases/GetMockHitsUseCase.java index e87df7a..86aa262 100644 --- a/src/main/java/io/github/osvaldjr/usecases/HitsMatcherUseCase.java +++ b/src/main/java/io/github/osvaldjr/usecases/GetMockHitsUseCase.java @@ -6,16 +6,16 @@ import io.github.osvaldjr.gateways.mock.MockGateway; @Component -public class HitsMatcherUseCase { +public class GetMockHitsUseCase { private MockGateway mockGateway; @Autowired - public HitsMatcherUseCase(MockGateway mockGateway) { + public GetMockHitsUseCase(MockGateway mockGateway) { this.mockGateway = mockGateway; } - public boolean execute(T id, Integer hits) { - return mockGateway.getMockHits(id).equals(hits); + public Integer execute(T id) { + return mockGateway.getMockHits(id); } } diff --git a/src/test/java/io/github/osvaldjr/unit/gateways/TargetGatewayTest.java b/src/test/java/io/github/osvaldjr/unit/gateways/TargetGatewayTest.java index 84e8bdf..5983f07 100644 --- a/src/test/java/io/github/osvaldjr/unit/gateways/TargetGatewayTest.java +++ b/src/test/java/io/github/osvaldjr/unit/gateways/TargetGatewayTest.java @@ -2,8 +2,10 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -86,6 +88,23 @@ void shouldDelete( ResponseEntity response = targetGateway.delete(host, uri, body, headers); verify(feignClient, times(1)).delete(uri, body, headers); + verify(feignClient, never()).delete(any(), any()); + verify(feignBuilder, times(1)).target(TargetClient.class, host); + assertThat(response, equalTo(responseEntity)); + } + + @Test + void shouldDeleteWithNullBody( + @Random String host, + @Random String uri, + @Random Map headers, + @Random ResponseEntity responseEntity) { + when(feignClient.delete(uri, headers)).thenReturn(responseEntity); + + ResponseEntity response = targetGateway.delete(host, uri, null, headers); + + verify(feignClient, times(1)).delete(uri, headers); + verify(feignClient, never()).delete(any(), any(), any()); verify(feignBuilder, times(1)).target(TargetClient.class, host); assertThat(response, equalTo(responseEntity)); } diff --git a/src/test/java/io/github/osvaldjr/unit/usecases/HitsMatcherUseCaseTest.java b/src/test/java/io/github/osvaldjr/unit/usecases/GetMockHitsUseCaseTest.java similarity index 74% rename from src/test/java/io/github/osvaldjr/unit/usecases/HitsMatcherUseCaseTest.java rename to src/test/java/io/github/osvaldjr/unit/usecases/GetMockHitsUseCaseTest.java index d1bf7f2..c9b1974 100644 --- a/src/test/java/io/github/osvaldjr/unit/usecases/HitsMatcherUseCaseTest.java +++ b/src/test/java/io/github/osvaldjr/unit/usecases/GetMockHitsUseCaseTest.java @@ -13,21 +13,21 @@ import io.github.glytching.junit.extension.random.Random; import io.github.osvaldjr.gateways.mock.MockGateway; import io.github.osvaldjr.unit.UnitTest; -import io.github.osvaldjr.usecases.HitsMatcherUseCase; +import io.github.osvaldjr.usecases.GetMockHitsUseCase; -class HitsMatcherUseCaseTest extends UnitTest { +class GetMockHitsUseCaseTest extends UnitTest { @Mock private MockGateway mockGateway; - @InjectMocks private HitsMatcherUseCase hitsMatcherUsecase; + @InjectMocks private GetMockHitsUseCase getMockHitsUsecase; @Test void shouldExecute(@Random String id) { Integer hits = 10; when(mockGateway.getMockHits(id)).thenReturn(hits); - boolean match = hitsMatcherUsecase.execute(id, hits); + Integer match = getMockHitsUsecase.execute(id); - Assertions.assertTrue(match); + Assertions.assertEquals(hits, match); verify(mockGateway, times(1)).getMockHits(anyString()); } } diff --git a/src/test/resources/features/integration.feature b/src/test/resources/features/integration.feature index 462941e..83e2593 100644 --- a/src/test/resources/features/integration.feature +++ b/src/test/resources/features/integration.feature @@ -53,4 +53,7 @@ Feature: Integration Then I make a GET to / Then I expect to receive a 200 status with body http_get_body_complete_response.json Then response contains property [0].request.json.name with value Linux - Then response does not contain property [1]updatedDate \ No newline at end of file + Then response does not contain property [1]updatedDate + Then I clear all mocks + And I make a GET to / + And I expect to receive a 204 status