mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 01:26:35 +00:00
add orchestrator
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.iluwatar.saga.orchestration;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SagaApplicationTest {
|
||||
|
||||
@Test
|
||||
public void mainTest() {
|
||||
SagaApplication.main(new String[]{});
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
package com.iluwatar.saga.orchestration;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SagaOrchestratorInternallyTest {
|
||||
|
||||
private List<String> records = new ArrayList<>();
|
||||
|
||||
@Test
|
||||
public void execute() {
|
||||
SagaOrchestrator sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery());
|
||||
Saga.Result result = sagaOrchestrator.execute(1);
|
||||
Assert.assertEquals(result, Saga.Result.ROLLBACK);
|
||||
Assert.assertArrayEquals(
|
||||
records.toArray(new String[]{}),
|
||||
new String[]{"+1","+2","+3","+4","-4","-3","-2","-1"});
|
||||
}
|
||||
|
||||
private static Saga newSaga() {
|
||||
return Saga
|
||||
.create()
|
||||
.chapter("1")
|
||||
.chapter("2")
|
||||
.chapter("3")
|
||||
.chapter("4");
|
||||
}
|
||||
|
||||
private ServiceDiscoveryService serviceDiscovery() {
|
||||
return
|
||||
new ServiceDiscoveryService()
|
||||
.discover(new Service1())
|
||||
.discover(new Service2())
|
||||
.discover(new Service3())
|
||||
.discover(new Service4());
|
||||
}
|
||||
|
||||
class Service1 extends Service<Integer> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "1";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChapterResult<Integer> process(Integer value) {
|
||||
records.add("+1");
|
||||
return ChapterResult.success(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChapterResult<Integer> rollback(Integer value) {
|
||||
records.add("-1");
|
||||
return ChapterResult.success(value);
|
||||
}
|
||||
}
|
||||
|
||||
class Service2 extends Service<Integer> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "2";
|
||||
}
|
||||
@Override
|
||||
public ChapterResult<Integer> process(Integer value) {
|
||||
records.add("+2");
|
||||
return ChapterResult.success(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChapterResult<Integer> rollback(Integer value) {
|
||||
records.add("-2");
|
||||
return ChapterResult.success(value);
|
||||
}
|
||||
}
|
||||
|
||||
class Service3 extends Service<Integer> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "3";
|
||||
}
|
||||
@Override
|
||||
public ChapterResult<Integer> process(Integer value) {
|
||||
records.add("+3");
|
||||
return ChapterResult.success(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChapterResult<Integer> rollback(Integer value) {
|
||||
records.add("-3");
|
||||
return ChapterResult.success(value);
|
||||
}
|
||||
}
|
||||
|
||||
class Service4 extends Service<Integer> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "4";
|
||||
}
|
||||
@Override
|
||||
public ChapterResult<Integer> process(Integer value) {
|
||||
records.add("+4");
|
||||
return ChapterResult.failure(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChapterResult<Integer> rollback(Integer value) {
|
||||
records.add("-4");
|
||||
return ChapterResult.success(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.iluwatar.saga.orchestration;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SagaOrchestratorTest {
|
||||
|
||||
@Test
|
||||
public void execute() {
|
||||
SagaOrchestrator sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery());
|
||||
Saga.Result badOrder = sagaOrchestrator.execute("bad_order");
|
||||
Saga.Result crashedOrder = sagaOrchestrator.execute("crashed_order");
|
||||
|
||||
Assert.assertEquals(badOrder, Saga.Result.ROLLBACK);
|
||||
Assert.assertEquals(crashedOrder, Saga.Result.CRASHED);
|
||||
}
|
||||
|
||||
private static Saga newSaga() {
|
||||
return Saga
|
||||
.create()
|
||||
.chapter("init an order")
|
||||
.chapter("booking a Fly")
|
||||
.chapter("booking a Hotel")
|
||||
.chapter("withdrawing Money");
|
||||
}
|
||||
|
||||
private static ServiceDiscoveryService serviceDiscovery() {
|
||||
return
|
||||
new ServiceDiscoveryService()
|
||||
.discover(new OrderService())
|
||||
.discover(new FlyBookingService())
|
||||
.discover(new HotelBookingService())
|
||||
.discover(new WithdrawMoneyService());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user