BAEL-3682 specify accept header to fix ambiguity of message converters in case XML converter added

This commit is contained in:
Yavuz Tas
2021-05-10 02:01:25 +02:00
parent 099b9d514f
commit e5c3f48f86
6 changed files with 56 additions and 56 deletions
@@ -24,6 +24,7 @@ import com.google.common.net.HttpHeaders;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.springframework.http.MediaType;
public abstract class AbstractBasicLiveTest<T extends Serializable> extends AbstractLiveTest<T> {
@@ -36,7 +37,7 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
@Test
public void whenResourcesAreRetrievedPaged_then200IsReceived() {
create();
final Response response = RestAssured.get(getURL() + "?page=0&size=10");
assertThat(response.getStatusCode(), is(200));
@@ -54,7 +55,8 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
create();
final Response response = RestAssured.get(getURL() + "?page=0&size=10");
final Response response = RestAssured.given()
.accept(MediaType.APPLICATION_JSON_VALUE).get(getURL() + "?page=0&size=10");
assertFalse(response.body().as(List.class).isEmpty());
}
@@ -64,7 +66,7 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
create();
create();
create();
final Response response = RestAssured.get(getURL() + "?page=0&size=2");
final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next");
@@ -95,7 +97,7 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
create();
create();
create();
final Response first = RestAssured.get(getURL() + "?page=0&size=2");
final String uriToLastPage = extractURIByRel(first.getHeader(HttpHeaders.LINK), "last");
@@ -104,7 +106,7 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next");
assertNull(uriToNextPage);
}
// etags
@Test
@@ -11,13 +11,12 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
/**
*
* We'll start the whole context, but not the server. We'll mock the REST calls instead.
*
* We'll start the whole context, but not the server. We'll mock the REST calls instead.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@@ -31,16 +30,18 @@ public class FooControllerAppIntegrationTest {
private IFooDao fooDao;
@Before
public void setup(){
public void setup() {
this.fooDao.deleteAll();
}
@Test
public void whenFindPaginatedRequest_thenEmptyResponse() throws Exception {
this.mockMvc.perform(get("/foos").param("page", "0")
.param("size", "2"))
.andExpect(status().isOk())
.andExpect(content().json("[]"));
this.mockMvc.perform(get("/foos")
.param("page", "0")
.param("size", "2")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json("[]"));
}
}
@@ -40,7 +40,7 @@ public class FooControllerCustomEtagIntegrationTest {
private static String createFooJson() throws Exception {
return serializeFoo(new Foo(randomAlphabetic(6)));
}
private static Foo deserializeFoo(String fooJson) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(fooJson, Foo.class);
@@ -97,7 +97,8 @@ public class FooControllerCustomEtagIntegrationTest {
.getResponse()
.getHeader(HttpHeaders.LOCATION);
ResultActions findOneResponse = this.mvc
.perform(get(createdResourceUri + CUSTOM_ETAG_ENDPOINT_SUFFIX).contentType(MediaType.APPLICATION_JSON));
.perform(get(createdResourceUri + CUSTOM_ETAG_ENDPOINT_SUFFIX)
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON));
String etag = findOneResponse.andReturn().getResponse().getHeader(HttpHeaders.ETAG);
Foo createdFoo = deserializeFoo(findOneResponse.andReturn().getResponse().getContentAsString());
createdFoo.setName("updated name");
@@ -20,6 +20,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -30,7 +31,7 @@ import com.baeldung.web.exception.CustomException1;
import com.baeldung.web.hateoas.event.PaginatedResultsRetrievedEvent;
/**
*
*
* We'll start only the web layer.
*
*/
@@ -54,20 +55,22 @@ public class FooControllerWebLayerIntegrationTest {
doNothing().when(publisher)
.publishEvent(any(PaginatedResultsRetrievedEvent.class));
this.mockMvc.perform(get("/foos").param("page", "0")
.param("size", "2"))
.andExpect(status().isOk())
.andExpect(jsonPath("$",Matchers.hasSize(1)));
this.mockMvc.perform(get("/foos")
.param("page", "0")
.param("size", "2")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.hasSize(1)));
}
@Test
public void delete_forException_fromService() throws Exception {
Mockito.when(service.findAll()).thenThrow(new CustomException1());
this.mockMvc.perform(get("/foos")).andDo(h -> {
final Exception expectedException = h.getResolvedException();
Assert.assertTrue(expectedException instanceof CustomException1);
});
}
}
@@ -11,6 +11,7 @@ import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -68,7 +69,9 @@ public class FooPageableLiveTest extends AbstractBasicLiveTest<Foo> {
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
create();
final Response response = RestAssured.get(getPageableURL() + "?page=0&size=10");
final Response response = RestAssured.given()
.accept(MediaType.APPLICATION_JSON_VALUE)
.get(getPageableURL() + "?page=0&size=10");
assertFalse(response.body().as(List.class).isEmpty());
}