Merge branch 'master' of github.com:eugenp/tutorials

This commit is contained in:
David Morley 2016-04-12 06:02:27 -05:00
commit 2ce4f5acd1
23 changed files with 182 additions and 36 deletions

View File

@ -17,6 +17,7 @@ import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
@ -33,6 +34,8 @@ public class RestClientLiveManualTest {
// tests
// old httpClient will throw UnsupportedOperationException
@Ignore
@Test
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws GeneralSecurityException {
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();

View File

@ -0,0 +1,16 @@
package org.baeldung.test;
import org.baeldung.client.ClientLiveTest;
import org.baeldung.client.RestClientLiveManualTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
// @formatter:off
RestClientLiveManualTest.class
,ClientLiveTest.class
}) //
public class LiveTestSuite {
}

View File

@ -25,6 +25,7 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">

View File

@ -0,0 +1,18 @@
package org.baeldung.test;
import org.baeldung.client.ClientNoSpringLiveTest;
import org.baeldung.client.ClientWithSpringLiveTest;
import org.baeldung.client.RawClientLiveTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
// @formatter:off
RawClientLiveTest.class
,ClientWithSpringLiveTest.class
,ClientNoSpringLiveTest.class
}) //
public class LiveTestSuite {
}

View File

@ -2,8 +2,10 @@ package org.baeldung.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
@ -20,4 +22,8 @@ public class Application extends WebMvcConfigurerAdapter {
SpringApplication.run(Application.class, args);
}
@Bean
public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
return new ShallowEtagHeaderFilter();
}
}

View File

@ -51,6 +51,8 @@ public class SecurityWithoutCsrfConfig extends WebSecurityConfigurerAdapter {
.and()
// .exceptionHandling().accessDeniedPage("/my-error-page")
.exceptionHandling().accessDeniedHandler(accessDeniedHandler)
.and()
.headers().cacheControl().disable()
;
// @formatter:on
}

View File

@ -0,0 +1,18 @@
package org.baeldung;
import org.baeldung.persistence.PersistenceTestSuite;
import org.baeldung.security.SecurityTestSuite;
import org.baeldung.web.LiveTestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
// @formatter:off
PersistenceTestSuite.class
,SecurityTestSuite.class
,LiveTestSuite.class
}) //
public class TestSuite {
}

View File

@ -6,6 +6,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@ -20,8 +21,10 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.baeldung.persistence.model.Foo;
import org.baeldung.spring.ConfigTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
@ -30,6 +33,10 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
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 org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;
@ -38,10 +45,13 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class)
@ActiveProfiles("test")
public class RestTemplateLiveTest {
private RestTemplate restTemplate;
private static final String fooResourceUrl = "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/foos";
private static final String fooResourceUrl = "http://localhost:" + APPLICATION_PORT + "/foos";
@Before
public void beforeTest() {
@ -66,7 +76,7 @@ public class RestTemplateLiveTest {
final JsonNode root = mapper.readTree(response.getBody());
final JsonNode name = root.path("name");
assertThat(name.asText(), is("bar"));
assertNotNull(name);
final JsonNode owner = root.path("id");
assertThat(owner.asText(), is("1"));
@ -75,7 +85,7 @@ public class RestTemplateLiveTest {
@Test
public void givenResourceUrl_whenSendGetForObject_thenReturnsRepoObject() {
final Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class);
assertThat(foo.getName(), is("bar"));
assertNotNull(foo.getName());
assertThat(foo.getId(), is(1L));
}

View File

@ -1,16 +1,17 @@
package org.baeldung.common.web;
import static org.baeldung.Consts.APPLICATION_PORT;
import java.io.Serializable;
import org.baeldung.test.IMarshaller;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.base.Preconditions;
import com.google.common.net.HttpHeaders;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
import org.baeldung.test.IMarshaller;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import static org.baeldung.Consts.APPLICATION_PORT;
public abstract class AbstractLiveTest<T extends Serializable> {
@ -56,7 +57,7 @@ public abstract class AbstractLiveTest<T extends Serializable> {
//
protected String getURL() {
return "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/foos";
return "http://localhost:" + APPLICATION_PORT + "/foos";
}
protected final RequestSpecification givenAuth() {

View File

@ -0,0 +1,22 @@
package org.baeldung.persistence;
import org.baeldung.persistence.query.JPACriteriaQueryTest;
import org.baeldung.persistence.query.JPAQuerydslTest;
import org.baeldung.persistence.query.JPASpecificationTest;
import org.baeldung.persistence.query.RsqlTest;
import org.baeldung.persistence.service.FooServicePersistenceIntegrationTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
// @formatter:off
RsqlTest.class
,JPASpecificationTest.class
,FooServicePersistenceIntegrationTest.class
,JPAQuerydslTest.class
,JPACriteriaQueryTest.class
}) //
public class PersistenceTestSuite {
}

View File

@ -15,15 +15,15 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class })
@Transactional
@TransactionConfiguration
@Rollback
public class JPACriteriaQueryTest {
@Autowired

View File

@ -14,15 +14,15 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class })
@Transactional
@TransactionConfiguration
@Rollback
public class JPAQuerydslTest {
@Autowired

View File

@ -32,7 +32,7 @@ public class JPASpecificationLiveTest {
private User userTom;
private final String URL_PREFIX = "http://localhost:8080/spring-security-rest-full/users/spec?search=";
private final String URL_PREFIX = "http://localhost:8080/users/spec?search=";
@Before
public void init() {

View File

@ -17,15 +17,15 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class })
@Transactional
@TransactionConfiguration
@Rollback
public class JPASpecificationTest {
@Autowired

View File

@ -15,9 +15,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import cz.jirutka.rsql.parser.RSQLParser;
@ -26,7 +26,7 @@ import cz.jirutka.rsql.parser.ast.Node;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class })
@Transactional
@TransactionConfiguration
@Rollback
public class RsqlTest {
@Autowired

View File

@ -0,0 +1,17 @@
package org.baeldung.security;
import org.baeldung.security.csrf.CsrfDisabledIntegrationTest;
import org.baeldung.security.csrf.CsrfEnabledIntegrationTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
// @formatter:off
CsrfEnabledIntegrationTest.class
,CsrfDisabledIntegrationTest.class
}) //
public class SecurityTestSuite {
}

