Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test (JAVA SPRING RESTCLIENT) Add echo tests for the Spring 6 RestClient #19145

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for improving the code format

{{#withXml}}
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
{{/withXml}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.openapitools.client.ApiClient;
import org.openapitools.client.GifHttpMessageConverter;
import org.openapitools.client.OctetStreamHttpMessageConverter;
import org.openapitools.client.model.Category;
import org.openapitools.client.model.Pet;
import org.openapitools.client.model.Pet.StatusEnum;
import org.openapitools.client.model.StringEnumRef;
import org.openapitools.client.model.Tag;

Expand Down Expand Up @@ -117,11 +122,18 @@ public void testBodyMultipartFormdataArrayOfBinaryTest() throws IOException {
* <p>Test single binary in multipart mime
*/
@Test
public void testBodyMultipartFormdataSingleBinaryTest() {
File myFile = null;
String response = api.testBodyMultipartFormdataSingleBinary(myFile);
public void testBodyMultipartFormdataSingleBinaryTest() throws IOException {
// given
var testFile = Files.createTempFile("test", ".txt");
String testFileContent = "Lorem ipsum dolor sit amet";
Files.writeString(testFile, testFileContent);

// when
String response = api.testBodyMultipartFormdataSingleBinary(testFile.toFile());

// TODO: test validations
// then
assertThat(response, containsString("Content-Type: multipart/form-data"));
assertThat(response, containsString(testFileContent));
}

/**
Expand All @@ -131,10 +143,26 @@ public void testBodyMultipartFormdataSingleBinaryTest() {
*/
@Test
public void testEchoBodyAllOfPetTest() {
Pet pet = null;
// given
// The content length must be set to disable the Transfer-Encoding: chunked which would lead to
// unparsable response because the echo server is replying them as body.
api.getApiClient().addDefaultHeader("Content-Length", "192");

Pet pet =
new Pet()
.id(42L)
.name("Corgi")
.category(new Category().id(1L).name("Dogs"))
.status(StatusEnum.SOLD)
.addPhotoUrlsItem(
"https://cdn.pixabay.com/photo/2021/10/13/09/01/corgi-6705821_1280.jpg")
.addTagsItem(new Tag().id(1L).name("cute"));

// when
Pet response = api.testEchoBodyAllOfPet(pet);

// TODO: test validations
// then
assertThat(response, is(pet));
}

/**
Expand All @@ -144,10 +172,22 @@ public void testEchoBodyAllOfPetTest() {
*/
@Test
public void testEchoBodyFreeFormObjectResponseStringTest() {
Object body = null;
String response = api.testEchoBodyFreeFormObjectResponseString(body);
// given
// The content length must be set to disable the Transfer-Encoding: chunked which would lead to
// unparsable response because the echo server is replying them as body.
api.getApiClient().addDefaultHeader("Content-Length", "51");

// TODO: test validations
Object mapAsObject =
new HashMap<>(
Map.of(
"firstKey", "firstValue",
"secondKey", "secondValue"));

// when
String response = api.testEchoBodyFreeFormObjectResponseString(mapAsObject);

// then
assertThat(response, is("{\"firstKey\":\"firstValue\",\"secondKey\":\"secondValue\"}"));
}

/**
Expand All @@ -157,10 +197,26 @@ public void testEchoBodyFreeFormObjectResponseStringTest() {
*/
@Test
public void testEchoBodyPetTest() {
Pet pet = null;
// given
// The content length must be set to disable the Transfer-Encoding: chunked which would lead to
// unparsable response because the echo server is replying them as body.
api.getApiClient().addDefaultHeader("Content-Length", "192");

Pet pet =
new Pet()
.id(42L)
.name("Corgi")
.category(new Category().id(1L).name("Dogs"))
.status(StatusEnum.SOLD)
.addPhotoUrlsItem(
"https://cdn.pixabay.com/photo/2021/10/13/09/01/corgi-6705821_1280.jpg")
.addTagsItem(new Tag().id(1L).name("cute"));

// when
Pet response = api.testEchoBodyPet(pet);

// TODO: test validations
// then
assertThat(response, is(pet));
}

/**
Expand All @@ -170,10 +226,29 @@ public void testEchoBodyPetTest() {
*/
@Test
public void testEchoBodyPetResponseStringTest() {
Pet pet = null;
// given
// The content length must be set to disable the Transfer-Encoding: chunked which would lead to
// unparsable response because the echo server is replying them as body.
api.getApiClient().addDefaultHeader("Content-Length", "192");

Pet pet =
new Pet()
.id(42L)
.name("Corgi")
.category(new Category().id(1L).name("Dogs"))
.status(StatusEnum.SOLD)
.addPhotoUrlsItem(
"https://cdn.pixabay.com/photo/2021/10/13/09/01/corgi-6705821_1280.jpg")
.addTagsItem(new Tag().id(1L).name("cute"));

// when
String response = api.testEchoBodyPetResponseString(pet);

// TODO: test validations
// then
assertThat(
response,
is(
"{\"id\":42,\"name\":\"Corgi\",\"category\":{\"id\":1,\"name\":\"Dogs\"},\"photoUrls\":[\"https://cdn.pixabay.com/photo/2021/10/13/09/01/corgi-6705821_1280.jpg\"],\"tags\":[{\"id\":1,\"name\":\"cute\"}],\"status\":\"sold\"}"));
}

/**
Expand All @@ -183,10 +258,14 @@ public void testEchoBodyPetResponseStringTest() {
*/
@Test
public void testEchoBodyStringEnumTest() {
String body = null;
// given
String body = "\"failure\"";

// when
StringEnumRef response = api.testEchoBodyStringEnum(body);

// TODO: test validations
// then
assertThat(response, is(StringEnumRef.FAILURE));
}

/**
Expand All @@ -196,9 +275,13 @@ public void testEchoBodyStringEnumTest() {
*/
@Test
public void testEchoBodyTagResponseStringTest() {
// given
Tag tag = null;

// when
String response = api.testEchoBodyTagResponseString(tag);

// TODO: test validations
// then
assertThat(response, nullValue());
}
}
Loading
Loading