Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
[submodule "testgitrepo"]
|
||||
path = testgitrepo
|
||||
url = /home/prd/Development/projects/idea/tutorials/spring-boot/src/main/resources/testgitrepo/
|
||||
+27
-27
@@ -1,52 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<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>cdi</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring.version>4.3.1.RELEASE</spring.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.8.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jboss.weld.se</groupId>
|
||||
<artifactId>weld-se-core</artifactId>
|
||||
<version>2.3.5.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.8.9</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.jboss.weld.se/weld-se-core -->
|
||||
<dependency>
|
||||
<groupId>org.jboss.weld.se</groupId>
|
||||
<artifactId>weld-se-core</artifactId>
|
||||
<version>2.3.5.Final</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring.version>4.3.1.RELEASE</spring.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.baeldung.interceptor;
|
||||
|
||||
import javax.interceptor.InterceptorBinding;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.interceptor.InterceptorBinding;
|
||||
|
||||
@InterceptorBinding
|
||||
@Target( {ElementType.METHOD, ElementType.TYPE } )
|
||||
@Retention(RetentionPolicy.RUNTIME )
|
||||
public @interface Audited {}
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Audited {
|
||||
}
|
||||
|
||||
@@ -3,12 +3,13 @@ package com.baeldung.interceptor;
|
||||
import javax.interceptor.AroundInvoke;
|
||||
import javax.interceptor.Interceptor;
|
||||
import javax.interceptor.InvocationContext;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@Audited @Interceptor
|
||||
@Audited
|
||||
@Interceptor
|
||||
public class AuditedInterceptor {
|
||||
public static boolean calledBefore = false;
|
||||
public static boolean calledAfter = false;
|
||||
|
||||
@AroundInvoke
|
||||
public Object auditMethod(InvocationContext ctx) throws Exception {
|
||||
calledBefore = true;
|
||||
|
||||
@@ -5,6 +5,6 @@ import com.baeldung.interceptor.Audited;
|
||||
public class SuperService {
|
||||
@Audited
|
||||
public String deliverService(String uid) {
|
||||
return uid;
|
||||
return uid;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,23 @@
|
||||
package com.baeldung.spring.aspect;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import java.util.List;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
|
||||
@Aspect
|
||||
public class SpringTestAspect {
|
||||
@Autowired
|
||||
private List<String> accumulator;
|
||||
|
||||
@Around("execution(* com.baeldung.spring.service.SpringSuperService.*(..))")
|
||||
public Object advice(ProceedingJoinPoint jp) throws Throwable {
|
||||
public Object auditMethod(ProceedingJoinPoint jp) throws Throwable {
|
||||
String methodName = jp.getSignature().getName();
|
||||
accumulator.add("Call to "+methodName);
|
||||
accumulator.add("Call to " + methodName);
|
||||
Object obj = jp.proceed();
|
||||
accumulator.add("Method called successfully: "+methodName);
|
||||
accumulator.add("Method called successfully: " + methodName);
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import com.baeldung.spring.aspect.SpringTestAspect;
|
||||
import com.baeldung.spring.service.SpringSuperService;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.baeldung.spring.aspect.SpringTestAspect;
|
||||
import com.baeldung.spring.service.SpringSuperService;
|
||||
|
||||
@Configuration
|
||||
@EnableAspectJAutoProxy
|
||||
@@ -18,12 +19,12 @@ public class AppConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SpringTestAspect springTestAspect(){
|
||||
public SpringTestAspect springTestAspect() {
|
||||
return new SpringTestAspect();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public List<String> getAccumulator(){
|
||||
public List<String> getAccumulator() {
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.baeldung.spring.service;
|
||||
|
||||
public class SpringSuperService {
|
||||
public String getInfoFromService(String code){
|
||||
public String getInfoFromService(String code) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import com.baeldung.interceptor.Audited;
|
||||
import com.baeldung.interceptor.AuditedInterceptor;
|
||||
import com.baeldung.service.SuperService;
|
||||
import org.jboss.weld.environment.se.Weld;
|
||||
import org.jboss.weld.environment.se.WeldContainer;
|
||||
import org.junit.After;
|
||||
@@ -10,24 +7,21 @@ import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.enterprise.inject.spi.BeanManager;
|
||||
import javax.enterprise.inject.spi.InterceptionType;
|
||||
import javax.enterprise.inject.spi.Interceptor;
|
||||
import javax.enterprise.util.AnnotationLiteral;
|
||||
|
||||
import static javafx.beans.binding.Bindings.select;
|
||||
import com.baeldung.interceptor.AuditedInterceptor;
|
||||
import com.baeldung.service.SuperService;
|
||||
|
||||
public class TestInterceptor {
|
||||
Weld weld;
|
||||
WeldContainer container;
|
||||
|
||||
@Before
|
||||
public void init(){
|
||||
public void init() {
|
||||
weld = new Weld();
|
||||
container = weld.initialize();
|
||||
}
|
||||
|
||||
@After
|
||||
public void shutdown(){
|
||||
public void shutdown() {
|
||||
weld.shutdown();
|
||||
}
|
||||
|
||||
@@ -36,7 +30,9 @@ public class TestInterceptor {
|
||||
SuperService superService = container.select(SuperService.class).get();
|
||||
String code = "123456";
|
||||
superService.deliverService(code);
|
||||
|
||||
Assert.assertTrue(AuditedInterceptor.calledBefore);
|
||||
Assert.assertTrue(AuditedInterceptor.calledAfter);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,25 +1,21 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import com.baeldung.spring.configuration.AppConfig;
|
||||
import com.baeldung.spring.service.SpringSuperService;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import com.baeldung.spring.configuration.AppConfig;
|
||||
import com.baeldung.spring.service.SpringSuperService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {AppConfig.class})
|
||||
@ContextConfiguration(classes = { AppConfig.class })
|
||||
public class TestSpringInterceptor {
|
||||
@Autowired
|
||||
SpringSuperService springSuperService;
|
||||
@@ -28,11 +24,11 @@ public class TestSpringInterceptor {
|
||||
private List<String> accumulator;
|
||||
|
||||
@Test
|
||||
public void givenService_whenServiceAndAspectExecuted_thenOk(){
|
||||
public void givenService_whenServiceAndAspectExecuted_thenOk() {
|
||||
String code = "123456";
|
||||
String result = springSuperService.getInfoFromService(code);
|
||||
Assert.assertThat(accumulator.size(), is(2));
|
||||
Assert.assertThat(accumulator.get(0),is("Call to getInfoFromService"));
|
||||
Assert.assertThat(accumulator.get(1),is("Method called successfully: getInfoFromService"));
|
||||
Assert.assertThat(accumulator.get(0), is("Call to getInfoFromService"));
|
||||
Assert.assertThat(accumulator.get(1), is("Method called successfully: getInfoFromService"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
package com.baeldung.completablefuture;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CompletableFutureTest {
|
||||
|
||||
@Test
|
||||
public void whenRunningCompletableFutureAsynchronously_thenGetMethodWaitsForResult() throws InterruptedException, ExecutionException {
|
||||
|
||||
Future<String> completableFuture = calculateAsync();
|
||||
|
||||
String result = completableFuture.get();
|
||||
assertEquals("Hello", result);
|
||||
|
||||
}
|
||||
|
||||
public Future<String> calculateAsync() throws InterruptedException {
|
||||
CompletableFuture<String> completableFuture = new CompletableFuture<>();
|
||||
|
||||
Executors.newCachedThreadPool().submit(() -> {
|
||||
Thread.sleep(500);
|
||||
completableFuture.complete("Hello");
|
||||
return null;
|
||||
});
|
||||
|
||||
return completableFuture;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRunningCompletableFutureWithResult_thenGetMethodReturnsImmediately() throws InterruptedException, ExecutionException {
|
||||
|
||||
Future<String> completableFuture = CompletableFuture.completedFuture("Hello");
|
||||
|
||||
String result = completableFuture.get();
|
||||
assertEquals("Hello", result);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Future<String> calculateAsyncWithCancellation() throws InterruptedException {
|
||||
CompletableFuture<String> completableFuture = new CompletableFuture<>();
|
||||
|
||||
Executors.newCachedThreadPool().submit(() -> {
|
||||
Thread.sleep(500);
|
||||
completableFuture.cancel(false);
|
||||
return null;
|
||||
});
|
||||
|
||||
return completableFuture;
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = CancellationException.class)
|
||||
public void whenCancelingTheFuture_thenThrowsCancellationException() throws ExecutionException, InterruptedException {
|
||||
|
||||
Future<String> future = calculateAsyncWithCancellation();
|
||||
future.get();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingCompletableFutureWithSupplyAsync_thenFutureReturnsValue() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
|
||||
|
||||
assertEquals("Hello", future.get());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddingThenAcceptToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
|
||||
|
||||
CompletableFuture<Void> future = completableFuture.thenAccept(s -> System.out.println("Computation returned: " + s));
|
||||
|
||||
future.get();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddingThenRunToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
|
||||
|
||||
CompletableFuture<Void> future = completableFuture.thenRun(() -> System.out.println("Computation finished."));
|
||||
|
||||
future.get();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddingThenApplyToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
|
||||
|
||||
CompletableFuture<String> future = completableFuture.thenApply(s -> s + " World");
|
||||
|
||||
assertEquals("Hello World", future.get());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingThenCompose_thenFuturesExecuteSequentially() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello")
|
||||
.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World"));
|
||||
|
||||
assertEquals("Hello World", completableFuture.get());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingThenCombine_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello")
|
||||
.thenCombine(CompletableFuture.supplyAsync(() -> " World"),
|
||||
(s1, s2) -> s1 + s2);
|
||||
|
||||
assertEquals("Hello World", completableFuture.get());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingThenAcceptBoth_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture.supplyAsync(() -> "Hello")
|
||||
.thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"),
|
||||
(s1, s2) -> System.out.println(s1 + s2));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFutureCombinedWithAllOfCompletes_thenAllFuturesAreDone() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
|
||||
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "Beautiful");
|
||||
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> "World");
|
||||
|
||||
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3);
|
||||
|
||||
// ...
|
||||
|
||||
combinedFuture.get();
|
||||
|
||||
assertTrue(future1.isDone());
|
||||
assertTrue(future2.isDone());
|
||||
assertTrue(future3.isDone());
|
||||
|
||||
String combined = Stream.of(future1, future2, future3)
|
||||
.map(CompletableFuture::join)
|
||||
.collect(Collectors.joining(" "));
|
||||
|
||||
assertEquals("Hello Beautiful World", combined);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFutureThrows_thenHandleMethodReceivesException() throws ExecutionException, InterruptedException {
|
||||
|
||||
String name = null;
|
||||
|
||||
// ...
|
||||
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
if (name == null) {
|
||||
throw new RuntimeException("Computation error!");
|
||||
}
|
||||
return "Hello, " + name;
|
||||
}).handle((s, t) -> s != null ? s : "Hello, Stranger!");
|
||||
|
||||
assertEquals("Hello, Stranger!", completableFuture.get());
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = ExecutionException.class)
|
||||
public void whenCompletingFutureExceptionally_thenGetMethodThrows() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> completableFuture = new CompletableFuture<>();
|
||||
|
||||
// ...
|
||||
|
||||
completableFuture.completeExceptionally(new RuntimeException("Calculation failed!"));
|
||||
|
||||
// ...
|
||||
|
||||
completableFuture.get();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddingThenApplyAsyncToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
|
||||
|
||||
CompletableFuture<String> future = completableFuture.thenApplyAsync(s -> s + " World");
|
||||
|
||||
assertEquals("Hello World", future.get());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+24
-16
@@ -12,9 +12,9 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.0.RELEASE</version>
|
||||
<relativePath></relativePath>
|
||||
</parent>
|
||||
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- General -->
|
||||
@@ -33,60 +33,68 @@
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
||||
|
||||
<hystrix-metrics-event-stream.version>1.3.16</hystrix-metrics-event-stream.version>
|
||||
<hystrix-dashboard.version>1.4.3</hystrix-dashboard.version>
|
||||
<spring-boot-starter-test.version>1.4.0.RELEASE</spring-boot-starter-test.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.netflix.hystrix</groupId>
|
||||
<artifactId>hystrix-core</artifactId>
|
||||
<version>${hystrix-core.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.netflix.hystrix</groupId>
|
||||
<artifactId>hystrix-metrics-event-stream</artifactId>
|
||||
<version>1.3.16</version>
|
||||
<version>${hystrix-metrics-event-stream.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-dashboard -->
|
||||
<!--<dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.hystrix</groupId>
|
||||
<artifactId>hystrix-dashboard</artifactId>
|
||||
<version>1.4.3</version>
|
||||
</dependency>-->
|
||||
|
||||
<version>${hystrix-dashboard.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.rxjava</groupId>
|
||||
<artifactId>rxjava-core</artifactId>
|
||||
<version>${rxjava-core.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>${hamcrest-all.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring-boot-starter-test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -17,6 +17,28 @@ public class HystrixAspect {
|
||||
private HystrixCommandProperties.Setter commandProperties;
|
||||
private HystrixThreadPoolProperties.Setter threadPoolProperties;
|
||||
|
||||
@Value("${remoteservice.command.execution.timeout}")
|
||||
private int executionTimeout;
|
||||
|
||||
@Value("${remoteservice.command.sleepwindow}")
|
||||
private int sleepWindow;
|
||||
|
||||
@Value("${remoteservice.command.threadpool.maxsize}")
|
||||
private int maxThreadCount;
|
||||
|
||||
@Value("${remoteservice.command.threadpool.coresize}")
|
||||
private int coreThreadCount;
|
||||
|
||||
@Value("${remoteservice.command.task.queue.size}")
|
||||
private int queueCount;
|
||||
|
||||
@Value("${remoteservice.command.group.key}")
|
||||
private String groupKey;
|
||||
|
||||
@Value("${remoteservice.command.key}")
|
||||
private String key;
|
||||
|
||||
|
||||
@Around("@annotation(com.baeldung.hystrix.HystrixCircuitBreaker)")
|
||||
public Object circuitBreakerAround(final ProceedingJoinPoint aJoinPoint) {
|
||||
return new RemoteServiceCommand(config, aJoinPoint).execute();
|
||||
@@ -31,7 +53,7 @@ public class HystrixAspect {
|
||||
this.commandProperties.withExecutionTimeoutInMilliseconds(executionTimeout);
|
||||
this.commandProperties.withCircuitBreakerSleepWindowInMilliseconds(sleepWindow);
|
||||
|
||||
this.threadPoolProperties= HystrixThreadPoolProperties.Setter();
|
||||
this.threadPoolProperties = HystrixThreadPoolProperties.Setter();
|
||||
this.threadPoolProperties.withMaxQueueSize(maxThreadCount).withCoreSize(coreThreadCount).withMaxQueueSize(queueCount);
|
||||
|
||||
this.config.andCommandPropertiesDefaults(commandProperties);
|
||||
@@ -58,24 +80,4 @@ public class HystrixAspect {
|
||||
}
|
||||
}
|
||||
|
||||
@Value("${remoteservice.command.execution.timeout}")
|
||||
private int executionTimeout;
|
||||
|
||||
@Value("${remoteservice.command.sleepwindow}")
|
||||
private int sleepWindow;
|
||||
|
||||
@Value("${remoteservice.command.threadpool.maxsize}")
|
||||
private int maxThreadCount;
|
||||
|
||||
@Value("${remoteservice.command.threadpool.coresize}")
|
||||
private int coreThreadCount;
|
||||
|
||||
@Value("${remoteservice.command.task.queue.size}")
|
||||
private int queueCount;
|
||||
|
||||
@Value("${remoteservice.command.group.key}")
|
||||
private String groupKey;
|
||||
|
||||
@Value("${remoteservice.command.key}")
|
||||
private String key;
|
||||
}
|
||||
|
||||
@@ -10,8 +10,13 @@ public class HystrixController {
|
||||
@Autowired
|
||||
private SpringExistingClient client;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String index() throws InterruptedException{
|
||||
return client.invokeRemoteService();
|
||||
@RequestMapping("/withHystrix")
|
||||
public String withHystrix() throws InterruptedException{
|
||||
return client.invokeRemoteServiceWithHystrix();
|
||||
}
|
||||
|
||||
@RequestMapping("/withOutHystrix")
|
||||
public String withOutHystrix() throws InterruptedException{
|
||||
return client.invokeRemoteServiceWithOutHystrix();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,11 @@ public class SpringExistingClient {
|
||||
private int remoteServiceDelay;
|
||||
|
||||
@HystrixCircuitBreaker
|
||||
public String invokeRemoteService() throws InterruptedException{
|
||||
public String invokeRemoteServiceWithHystrix() throws InterruptedException{
|
||||
return new RemoteServiceTestSimulator(remoteServiceDelay).execute();
|
||||
}
|
||||
|
||||
public String invokeRemoteServiceWithOutHystrix() throws InterruptedException{
|
||||
return new RemoteServiceTestSimulator(remoteServiceDelay).execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ remoteservice.command.threadpool.coresize=5
|
||||
remoteservice.command.threadpool.maxsize=10
|
||||
remoteservice.command.task.queue.size=5
|
||||
remoteservice.command.sleepwindow=5000
|
||||
remoteservice.timeout=5000
|
||||
remoteservice.timeout=15000
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandProperties;
|
||||
import com.netflix.hystrix.HystrixThreadPoolProperties;
|
||||
import com.netflix.hystrix.exception.HystrixRuntimeException;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@FixMethodOrder(MethodSorters.JVM)
|
||||
public class HystrixTimeShortCircuitTest {
|
||||
|
||||
private HystrixCommand.Setter config;
|
||||
private HystrixCommandProperties.Setter commandProperties;
|
||||
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
commandProperties = HystrixCommandProperties.Setter();
|
||||
config = HystrixCommand
|
||||
.Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess()
|
||||
throws InterruptedException {
|
||||
|
||||
commandProperties.withExecutionTimeoutInMilliseconds(1000);
|
||||
|
||||
commandProperties.withCircuitBreakerSleepWindowInMilliseconds(4000);
|
||||
commandProperties.withExecutionIsolationStrategy(
|
||||
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
|
||||
commandProperties.withCircuitBreakerEnabled(true);
|
||||
commandProperties.withCircuitBreakerRequestVolumeThreshold(1);
|
||||
|
||||
config.andCommandPropertiesDefaults(commandProperties);
|
||||
|
||||
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
|
||||
.withMaxQueueSize(1)
|
||||
.withCoreSize(1)
|
||||
.withQueueSizeRejectionThreshold(1));
|
||||
|
||||
assertThat(this.invokeRemoteService(10000), equalTo(null));
|
||||
assertThat(this.invokeRemoteService(10000), equalTo(null));
|
||||
Thread.sleep(5000);
|
||||
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
|
||||
equalTo("Success"));
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
|
||||
equalTo("Success"));
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
|
||||
equalTo("Success"));
|
||||
}
|
||||
|
||||
String invokeRemoteService(long timeout) throws InterruptedException {
|
||||
String response = null;
|
||||
try {
|
||||
response = new RemoteServiceTestCommand(config,
|
||||
new RemoteServiceTestSimulator(timeout)).execute();
|
||||
} catch (HystrixRuntimeException ex) {
|
||||
System.out.println("ex = " + ex);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,18 +5,18 @@ import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandProperties;
|
||||
import com.netflix.hystrix.HystrixThreadPoolProperties;
|
||||
import com.netflix.hystrix.exception.HystrixRuntimeException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@FixMethodOrder(MethodSorters.JVM)
|
||||
public class HystrixTimeoutTest {
|
||||
|
||||
private HystrixCommand.Setter config;
|
||||
private HystrixCommandProperties.Setter commandProperties ;
|
||||
private HystrixCommandProperties.Setter commandProperties;
|
||||
|
||||
|
||||
@Rule
|
||||
@@ -26,37 +26,45 @@ public class HystrixTimeoutTest {
|
||||
public void setup() {
|
||||
commandProperties = HystrixCommandProperties.Setter();
|
||||
config = HystrixCommand
|
||||
.Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1"));
|
||||
.Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputBob_andDefaultSettings_thenReturnHelloBob(){
|
||||
public void givenInputBobAndDefaultSettings_whenExecuted_thenReturnHelloBob(){
|
||||
assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServiceTimeoutEqualTo100_andDefaultSettings_thenReturnSuccess() throws InterruptedException {
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(), equalTo("Success"));
|
||||
public void givenSvcTimeoutOf100AndDefaultSettings_whenExecuted_thenReturnSuccess()
|
||||
throws InterruptedException {
|
||||
|
||||
HystrixCommand.Setter config = HystrixCommand
|
||||
.Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1"));
|
||||
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(),
|
||||
equalTo("Success"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServiceTimeoutEqualTo10000_andDefaultSettings_thenExpectHRE() throws InterruptedException {
|
||||
public void givenSvcTimeoutOf10000AndDefaultSettings__whenExecuted_thenExpectHRE() throws InterruptedException {
|
||||
exception.expect(HystrixRuntimeException.class);
|
||||
new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServiceTimeoutEqualTo500_andExecutionTimeoutEqualTo10000_thenReturnSuccess()
|
||||
public void givenSvcTimeoutOf5000AndExecTimeoutOf10000__whenExecuted_thenReturnSuccess()
|
||||
throws InterruptedException {
|
||||
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
|
||||
config.andCommandPropertiesDefaults(commandProperties);
|
||||
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
|
||||
equalTo("Success"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServiceTimeoutEqualTo15000_andExecutionTimeoutEqualTo5000_thenExpectHRE()
|
||||
public void givenSvcTimeoutOf15000AndExecTimeoutOf5000__whenExecuted_thenExpectHRE()
|
||||
throws InterruptedException {
|
||||
exception.expect(HystrixRuntimeException.class);
|
||||
commandProperties.withExecutionTimeoutInMilliseconds(5_000);
|
||||
@@ -65,7 +73,7 @@ public class HystrixTimeoutTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServiceTimeoutEqual_andExecutionTimeout_andThreadPool_thenReturnSuccess()
|
||||
public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool__whenExecuted_thenReturnSuccess()
|
||||
throws InterruptedException {
|
||||
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
|
||||
config.andCommandPropertiesDefaults(commandProperties);
|
||||
@@ -73,49 +81,8 @@ public class HystrixTimeoutTest {
|
||||
.withMaxQueueSize(10)
|
||||
.withCoreSize(3)
|
||||
.withQueueSizeRejectionThreshold(10));
|
||||
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
|
||||
equalTo("Success"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCircuitBreakerSetup_thenReturnSuccess() throws InterruptedException {
|
||||
|
||||
commandProperties.withExecutionTimeoutInMilliseconds(1000);
|
||||
|
||||
commandProperties.withCircuitBreakerSleepWindowInMilliseconds(4000);
|
||||
commandProperties.withExecutionIsolationStrategy(
|
||||
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
|
||||
commandProperties.withCircuitBreakerEnabled(true);
|
||||
commandProperties.withCircuitBreakerRequestVolumeThreshold(1);
|
||||
|
||||
config.andCommandPropertiesDefaults(commandProperties);
|
||||
|
||||
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
|
||||
.withMaxQueueSize(1)
|
||||
.withCoreSize(1)
|
||||
.withQueueSizeRejectionThreshold(1));
|
||||
|
||||
assertThat(this.invokeRemoteService(10000), equalTo(null));
|
||||
assertThat(this.invokeRemoteService(10000), equalTo(null));
|
||||
Thread.sleep(5000);
|
||||
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
|
||||
equalTo("Success"));
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
|
||||
equalTo("Success"));
|
||||
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
|
||||
equalTo("Success"));
|
||||
}
|
||||
|
||||
public String invokeRemoteService(long timeout) throws InterruptedException{
|
||||
String response = null;
|
||||
try{
|
||||
response = new RemoteServiceTestCommand(config,
|
||||
new RemoteServiceTestSimulator(timeout)).execute();
|
||||
}catch(HystrixRuntimeException ex){
|
||||
System.out.println("ex = " + ex);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.exception.HystrixRuntimeException;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = AppConfig.class)
|
||||
public class SpringAndHystrixIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private HystrixController hystrixController;
|
||||
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void givenTimeOutOf15000_whenClientCalledWithHystrix_thenExpectHystrixRuntimeException() throws InterruptedException {
|
||||
exception.expect(HystrixRuntimeException.class);
|
||||
hystrixController.withHystrix();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimeOutOf15000_whenClientCalledWithOutHystrix_thenExpectSuccess() throws InterruptedException {
|
||||
assertThat(hystrixController.withOutHystrix(), equalTo("Success"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<name>mapstruct</name>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<org.mapstruct.version>1.0.0.Final</org.mapstruct.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-jdk8</artifactId>
|
||||
<version>${org.mapstruct.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>mapstruct</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${org.mapstruct.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.baeldung.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
|
||||
private int employeeId;
|
||||
private String employeeName;
|
||||
private int divisionId;
|
||||
private String divisionName;
|
||||
|
||||
public int getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public void setEmployeeId(int employeeId) {
|
||||
this.employeeId = employeeId;
|
||||
}
|
||||
|
||||
public String getEmployeeName() {
|
||||
return employeeName;
|
||||
}
|
||||
|
||||
public void setEmployeeName(String employeeName) {
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
public int getDivisionId() {
|
||||
return divisionId;
|
||||
}
|
||||
|
||||
public void setDivisionId(int divisionId) {
|
||||
this.divisionId = divisionId;
|
||||
}
|
||||
|
||||
public String getDivisionName() {
|
||||
return divisionName;
|
||||
}
|
||||
|
||||
public void setDivisionName(String divisionName) {
|
||||
this.divisionName = divisionName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.baeldung.dto;
|
||||
|
||||
public class SimpleSource {
|
||||
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.baeldung.entity;
|
||||
|
||||
public class Division {
|
||||
|
||||
public Division() {
|
||||
}
|
||||
|
||||
public Division(int id, String name) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.baeldung.entity;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private Division division;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Division getDivision() {
|
||||
return division;
|
||||
}
|
||||
|
||||
public void setDivision(Division division) {
|
||||
this.division = division;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.baeldung.entity;
|
||||
|
||||
public class SimpleDestination {
|
||||
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.baeldung.mapper;
|
||||
|
||||
import org.baeldung.dto.EmployeeDTO;
|
||||
import org.baeldung.entity.Employee;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
|
||||
@Mapper
|
||||
public interface EmployeeMapper {
|
||||
|
||||
@Mappings({
|
||||
@Mapping(target="divisionId",source="entity.division.id"),
|
||||
@Mapping(target="divisionName",source="entity.division.name"),
|
||||
@Mapping(target="employeeId",source="entity.id"),
|
||||
@Mapping(target="employeeName",source="entity.name")
|
||||
})
|
||||
EmployeeDTO employeeToEmployeeDTO(Employee entity);
|
||||
|
||||
@Mappings({
|
||||
@Mapping(target="id",source="dto.employeeId"),
|
||||
@Mapping(target="name",source="dto.employeeName"),
|
||||
@Mapping(target="division",expression="java(new org.baeldung.entity.Division(dto.getDivisionId(),dto.getDivisionName()))")
|
||||
})
|
||||
Employee employeeDTOtoEmployee(EmployeeDTO dto);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.baeldung.mapper;
|
||||
|
||||
import org.baeldung.dto.SimpleSource;
|
||||
import org.baeldung.entity.SimpleDestination;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SimpleSourceDestinationMapper {
|
||||
|
||||
SimpleDestination sourceToDestination(SimpleSource source);
|
||||
SimpleSource destinationToSource(SimpleDestination destination);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.baeldung.mapper;
|
||||
|
||||
import org.baeldung.dto.SimpleSource;
|
||||
import org.baeldung.entity.SimpleDestination;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper(componentModel="spring")
|
||||
public interface SimpleSourceDestinationSpringMapper {
|
||||
|
||||
SimpleDestination sourceToDestination(SimpleSource source);
|
||||
SimpleSource destinationToSource(SimpleDestination destination);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.baeldung.mapper;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.baeldung.dto.EmployeeDTO;
|
||||
import org.baeldung.entity.Division;
|
||||
import org.baeldung.entity.Employee;
|
||||
import org.junit.Test;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
public class EmployeeMapperTest {
|
||||
|
||||
@Test
|
||||
public void givenEmployeeDTOtoEmployee_whenMaps_thenCorrect(){
|
||||
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
|
||||
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setDivisionId(1);
|
||||
dto.setDivisionName("IT Division");
|
||||
dto.setEmployeeId(1);
|
||||
dto.setEmployeeName("John");
|
||||
|
||||
Employee entity = mapper.employeeDTOtoEmployee(dto);
|
||||
|
||||
assertEquals(dto.getDivisionId(), entity.getDivision().getId());
|
||||
assertEquals(dto.getDivisionName(), entity.getDivision().getName());
|
||||
assertEquals(dto.getEmployeeId(),entity.getId());
|
||||
assertEquals(dto.getEmployeeName(),entity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeetoEmployeeDTO_whenMaps_thenCorrect(){
|
||||
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
|
||||
|
||||
Employee entity = new Employee();
|
||||
entity.setDivision(new Division(1,"IT Division"));
|
||||
entity.setId(1);
|
||||
entity.setName("John");
|
||||
|
||||
EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity);
|
||||
|
||||
assertEquals(dto.getDivisionId(), entity.getDivision().getId());
|
||||
assertEquals(dto.getDivisionName(), entity.getDivision().getName());
|
||||
assertEquals(dto.getEmployeeId(),entity.getId());
|
||||
assertEquals(dto.getEmployeeName(),entity.getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.baeldung.mapper;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.baeldung.dto.SimpleSource;
|
||||
import org.baeldung.entity.SimpleDestination;
|
||||
import org.junit.Test;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
public class SimpleSourceDestinationMapperTest {
|
||||
|
||||
@Test
|
||||
public void givenSimpleSourceToSimpleDestination_whenMaps_thenCorrect() {
|
||||
SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers
|
||||
.getMapper(SimpleSourceDestinationMapper.class);
|
||||
|
||||
SimpleSource simpleSource = new SimpleSource();
|
||||
simpleSource.setName("SourceName");
|
||||
simpleSource.setDescription("SourceDescription");
|
||||
|
||||
SimpleDestination destination =
|
||||
simpleSourceDestinationMapper.sourceToDestination(simpleSource);
|
||||
|
||||
assertEquals(simpleSource.getName(), destination.getName());
|
||||
assertEquals(simpleSource.getDescription(), destination.getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSimpleDestinationToSourceDestination_whenMaps_thenCorrect() {
|
||||
SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers
|
||||
.getMapper(SimpleSourceDestinationMapper.class);
|
||||
|
||||
SimpleDestination destination = new SimpleDestination();
|
||||
destination.setName("DestinationName");
|
||||
destination.setDescription("DestinationDescription");
|
||||
|
||||
SimpleSource source =
|
||||
simpleSourceDestinationMapper.destinationToSource(destination);
|
||||
|
||||
assertEquals(destination.getName(), source.getName());
|
||||
assertEquals(destination.getDescription(), source.getDescription());
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user