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;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.projection.ProjectionFactory;
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}.
*
* @author Oliver Gierke
* @author Divya Srivastava
*/
public class SimpleProjectionTests {
class SimpleProjectionTests {
ProjectionFactory factory = new SpelAwareProxyProjectionFactory();
private ProjectionFactory factory = new SpelAwareProxyProjectionFactory();
@Test
public void createMapBackedProjection() {
void createMapBackedProjection() {
Customer customer = factory.createProjection(Customer.class);
customer.setFirstname("Dave");
@@ -51,7 +53,7 @@ public class SimpleProjectionTests {
}
@Test
public void createsProxyForSourceMap() {
void createsProxyForSourceMap() {
Map<String, Object> backingMap = new HashMap<>();
backingMap.put("firstname", "Dave");

View File

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

View File

@@ -16,7 +16,7 @@
package example.springdata.rest.security;
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.result.MockMvcResultMatchers.*;
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.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.hateoas.MediaTypes;
@@ -42,19 +43,21 @@ import com.fasterxml.jackson.databind.ObjectMapper;
*
* @author Greg Turnquist
* @author Oliver Gierke
* @author Divya Srivastava
*/
@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 FilterChainProxy filterChain;
MockMvc mvc;
private MockMvc mvc;
@BeforeEach
public void setUp() {
void setUp() {
this.mvc = webAppContextSetup(context).addFilters(filterChain).build();
@@ -62,7 +65,7 @@ public class UrlLevelSecurityTests {
}
@Test
public void allowsAccessToRootResource() throws Exception {
void allowsAccessToRootResource() throws Exception {
mvc.perform(get("/").//
accept(MediaTypes.HAL_JSON)).//
@@ -71,7 +74,7 @@ public class UrlLevelSecurityTests {
}
@Test
public void rejectsPostAccessToCollectionResource() throws Exception {
void rejectsPostAccessToCollectionResource() throws Exception {
mvc.perform(post("/employees").//
content(PAYLOAD).//
@@ -80,7 +83,7 @@ public class UrlLevelSecurityTests {
}
@Test
public void allowsGetRequestsButRejectsPostForUser() throws Exception {
void allowsGetRequestsButRejectsPostForUser() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);
@@ -98,7 +101,7 @@ public class UrlLevelSecurityTests {
}
@Test
public void allowsPostRequestForAdmin() throws Exception {
void allowsPostRequestForAdmin() throws Exception {
HttpHeaders headers = new HttpHeaders();
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.
*
* @author Oliver Gierke
* @author Divya Srivastava
*/
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Slf4j
public class StarbucksClient {
class StarbucksClient {
@SpringBootApplication
static class Config {
@@ -70,7 +71,7 @@ public class StarbucksClient {
private static final String SERVICE_URI = "http://localhost:%s/api";
@Test
public void discoverStoreSearch() {
void discoverStoreSearch() {
Traverson traverson = new Traverson(URI.create(String.format(SERVICE_URI, port)), MediaTypes.HAL_JSON);
@@ -103,7 +104,7 @@ public class StarbucksClient {
@Autowired RestOperations restOperations;
@Test
public void accessServiceUsingRestTemplate() {
void accessServiceUsingRestTemplate() {
// Access root resource
@@ -125,12 +126,12 @@ public class StarbucksClient {
static class Store {
public String name;
public Address address;
Address address;
static class Address {
public String city, zip, street;
public Location location;
String city, zip, street;
Location location;
@Override
public String toString() {
@@ -138,7 +139,7 @@ public class StarbucksClient {
}
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.
*
* @author Oliver Gierke
* @author Divya Srivastava
* @soundtrack Clueso - Gewinner (Stadtrandlichter Live)
*/
@SpringBootTest
public class WebIntegrationTests {
class WebIntegrationTests {
@Autowired WebApplicationContext context;
MockMvc mvc;
private MockMvc mvc;
@BeforeEach
public void setUp() {
void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void identifiesResourcesUsingUsername() throws Exception {
void identifiesResourcesUsingUsername() throws Exception {
mvc.perform(get("/users/olivergierke")).//
andExpect(status().isOk()).//