Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
3ee473d30e
|
@ -1,22 +1,15 @@
|
|||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class EditDistanceBase {
|
||||
|
||||
public static int costOfSubstitution(char a, char b) {
|
||||
if (a == b) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
static int costOfSubstitution(char a, char b) {
|
||||
return a == b ? 0 : 1;
|
||||
}
|
||||
|
||||
public static int min(int... numbers) {
|
||||
int min = Integer.MAX_VALUE;
|
||||
|
||||
for (int x : numbers) {
|
||||
if (x < min)
|
||||
min = x;
|
||||
}
|
||||
|
||||
return min;
|
||||
static int min(int... numbers) {
|
||||
return Arrays.stream(numbers)
|
||||
.min().orElse(Integer.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.baeldung.algorithms.editdistance;
|
|||
|
||||
public class EditDistanceDynamicProgramming extends EditDistanceBase {
|
||||
|
||||
public static int calculate(String x, String y) {
|
||||
static int calculate(String x, String y) {
|
||||
int[][] dp = new int[x.length() + 1][y.length() + 1];
|
||||
|
||||
for (int i = 0; i <= x.length(); i++) {
|
||||
|
@ -14,12 +14,13 @@ public class EditDistanceDynamicProgramming extends EditDistanceBase {
|
|||
dp[i][j] = i;
|
||||
|
||||
else {
|
||||
dp[i][j] = min(dp[i - 1][j - 1] + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)), dp[i - 1][j] + 1, dp[i][j - 1] + 1);
|
||||
dp[i][j] = min(dp[i - 1][j - 1]
|
||||
+ costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)),
|
||||
dp[i - 1][j] + 1, dp[i][j - 1] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dp[x.length()][y.length()];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,13 +2,15 @@ package com.baeldung.algorithms.editdistance;
|
|||
|
||||
public class EditDistanceRecursive extends EditDistanceBase {
|
||||
|
||||
public static int calculate(String x, String y) {
|
||||
static int calculate(String x, String y) {
|
||||
|
||||
if (x.isEmpty())
|
||||
if (x.isEmpty()) {
|
||||
return y.length();
|
||||
}
|
||||
|
||||
if (y.isEmpty())
|
||||
if (y.isEmpty()) {
|
||||
return x.length();
|
||||
}
|
||||
|
||||
int substitution = calculate(x.substring(1), y.substring(1)) + costOfSubstitution(x.charAt(0), y.charAt(0));
|
||||
int insertion = calculate(x, y.substring(1)) + 1;
|
||||
|
@ -16,5 +18,4 @@ public class EditDistanceRecursive extends EditDistanceBase {
|
|||
|
||||
return min(substitution, insertion, deletion);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class EditDistanceTest extends EditDistanceDataProvider {
|
||||
|
||||
String x;
|
||||
String y;
|
||||
int result;
|
||||
private String x;
|
||||
private String y;
|
||||
private int result;
|
||||
|
||||
public EditDistanceTest(String a, String b, int res) {
|
||||
super();
|
||||
|
@ -21,11 +22,11 @@ public class EditDistanceTest extends EditDistanceDataProvider {
|
|||
|
||||
@Test
|
||||
public void testEditDistance_RecursiveImplementation() {
|
||||
Assert.assertEquals(result, EditDistanceRecursive.calculate(x, y));
|
||||
assertEquals(result, EditDistanceRecursive.calculate(x, y));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditDistance_givenDynamicProgrammingImplementation() {
|
||||
Assert.assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
|
||||
assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -106,5 +106,75 @@
|
|||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/AutoconfigurationTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>autoconfiguration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/AutoconfigurationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class CasSecuredAppApplicationTests {
|
||||
public class CasSecuredAppApplicationIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
|
@ -0,0 +1,110 @@
|
|||
package com.baeldung.concurrent.runnable;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class RunnableVsThreadTest {
|
||||
|
||||
private static Logger log =
|
||||
LoggerFactory.getLogger(RunnableVsThreadTest.class);
|
||||
|
||||
@Test
|
||||
public void givenARunnable_whenRunIt_thenResult() throws Exception{
|
||||
Thread thread = new Thread(new SimpleRunnable(
|
||||
"SimpleRunnable executed using Thread"));
|
||||
thread.start();
|
||||
thread.join();
|
||||
|
||||
ExecutorService executorService =
|
||||
Executors.newCachedThreadPool();
|
||||
|
||||
executorService.submit(new SimpleRunnable(
|
||||
"SimpleRunnable executed using ExecutorService")).get();
|
||||
|
||||
executorService.submit(()->
|
||||
log.info("Lambda runnable executed!!!")).get();
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAThread_whenRunIt_thenResult() throws Exception{
|
||||
Thread thread = new SimpleThread(
|
||||
"SimpleThread executed using Thread");
|
||||
thread.start();
|
||||
thread.join();
|
||||
|
||||
ExecutorService executorService =
|
||||
Executors.newCachedThreadPool();
|
||||
executorService.submit(new SimpleThread(
|
||||
"SimpleThread executed using ExecutorService")).get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACallable_whenRunIt_thenResult() throws Exception {
|
||||
ExecutorService executorService =
|
||||
Executors.newCachedThreadPool();
|
||||
|
||||
Future<Integer> future = executorService.submit(new SimpleCallable());
|
||||
log.info("Result from callable: {}", future.get());
|
||||
|
||||
future = executorService.submit(() -> {
|
||||
return RandomUtils.nextInt(0, 100);
|
||||
});
|
||||
log.info("Result from callable: {}", future.get());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class SimpleThread extends Thread{
|
||||
|
||||
private static final Logger log =
|
||||
LoggerFactory.getLogger(SimpleThread.class);
|
||||
|
||||
private String message;
|
||||
|
||||
public SimpleThread(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
log.info(message);
|
||||
}
|
||||
}
|
||||
|
||||
class SimpleRunnable implements Runnable {
|
||||
|
||||
private static final Logger log =
|
||||
LoggerFactory.getLogger(SimpleRunnable.class);
|
||||
|
||||
private String message;
|
||||
|
||||
public SimpleRunnable(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
log.info(message);
|
||||
}
|
||||
}
|
||||
|
||||
class SimpleCallable implements Callable<Integer> {
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
return RandomUtils.nextInt(0, 100);
|
||||
}
|
||||
|
||||
}
|
|
@ -7,12 +7,12 @@ class CalculatorTest5 {
|
|||
private val calculator = Calculator()
|
||||
|
||||
@Test
|
||||
fun testAddition() {
|
||||
fun whenAdding1and3_thenAnswerIs4() {
|
||||
Assertions.assertEquals(4, calculator.add(1, 3))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDivideByZero() {
|
||||
fun whenDividingBy0_thenErrorOccurs() {
|
||||
val exception = Assertions.assertThrows(DivideByZeroException::class.java) {
|
||||
calculator.divide(5, 0)
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ class CalculatorTest5 {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun testSquares() {
|
||||
fun whenSquaringNumbers_thenCorrectAnswerGiven() {
|
||||
Assertions.assertAll(
|
||||
Executable { Assertions.assertEquals(1, calculator.square(1)) },
|
||||
Executable { Assertions.assertEquals(4, calculator.square(2)) },
|
||||
|
@ -31,9 +31,9 @@ class CalculatorTest5 {
|
|||
|
||||
@TestFactory
|
||||
fun testSquaresFactory() = listOf(
|
||||
DynamicTest.dynamicTest("1 squared") { Assertions.assertEquals(1,calculator.square(1))},
|
||||
DynamicTest.dynamicTest("2 squared") { Assertions.assertEquals(4,calculator.square(2))},
|
||||
DynamicTest.dynamicTest("3 squared") { Assertions.assertEquals(9,calculator.square(3))}
|
||||
DynamicTest.dynamicTest("when I calculate 1^2 then I get 1") { Assertions.assertEquals(1,calculator.square(1))},
|
||||
DynamicTest.dynamicTest("when I calculate 2^2 then I get 4") { Assertions.assertEquals(4,calculator.square(2))},
|
||||
DynamicTest.dynamicTest("when I calculate 3^2 then I get 9") { Assertions.assertEquals(9,calculator.square(3))}
|
||||
)
|
||||
|
||||
@TestFactory
|
||||
|
@ -44,7 +44,7 @@ class CalculatorTest5 {
|
|||
4 to 16,
|
||||
5 to 25)
|
||||
.map { (input, expected) ->
|
||||
DynamicTest.dynamicTest("$input squared") {
|
||||
DynamicTest.dynamicTest("when I calculate $input^2 then I get $expected") {
|
||||
Assertions.assertEquals(expected, calculator.square(input))
|
||||
}
|
||||
}
|
||||
|
@ -59,14 +59,14 @@ class CalculatorTest5 {
|
|||
@TestFactory
|
||||
fun testSquaresFactory3() = squaresTestData
|
||||
.map { (input, expected) ->
|
||||
DynamicTest.dynamicTest("$input squared") {
|
||||
DynamicTest.dynamicTest("when I calculate $input^2 then I get $expected") {
|
||||
Assertions.assertEquals(expected, calculator.square(input))
|
||||
}
|
||||
}
|
||||
@TestFactory
|
||||
fun testSquareRootsFactory3() = squaresTestData
|
||||
.map { (expected, input) ->
|
||||
DynamicTest.dynamicTest("Square root of $input") {
|
||||
DynamicTest.dynamicTest("I calculate the square root of $input then I get $expected") {
|
||||
Assertions.assertEquals(expected.toDouble(), calculator.squareRoot(input))
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ class CalculatorTest5 {
|
|||
Tag("logarithms")
|
||||
)
|
||||
@Test
|
||||
fun testLogarithms() {
|
||||
fun whenIcalculateLog2Of8_thenIget3() {
|
||||
Assertions.assertEquals(3.0, calculator.log(2, 8))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,14 +6,14 @@ import org.junit.jupiter.api.Test
|
|||
|
||||
class SimpleTest5 {
|
||||
@Test
|
||||
fun testEmpty() {
|
||||
fun whenEmptyList_thenListIsEmpty() {
|
||||
val list = listOf<String>()
|
||||
Assertions.assertTrue(list::isEmpty)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
fun testMessage() {
|
||||
fun when3equals4_thenTestFails() {
|
||||
Assertions.assertEquals(3, 4) {
|
||||
"Three does not equal four"
|
||||
}
|
||||
|
|
|
@ -35,3 +35,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array)
|
||||
- [Jackson – Change Name of Field](http://www.baeldung.com/jackson-name-of-property)
|
||||
- [Serialize Only Fields that meet a Custom Criteria with Jackson](http://www.baeldung.com/jackson-serialize-field-custom-criteria)
|
||||
- [Mapping Nested Values with Jackson](http://www.baeldung.com/jackson-nested-values)
|
||||
|
|
|
@ -16,7 +16,7 @@ import com.j256.ormlite.dao.DaoManager;
|
|||
import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;
|
||||
import com.j256.ormlite.table.TableUtils;
|
||||
|
||||
public class ORMLiteTest {
|
||||
public class ORMLiteIntegrationTest {
|
||||
private static JdbcPooledConnectionSource connectionSource;
|
||||
|
||||
private static Dao<Library, Long> libraryDao;
|
|
@ -9,7 +9,7 @@ import org.junit.Test;
|
|||
import com.gs.fw.common.mithra.test.ConnectionManagerForTests;
|
||||
import com.gs.fw.common.mithra.test.MithraTestResource;
|
||||
|
||||
public class ReladomoTest {
|
||||
public class ReladomoIntegrationTest {
|
||||
private MithraTestResource mithraTestResource;
|
||||
|
||||
@Before
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.awaitility;
|
||||
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Duration;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
|
3
pom.xml
3
pom.xml
|
@ -192,6 +192,8 @@
|
|||
<module>spring-quartz</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-docs</module>
|
||||
<module>spring-rest-full</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-security-cache-control</module>
|
||||
|
@ -213,7 +215,6 @@
|
|||
<module>spring-security-mvc-socket</module>
|
||||
<module>spring-security-rest-basic-auth</module>
|
||||
<module>spring-security-rest-custom</module>
|
||||
<module>spring-security-rest-full</module>
|
||||
<module>spring-security-rest</module>
|
||||
<module>spring-security-sso</module>
|
||||
<module>spring-security-x509</module>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
=========
|
||||
|
||||
## REST Example Project with Spring Security
|
||||
## REST Example Project with Spring
|
||||
|
||||
### Courses
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
@ -15,15 +15,9 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com
|
|||
- [Integration Testing with the Maven Cargo plugin](http://www.baeldung.com/2011/10/16/how-to-set-up-integration-testing-with-the-maven-cargo-plugin/)
|
||||
- [Introduction to Spring Data JPA](http://www.baeldung.com/2011/12/22/the-persistence-layer-with-spring-data-jpa/)
|
||||
- [Project Configuration with Spring](http://www.baeldung.com/2012/03/12/project-configuration-with-spring/)
|
||||
- [REST Query Language with Spring and JPA Criteria](http://www.baeldung.com/rest-search-language-spring-jpa-criteria)
|
||||
- [REST Query Language with Spring Data JPA Specifications](http://www.baeldung.com/rest-api-search-language-spring-data-specifications)
|
||||
- [REST Query Language with Spring Data JPA and QueryDSL](http://www.baeldung.com/rest-api-search-language-spring-data-querydsl)
|
||||
- [REST Query Language – Advanced Search Operations](http://www.baeldung.com/rest-api-query-search-language-more-operations)
|
||||
- [Metrics for your Spring REST API](http://www.baeldung.com/spring-rest-api-metrics)
|
||||
- [REST Query Language with RSQL](http://www.baeldung.com/rest-api-search-language-rsql-fiql)
|
||||
- [Spring RestTemplate Tutorial](http://www.baeldung.com/rest-template)
|
||||
- [Bootstrap a Web Application with Spring 4](http://www.baeldung.com/bootstraping-a-web-application-with-spring-and-java-based-configuration)
|
||||
- [REST Query Language – Implementing OR Operation](http://www.baeldung.com/rest-api-query-search-or-operation)
|
||||
|
||||
|
||||
|
||||
|
@ -46,5 +40,5 @@ mysql -u root -p
|
|||
### Use the REST Service
|
||||
|
||||
```
|
||||
curl http://localhost:8080/spring-security-rest-full/foos
|
||||
curl http://localhost:8080/spring-rest-full/foos
|
||||
```
|
|
@ -0,0 +1,378 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-rest-full</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<name>spring-rest-full</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-4</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-4</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring Boot Dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-jasper</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-expression</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- deployment -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- persistence -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xml-apis</groupId>
|
||||
<artifactId>xml-apis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javassist</groupId>
|
||||
<artifactId>javassist</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- marshalling -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.thoughtworks.xstream</groupId>
|
||||
<artifactId>xstream</artifactId>
|
||||
<version>${xstream.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- util -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- <dependency> -->
|
||||
<!-- <groupId>org.hamcrest</groupId> -->
|
||||
<!-- <artifactId>hamcrest-core</artifactId> -->
|
||||
<!-- <scope>test</scope> -->
|
||||
<!-- </dependency> -->
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-rest-full</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven2-plugin</artifactId>
|
||||
<version>${cargo-maven2-plugin.version}</version>
|
||||
<configuration>
|
||||
<wait>true</wait>
|
||||
<container>
|
||||
<containerId>jetty8x</containerId>
|
||||
<type>embedded</type>
|
||||
<systemProperties>
|
||||
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
||||
</systemProperties>
|
||||
</container>
|
||||
<configuration>
|
||||
<properties>
|
||||
<cargo.servlet.port>8082</cargo.servlet.port>
|
||||
</properties>
|
||||
</configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Querydsl and Specifications -->
|
||||
|
||||
<plugin>
|
||||
<groupId>com.mysema.maven</groupId>
|
||||
<artifactId>apt-maven-plugin</artifactId>
|
||||
<version>${apt-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>process</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>target/generated-sources/java</outputDirectory>
|
||||
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>live</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*LiveTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven2-plugin</artifactId>
|
||||
<configuration>
|
||||
<wait>false</wait>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>start-server</id>
|
||||
<phase>pre-integration-test</phase>
|
||||
<goals>
|
||||
<goal>start</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>stop-server</id>
|
||||
<phase>post-integration-test</phase>
|
||||
<goals>
|
||||
<goal>stop</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- various -->
|
||||
<xstream.version>1.4.9</xstream.version>
|
||||
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
|
||||
<!-- Maven plugins -->
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
<apt-maven-plugin.version>1.1.3</apt-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -2,13 +2,10 @@ package org.baeldung.persistence.dao;
|
|||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
public interface IFooDao extends JpaRepository<Foo, Long>, JpaSpecificationExecutor<Foo> {
|
||||
public interface IFooDao extends JpaRepository<Foo, Long> {
|
||||
|
||||
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")
|
||||
Foo retrieveByName(@Param("name") String name);
|
||||
|
||||
}
|
|
@ -16,7 +16,7 @@ import org.springframework.web.filter.ShallowEtagHeaderFilter;
|
|||
|
||||
/**
|
||||
* Main Application Class - uses Spring Boot. Just run this as a normal Java
|
||||
* class to run up a Jetty Server (on http://localhost:8082/spring-security-rest-full)
|
||||
* class to run up a Jetty Server (on http://localhost:8082/spring-rest-full)
|
||||
*
|
||||
*/
|
||||
@EnableScheduling
|
|
@ -1,3 +1,3 @@
|
|||
server.port=8082
|
||||
server.context-path=/spring-security-rest-full
|
||||
server.context-path=/spring-rest-full
|
||||
endpoints.metrics.enabled=true
|
|
@ -5,7 +5,7 @@
|
|||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"
|
||||
>
|
||||
|
||||
<display-name>Spring Security REST Application</display-name>
|
||||
<display-name>Spring REST Application</display-name>
|
||||
|
||||
<!-- Spring root -->
|
||||
<context-param>
|
||||
|
@ -43,16 +43,6 @@
|
|||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<!-- Spring Security -->
|
||||
<filter>
|
||||
<filter-name>springSecurityFilterChain</filter-name>
|
||||
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>springSecurityFilterChain</filter-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
<!-- Metric filter -->
|
||||
<filter>
|
||||
<filter-name>metricFilter</filter-name>
|
|
@ -10,6 +10,8 @@ import static org.junit.Assert.assertNotNull;
|
|||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
@ -18,7 +20,6 @@ import org.junit.Ignore;
|
|||
import org.junit.Test;
|
||||
|
||||
import com.google.common.net.HttpHeaders;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
public abstract class AbstractBasicLiveTest<T extends Serializable> extends AbstractLiveTest<T> {
|
||||
|
||||
|
@ -34,7 +35,9 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
final String uriOfResource = createAsUri();
|
||||
|
||||
// When
|
||||
final Response findOneResponse = givenAuth().header("Accept", "application/json").get(uriOfResource);
|
||||
final Response findOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.get(uriOfResource);
|
||||
|
||||
// Then
|
||||
assertNotNull(findOneResponse.getHeader(HttpHeaders.ETAG));
|
||||
|
@ -44,11 +47,16 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
public void givenResourceWasRetrieved_whenRetrievingAgainWithEtag_thenNotModifiedReturned() {
|
||||
// Given
|
||||
final String uriOfResource = createAsUri();
|
||||
final Response findOneResponse = givenAuth().header("Accept", "application/json").get(uriOfResource);
|
||||
final Response findOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.get(uriOfResource);
|
||||
final String etagValue = findOneResponse.getHeader(HttpHeaders.ETAG);
|
||||
|
||||
// When
|
||||
final Response secondFindOneResponse = givenAuth().header("Accept", "application/json").headers("If-None-Match", etagValue).get(uriOfResource);
|
||||
final Response secondFindOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.headers("If-None-Match", etagValue)
|
||||
.get(uriOfResource);
|
||||
|
||||
// Then
|
||||
assertTrue(secondFindOneResponse.getStatusCode() == 304);
|
||||
|
@ -59,14 +67,19 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
public void givenResourceWasRetrievedThenModified_whenRetrievingAgainWithEtag_thenResourceIsReturned() {
|
||||
// Given
|
||||
final String uriOfResource = createAsUri();
|
||||
final Response findOneResponse = givenAuth().header("Accept", "application/json").get(uriOfResource);
|
||||
final Response findOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.get(uriOfResource);
|
||||
final String etagValue = findOneResponse.getHeader(HttpHeaders.ETAG);
|
||||
|
||||
// existingResource.setName(randomAlphabetic(6));
|
||||
// getApi().update(existingResource.setName("randomString"));
|
||||
|
||||
// When
|
||||
final Response secondFindOneResponse = givenAuth().header("Accept", "application/json").headers("If-None-Match", etagValue).get(uriOfResource);
|
||||
final Response secondFindOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.headers("If-None-Match", etagValue)
|
||||
.get(uriOfResource);
|
||||
|
||||
// Then
|
||||
assertTrue(secondFindOneResponse.getStatusCode() == 200);
|
||||
|
@ -79,7 +92,10 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
final String uriOfResource = createAsUri();
|
||||
|
||||
// When
|
||||
final Response findOneResponse = givenAuth().header("Accept", "application/json").headers("If-Match", randomAlphabetic(8)).get(uriOfResource);
|
||||
final Response findOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.headers("If-Match", randomAlphabetic(8))
|
||||
.get(uriOfResource);
|
||||
|
||||
// Then
|
||||
assertTrue(findOneResponse.getStatusCode() == 412);
|
||||
|
@ -93,7 +109,7 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
|
||||
@Test
|
||||
public void whenResourcesAreRetrievedPaged_then200IsReceived() {
|
||||
final Response response = givenAuth().get(getURL() + "?page=0&size=10");
|
||||
final Response response = RestAssured.get(getURL() + "?page=0&size=10");
|
||||
|
||||
assertThat(response.getStatusCode(), is(200));
|
||||
}
|
||||
|
@ -101,7 +117,7 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
@Test
|
||||
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() {
|
||||
final String url = getURL() + "?page=" + randomNumeric(5) + "&size=10";
|
||||
final Response response = givenAuth().get(url);
|
||||
final Response response = RestAssured.get(url);
|
||||
|
||||
assertThat(response.getStatusCode(), is(404));
|
||||
}
|
||||
|
@ -110,14 +126,14 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
|
||||
create();
|
||||
|
||||
final Response response = givenAuth().get(getURL() + "?page=0&size=10");
|
||||
final Response response = RestAssured.get(getURL() + "?page=0&size=10");
|
||||
|
||||
assertFalse(response.body().as(List.class).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFirstPageOfResourcesAreRetrieved_thenSecondPageIsNext() {
|
||||
final Response response = givenAuth().get(getURL() + "?page=0&size=2");
|
||||
final Response response = RestAssured.get(getURL() + "?page=0&size=2");
|
||||
|
||||
final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next");
|
||||
assertEquals(getURL() + "?page=1&size=2", uriToNextPage);
|
||||
|
@ -125,7 +141,7 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
|
||||
@Test
|
||||
public void whenFirstPageOfResourcesAreRetrieved_thenNoPreviousPage() {
|
||||
final Response response = givenAuth().get(getURL() + "?page=0&size=2");
|
||||
final Response response = RestAssured.get(getURL() + "?page=0&size=2");
|
||||
|
||||
final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev");
|
||||
assertNull(uriToPrevPage);
|
||||
|
@ -136,7 +152,7 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
create();
|
||||
create();
|
||||
|
||||
final Response response = givenAuth().get(getURL() + "?page=1&size=2");
|
||||
final Response response = RestAssured.get(getURL() + "?page=1&size=2");
|
||||
|
||||
final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev");
|
||||
assertEquals(getURL() + "?page=0&size=2", uriToPrevPage);
|
||||
|
@ -144,10 +160,10 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
|
|||
|
||||
@Test
|
||||
public void whenLastPageOfResourcesIsRetrieved_thenNoNextPageIsDiscoverable() {
|
||||
final Response first = givenAuth().get(getURL() + "?page=0&size=2");
|
||||
final Response first = RestAssured.get(getURL() + "?page=0&size=2");
|
||||
final String uriToLastPage = extractURIByRel(first.getHeader(HttpHeaders.LINK), "last");
|
||||
|
||||
final Response response = givenAuth().get(uriToLastPage);
|
||||
final Response response = RestAssured.get(uriToLastPage);
|
||||
|
||||
final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next");
|
||||
assertNull(uriToNextPage);
|
|
@ -5,6 +5,8 @@ import static org.hamcrest.Matchers.containsString;
|
|||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
|
@ -15,7 +17,6 @@ import org.junit.Test;
|
|||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.google.common.net.HttpHeaders;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
public abstract class AbstractDiscoverabilityLiveTest<T extends Serializable> extends AbstractLiveTest<T> {
|
||||
|
||||
|
@ -33,7 +34,7 @@ public abstract class AbstractDiscoverabilityLiveTest<T extends Serializable> ex
|
|||
final String uriOfExistingResource = createAsUri();
|
||||
|
||||
// When
|
||||
final Response res = givenAuth().post(uriOfExistingResource);
|
||||
final Response res = RestAssured.post(uriOfExistingResource);
|
||||
|
||||
// Then
|
||||
final String allowHeader = res.getHeader(HttpHeaders.ALLOW);
|
||||
|
@ -44,11 +45,16 @@ public abstract class AbstractDiscoverabilityLiveTest<T extends Serializable> ex
|
|||
public void whenResourceIsCreated_thenUriOfTheNewlyCreatedResourceIsDiscoverable() {
|
||||
// When
|
||||
final Foo newResource = new Foo(randomAlphabetic(6));
|
||||
final Response createResp = givenAuth().contentType(MediaType.APPLICATION_JSON_VALUE).body(newResource).post(getURL());
|
||||
final Response createResp = RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(newResource)
|
||||
.post(getURL());
|
||||
final String uriOfNewResource = createResp.getHeader(HttpHeaders.LOCATION);
|
||||
|
||||
// Then
|
||||
final Response response = givenAuth().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).get(uriOfNewResource);
|
||||
final Response response = RestAssured.given()
|
||||
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
|
||||
.get(uriOfNewResource);
|
||||
|
||||
final Foo resourceFromServer = response.body().as(Foo.class);
|
||||
assertThat(newResource, equalTo(resourceFromServer));
|
||||
|
@ -60,13 +66,13 @@ public abstract class AbstractDiscoverabilityLiveTest<T extends Serializable> ex
|
|||
final String uriOfExistingResource = createAsUri();
|
||||
|
||||
// When
|
||||
final Response getResponse = givenAuth().get(uriOfExistingResource);
|
||||
final Response getResponse = RestAssured.get(uriOfExistingResource);
|
||||
|
||||
// Then
|
||||
final String uriToAllResources = HTTPLinkHeaderUtil.extractURIByRel(getResponse.getHeader("Link"), "collection");
|
||||
|
||||
final Response getAllResponse = givenAuth().get(uriToAllResources);
|
||||
assertThat(getAllResponse.getStatusCode(), is(403));
|
||||
final Response getAllResponse = RestAssured.get(uriToAllResources);
|
||||
assertThat(getAllResponse.getStatusCode(), is(200));
|
||||
}
|
||||
|
||||
// template method
|
|
@ -1,6 +1,8 @@
|
|||
package org.baeldung.common.web;
|
||||
|
||||
import static org.baeldung.Consts.APPLICATION_PORT;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
|
@ -9,9 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
|
||||
public abstract class AbstractLiveTest<T extends Serializable> {
|
||||
|
||||
|
@ -48,20 +47,18 @@ public abstract class AbstractLiveTest<T extends Serializable> {
|
|||
|
||||
final Response createAsResponse(final T resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
final RequestSpecification givenAuthenticated = givenAuth();
|
||||
|
||||
final String resourceAsString = marshaller.encode(resource);
|
||||
return givenAuthenticated.contentType(marshaller.getMime()).body(resourceAsString).post(getURL());
|
||||
return RestAssured.given()
|
||||
.contentType(marshaller.getMime())
|
||||
.body(resourceAsString)
|
||||
.post(getURL());
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
protected String getURL() {
|
||||
return "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/auth/foos";
|
||||
}
|
||||
|
||||
protected final RequestSpecification givenAuth() {
|
||||
return RestAssured.given().auth().preemptive().basic("user1", "user1Pass");
|
||||
return "http://localhost:" + APPLICATION_PORT + "/spring-rest-full/auth/foos";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.baeldung.persistence;
|
||||
|
||||
import org.baeldung.persistence.service.FooServicePersistenceIntegrationTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
// @formatter:off
|
||||
|
||||
FooServicePersistenceIntegrationTest.class
|
||||
|
||||
}) //
|
||||
public class PersistenceTestSuite {
|
||||
|
||||
}
|
|
@ -1,29 +1,26 @@
|
|||
package org.baeldung.web;
|
||||
|
||||
import static org.baeldung.Consts.APPLICATION_PORT;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
|
||||
import static org.baeldung.Consts.APPLICATION_PORT;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.common.web.AbstractBasicLiveTest;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.spring.ConfigTest;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.baeldung.spring.ConfigTest;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Test;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ActiveProfiles("test")
|
||||
|
@ -45,32 +42,35 @@ public class FooPageableLiveTest extends AbstractBasicLiveTest<Foo> {
|
|||
return createAsUri(new Foo(randomAlphabetic(6)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void whenResourcesAreRetrievedPaged_then200IsReceived() {
|
||||
final Response response = givenAuth().get(getPageableURL() + "?page=0&size=10");
|
||||
final Response response = RestAssured.get(getPageableURL() + "?page=0&size=10");
|
||||
|
||||
assertThat(response.getStatusCode(), is(200));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() {
|
||||
final String url = getPageableURL() + "?page=" + randomNumeric(5) + "&size=10";
|
||||
final Response response = givenAuth().get(url);
|
||||
final Response response = RestAssured.get(url);
|
||||
|
||||
assertThat(response.getStatusCode(), is(404));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
|
||||
create();
|
||||
|
||||
final Response response = givenAuth().get(getPageableURL() + "?page=0&size=10");
|
||||
final Response response = RestAssured.get(getPageableURL() + "?page=0&size=10");
|
||||
|
||||
assertFalse(response.body().as(List.class).isEmpty());
|
||||
}
|
||||
|
||||
protected String getPageableURL() {
|
||||
return "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/auth/foos/pageable";
|
||||
return "http://localhost:" + APPLICATION_PORT + "/spring-rest-full/auth/foos/pageable";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,17 +1,14 @@
|
|||
package org.baeldung.web;
|
||||
|
||||
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
|
||||
FooDiscoverabilityLiveTest.class
|
||||
,FooLiveTest.class
|
||||
,FooPageableLiveTest.class
|
||||
,MyUserLiveTest.class
|
||||
}) //
|
||||
public class LiveTestSuite {
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -0,0 +1,35 @@
|
|||
=========
|
||||
|
||||
## REST Example Project Query Language
|
||||
|
||||
### Courses
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
The "Learn Spring Security" Classes: http://github.learnspringsecurity.com
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [REST Query Language with Spring and JPA Criteria](http://www.baeldung.com/rest-search-language-spring-jpa-criteria)
|
||||
- [REST Query Language with Spring Data JPA Specifications](http://www.baeldung.com/rest-api-search-language-spring-data-specifications)
|
||||
- [REST Query Language with Spring Data JPA and QueryDSL](http://www.baeldung.com/rest-api-search-language-spring-data-querydsl)
|
||||
- [REST Query Language – Advanced Search Operations](http://www.baeldung.com/rest-api-query-search-language-more-operations)
|
||||
- [REST Query Language with RSQL](http://www.baeldung.com/rest-api-search-language-rsql-fiql)
|
||||
- [REST Query Language – Implementing OR Operation](http://www.baeldung.com/rest-api-query-search-or-operation)
|
||||
|
||||
|
||||
|
||||
|
||||
### Build the Project
|
||||
```
|
||||
mvn clean install
|
||||
```
|
||||
|
||||
|
||||
### Set up MySQL
|
||||
```
|
||||
mysql -u root -p
|
||||
> CREATE USER 'tutorialuser'@'localhost' IDENTIFIED BY 'tutorialmy5ql';
|
||||
> GRANT ALL PRIVILEGES ON *.* TO 'tutorialuser'@'localhost';
|
||||
> FLUSH PRIVILEGES;
|
||||
```
|
||||
|
|
@ -2,10 +2,10 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-security-rest-full</artifactId>
|
||||
<artifactId>spring-rest-query-language</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<name>spring-security-rest-full</name>
|
||||
<name>spring-rest-query-language</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
|
@ -230,7 +230,7 @@
|
|||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-security-rest-full</finalName>
|
||||
<finalName>spring-rest-query-language</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
|
@ -0,0 +1,94 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String email;
|
||||
|
||||
private int age;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(final String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(final String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(final String username) {
|
||||
email = username;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(final int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((email == null) ? 0 : email.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final User user = (User) obj;
|
||||
return email.equals(user.email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(email).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package org.baeldung.spring;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.support.SpringBootServletInitializer;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.web.context.request.RequestContextListener;
|
||||
|
||||
/**
|
||||
* Main Application Class - uses Spring Boot. Just run this as a normal Java
|
||||
* class to run up a Jetty Server (on http://localhost:8082/spring-rest-query-language)
|
||||
*
|
||||
*/
|
||||
@EnableScheduling
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan("org.baeldung")
|
||||
@SpringBootApplication
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(Application.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext sc) throws ServletException {
|
||||
// Manages the lifecycle of the root application context
|
||||
sc.addListener(new RequestContextListener());
|
||||
}
|
||||
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
package org.baeldung.spring;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-${envTarget:h2}.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence" })
|
||||
// @ImportResource("classpath*:springDataPersistenceConfig.xml")
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
// vendorAdapter.set
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
em.setJpaProperties(additionalProperties());
|
||||
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager() {
|
||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
||||
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties additionalProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
return hibernateProperties;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package org.baeldung.spring;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.web")
|
||||
@EnableWebMvc
|
||||
public class WebConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
public WebConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
||||
viewResolver.setPrefix("/WEB-INF/view/");
|
||||
viewResolver.setSuffix(".jsp");
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
// API
|
||||
@Override
|
||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||
super.addViewControllers(registry);
|
||||
registry.addViewController("/homepage.html");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package org.baeldung.web.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
@RequestMapping(value = "/")
|
||||
public class HomeController {
|
||||
|
||||
public String index() {
|
||||
return "homepage";
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue