Skip to content

Commit

Permalink
test (JAVA SPRING RESTCLIENT) Add echo tests for the Spring 6 RestCli…
Browse files Browse the repository at this point in the history
…ent (#19145)

* test (JAVA SPRING RESTTEMPLATE) 17571: Add echo-api multipart form single file test

* feat (JAVA SPRING RESTCLIENT) 18522: Add RestClient to README

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo all of Pet test

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo body free form object response string test

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo body pet test

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo body pet response string test

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo body string enum test

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo body tag response test

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo form tests

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo header test

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo path test

* test (JAVA SPRING RESTCLIENT) 18522: Add RestClient echo query tests

* feat (JAVA SPRING RESTCLIENT) 18522: Regenerasted API Clients for RestClient with fixed import indentation
  • Loading branch information
Nicklas2751 authored Jul 17, 2024
1 parent 0177ced commit 3dc3ee0
Show file tree
Hide file tree
Showing 12 changed files with 530 additions and 319 deletions.
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;
{{#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

0 comments on commit 3dc3ee0

Please sign in to comment.