@@ -21,7 +21,6 @@ import javax.transaction.Transactional;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
@@ -57,7 +56,7 @@ public class UserManagement {
|
||||
throw new IllegalArgumentException("User with that name already exists!");
|
||||
});
|
||||
|
||||
Password encryptedPassword = Password.encrypted(encoder.encode(password));
|
||||
var encryptedPassword = Password.encrypted(encoder.encode(password));
|
||||
|
||||
return repository.save(new User(username, encryptedPassword));
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.util.Map;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
@@ -91,7 +90,7 @@ class UserController {
|
||||
|
||||
userManagement.register(new Username(userForm.getUsername()), Password.raw(userForm.getPassword()));
|
||||
|
||||
RedirectView redirectView = new RedirectView("redirect:/users");
|
||||
var redirectView = new RedirectView("redirect:/users");
|
||||
redirectView.setPropagateQueryParams(true);
|
||||
|
||||
return redirectView;
|
||||
|
||||
@@ -15,9 +15,7 @@
|
||||
*/
|
||||
package example.users;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
/**
|
||||
* Integration tests to bootstrap the application.
|
||||
@@ -25,6 +23,5 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
* @author Oliver Gierke
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
public abstract class AbstractIntegrationTests {}
|
||||
abstract class AbstractIntegrationTests {}
|
||||
|
||||
@@ -27,7 +27,7 @@ import static org.assertj.core.api.Assertions.*;
|
||||
* @author Divya Srivastava
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public class UserManagementIntegrationTests extends AbstractIntegrationTests {
|
||||
class UserManagementIntegrationTests extends AbstractIntegrationTests {
|
||||
|
||||
@Autowired UserManagement userManagement;
|
||||
|
||||
@@ -35,9 +35,9 @@ public class UserManagementIntegrationTests extends AbstractIntegrationTests {
|
||||
* @see #65
|
||||
*/
|
||||
@Test
|
||||
public void encryptsPasswordWhenCreatingAUser() {
|
||||
void encryptsPasswordWhenCreatingAUser() {
|
||||
|
||||
User user = userManagement.register(new Username("olivergierke"), Password.raw("foobar"));
|
||||
var user = userManagement.register(new Username("olivergierke"), Password.raw("foobar"));
|
||||
|
||||
assertThat(user.getPassword().isEncrypted()).isTrue();
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
* @author Oliver Gierke
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
public class UserRepositoryIntegrationTests extends AbstractIntegrationTests {
|
||||
class UserRepositoryIntegrationTests extends AbstractIntegrationTests {
|
||||
|
||||
@Autowired UserRepository users;
|
||||
|
||||
@@ -34,7 +34,7 @@ public class UserRepositoryIntegrationTests extends AbstractIntegrationTests {
|
||||
* @see #65
|
||||
*/
|
||||
@Test
|
||||
public void repositoryRejectsUnencryptedPassword() {
|
||||
void repositoryRejectsUnencryptedPassword() {
|
||||
Assertions.assertThrows(InvalidDataAccessApiUsageException.class, () -> {
|
||||
users.save(new User(new Username("olivergierke"), Password.raw("foobar")));
|
||||
});
|
||||
|
||||
@@ -46,9 +46,8 @@ import static org.assertj.core.api.Assertions.*;
|
||||
* @author Divya Srivastava
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class UserControllerClientTests {
|
||||
class UserControllerClientTests {
|
||||
|
||||
@Autowired TestRestTemplate template;
|
||||
|
||||
@@ -71,28 +70,28 @@ public class UserControllerClientTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessJsonFieldsOnSimplePayload() {
|
||||
void accessJsonFieldsOnSimplePayload() {
|
||||
assertDave(issueGet("/", MediaType.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessJsonFieldsOnNestedPayload() {
|
||||
void accessJsonFieldsOnNestedPayload() {
|
||||
assertDave(issueGet("/changed", MediaType.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessXmlElementsOnSimplePayload() {
|
||||
void accessXmlElementsOnSimplePayload() {
|
||||
assertDave(issueGet("/", MediaType.APPLICATION_XML));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessXmlElementsOnNestedPayload() {
|
||||
void accessXmlElementsOnNestedPayload() {
|
||||
assertDave(issueGet("/changed", MediaType.APPLICATION_XML));
|
||||
}
|
||||
|
||||
private UserPayload issueGet(String path, MediaType mediaType) {
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
var headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.ACCEPT, mediaType.toString());
|
||||
|
||||
return template.exchange(path, HttpMethod.GET, new HttpEntity<Void>(headers), UserPayload.class).getBody();
|
||||
|
||||
@@ -20,15 +20,13 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrint;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link UserController}.
|
||||
@@ -36,33 +34,32 @@ import org.springframework.test.web.servlet.ResultActions;
|
||||
* @author Oliver Gierke
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc(print = MockMvcPrint.NONE)
|
||||
public class UserControllerIntegrationTests {
|
||||
class UserControllerIntegrationTests {
|
||||
|
||||
@Autowired MockMvc mvc;
|
||||
|
||||
@Test
|
||||
public void handlesJsonPayloadWithExactProperties() throws Exception {
|
||||
void handlesJsonPayloadWithExactProperties() throws Exception {
|
||||
postAndExpect("{ \"firstname\" : \"Dave\", \"lastname\" : \"Matthews\" }", MediaType.APPLICATION_JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesJsonPayloadWithNestedProperties() throws Exception {
|
||||
void handlesJsonPayloadWithNestedProperties() throws Exception {
|
||||
postAndExpect("{ \"user\" : { \"firstname\" : \"Dave\", \"lastname\" : \"Matthews\" } }",
|
||||
MediaType.APPLICATION_JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesXmlPayLoadWithExactProperties() throws Exception {
|
||||
void handlesXmlPayLoadWithExactProperties() throws Exception {
|
||||
|
||||
postAndExpect("<user><firstname>Dave</firstname><lastname>Matthews</lastname></user>", MediaType.APPLICATION_XML);
|
||||
}
|
||||
|
||||
private void postAndExpect(String payload, MediaType mediaType) throws Exception {
|
||||
|
||||
ResultActions actions = mvc.perform(post("/")//
|
||||
var actions = mvc.perform(post("/")//
|
||||
.content(payload)//
|
||||
.contentType(mediaType))//
|
||||
.andExpect(status().isOk());
|
||||
|
||||
@@ -17,18 +17,16 @@ package example.users;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
public class ApplicationTests {
|
||||
class ApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextBootstraps() {}
|
||||
void contextBootstraps() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user