-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from allegro/simple-wiremock-test
Add /emails/{id} resource and corresponding test to part2.2-rest
- Loading branch information
Showing
22 changed files
with
248 additions
and
68 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
.../src/main/groovy/pl/allegro/tech/workshops/testsparallelexecution/BaseTestWithRest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...rest/src/main/java/pl/allegro/tech/workshops/testsparallelexecution/email/rest/Email.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package pl.allegro.tech.workshops.testsparallelexecution.email.rest; | ||
|
||
|
||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record Email(String subject, @NotBlank String sender, String recipient) { | ||
|
||
static public Email of(String subject, String sender, String recipient) { | ||
return new Email(subject, sender, recipient); | ||
} | ||
|
||
} |
3 changes: 2 additions & 1 deletion
3
...rc/main/java/pl/allegro/tech/workshops/testsparallelexecution/email/rest/EmailClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package pl.allegro.tech.workshops.testsparallelexecution.email.rest; | ||
|
||
public interface EmailClient { | ||
void send(EmailRequest email); | ||
void send(Email email); | ||
|
||
Email read(String id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
...c/main/java/pl/allegro/tech/workshops/testsparallelexecution/email/rest/EmailRequest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...y/pl/allegro/tech/workshops/testsparallelexecution/email/rest/GetEmailResourceTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package pl.allegro.tech.workshops.testsparallelexecution.email.rest | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.stubbing.Scenario | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.context.annotation.Import | ||
import org.springframework.http.ProblemDetail | ||
import pl.allegro.tech.workshops.testsparallelexecution.BaseTestWithRest | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse | ||
import static com.github.tomakehurst.wiremock.client.WireMock.get | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching | ||
import static com.github.tomakehurst.wiremock.http.Fault.CONNECTION_RESET_BY_PEER | ||
import static com.github.tomakehurst.wiremock.http.Fault.EMPTY_RESPONSE | ||
import static org.springframework.http.HttpHeaders.CONTENT_TYPE | ||
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR | ||
import static org.springframework.http.HttpStatus.OK | ||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE | ||
|
||
@Import(WiremockConfig) | ||
class GetEmailResourceTest extends BaseTestWithRest implements WiremockPortSupport { | ||
|
||
@Autowired | ||
private WireMockServer wiremockServer | ||
|
||
private static final String VALID_BODY = """{"subject": "test subject", "sender": "[email protected]", "recipient": "[email protected]"}""" | ||
|
||
private String emailId = "2" | ||
|
||
def cleanup() { | ||
wiremockServer.resetAll() | ||
wiremockServer.resetScenarios() | ||
} | ||
|
||
def "get e-mail"() { | ||
given: | ||
wiremockServer.stubFor(get(urlPathMatching("/external-api-service/emails/.*")) | ||
.willReturn(aResponse() | ||
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) | ||
.withStatus(200) | ||
.withBody("""{"subject": "test subject", "sender": "[email protected]", "recipient": "[email protected]"}""") | ||
) | ||
) | ||
|
||
when: | ||
def result = restClient.get("/emails/$emailId", Email) | ||
|
||
then: | ||
result.statusCode == OK | ||
result.body == Email.of("test subject", "[email protected]", "[email protected]") | ||
} | ||
|
||
def "handle email service errors (status=#errorResponse.status, fault=#errorResponse.fault, delay=#errorResponse.fixedDelayMilliseconds)"() { | ||
given: | ||
wiremockServer.stubFor(get(urlPathMatching("/external-api-service/emails/.*")) | ||
.willReturn(errorResponse) | ||
) | ||
when: | ||
def result = restClient.get("/emails/$emailId", ProblemDetail) | ||
then: | ||
result.statusCode == INTERNAL_SERVER_ERROR | ||
result.body.detail.contains expectedDetail | ||
where: | ||
errorResponse || expectedDetail | ||
aResponse().withStatus(400) || "400 Bad Request" | ||
aResponse().withStatus(500) || "500 Server Error" | ||
aResponse().withFault(EMPTY_RESPONSE) || "Unexpected end of file from server" | ||
aResponse().withFault(CONNECTION_RESET_BY_PEER) || "Connection reset" | ||
aResponse().withFixedDelay(1000) | ||
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) | ||
.withStatus(200) | ||
.withBody(VALID_BODY) || "Read timed out" | ||
} | ||
def "retry email fetching after error response (status=#errorResponse.status, fault=#errorResponse.fault, delay=#errorResponse.fixedDelayMilliseconds)"() { | ||
given: | ||
wiremockServer.stubFor(get(urlPathMatching("/external-api-service/emails/.*")) | ||
.willReturn(errorResponse) | ||
.inScenario("retry scenario") | ||
.whenScenarioStateIs(Scenario.STARTED) | ||
.willSetStateTo('after error') | ||
) | ||
wiremockServer.stubFor(get(urlPathMatching("/external-api-service/emails/.*")) | ||
.willReturn(aResponse() | ||
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) | ||
.withStatus(200) | ||
.withBody("""{"subject": "test subject", "sender": "[email protected]", "recipient": "[email protected]"}""") | ||
) | ||
.inScenario("retry scenario") | ||
.whenScenarioStateIs('after error') | ||
.willSetStateTo('after ok') | ||
) | ||
when: | ||
def result = restClient.get("/emails/$emailId", Email) | ||
then: | ||
result.statusCode == OK | ||
result.body == Email.of("test subject", "[email protected]", "[email protected]") | ||
where: | ||
errorResponse << [ | ||
aResponse().withStatus(400), | ||
aResponse().withStatus(500), | ||
aResponse().withFault(EMPTY_RESPONSE), | ||
aResponse().withFault(CONNECTION_RESET_BY_PEER), | ||
aResponse().withFixedDelay(1000) | ||
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) | ||
.withStatus(200) | ||
.withBody(VALID_BODY) | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,16 @@ package pl.allegro.tech.workshops.testsparallelexecution.email.rest | |
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.stubbing.Scenario | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.context.annotation.Import | ||
import org.springframework.http.ProblemDetail | ||
import pl.allegro.tech.workshops.testsparallelexecution.BaseTestWithRest | ||
import spock.lang.Shared | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse | ||
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo | ||
import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath | ||
import static com.github.tomakehurst.wiremock.client.WireMock.post | ||
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo | ||
import static com.github.tomakehurst.wiremock.http.Fault.CONNECTION_RESET_BY_PEER | ||
import static com.github.tomakehurst.wiremock.http.Fault.EMPTY_RESPONSE | ||
import static org.springframework.http.HttpHeaders.ACCEPT | ||
|
@@ -39,31 +39,23 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE | |
} | ||
</pre> | ||
*/ | ||
class EmailsByRestResourceTest extends BaseTestWithRest { | ||
@Import(WiremockConfig) | ||
class SendEmailResourceTest extends BaseTestWithRest implements WiremockPortSupport { | ||
|
||
@Shared | ||
WireMockServer wiremockServer | ||
@Autowired | ||
private WireMockServer wiremockServer | ||
|
||
private String subject = "New workshops!" | ||
|
||
def setupSpec() { | ||
wiremockServer = new WireMockServer(8099) | ||
wiremockServer.start() | ||
} | ||
|
||
def cleanupSpec() { | ||
wiremockServer.stop() | ||
} | ||
|
||
def cleanup() { | ||
wiremockServer.resetAll() | ||
wiremockServer.resetScenarios() | ||
} | ||
|
||
def "send e-mail"() { | ||
given: | ||
def email = EmailRequest.of(subject, "[email protected]", "[email protected]") | ||
wiremockServer.stubFor(post(urlEqualTo("/external-api-service/emails")) | ||
def email = Email.of(subject, "[email protected]", "[email protected]") | ||
wiremockServer.stubFor(post(urlPathEqualTo("/external-api-service/emails")) | ||
.willReturn(aResponse() | ||
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) | ||
.withStatus(200) | ||
|
@@ -75,15 +67,15 @@ class EmailsByRestResourceTest extends BaseTestWithRest { | |
|
||
then: | ||
result.statusCode == OK | ||
wiremockServer.verify(1, postRequestedFor(urlEqualTo("/external-api-service/emails")) | ||
wiremockServer.verify(1, postRequestedFor(urlPathEqualTo("/external-api-service/emails")) | ||
.withHeader(ACCEPT, equalTo("application/json, application/*+json")) | ||
) | ||
} | ||
|
||
def "do not sent email without sender"() { | ||
given: | ||
def email = EmailRequest.of(subject, sender, "[email protected]") | ||
wiremockServer.stubFor(post(urlEqualTo("/external-api-service/emails")) | ||
def email = Email.of(subject, sender, "[email protected]") | ||
wiremockServer.stubFor(post(urlPathEqualTo("/external-api-service/emails")) | ||
.willReturn(aResponse() | ||
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) | ||
.withStatus(200) | ||
|
@@ -97,7 +89,7 @@ class EmailsByRestResourceTest extends BaseTestWithRest { | |
|
||
then: | ||
result.statusCode == BAD_REQUEST | ||
wiremockServer.verify(0, postRequestedFor(urlEqualTo("/external-api-service/emails")) | ||
wiremockServer.verify(0, postRequestedFor(urlPathEqualTo("/external-api-service/emails")) | ||
.withHeader(ACCEPT, equalTo("application/json, application/*+json")) | ||
) | ||
|
||
|
@@ -106,8 +98,9 @@ class EmailsByRestResourceTest extends BaseTestWithRest { | |
} | ||
|
||
def "handle email service errors (status=#errorResponse.status, fault=#errorResponse.fault, delay=#errorResponse.fixedDelayMilliseconds)"() { | ||
def email = EmailRequest.of(subject, "from@example.com", "[email protected]") | ||
wiremockServer.stubFor(post(urlEqualTo("/external-api-service/emails")) | ||
given: | ||
def email = Email.of(subject, "from@example.com", "[email protected]") | ||
wiremockServer.stubFor(post(urlPathEqualTo("/external-api-service/emails")) | ||
.willReturn(errorResponse) | ||
) | ||
|
||
|
@@ -130,14 +123,15 @@ class EmailsByRestResourceTest extends BaseTestWithRest { | |
} | ||
|
||
def "retry email sending after error response (status=#errorResponse.status, fault=#errorResponse.fault, delay=#errorResponse.fixedDelayMilliseconds)"() { | ||
def email = EmailRequest.of(subject, "from@example.com", "[email protected]") | ||
wiremockServer.stubFor(post(urlEqualTo("/external-api-service/emails")) | ||
given: | ||
def email = Email.of(subject, "from@example.com", "[email protected]") | ||
wiremockServer.stubFor(post(urlPathEqualTo("/external-api-service/emails")) | ||
.willReturn(errorResponse) | ||
.inScenario("retry scenario") | ||
.whenScenarioStateIs(Scenario.STARTED) | ||
.willSetStateTo('after error') | ||
) | ||
wiremockServer.stubFor(post(urlEqualTo("/external-api-service/emails")) | ||
wiremockServer.stubFor(post(urlPathEqualTo("/external-api-service/emails")) | ||
.willReturn(aResponse() | ||
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) | ||
.withStatus(200) | ||
|
17 changes: 17 additions & 0 deletions
17
.../groovy/pl/allegro/tech/workshops/testsparallelexecution/email/rest/WiremockConfig.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package pl.allegro.tech.workshops.testsparallelexecution.email.rest | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.boot.test.context.TestConfiguration | ||
import org.springframework.context.annotation.Bean | ||
|
||
@TestConfiguration | ||
class WiremockConfig { | ||
|
||
@Bean | ||
WireMockServer getWiremockServer(@Value('${wiremock.port}') int wiremockPort) { | ||
return new WireMockServer(wiremockPort).tap { | ||
it.start() | ||
} | ||
} | ||
} |
Oops, something went wrong.