cleanup test

This commit is contained in:
DOHA 2016-10-15 13:48:40 +02:00
parent eb2fc38316
commit 63024cc8e9
13 changed files with 70 additions and 85 deletions

View File

@ -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")

View File

@ -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);

View File

@ -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() {

View File

@ -57,7 +57,7 @@ public abstract class AbstractLiveTest<T extends Serializable> {
//
protected String getURL() {
return "http://localhost:" + APPLICATION_PORT + "/foos";
return "http://localhost:" + APPLICATION_PORT + "/auth/foos";
}
protected final RequestSpecification givenAuth() {

View File

@ -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<SearchCriteria> params = new ArrayList<SearchCriteria>();
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<User> results = userApi.searchUser(params);
@ -65,7 +65,7 @@ public class JPACriteriaQueryTest {
@Test
public void givenLast_whenGettingListOfUsers_thenCorrect() {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
params.add(new SearchCriteria("lastName", ":", "Doe"));
params.add(new SearchCriteria("lastName", ":", "doe"));
final List<User> results = userApi.searchUser(params);
assertThat(userJohn, isIn(results));
@ -75,7 +75,7 @@ public class JPACriteriaQueryTest {
@Test
public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
params.add(new SearchCriteria("lastName", ":", "Doe"));
params.add(new SearchCriteria("lastName", ":", "doe"));
params.add(new SearchCriteria("age", ">", "25"));
final List<User> results = userApi.searchUser(params);
@ -87,8 +87,8 @@ public class JPACriteriaQueryTest {
@Test
public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
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<User> results = userApi.searchUser(params);
assertThat(userJohn, not(isIn(results)));
@ -98,7 +98,7 @@ public class JPACriteriaQueryTest {
@Test
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
params.add(new SearchCriteria("firstName", ":", "Jo"));
params.add(new SearchCriteria("firstName", ":", "jo"));
final List<User> results = userApi.searchUser(params);

View File

@ -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<MyUser> 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<MyUser> 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<MyUser> 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<MyUser> results = repo.findAll(builder.build());
assertThat(results, emptyIterable());

View File

@ -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

View File

@ -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<User> 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<User> 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<User> results = repository.findAll(spec);
assertThat(userJohn, isIn(results));

View File

@ -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() {

View File

@ -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());
}
}

View File

@ -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()

View File

@ -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));
}
}