From 53ee0fe2b5d61e33f36c9df6b942eac7e2ce1a1b Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 30 Apr 2021 08:45:43 +0200 Subject: [PATCH] Polishing. Original pull request: #620. See #583. --- .../projections/SimpleProjectionTests.java | 12 ++++++----- .../security/MethodLevelSecurityTests.java | 14 +++++++------ .../rest/security/UrlLevelSecurityTests.java | 21 +++++++++++-------- .../rest/stores/StarbucksClient.java | 15 ++++++------- .../rest/uris/WebIntegrationTests.java | 9 ++++---- 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/rest/projections/src/test/java/example/springdata/rest/projections/SimpleProjectionTests.java b/rest/projections/src/test/java/example/springdata/rest/projections/SimpleProjectionTests.java index cb2fab57..4cc77fec 100644 --- a/rest/projections/src/test/java/example/springdata/rest/projections/SimpleProjectionTests.java +++ b/rest/projections/src/test/java/example/springdata/rest/projections/SimpleProjectionTests.java @@ -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 backingMap = new HashMap<>(); backingMap.put("firstname", "Dave"); diff --git a/rest/security/src/test/java/example/springdata/rest/security/MethodLevelSecurityTests.java b/rest/security/src/test/java/example/springdata/rest/security/MethodLevelSecurityTests.java index 1b14c287..0a8bf37f 100644 --- a/rest/security/src/test/java/example/springdata/rest/security/MethodLevelSecurityTests.java +++ b/rest/security/src/test/java/example/springdata/rest/security/MethodLevelSecurityTests.java @@ -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"); diff --git a/rest/security/src/test/java/example/springdata/rest/security/UrlLevelSecurityTests.java b/rest/security/src/test/java/example/springdata/rest/security/UrlLevelSecurityTests.java index 024a967c..1de66e3c 100644 --- a/rest/security/src/test/java/example/springdata/rest/security/UrlLevelSecurityTests.java +++ b/rest/security/src/test/java/example/springdata/rest/security/UrlLevelSecurityTests.java @@ -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); diff --git a/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java b/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java index 1815011e..919dac71 100644 --- a/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java +++ b/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java @@ -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; } } } diff --git a/rest/uri-customization/src/test/java/example/springdata/rest/uris/WebIntegrationTests.java b/rest/uri-customization/src/test/java/example/springdata/rest/uris/WebIntegrationTests.java index 7778cb50..dd925cd4 100644 --- a/rest/uri-customization/src/test/java/example/springdata/rest/uris/WebIntegrationTests.java +++ b/rest/uri-customization/src/test/java/example/springdata/rest/uris/WebIntegrationTests.java @@ -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()).//