Polishing.

Original pull request: #620.
See #583.
This commit is contained in:
Mark Paluch
2021-04-30 08:45:43 +02:00
parent ce39a53448
commit 53ee0fe2b5
5 changed files with 40 additions and 31 deletions

View File

@@ -16,12 +16,13 @@
package example.springdata.rest.projections; package example.springdata.rest.projections;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory; import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
@@ -30,13 +31,14 @@ import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
* Test cases showing the programatic use of a {@link ProjectionFactory}. * Test cases showing the programatic use of a {@link ProjectionFactory}.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Divya Srivastava
*/ */
public class SimpleProjectionTests { class SimpleProjectionTests {
ProjectionFactory factory = new SpelAwareProxyProjectionFactory(); private ProjectionFactory factory = new SpelAwareProxyProjectionFactory();
@Test @Test
public void createMapBackedProjection() { void createMapBackedProjection() {
Customer customer = factory.createProjection(Customer.class); Customer customer = factory.createProjection(Customer.class);
customer.setFirstname("Dave"); customer.setFirstname("Dave");
@@ -51,7 +53,7 @@ public class SimpleProjectionTests {
} }
@Test @Test
public void createsProxyForSourceMap() { void createsProxyForSourceMap() {
Map<String, Object> backingMap = new HashMap<>(); Map<String, Object> backingMap = new HashMap<>();
backingMap.put("firstname", "Dave"); backingMap.put("firstname", "Dave");

View File

@@ -15,10 +15,11 @@
*/ */
package example.springdata.rest.security; package example.springdata.rest.security;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
@@ -30,19 +31,20 @@ import org.springframework.security.core.context.SecurityContextHolder;
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Oliver Gierke * @author Oliver Gierke
* @author Divya Srivastava
*/ */
@SpringBootTest @SpringBootTest
public class MethodLevelSecurityTests { class MethodLevelSecurityTests {
@Autowired ItemRepository itemRepository; @Autowired ItemRepository itemRepository;
@BeforeEach @BeforeEach
public void setUp() { void setUp() {
SecurityContextHolder.clearContext(); SecurityContextHolder.clearContext();
} }
@Test @Test
public void rejectsMethodInvocationsForNoAuth() { void rejectsMethodInvocationsForNoAuth() {
try { try {
itemRepository.findAll(); itemRepository.findAll();
@@ -67,7 +69,7 @@ public class MethodLevelSecurityTests {
} }
@Test @Test
public void rejectsMethodInvocationsForAuthWithInsufficientPermissions() { void rejectsMethodInvocationsForAuthWithInsufficientPermissions() {
SecurityUtils.runAs("system", "system", "ROLE_USER"); SecurityUtils.runAs("system", "system", "ROLE_USER");
@@ -88,7 +90,7 @@ public class MethodLevelSecurityTests {
} }
@Test @Test
public void allowsMethodInvocationsForAuthWithSufficientPermissions() { void allowsMethodInvocationsForAuthWithSufficientPermissions() {
SecurityUtils.runAs("system", "system", "ROLE_USER", "ROLE_ADMIN"); SecurityUtils.runAs("system", "system", "ROLE_USER", "ROLE_ADMIN");

View File

@@ -16,7 +16,7 @@
package example.springdata.rest.security; package example.springdata.rest.security;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
@@ -25,6 +25,7 @@ import java.util.Base64;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.hateoas.MediaTypes; import org.springframework.hateoas.MediaTypes;
@@ -42,19 +43,21 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Oliver Gierke * @author Oliver Gierke
* @author Divya Srivastava
*/ */
@SpringBootTest @SpringBootTest
public class UrlLevelSecurityTests { class UrlLevelSecurityTests {
static final String PAYLOAD = "{\"firstName\": \"Saruman\", \"lastName\": \"the White\", " + "\"title\": \"Wizard\"}"; private static final String PAYLOAD = "{\"firstName\": \"Saruman\", \"lastName\": \"the White\", "
+ "\"title\": \"Wizard\"}";
@Autowired WebApplicationContext context; @Autowired WebApplicationContext context;
@Autowired FilterChainProxy filterChain; @Autowired FilterChainProxy filterChain;
MockMvc mvc; private MockMvc mvc;
@BeforeEach @BeforeEach
public void setUp() { void setUp() {
this.mvc = webAppContextSetup(context).addFilters(filterChain).build(); this.mvc = webAppContextSetup(context).addFilters(filterChain).build();
@@ -62,7 +65,7 @@ public class UrlLevelSecurityTests {
} }
@Test @Test
public void allowsAccessToRootResource() throws Exception { void allowsAccessToRootResource() throws Exception {
mvc.perform(get("/").// mvc.perform(get("/").//
accept(MediaTypes.HAL_JSON)).// accept(MediaTypes.HAL_JSON)).//
@@ -71,7 +74,7 @@ public class UrlLevelSecurityTests {
} }
@Test @Test
public void rejectsPostAccessToCollectionResource() throws Exception { void rejectsPostAccessToCollectionResource() throws Exception {
mvc.perform(post("/employees").// mvc.perform(post("/employees").//
content(PAYLOAD).// content(PAYLOAD).//
@@ -80,7 +83,7 @@ public class UrlLevelSecurityTests {
} }
@Test @Test
public void allowsGetRequestsButRejectsPostForUser() throws Exception { void allowsGetRequestsButRejectsPostForUser() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE); headers.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);
@@ -98,7 +101,7 @@ public class UrlLevelSecurityTests {
} }
@Test @Test
public void allowsPostRequestForAdmin() throws Exception { void allowsPostRequestForAdmin() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE); headers.set(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);

View File

@@ -51,10 +51,11 @@ import org.springframework.web.client.RestTemplate;
* A test case to discover the search resource and execute a predefined search with it. * A test case to discover the search resource and execute a predefined search with it.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Divya Srivastava
*/ */
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Slf4j @Slf4j
public class StarbucksClient { class StarbucksClient {
@SpringBootApplication @SpringBootApplication
static class Config { static class Config {
@@ -70,7 +71,7 @@ public class StarbucksClient {
private static final String SERVICE_URI = "http://localhost:%s/api"; private static final String SERVICE_URI = "http://localhost:%s/api";
@Test @Test
public void discoverStoreSearch() { void discoverStoreSearch() {
Traverson traverson = new Traverson(URI.create(String.format(SERVICE_URI, port)), MediaTypes.HAL_JSON); Traverson traverson = new Traverson(URI.create(String.format(SERVICE_URI, port)), MediaTypes.HAL_JSON);
@@ -103,7 +104,7 @@ public class StarbucksClient {
@Autowired RestOperations restOperations; @Autowired RestOperations restOperations;
@Test @Test
public void accessServiceUsingRestTemplate() { void accessServiceUsingRestTemplate() {
// Access root resource // Access root resource
@@ -125,12 +126,12 @@ public class StarbucksClient {
static class Store { static class Store {
public String name; public String name;
public Address address; Address address;
static class Address { static class Address {
public String city, zip, street; String city, zip, street;
public Location location; Location location;
@Override @Override
public String toString() { public String toString() {
@@ -138,7 +139,7 @@ public class StarbucksClient {
} }
static class Location { static class Location {
public double x, y; double x, y;
} }
} }
} }

View File

@@ -31,22 +31,23 @@ import org.springframework.web.context.WebApplicationContext;
* Integration tests to make sure the URI customizations are applied. * Integration tests to make sure the URI customizations are applied.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Divya Srivastava
* @soundtrack Clueso - Gewinner (Stadtrandlichter Live) * @soundtrack Clueso - Gewinner (Stadtrandlichter Live)
*/ */
@SpringBootTest @SpringBootTest
public class WebIntegrationTests { class WebIntegrationTests {
@Autowired WebApplicationContext context; @Autowired WebApplicationContext context;
MockMvc mvc; private MockMvc mvc;
@BeforeEach @BeforeEach
public void setUp() { void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(context).build(); this.mvc = MockMvcBuilders.webAppContextSetup(context).build();
} }
@Test @Test
public void identifiesResourcesUsingUsername() throws Exception { void identifiesResourcesUsingUsername() throws Exception {
mvc.perform(get("/users/olivergierke")).// mvc.perform(get("/users/olivergierke")).//
andExpect(status().isOk()).// andExpect(status().isOk()).//