Merge branch 'master' into JAVA-3530-move-spring-mvc-webflow
This commit is contained in:
commit
48b2da7f16
|
@ -1,13 +1,13 @@
|
|||
package com.baeldung.spring.data.jpa.query.datetime;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.spring.data.jpa.query;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class QueryApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(QueryApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
insert into Users(id, name, creation_date, last_login_date, active, age, email, status)
|
||||
values(1, 'John', TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('01/01/2020', 'DD/MM/YYYY'), 1, 23, 'john@email.com', 1);
|
||||
|
||||
insert into Users(id, name, creation_date, last_login_date, active, age, email, status)
|
||||
values(2, 'Bob', TO_DATE('02/02/2019', 'DD/MM/YYYY'), TO_DATE('02/02/2020', 'DD/MM/YYYY'), 1, 56, 'bob@email.com', 1);
|
||||
|
||||
insert into Users(id, name, creation_date, last_login_date, active, age, email, status)
|
||||
values(3, 'Cindy', TO_DATE('02/02/2019', 'DD/MM/YYYY'), TO_DATE('02/02/2020', 'DD/MM/YYYY'), 1, 18, 'cindy@email.com', 0);
|
|
@ -0,0 +1,162 @@
|
|||
package com.baeldung.spring.data.jpa.query;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.JpaSort;
|
||||
import org.springframework.data.mapping.PropertyReferenceException;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest(properties = "spring.datasource.data=classpath:insert_users.sql")
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
public void whenFindAllActiveUsersThenAllActiveFound() {
|
||||
Collection<User> allActiveUsers = userRepository.findAllActiveUsers();
|
||||
assertThat(allActiveUsers).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindAllActiveUsersNativeThenAllActiveFound() {
|
||||
Collection<User> allActiveUsers = userRepository.findAllActiveUsersNative();
|
||||
assertThat(allActiveUsers).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindAllSortedByNameThenAllSorted() {
|
||||
List<User> allUsersSortedByName = userRepository.findAll(Sort.by(Sort.Direction.ASC, "name"));
|
||||
assertThat(allUsersSortedByName)
|
||||
.extracting("name")
|
||||
.containsSequence("Bob", "Cindy", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindAllSortedByNameLengthThenException() {
|
||||
assertThatThrownBy(() -> userRepository.findAll(Sort.by("LENGTH(name)")))
|
||||
.isInstanceOf(PropertyReferenceException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindAllUsersSortedByNameThenAllSorted() {
|
||||
List<User> allUsersSortedByName = userRepository.findAllUsers(Sort.by(Sort.Direction.ASC, "name"));
|
||||
assertThat(allUsersSortedByName)
|
||||
.extracting("name")
|
||||
.containsSequence("Bob", "Cindy", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindAllUsersSortedByNameLengthThenAllSorted() {
|
||||
List<User> allUsersSortedByName = userRepository.findAllUsers(JpaSort.unsafe("LENGTH(name)"));
|
||||
assertThat(allUsersSortedByName)
|
||||
.extracting("name")
|
||||
.containsSequence("Bob", "John", "Cindy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindAllUsersWithPaginationThenPaginated() {
|
||||
Page<User> page = userRepository.findAllUsersWithPagination(PageRequest.of(0, 1));
|
||||
assertThat(page.stream().map(User::getId))
|
||||
.hasSize(1)
|
||||
.containsOnly(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindAllUsersWithPaginationNativeThenPaginated() {
|
||||
Page<User> page = userRepository.findAllUsersWithPaginationNative(PageRequest.of(1, 1));
|
||||
assertThat(page.stream().map(User::getId))
|
||||
.hasSize(1)
|
||||
.containsOnly(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindUserByStatusThenFound() {
|
||||
User user = userRepository.findUserByStatus(0);
|
||||
assertThat(user.getStatus()).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindUserByStatusAndNameThenFound() {
|
||||
User user = userRepository.findUserByStatusAndName(1, "John");
|
||||
assertThat(user.getStatus()).isOne();
|
||||
assertThat(user.getName()).isEqualTo("John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindUserByStatusNativeThenFound() {
|
||||
User user = userRepository.findUserByStatusNative(0);
|
||||
assertThat(user.getStatus()).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindUserByStatusAndNameNamedParamsThenFound() {
|
||||
User user = userRepository.findUserByStatusAndNameNamedParams(1, "John");
|
||||
assertThat(user.getStatus()).isOne();
|
||||
assertThat(user.getName()).isEqualTo("John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindUserByUserStatusAndUserNameThenFound() {
|
||||
User user = userRepository.findUserByUserStatusAndUserName(1, "John");
|
||||
assertThat(user.getStatus()).isOne();
|
||||
assertThat(user.getName()).isEqualTo("John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindUserByStatusAndNameNamedParamsNativeThenFound() {
|
||||
User user = userRepository.findUserByStatusAndNameNamedParamsNative(1, "Bob");
|
||||
assertThat(user.getStatus()).isOne();
|
||||
assertThat(user.getName()).isEqualTo("Bob");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindUserByNameListThenAllFound() {
|
||||
List<User> users = userRepository.findUserByNameList(Arrays.asList("Bob", "Cindy"));
|
||||
assertThat(users)
|
||||
.extracting("name")
|
||||
.containsOnly("Bob", "Cindy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdateUserSetStatusForNameThenUpdated() {
|
||||
int updated = userRepository.updateUserSetStatusForName(0, "John");
|
||||
assertThat(updated).isOne();
|
||||
|
||||
User john = userRepository.findUserByStatusAndName(0, "John");
|
||||
assertThat(john).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdateUserSetStatusForNameNativeThenUpdated() {
|
||||
int updated = userRepository.updateUserSetStatusForNameNative(0, "John");
|
||||
assertThat(updated).isOne();
|
||||
|
||||
User john = userRepository.findUserByStatusAndName(0, "John");
|
||||
assertThat(john).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertUserThenInserted() {
|
||||
User beforeInsert = userRepository.findUserByStatusAndName(0, "Mandy");
|
||||
assertThat(beforeInsert).isNull();
|
||||
|
||||
userRepository.insertUser("Mandy", 20, "mandy@email.com", 0, true);
|
||||
|
||||
User afterInsert = userRepository.findUserByStatusAndName(0, "Mandy");
|
||||
assertThat(afterInsert).isNotNull();
|
||||
}
|
||||
}
|
4
pom.xml
4
pom.xml
|
@ -671,13 +671,11 @@
|
|||
|
||||
<module>spring-reactor</module>
|
||||
<module>spring-remoting</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-http-2</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
<module>spring-resttemplate-2</module>
|
||||
<module>spring-rest-testing</module>
|
||||
<module>spring-roo</module>
|
||||
|
||||
|
@ -1140,12 +1138,10 @@
|
|||
|
||||
<module>spring-reactor</module>
|
||||
<module>spring-remoting</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
<module>spring-resttemplate-2</module>
|
||||
<module>spring-rest-testing</module>
|
||||
<module>spring-roo</module>
|
||||
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.method.info;
|
||||
|
||||
public class Account {
|
||||
|
||||
private String accountNumber;
|
||||
private double balance;
|
||||
|
||||
public String getAccountNumber() {
|
||||
return accountNumber;
|
||||
}
|
||||
|
||||
public void setAccountNumber(String accountNumber) {
|
||||
this.accountNumber = accountNumber;
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public void setBalance(double balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Account{" + "accountNumber='" + accountNumber + '\'' + ", balance=" + balance + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.method.info;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AccountOperation {
|
||||
String operation();
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.method.info;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class BankAccountAspect {
|
||||
|
||||
@Before(value = "@annotation(com.baeldung.method.info.AccountOperation)")
|
||||
public void getAccountOperationInfo(JoinPoint joinPoint) {
|
||||
|
||||
// Method Information
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
|
||||
System.out.println("full method description: " + signature.getMethod());
|
||||
|
||||
System.out.println("method name: " + signature.getMethod().getName());
|
||||
|
||||
System.out.println("declaring type: " + signature.getDeclaringType());
|
||||
|
||||
// Method args
|
||||
System.out.println("Method args names:");
|
||||
Arrays.stream(signature.getParameterNames())
|
||||
.forEach(s -> System.out.println("arg name: " + s));
|
||||
|
||||
System.out.println("Method args types:");
|
||||
Arrays.stream(signature.getParameterTypes())
|
||||
.forEach(s -> System.out.println("arg type: " + s));
|
||||
|
||||
System.out.println("Method args values:");
|
||||
Arrays.stream(joinPoint.getArgs())
|
||||
.forEach(o -> System.out.println("arg value: " + o.toString()));
|
||||
|
||||
// Additional Information
|
||||
System.out.println("returning type: " + signature.getReturnType());
|
||||
System.out.println("method modifier: " + Modifier.toString(signature.getModifiers()));
|
||||
Arrays.stream(signature.getExceptionTypes())
|
||||
.forEach(aClass -> System.out.println("exception type: " + aClass));
|
||||
|
||||
// Method annotation
|
||||
Method method = signature.getMethod();
|
||||
AccountOperation accountOperation = method.getAnnotation(AccountOperation.class);
|
||||
System.out.println("Account operation annotation: " + accountOperation);
|
||||
System.out.println("Account operation value: " + accountOperation.operation());
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.method.info;
|
||||
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class BankAccountService {
|
||||
|
||||
@AccountOperation(operation = "deposit")
|
||||
public void deposit(Account account, Double amount) {
|
||||
account.setBalance(account.getBalance() + amount);
|
||||
}
|
||||
|
||||
@AccountOperation(operation = "withdraw")
|
||||
public void withdraw(Account account, Double amount) throws WithdrawLimitException {
|
||||
|
||||
if (amount > 500.0) {
|
||||
throw new WithdrawLimitException("Withdraw limit exceeded.");
|
||||
}
|
||||
|
||||
account.setBalance(account.getBalance() - amount);
|
||||
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return RandomUtils.nextDouble();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.method.info;
|
||||
|
||||
public class WithdrawLimitException extends RuntimeException {
|
||||
public WithdrawLimitException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.method.info;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@SpringBootTest
|
||||
class BankAccountServiceIntegrationTest {
|
||||
|
||||
private Account account;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
account = new Account();
|
||||
account.setAccountNumber("12345");
|
||||
account.setBalance(2000.0);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
BankAccountService bankAccountService;
|
||||
|
||||
@Test
|
||||
void withdraw() {
|
||||
bankAccountService.withdraw(account, 500.0);
|
||||
assertTrue(account.getBalance() == 1500.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withdrawWhenLimitReached() {
|
||||
Assertions.assertThatExceptionOfType(WithdrawLimitException.class)
|
||||
.isThrownBy(() -> bankAccountService.withdraw(account, 600.0));
|
||||
assertTrue(account.getBalance() == 2000.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deposit() {
|
||||
bankAccountService.deposit(account, 500.0);
|
||||
assertTrue(account.getBalance() == 2500.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBalance() {
|
||||
bankAccountService.getBalance();
|
||||
}
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
### Relevant Articles:
|
||||
- [Introduction to Spring Cloud Netflix – Eureka](http://www.baeldung.com/spring-cloud-netflix-eureka)
|
||||
- [Integration Tests With Spring Cloud Netflix and Feign](https://www.baeldung.com/spring-cloud-feign-integration-tests)
|
||||
|
|
|
@ -88,7 +88,7 @@
|
|||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<forkCount>1</forkCount>
|
||||
<reuseForks>true</reuseForks>
|
||||
<reuseForks>false</reuseForks>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
|
|
@ -25,7 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ContextConfiguration(classes = { MockBookServiceConfig.class }, initializers = { EurekaContainerConfig.Initializer.class })
|
||||
class ServiceDiscoveryBooksClientIntegrationTest {
|
||||
class ServiceDiscoveryBooksClientLiveTest {
|
||||
|
||||
@Autowired
|
||||
private BooksClient booksClient;
|
|
@ -21,7 +21,9 @@
|
|||
<module>spring-mvc-crash</module>
|
||||
<module>spring-mvc-forms-jsp</module>
|
||||
<module>spring-mvc-webflow</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-resttemplate-2</module>
|
||||
<module>spring-thymeleaf</module>
|
||||
<module>spring-thymeleaf-2</module>
|
||||
<module>spring-thymeleaf-3</module>
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
|
@ -11,7 +11,7 @@
|
|||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
<relativePath>parent-boot-2/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
Loading…
Reference in New Issue