View File

@ -1,4 +1,4 @@
package org.baeldung.csrf;
package org.baeldung.security.csrf;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;

View File

@ -1,4 +1,4 @@
package org.baeldung.csrf;
package org.baeldung.security.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

View File

@ -1,11 +1,10 @@
package org.baeldung.csrf;
package org.baeldung.security.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.baeldung.spring.PersistenceConfig;
import org.baeldung.spring.SecurityWithCsrfConfig;
import org.baeldung.spring.WebConfig;
import org.junit.Test;
import org.springframework.http.MediaType;

View File

@ -1,4 +1,4 @@
package org.baeldung.spring;
package org.baeldung.security.csrf;
import org.baeldung.web.error.CustomAccessDeniedHandler;
import org.springframework.beans.factory.annotation.Autowired;
@ -11,10 +11,10 @@ import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
// @Configuration
// @EnableAutoConfiguration
// @EnableWebSecurity
// @EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
@EnableAutoConfiguration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityWithCsrfConfig extends WebSecurityConfigurerAdapter {
@Autowired
@ -47,6 +47,8 @@ public class SecurityWithCsrfConfig extends WebSecurityConfigurerAdapter {
.httpBasic()
.and()
.exceptionHandling().accessDeniedHandler(accessDeniedHandler)
.and()
.headers().cacheControl().disable()
;
// @formatter:on
}

View File

@ -0,0 +1,19 @@
package org.baeldung.web;
import org.baeldung.client.RestTemplateLiveTest;
import org.baeldung.persistence.query.JPASpecificationLiveTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
// @formatter:off
JPASpecificationLiveTest.class
,FooDiscoverabilityLiveTest.class
,FooLiveTest.class
,MyUserLiveTest.class
,RestTemplateLiveTest.class
}) //
public class LiveTestSuite {
}

View File

@ -3,14 +3,15 @@ package org.baeldung.web;
import static org.junit.Assert.assertEquals;
import org.baeldung.persistence.model.MyUser;
import org.baeldung.spring.Application;
import org.baeldung.spring.ConfigTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
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.web.WebAppConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -19,8 +20,8 @@ import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class)
@ActiveProfiles("test")
public class MyUserLiveTest {
private ObjectMapper mapper = new ObjectMapper();

View File

@ -11,7 +11,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.authentication.FormAuthConfig;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
@ -19,10 +18,22 @@ import com.jayway.restassured.specification.RequestSpecification;
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class FooLiveTest {
private static final String URL_PREFIX = "http://localhost:8080/spring-security-rest";
private FormAuthConfig formConfig = new FormAuthConfig(URL_PREFIX + "/login", "username", "password");
// private FormAuthConfig formConfig = new FormAuthConfig(URL_PREFIX + "/login", "temporary", "temporary");
private String cookie;
private RequestSpecification givenAuth() {
return RestAssured.given().auth().form("user", "userPass", formConfig);
// return RestAssured.given().auth().form("user", "userPass", formConfig);
if (cookie == null)
cookie = RestAssured.given().contentType("application/x-www-form-urlencoded").formParam("password", "userPass").formParam("username", "user").post(URL_PREFIX + "/login").getCookie("JSESSIONID");
return RestAssured.given().cookie("JSESSIONID", cookie);
}
@Test
public void whenTry_thenOK() {
final Response response = givenAuth().get(URL_PREFIX + "/api/foos");
assertEquals(200, response.statusCode());
System.out.println(response.asString());
}
@Test