forked from citrusframework/citrus
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(citrusframework#1156): provide test api generator
- Loading branch information
Thorsten Schlathoelter
authored and
Sterchi Daniel
committed
Jul 17, 2024
1 parent
85dbe5d
commit 754115c
Showing
37 changed files
with
5,202 additions
and
40 deletions.
There are no files selected for viewing
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
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
1 change: 1 addition & 0 deletions
1
...pi-generator/citrus-test-api-generator-core/src/main/resources/java-citrus/model.mustache
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 @@ | ||
// not in use |
1 change: 1 addition & 0 deletions
1
...enerator/citrus-test-api-generator-core/src/main/resources/java-citrus/model_doc.mustache
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 @@ | ||
# not in use |
254 changes: 254 additions & 0 deletions
254
...pi-generator-core/src/test/java/org/citrusframework/openapi/generator/GetPetByIdTest.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,254 @@ | ||
package org.citrusframework.openapi.generator; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.citrusframework.container.Assert.Builder.assertException; | ||
import static org.citrusframework.util.FileUtils.readToString; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import org.citrusframework.TestCaseRunner; | ||
import org.citrusframework.annotations.CitrusResource; | ||
import org.citrusframework.annotations.CitrusTest; | ||
import org.citrusframework.config.CitrusSpringConfig; | ||
import org.citrusframework.context.TestContext; | ||
import org.citrusframework.endpoint.EndpointConfiguration; | ||
import org.citrusframework.http.client.HttpClient; | ||
import org.citrusframework.http.client.HttpEndpointConfiguration; | ||
import org.citrusframework.junit.jupiter.spring.CitrusSpringExtension; | ||
import org.citrusframework.message.DefaultMessage; | ||
import org.citrusframework.message.Message; | ||
import org.citrusframework.messaging.Producer; | ||
import org.citrusframework.messaging.SelectiveConsumer; | ||
import org.citrusframework.openapi.generator.GetPetByIdTest.Config; | ||
import org.citrusframework.openapi.generator.rest.petstore.request.PetApi.GetPetByIdRequest; | ||
import org.citrusframework.openapi.generator.rest.petstore.spring.PetStoreBeanConfiguration; | ||
import org.citrusframework.spi.Resources; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@ExtendWith(CitrusSpringExtension.class) | ||
@SpringBootTest(classes = {PetStoreBeanConfiguration.class, CitrusSpringConfig.class, Config.class}) | ||
class GetPetByIdTest { | ||
|
||
@Autowired | ||
private GetPetByIdRequest getPetByIdRequest; | ||
|
||
@Autowired | ||
@Qualifier("petStoreEndpoint") | ||
private HttpClient httpClient; | ||
|
||
private String defaultResponse; | ||
|
||
@BeforeEach | ||
public void beforeTest() throws IOException { | ||
defaultResponse = readToString(Resources.create( | ||
"org/citrusframework/openapi/generator/GeneratedApiTest/payloads/getPetByIdControlMessage1.json"), | ||
StandardCharsets.UTF_8) ; | ||
|
||
mockProducer(); | ||
mockConsumer(); | ||
} | ||
|
||
/** | ||
* TODO #1161 - Improve with builder pattern | ||
*/ | ||
@Test | ||
@CitrusTest | ||
void testByJsonPath(@CitrusResource TestCaseRunner runner) { | ||
|
||
// Given | ||
getPetByIdRequest.setPetId("1234"); | ||
|
||
// Then | ||
getPetByIdRequest.setResponseStatus(HttpStatus.OK.value()); | ||
getPetByIdRequest.setResponseReasonPhrase(HttpStatus.OK.getReasonPhrase()); | ||
|
||
// Assert body by json path | ||
getPetByIdRequest.setResponseValue(Map.of("$.name", "Snoopy")); | ||
|
||
// When | ||
runner.$(getPetByIdRequest); | ||
} | ||
|
||
/** | ||
* TODO #1161 - Improve with builder pattern | ||
*/ | ||
@Test | ||
@CitrusTest | ||
void testValidationFailureByJsonPath(@CitrusResource TestCaseRunner runner) { | ||
|
||
// Given | ||
getPetByIdRequest.setPetId("1234"); | ||
|
||
// Then | ||
getPetByIdRequest.setResponseStatus(HttpStatus.OK.value()); | ||
getPetByIdRequest.setResponseReasonPhrase(HttpStatus.OK.getReasonPhrase()); | ||
|
||
// Assert body by json path | ||
getPetByIdRequest.setResponseValue(Map.of("$.name", "Garfield")); | ||
|
||
// When | ||
runner.$(assertException() | ||
.exception(org.citrusframework.exceptions.CitrusRuntimeException.class) | ||
.message("Values not equal for element '$.name', expected 'Garfield' but was 'Snoopy'") | ||
.when( | ||
getPetByIdRequest | ||
) | ||
); | ||
// When | ||
|
||
} | ||
|
||
/** | ||
* TODO #1161 - Improve with builder pattern | ||
*/ | ||
@Test | ||
@CitrusTest | ||
void testByResource(@CitrusResource TestCaseRunner runner) { | ||
|
||
// Given | ||
getPetByIdRequest.setPetId("1234"); | ||
|
||
// Then | ||
getPetByIdRequest.setResponseStatus(HttpStatus.OK.value()); | ||
getPetByIdRequest.setResponseReasonPhrase(HttpStatus.OK.getReasonPhrase()); | ||
// Assert body by resource | ||
getPetByIdRequest.setResource( | ||
"org/citrusframework/openapi/generator/GeneratedApiTest/payloads/getPetByIdControlMessage1.json"); | ||
|
||
// When | ||
runner.$(getPetByIdRequest); | ||
} | ||
|
||
/** | ||
* TODO #1161 - Improve with builder pattern | ||
*/ | ||
@Test | ||
@CitrusTest | ||
void testValidationFailureByResource(@CitrusResource TestCaseRunner runner) { | ||
|
||
// Given | ||
getPetByIdRequest.setPetId("1234"); | ||
|
||
// Then | ||
getPetByIdRequest.setResponseStatus(HttpStatus.OK.value()); | ||
getPetByIdRequest.setResponseReasonPhrase(HttpStatus.OK.getReasonPhrase()); | ||
// Assert body by resource | ||
getPetByIdRequest.setResource( | ||
"org/citrusframework/openapi/generator/GeneratedApiTest/payloads/getPetByIdControlMessage2.json"); | ||
|
||
// When | ||
runner.$(assertException() | ||
.exception(org.citrusframework.exceptions.CitrusRuntimeException.class) | ||
.message("Values not equal for entry: '$['name']', expected 'Garfield' but was 'Snoopy'") | ||
.when( | ||
getPetByIdRequest | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* TODO #1161 - Improve with builder pattern | ||
*/ | ||
@Test | ||
@CitrusTest | ||
void validateByVariable(@CitrusResource TestContext testContext, | ||
@CitrusResource TestCaseRunner runner) { | ||
|
||
// Given | ||
getPetByIdRequest.setPetId("1234"); | ||
|
||
// Then | ||
getPetByIdRequest.setResponseStatus(HttpStatus.OK.value()); | ||
getPetByIdRequest.setResponseReasonPhrase(HttpStatus.OK.getReasonPhrase()); | ||
|
||
// Assert load data into variables | ||
getPetByIdRequest.setResponseVariable(Map.of("$", "RESPONSE", "$.id", "ID")); | ||
|
||
// When | ||
runner.$(getPetByIdRequest); | ||
|
||
// Then | ||
assertThat(testContext) | ||
.satisfies( | ||
c -> assertThat(c.getVariable("RESPONSE")) | ||
.isNotNull(), | ||
c -> assertThat(c.getVariable("ID")) | ||
.isNotNull() | ||
.isEqualTo("12") | ||
); | ||
} | ||
|
||
/** | ||
* TODO #1161 - Improve with builder pattern | ||
*/ | ||
@Test | ||
@CitrusTest | ||
void validateReceivedResponse(@CitrusResource TestContext testContext) { | ||
|
||
// Given | ||
getPetByIdRequest.setPetId("1234"); | ||
|
||
// When | ||
getPetByIdRequest.sendRequest(testContext); | ||
|
||
// Then | ||
Message receiveResponse = getPetByIdRequest.receiveResponse(testContext); | ||
assertThat(receiveResponse) | ||
.isNotNull() | ||
.extracting(Message::getPayload) | ||
.asString() | ||
.isEqualToIgnoringWhitespace(defaultResponse); | ||
assertThat(receiveResponse.getHeaders()) | ||
.containsEntry("citrus_http_status_code", 200) | ||
.containsEntry("citrus_http_reason_phrase", "OK"); | ||
} | ||
|
||
private void mockProducer() { | ||
Producer producerMock = mock(); | ||
when(httpClient.createProducer()).thenReturn(producerMock); | ||
} | ||
|
||
private void mockConsumer() { | ||
Message receiveMessage = createReceiveMessage(); | ||
|
||
SelectiveConsumer consumer = mock(SelectiveConsumer.class); | ||
when(httpClient.createConsumer()).thenReturn(consumer); | ||
when(consumer.receive(any(), eq(5000L))).thenReturn(receiveMessage); | ||
} | ||
|
||
private Message createReceiveMessage() { | ||
Message receiveMessage = new DefaultMessage(); | ||
receiveMessage.setPayload(defaultResponse); | ||
receiveMessage.getHeaders().put("citrus_http_reason_phrase", "OK"); | ||
receiveMessage.getHeaders().put("citrus_http_version", "HTTP/1.1"); | ||
receiveMessage.getHeaders().put("Content-Type", 200); | ||
receiveMessage.getHeaders().put("citrus_http_status_code", 200); | ||
return receiveMessage; | ||
} | ||
|
||
@TestConfiguration | ||
public static class Config { | ||
|
||
@Bean(name = {"applicationServiceClient", "petStoreEndpoint"}) | ||
public HttpClient applicationServiceClient() { | ||
HttpClient client = mock(HttpClient.class); | ||
EndpointConfiguration endpointConfiguration = mock(EndpointConfiguration.class); | ||
when(client.getEndpointConfiguration()).thenReturn(new HttpEndpointConfiguration()); | ||
when(endpointConfiguration.getTimeout()).thenReturn(5000L); | ||
return client; | ||
} | ||
} | ||
} |
Oops, something went wrong.