Skip to content

Commit

Permalink
Add step definition clear all mocks and fix delete with null body
Browse files Browse the repository at this point in the history
  • Loading branch information
osvaldjr committed Jul 27, 2019
1 parent 16feb48 commit 79438be
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<groupId>io.github.osvaldjr</groupId>
<artifactId>easy-cucumber</artifactId>
<packaging>jar</packaging>
<version>0.0.8</version>
<version>0.0.9</version>
<name>io.github.osvaldjr:easy-cucumber</name>
<description>Easy Cucumber JVM Testing</description>

Expand Down
17 changes: 16 additions & 1 deletion src/main/java/io/github/osvaldjr/gateways/TargetGateway.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,7 +42,20 @@ public <T> ResponseEntity<Object> post(

public <T> ResponseEntity<Object> delete(
String host, String uri, T body, Map<String, String> headers) {
return buildClient(host).delete(uri, body, headers);
return requestDelete(host, uri, body, headers);
}

private <T> ResponseEntity<Object> requestDelete(
String host, String uri, T body, Map<String, String> headers) {
TargetClient targetClient = buildClient(host);
ResponseEntity<Object> delete;

if (ofNullable(body).isPresent()) {
delete = targetClient.delete(uri, body, headers);
} else {
delete = targetClient.delete(uri, headers);
}
return delete;
}

public <T> ResponseEntity<Object> put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ public interface TargetClient {
ResponseEntity<Object> get(@Param("url") String url, @HeaderMap Map<String, String> headers);

@RequestLine("POST {url}")
@Body("{request}")
@Body("{body}")
<R> ResponseEntity<Object> post(
@Param("url") String uri, R body, @HeaderMap Map<String, String> headers);

@RequestLine("DELETE {url}")
@Body("{request}")
@Body("{body}")
<R> ResponseEntity<Object> delete(
@Param("url") String uri, R body, @HeaderMap Map<String, String> headers);

@RequestLine("DELETE {url}")
ResponseEntity<Object> delete(@Param("url") String uri, @HeaderMap Map<String, String> headers);

@RequestLine("PUT {url}")
@Body("{request}")
@Body("{body}")
<R> ResponseEntity<Object> put(
@Param("url") String uri, R body, @HeaderMap Map<String, String> headers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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<String, Object> 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;
Expand Down Expand Up @@ -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 ([^\"]*)")
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> boolean execute(T id, Integer hits) {
return mockGateway.getMockHits(id).equals(hits);
public <T> Integer execute(T id) {
return mockGateway.getMockHits(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
5 changes: 4 additions & 1 deletion src/test/resources/features/integration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
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

0 comments on commit 79438be

Please sign in to comment.