diff --git a/spring-security-rest-full/src/main/java/org/baeldung/spring/PersistenceConfig.java b/spring-security-rest-full/src/main/java/org/baeldung/spring/PersistenceConfig.java index dad4808a56..f3a87b189e 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/spring/PersistenceConfig.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/spring/PersistenceConfig.java @@ -23,7 +23,7 @@ import com.google.common.base.Preconditions; @Configuration @EnableTransactionManagement -@PropertySource({ "classpath:persistence-${envTarget:mysql}.properties" }) +@PropertySource({ "classpath:persistence-${envTarget:h2}.properties" }) @ComponentScan({ "org.baeldung.persistence" }) // @ImportResource("classpath*:springDataPersistenceConfig.xml") @EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") diff --git a/spring-security-rest-full/src/main/resources/data.sql b/spring-security-rest-full/src/main/resources/data.sql new file mode 100644 index 0000000000..5fba12dd85 --- /dev/null +++ b/spring-security-rest-full/src/main/resources/data.sql @@ -0,0 +1,5 @@ +insert into User (id, firstName, lastName, email, age) values (1, 'john', 'doe', 'john@doe.com', 22); +insert into User (id, firstName, lastName, email, age) values (2, 'tom', 'doe', 'tom@doe.com', 26); + +insert into MyUser (id, firstName, lastName, email, age) values (1, 'john', 'doe', 'john@doe.com', 22); +insert into MyUser (id, firstName, lastName, email, age) values (2, 'tom', 'doe', 'tom@doe.com', 26); diff --git a/spring-security-rest-full/src/test/resources/persistence-mysql.properties b/spring-security-rest-full/src/main/resources/persistence-h2.properties similarity index 100% rename from spring-security-rest-full/src/test/resources/persistence-mysql.properties rename to spring-security-rest-full/src/main/resources/persistence-h2.properties diff --git a/spring-security-rest-full/src/test/java/org/baeldung/client/RestTemplateLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/client/RestTemplateLiveTest.java index fb40bd9d62..0f0a73b536 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/client/RestTemplateLiveTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/client/RestTemplateLiveTest.java @@ -51,7 +51,7 @@ import com.google.common.base.Charsets; public class RestTemplateLiveTest { private RestTemplate restTemplate; - private static final String fooResourceUrl = "http://localhost:" + APPLICATION_PORT + "/foos"; + private static final String fooResourceUrl = "http://localhost:" + APPLICATION_PORT + "/auth/foos"; @Before public void beforeTest() { diff --git a/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractLiveTest.java index 95fce10e45..a3e6c8858d 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractLiveTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractLiveTest.java @@ -57,7 +57,7 @@ public abstract class AbstractLiveTest { // protected String getURL() { - return "http://localhost:" + APPLICATION_PORT + "/foos"; + return "http://localhost:" + APPLICATION_PORT + "/auth/foos"; } protected final RequestSpecification givenAuth() { diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java index e9c8659347..1d0826e6ba 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java @@ -36,15 +36,15 @@ public class JPACriteriaQueryTest { @Before public void init() { userJohn = new User(); - userJohn.setFirstName("John"); - userJohn.setLastName("Doe"); + userJohn.setFirstName("john"); + userJohn.setLastName("doe"); userJohn.setEmail("john@doe.com"); userJohn.setAge(22); userApi.save(userJohn); userTom = new User(); - userTom.setFirstName("Tom"); - userTom.setLastName("Doe"); + userTom.setFirstName("tom"); + userTom.setLastName("doe"); userTom.setEmail("tom@doe.com"); userTom.setAge(26); userApi.save(userTom); @@ -53,8 +53,8 @@ public class JPACriteriaQueryTest { @Test public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { final List params = new ArrayList(); - params.add(new SearchCriteria("firstName", ":", "John")); - params.add(new SearchCriteria("lastName", ":", "Doe")); + params.add(new SearchCriteria("firstName", ":", "john")); + params.add(new SearchCriteria("lastName", ":", "doe")); final List results = userApi.searchUser(params); @@ -65,7 +65,7 @@ public class JPACriteriaQueryTest { @Test public void givenLast_whenGettingListOfUsers_thenCorrect() { final List params = new ArrayList(); - params.add(new SearchCriteria("lastName", ":", "Doe")); + params.add(new SearchCriteria("lastName", ":", "doe")); final List results = userApi.searchUser(params); assertThat(userJohn, isIn(results)); @@ -75,7 +75,7 @@ public class JPACriteriaQueryTest { @Test public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { final List params = new ArrayList(); - params.add(new SearchCriteria("lastName", ":", "Doe")); + params.add(new SearchCriteria("lastName", ":", "doe")); params.add(new SearchCriteria("age", ">", "25")); final List results = userApi.searchUser(params); @@ -87,8 +87,8 @@ public class JPACriteriaQueryTest { @Test public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { final List params = new ArrayList(); - params.add(new SearchCriteria("firstName", ":", "Adam")); - params.add(new SearchCriteria("lastName", ":", "Fox")); + params.add(new SearchCriteria("firstName", ":", "adam")); + params.add(new SearchCriteria("lastName", ":", "fox")); final List results = userApi.searchUser(params); assertThat(userJohn, not(isIn(results))); @@ -98,7 +98,7 @@ public class JPACriteriaQueryTest { @Test public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() { final List params = new ArrayList(); - params.add(new SearchCriteria("firstName", ":", "Jo")); + params.add(new SearchCriteria("firstName", ":", "jo")); final List results = userApi.searchUser(params); diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java index b7b38a4fcb..d5fbc819fd 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java @@ -35,15 +35,15 @@ public class JPAQuerydslTest { @Before public void init() { userJohn = new MyUser(); - userJohn.setFirstName("John"); - userJohn.setLastName("Doe"); + userJohn.setFirstName("john"); + userJohn.setLastName("doe"); userJohn.setEmail("john@doe.com"); userJohn.setAge(22); repo.save(userJohn); userTom = new MyUser(); - userTom.setFirstName("Tom"); - userTom.setLastName("Doe"); + userTom.setFirstName("tom"); + userTom.setLastName("doe"); userTom.setEmail("tom@doe.com"); userTom.setAge(26); repo.save(userTom); @@ -51,7 +51,7 @@ public class JPAQuerydslTest { @Test public void givenLast_whenGettingListOfUsers_thenCorrect() { - final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "Doe"); + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "doe"); final Iterable results = repo.findAll(builder.build()); assertThat(results, containsInAnyOrder(userJohn, userTom)); @@ -59,7 +59,7 @@ public class JPAQuerydslTest { @Test public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { - final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "John").with("lastName", ":", "Doe"); + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "john").with("lastName", ":", "doe"); final Iterable results = repo.findAll(builder.build()); @@ -69,7 +69,7 @@ public class JPAQuerydslTest { @Test public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { - final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "Doe").with("age", ">", "25"); + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "doe").with("age", ">", "25"); final Iterable results = repo.findAll(builder.build()); @@ -79,7 +79,7 @@ public class JPAQuerydslTest { @Test public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { - final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "Adam").with("lastName", ":", "Fox"); + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "adam").with("lastName", ":", "fox"); final Iterable results = repo.findAll(builder.build()); assertThat(results, emptyIterable()); diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationLiveTest.java index 544161dfd5..4fa15d536a 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationLiveTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationLiveTest.java @@ -3,52 +3,44 @@ package org.baeldung.persistence.query; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.baeldung.persistence.dao.UserRepository; import org.baeldung.persistence.model.User; -import org.baeldung.spring.ConfigTest; -import org.baeldung.spring.PersistenceConfig; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; import com.jayway.restassured.RestAssured; import com.jayway.restassured.response.Response; import com.jayway.restassured.specification.RequestSpecification; -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { ConfigTest.class, PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +//@RunWith(SpringJUnit4ClassRunner.class) +//@ContextConfiguration(classes = { ConfigTest.class, PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) @ActiveProfiles("test") public class JPASpecificationLiveTest { - @Autowired - private UserRepository repository; + // @Autowired + // private UserRepository repository; private User userJohn; private User userTom; - private final String URL_PREFIX = "http://localhost:8080/users/spec?search="; + private final String URL_PREFIX = "http://localhost:8080/auth/users/spec?search="; @Before public void init() { userJohn = new User(); - userJohn.setFirstName("John"); - userJohn.setLastName("Doe"); + userJohn.setFirstName("john"); + userJohn.setLastName("doe"); userJohn.setEmail("john@doe.com"); userJohn.setAge(22); - repository.save(userJohn); + // repository.save(userJohn); userTom = new User(); - userTom.setFirstName("Tom"); - userTom.setLastName("Doe"); + userTom.setFirstName("tom"); + userTom.setLastName("doe"); userTom.setEmail("tom@doe.com"); userTom.setAge(26); - repository.save(userTom); + // repository.save(userTom); } @Test diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationTest.java index 52bd43cc8c..0d8773dd61 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationTest.java @@ -38,15 +38,15 @@ public class JPASpecificationTest { @Before public void init() { userJohn = new User(); - userJohn.setFirstName("John"); - userJohn.setLastName("Doe"); + userJohn.setFirstName("john"); + userJohn.setLastName("doe"); userJohn.setEmail("john@doe.com"); userJohn.setAge(22); repository.save(userJohn); userTom = new User(); - userTom.setFirstName("Tom"); - userTom.setLastName("Doe"); + userTom.setFirstName("tom"); + userTom.setLastName("doe"); userTom.setEmail("tom@doe.com"); userTom.setAge(26); repository.save(userTom); @@ -54,8 +54,8 @@ public class JPASpecificationTest { @Test public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { - final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "John")); - final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "Doe")); + final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john")); + final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe")); final List results = repository.findAll(Specifications.where(spec).and(spec1)); assertThat(userJohn, isIn(results)); @@ -64,7 +64,7 @@ public class JPASpecificationTest { @Test public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() { - final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.NEGATION, "John")); + final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.NEGATION, "john")); final List results = repository.findAll(Specifications.where(spec)); assertThat(userTom, isIn(results)); @@ -82,7 +82,7 @@ public class JPASpecificationTest { @Test public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() { - final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.STARTS_WITH, "Jo")); + final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.STARTS_WITH, "jo")); final List results = repository.findAll(spec); assertThat(userJohn, isIn(results)); diff --git a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java index 13cb92a745..be9b7cf27e 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java @@ -23,7 +23,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @Transactional -public class CsrfAbstractIntegrationTest { +public abstract class CsrfAbstractIntegrationTest { @Autowired private WebApplicationContext context; @@ -41,7 +41,7 @@ public class CsrfAbstractIntegrationTest { } protected RequestPostProcessor testUser() { - return user("user").password("userPass").roles("USER"); + return user("user1").password("user1Pass").roles("USER"); } protected RequestPostProcessor testAdmin() { diff --git a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfEnabledIntegrationTest.java b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfEnabledIntegrationTest.java index c7caf61525..b04644f847 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfEnabledIntegrationTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfEnabledIntegrationTest.java @@ -15,12 +15,12 @@ public class CsrfEnabledIntegrationTest extends CsrfAbstractIntegrationTest { @Test public void givenNoCsrf_whenAddFoo_thenForbidden() throws Exception { - mvc.perform(post("/foos").contentType(MediaType.APPLICATION_JSON).content(createFoo()).with(testUser())).andExpect(status().isForbidden()); + mvc.perform(post("/auth/foos").contentType(MediaType.APPLICATION_JSON).content(createFoo()).with(testUser())).andExpect(status().isForbidden()); } @Test public void givenCsrf_whenAddFoo_thenCreated() throws Exception { - mvc.perform(post("/foos").contentType(MediaType.APPLICATION_JSON).content(createFoo()).with(testUser()).with(csrf())).andExpect(status().isCreated()); + mvc.perform(post("/auth/foos").contentType(MediaType.APPLICATION_JSON).content(createFoo()).with(testUser()).with(csrf())).andExpect(status().isCreated()); } } diff --git a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/SecurityWithCsrfConfig.java b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/SecurityWithCsrfConfig.java index 99b94cd7b5..ae4a655265 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/SecurityWithCsrfConfig.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/SecurityWithCsrfConfig.java @@ -41,7 +41,7 @@ public class SecurityWithCsrfConfig extends WebSecurityConfigurerAdapter { // @formatter:off http .authorizeRequests() - .antMatchers("/admin/*").hasAnyRole("ROLE_ADMIN") + .antMatchers("/auth/admin/*").hasAnyRole("ROLE_ADMIN") .anyRequest().authenticated() .and() .httpBasic() diff --git a/spring-security-rest-full/src/test/java/org/baeldung/web/MyUserLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/web/MyUserLiveTest.java index 835b32c95c..c7fb26d70b 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/web/MyUserLiveTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/web/MyUserLiveTest.java @@ -3,52 +3,44 @@ package org.baeldung.web; import static org.junit.Assert.assertEquals; import org.baeldung.persistence.model.MyUser; -import org.baeldung.spring.ConfigTest; -import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.restassured.RestAssured; import com.jayway.restassured.response.Response; import com.jayway.restassured.specification.RequestSpecification; -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class) +//@RunWith(SpringJUnit4ClassRunner.class) +//@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class) @ActiveProfiles("test") public class MyUserLiveTest { - private ObjectMapper mapper = new ObjectMapper(); - private MyUser userJohn = new MyUser("john", "doe", "john@test.com", 11); - private MyUser userTom = new MyUser("tom", "doe", "tom@test.com", 20); + private final ObjectMapper mapper = new ObjectMapper(); + private final MyUser userJohn = new MyUser("john", "doe", "john@doe.com", 11); + private final MyUser userTom = new MyUser("tom", "doe", "tom@doe.com", 20); - private static boolean setupDataCreated = false; - - @Before - public void setupData() throws JsonProcessingException { - if (!setupDataCreated) { - withRequestBody(givenAuth(), userJohn).post("http://localhost:8080/myusers"); - withRequestBody(givenAuth(), userTom).post("http://localhost:8080/myusers"); - setupDataCreated = true; - } - } + // private static boolean setupDataCreated = false; + // + // @Before + // public void setupData() throws JsonProcessingException { + // if (!setupDataCreated) { + // withRequestBody(givenAuth(), userJohn).post("http://localhost:8080/auth/myusers"); + // withRequestBody(givenAuth(), userTom).post("http://localhost:8080/auth/myusers"); + // setupDataCreated = true; + // } + // } @Test public void whenGettingListOfUsers_thenCorrect() { - final Response response = givenAuth().get("http://localhost:8080/api/myusers"); + final Response response = givenAuth().get("http://localhost:8080/auth/api/myusers"); final MyUser[] result = response.as(MyUser[].class); assertEquals(result.length, 2); } @Test public void givenFirstName_whenGettingListOfUsers_thenCorrect() { - final Response response = givenAuth().get("http://localhost:8080/api/myusers?firstName=john"); + final Response response = givenAuth().get("http://localhost:8080/auth/api/myusers?firstName=john"); final MyUser[] result = response.as(MyUser[].class); assertEquals(result.length, 1); assertEquals(result[0].getEmail(), userJohn.getEmail()); @@ -56,14 +48,14 @@ public class MyUserLiveTest { @Test public void givenPartialLastName_whenGettingListOfUsers_thenCorrect() { - final Response response = givenAuth().get("http://localhost:8080/api/myusers?lastName=do"); + final Response response = givenAuth().get("http://localhost:8080/auth/api/myusers?lastName=do"); final MyUser[] result = response.as(MyUser[].class); assertEquals(result.length, 2); } @Test public void givenEmail_whenGettingListOfUsers_thenIgnored() { - final Response response = givenAuth().get("http://localhost:8080/api/myusers?email=john"); + final Response response = givenAuth().get("http://localhost:8080/auth/api/myusers?email=john"); final MyUser[] result = response.as(MyUser[].class); assertEquals(result.length, 2); } @@ -71,8 +63,4 @@ public class MyUserLiveTest { private RequestSpecification givenAuth() { return RestAssured.given().auth().preemptive().basic("user1", "user1Pass"); } - - private RequestSpecification withRequestBody(final RequestSpecification req, final Object obj) throws JsonProcessingException { - return req.contentType(MediaType.APPLICATION_JSON_VALUE).body(mapper.writeValueAsString(obj)); - } }