[BAEL-18367] Moving articles from rest-spring-** pt 4

This commit is contained in:
Sjmillington
2019-12-08 10:42:40 +00:00
parent 8de37745a1
commit 05c5002b73
35 changed files with 190 additions and 562 deletions
@@ -0,0 +1,82 @@
package com.baeldung.requestmapping;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.config.MvcConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MvcConfig.class)
@WebAppConfiguration
@AutoConfigureWebClient
public class BazzNewMappingsExampleIntegrationTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void whenGettingAllBazz_thenSuccess() throws Exception{
mockMvc.perform(get("/bazz"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(4)))
.andExpect(jsonPath("$[1].id", is("2")))
.andExpect(jsonPath("$[1].name", is("Bazz2")));
}
@Test
public void whenGettingABazz_thenSuccess() throws Exception{
mockMvc.perform(get("/bazz/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is("1")))
.andExpect(jsonPath("$.name", is("Bazz1")));
}
@Test
public void whenAddingABazz_thenSuccess() throws Exception{
mockMvc.perform(post("/bazz").param("name", "Bazz5"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is("5")))
.andExpect(jsonPath("$.name", is("Bazz5")));
}
@Test
public void whenUpdatingABazz_thenSuccess() throws Exception{
mockMvc.perform(put("/bazz/5").param("name", "Bazz6"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is("5")))
.andExpect(jsonPath("$.name", is("Bazz6")));
}
@Test
public void whenDeletingABazz_thenSuccess() throws Exception{
mockMvc.perform(delete("/bazz/5"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is("5")));
}
}
@@ -0,0 +1,103 @@
package com.baeldung.responseheaders;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ResponseHeaderLiveTest {
private static final String BASE_URL = "http://localhost:8082/spring-rest";
private static final String SINGLE_BASE_URL = BASE_URL + "/single-response-header";
private static final String FILTER_BASE_URL = BASE_URL + "/filter-response-header";
private static final String SERVICE_SINGLE_RESPONSE_HEADER = "Baeldung-Example-Header";
private static final String SERVICE_FILTER_RESPONSE_HEADER = "Baeldung-Example-Filter-Header";
@Autowired
private TestRestTemplate template;
@Test
public void whenHttpServletResponseRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/http-servlet-response";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-HttpServletResponse"));
}
@Test
public void whenResponseEntityConstructorRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/response-entity-constructor";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ResponseEntityContructor"));
}
@Test
public void whenResponseEntityConstructorAndMultipleHeadersRequest_thenObtainResponseWithCorrectHeaders() {
final String requestUrl = "/response-entity-contructor-multiple-headers";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ResponseEntityConstructorAndHeaders"));
assertThat(responseHeaders).containsEntry("Accept", Arrays.asList(MediaType.APPLICATION_JSON.toString()));
}
@Test
public void whenResponseEntityBuilderRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/response-entity-builder";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ResponseEntityBuilder"));
}
@Test
public void whenResponseEntityBuilderAndHttpHeadersRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/response-entity-builder-with-http-headers";
ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ResponseEntityBuilderWithHttpHeaders"));
}
@Test
public void whenFilterWithNoExtraHeaderRequest_thenObtainResponseWithCorrectHeader() {
final String requestUrl = "/no-extra-header";
ResponseEntity<String> response = template.getForEntity(FILTER_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_FILTER_RESPONSE_HEADER, Arrays.asList("Value-Filter"));
}
@Test
public void whenFilterWithExtraHeaderRequest_thenObtainResponseWithCorrectHeaders() {
final String requestUrl = "/extra-header";
ResponseEntity<String> response = template.getForEntity(FILTER_BASE_URL + requestUrl, String.class);
HttpHeaders responseHeaders = response.getHeaders();
assertThat(responseHeaders).isNotEmpty();
assertThat(responseHeaders).containsEntry(SERVICE_FILTER_RESPONSE_HEADER, Arrays.asList("Value-Filter"));
assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ExtraHeader"));
}
}
@@ -0,0 +1,49 @@
package com.baeldung.uribuilder;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import org.junit.Test;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
public class SpringUriBuilderIntegrationTest {
@Test
public void constructUri() {
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/junit-5").build();
assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString());
}
@Test
public void constructUriEncoded() {
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/junit 5").build().encode();
assertEquals("http://www.baeldung.com/junit%205", uriComponents.toUriString());
}
@Test
public void constructUriFromTemplate() {
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/{article-name}").buildAndExpand("junit-5");
assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString());
}
@Test
public void constructUriWithQueryParameter() {
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.google.com").path("/").query("q={keyword}").buildAndExpand("baeldung");
assertEquals("http://www.google.com/?q=baeldung", uriComponents.toUriString());
}
@Test
public void expandWithRegexVar() {
String template = "/myurl/{name:[a-z]{1,5}}/show";
UriComponents uriComponents = UriComponentsBuilder.fromUriString(template).build();
uriComponents = uriComponents.expand(Collections.singletonMap("name", "test"));
assertEquals("/myurl/test/show", uriComponents.getPath());
}
}
@@ -0,0 +1,44 @@
package com.baeldung.web.controller.status;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.config.MvcConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MvcConfig.class)
@WebAppConfiguration
@AutoConfigureWebClient
public class ExampleControllerIntegrationTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void whenGetRequestSentToController_thenReturnsStatusNotAcceptable() throws Exception {
mockMvc.perform(get("/controller")).andExpect(status().isNotAcceptable());
}
@Test
public void whenGetRequestSentToException_thenReturnsStatusForbidden() throws Exception {
mockMvc.perform(get("/exception")).andExpect(status().isForbidden());
}
}