Java 11 migration: patterns (remaining b-c) (#1081)
* Moves business-delegate pattern to java 11 * Moves bytecode pattern to java 11 * Moves caching pattern to java 11 * Moves callback pattern to java 11 * Moves chain pattern to java 11 * Moves circuit-breaker pattern to java 11 * Moves collection-pipeline pattern to java 11 * Moves command pattern to java 11 * Moves commander pattern to java 11 * Moves composite pattern to java 11 * Corrects test cases
This commit is contained in:
parent
6ef840f3cf
commit
33ea7335b1
@ -46,15 +46,15 @@ public class App {
|
|||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
BusinessDelegate businessDelegate = new BusinessDelegate();
|
var businessDelegate = new BusinessDelegate();
|
||||||
BusinessLookup businessLookup = new BusinessLookup();
|
var businessLookup = new BusinessLookup();
|
||||||
businessLookup.setEjbService(new EjbService());
|
businessLookup.setEjbService(new EjbService());
|
||||||
businessLookup.setJmsService(new JmsService());
|
businessLookup.setJmsService(new JmsService());
|
||||||
|
|
||||||
businessDelegate.setLookupService(businessLookup);
|
businessDelegate.setLookupService(businessLookup);
|
||||||
businessDelegate.setServiceType(ServiceType.EJB);
|
businessDelegate.setServiceType(ServiceType.EJB);
|
||||||
|
|
||||||
Client client = new Client(businessDelegate);
|
var client = new Client(businessDelegate);
|
||||||
client.doTask();
|
client.doTask();
|
||||||
|
|
||||||
businessDelegate.setServiceType(ServiceType.JMS);
|
businessDelegate.setServiceType(ServiceType.JMS);
|
||||||
|
@ -76,7 +76,7 @@ public class BusinessDelegateTest {
|
|||||||
public void testBusinessDelegate() {
|
public void testBusinessDelegate() {
|
||||||
|
|
||||||
// setup a client object
|
// setup a client object
|
||||||
Client client = new Client(businessDelegate);
|
var client = new Client(businessDelegate);
|
||||||
|
|
||||||
// set the service type
|
// set the service type
|
||||||
businessDelegate.setServiceType(ServiceType.EJB);
|
businessDelegate.setServiceType(ServiceType.EJB);
|
||||||
|
@ -24,7 +24,6 @@
|
|||||||
package com.iluwatar.bytecode;
|
package com.iluwatar.bytecode;
|
||||||
|
|
||||||
import com.iluwatar.bytecode.util.InstructionConverterUtil;
|
import com.iluwatar.bytecode.util.InstructionConverterUtil;
|
||||||
import java.util.Stack;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -51,12 +50,12 @@ public class App {
|
|||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Wizard wizard = new Wizard();
|
var wizard = new Wizard();
|
||||||
wizard.setHealth(45);
|
wizard.setHealth(45);
|
||||||
wizard.setAgility(7);
|
wizard.setAgility(7);
|
||||||
wizard.setWisdom(11);
|
wizard.setWisdom(11);
|
||||||
|
|
||||||
VirtualMachine vm = new VirtualMachine();
|
var vm = new VirtualMachine();
|
||||||
vm.getWizards()[0] = wizard;
|
vm.getWizards()[0] = wizard;
|
||||||
|
|
||||||
interpretInstruction("LITERAL 0", vm);
|
interpretInstruction("LITERAL 0", vm);
|
||||||
@ -74,9 +73,8 @@ public class App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void interpretInstruction(String instruction, VirtualMachine vm) {
|
private static void interpretInstruction(String instruction, VirtualMachine vm) {
|
||||||
InstructionConverterUtil converter = new InstructionConverterUtil();
|
vm.execute(InstructionConverterUtil.convertToByteCode(instruction));
|
||||||
vm.execute(converter.convertToByteCode(instruction));
|
var stack = vm.getStack();
|
||||||
Stack<Integer> stack = vm.getStack();
|
|
||||||
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "") + stack);
|
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "") + stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ public enum Instruction {
|
|||||||
ADD(10),
|
ADD(10),
|
||||||
DIVIDE(11);
|
DIVIDE(11);
|
||||||
|
|
||||||
private int value;
|
private final int value;
|
||||||
|
|
||||||
Instruction(int value) {
|
Instruction(int value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
@ -57,7 +57,7 @@ public enum Instruction {
|
|||||||
* @return representation of the instruction
|
* @return representation of the instruction
|
||||||
*/
|
*/
|
||||||
public static Instruction getInstruction(int value) {
|
public static Instruction getInstruction(int value) {
|
||||||
for (int i = 0; i < Instruction.values().length; i++) {
|
for (var i = 0; i < Instruction.values().length; i++) {
|
||||||
if (Instruction.values()[i].getIntValue() == value) {
|
if (Instruction.values()[i].getIntValue() == value) {
|
||||||
return Instruction.values()[i];
|
return Instruction.values()[i];
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ import java.util.Stack;
|
|||||||
*/
|
*/
|
||||||
public class VirtualMachine {
|
public class VirtualMachine {
|
||||||
|
|
||||||
private Stack<Integer> stack = new Stack();
|
private Stack<Integer> stack = new Stack<>();
|
||||||
|
|
||||||
private Wizard[] wizards = new Wizard[2];
|
private Wizard[] wizards = new Wizard[2];
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ public class VirtualMachine {
|
|||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public VirtualMachine() {
|
public VirtualMachine() {
|
||||||
for (int i = 0; i < wizards.length; i++) {
|
for (var i = 0; i < wizards.length; i++) {
|
||||||
wizards[i] = new Wizard();
|
wizards[i] = new Wizard();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,10 +49,8 @@ public class VirtualMachine {
|
|||||||
* @param bytecode to execute
|
* @param bytecode to execute
|
||||||
*/
|
*/
|
||||||
public void execute(int[] bytecode) {
|
public void execute(int[] bytecode) {
|
||||||
for (int i = 0; i < bytecode.length; i++) {
|
for (var i = 0; i < bytecode.length; i++) {
|
||||||
Instruction instruction = Instruction.getInstruction(bytecode[i]);
|
Instruction instruction = Instruction.getInstruction(bytecode[i]);
|
||||||
int wizard;
|
|
||||||
int amount;
|
|
||||||
switch (instruction) {
|
switch (instruction) {
|
||||||
case LITERAL:
|
case LITERAL:
|
||||||
// Read the next byte from the bytecode.
|
// Read the next byte from the bytecode.
|
||||||
@ -60,8 +58,8 @@ public class VirtualMachine {
|
|||||||
stack.push(value);
|
stack.push(value);
|
||||||
break;
|
break;
|
||||||
case SET_AGILITY:
|
case SET_AGILITY:
|
||||||
amount = stack.pop();
|
var amount = stack.pop();
|
||||||
wizard = stack.pop();
|
var wizard = stack.pop();
|
||||||
setAgility(wizard, amount);
|
setAgility(wizard, amount);
|
||||||
break;
|
break;
|
||||||
case SET_WISDOM:
|
case SET_WISDOM:
|
||||||
@ -87,8 +85,8 @@ public class VirtualMachine {
|
|||||||
stack.push(getWisdom(wizard));
|
stack.push(getWisdom(wizard));
|
||||||
break;
|
break;
|
||||||
case ADD:
|
case ADD:
|
||||||
int a = stack.pop();
|
var a = stack.pop();
|
||||||
int b = stack.pop();
|
var b = stack.pop();
|
||||||
stack.push(a + b);
|
stack.push(a + b);
|
||||||
break;
|
break;
|
||||||
case DIVIDE:
|
case DIVIDE:
|
||||||
|
@ -40,15 +40,15 @@ public class InstructionConverterUtil {
|
|||||||
return new int[0];
|
return new int[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] splitedInstructions = instructions.trim().split(" ");
|
var splitedInstructions = instructions.trim().split(" ");
|
||||||
int[] bytecode = new int[splitedInstructions.length];
|
var bytecode = new int[splitedInstructions.length];
|
||||||
for (int i = 0; i < splitedInstructions.length; i++) {
|
for (var i = 0; i < splitedInstructions.length; i++) {
|
||||||
if (isValidInstruction(splitedInstructions[i])) {
|
if (isValidInstruction(splitedInstructions[i])) {
|
||||||
bytecode[i] = Instruction.valueOf(splitedInstructions[i]).getIntValue();
|
bytecode[i] = Instruction.valueOf(splitedInstructions[i]).getIntValue();
|
||||||
} else if (isValidInt(splitedInstructions[i])) {
|
} else if (isValidInt(splitedInstructions[i])) {
|
||||||
bytecode[i] = Integer.valueOf(splitedInstructions[i]);
|
bytecode[i] = Integer.parseInt(splitedInstructions[i]);
|
||||||
} else {
|
} else {
|
||||||
String errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
|
var errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
|
||||||
throw new IllegalArgumentException(errorMessage);
|
throw new IllegalArgumentException(errorMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,6 @@ public class AppTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
App.main(new String[]{});
|
||||||
App.main(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,11 +36,11 @@ public class VirtualMachineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLiteral() {
|
public void testLiteral() {
|
||||||
int[] bytecode = new int[2];
|
var bytecode = new int[2];
|
||||||
bytecode[0] = LITERAL.getIntValue();
|
bytecode[0] = LITERAL.getIntValue();
|
||||||
bytecode[1] = 10;
|
bytecode[1] = 10;
|
||||||
|
|
||||||
VirtualMachine vm = new VirtualMachine();
|
var vm = new VirtualMachine();
|
||||||
vm.execute(bytecode);
|
vm.execute(bytecode);
|
||||||
|
|
||||||
assertEquals(1, vm.getStack().size());
|
assertEquals(1, vm.getStack().size());
|
||||||
@ -49,15 +49,15 @@ public class VirtualMachineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetHealth() {
|
public void testSetHealth() {
|
||||||
int wizardNumber = 0;
|
var wizardNumber = 0;
|
||||||
int[] bytecode = new int[5];
|
var bytecode = new int[5];
|
||||||
bytecode[0] = LITERAL.getIntValue();
|
bytecode[0] = LITERAL.getIntValue();
|
||||||
bytecode[1] = wizardNumber;
|
bytecode[1] = wizardNumber;
|
||||||
bytecode[2] = LITERAL.getIntValue();
|
bytecode[2] = LITERAL.getIntValue();
|
||||||
bytecode[3] = 50; // health amount
|
bytecode[3] = 50; // health amount
|
||||||
bytecode[4] = SET_HEALTH.getIntValue();
|
bytecode[4] = SET_HEALTH.getIntValue();
|
||||||
|
|
||||||
VirtualMachine vm = new VirtualMachine();
|
var vm = new VirtualMachine();
|
||||||
vm.execute(bytecode);
|
vm.execute(bytecode);
|
||||||
|
|
||||||
assertEquals(50, vm.getWizards()[wizardNumber].getHealth());
|
assertEquals(50, vm.getWizards()[wizardNumber].getHealth());
|
||||||
@ -65,15 +65,15 @@ public class VirtualMachineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetAgility() {
|
public void testSetAgility() {
|
||||||
int wizardNumber = 0;
|
var wizardNumber = 0;
|
||||||
int[] bytecode = new int[5];
|
var bytecode = new int[5];
|
||||||
bytecode[0] = LITERAL.getIntValue();
|
bytecode[0] = LITERAL.getIntValue();
|
||||||
bytecode[1] = wizardNumber;
|
bytecode[1] = wizardNumber;
|
||||||
bytecode[2] = LITERAL.getIntValue();
|
bytecode[2] = LITERAL.getIntValue();
|
||||||
bytecode[3] = 50; // agility amount
|
bytecode[3] = 50; // agility amount
|
||||||
bytecode[4] = SET_AGILITY.getIntValue();
|
bytecode[4] = SET_AGILITY.getIntValue();
|
||||||
|
|
||||||
VirtualMachine vm = new VirtualMachine();
|
var vm = new VirtualMachine();
|
||||||
vm.execute(bytecode);
|
vm.execute(bytecode);
|
||||||
|
|
||||||
assertEquals(50, vm.getWizards()[wizardNumber].getAgility());
|
assertEquals(50, vm.getWizards()[wizardNumber].getAgility());
|
||||||
@ -81,15 +81,15 @@ public class VirtualMachineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetWisdom() {
|
public void testSetWisdom() {
|
||||||
int wizardNumber = 0;
|
var wizardNumber = 0;
|
||||||
int[] bytecode = new int[5];
|
var bytecode = new int[5];
|
||||||
bytecode[0] = LITERAL.getIntValue();
|
bytecode[0] = LITERAL.getIntValue();
|
||||||
bytecode[1] = wizardNumber;
|
bytecode[1] = wizardNumber;
|
||||||
bytecode[2] = LITERAL.getIntValue();
|
bytecode[2] = LITERAL.getIntValue();
|
||||||
bytecode[3] = 50; // wisdom amount
|
bytecode[3] = 50; // wisdom amount
|
||||||
bytecode[4] = SET_WISDOM.getIntValue();
|
bytecode[4] = SET_WISDOM.getIntValue();
|
||||||
|
|
||||||
VirtualMachine vm = new VirtualMachine();
|
var vm = new VirtualMachine();
|
||||||
vm.execute(bytecode);
|
vm.execute(bytecode);
|
||||||
|
|
||||||
assertEquals(50, vm.getWizards()[wizardNumber].getWisdom());
|
assertEquals(50, vm.getWizards()[wizardNumber].getWisdom());
|
||||||
@ -97,8 +97,8 @@ public class VirtualMachineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetHealth() {
|
public void testGetHealth() {
|
||||||
int wizardNumber = 0;
|
var wizardNumber = 0;
|
||||||
int[] bytecode = new int[8];
|
var bytecode = new int[8];
|
||||||
bytecode[0] = LITERAL.getIntValue();
|
bytecode[0] = LITERAL.getIntValue();
|
||||||
bytecode[1] = wizardNumber;
|
bytecode[1] = wizardNumber;
|
||||||
bytecode[2] = LITERAL.getIntValue();
|
bytecode[2] = LITERAL.getIntValue();
|
||||||
@ -108,7 +108,7 @@ public class VirtualMachineTest {
|
|||||||
bytecode[6] = wizardNumber;
|
bytecode[6] = wizardNumber;
|
||||||
bytecode[7] = GET_HEALTH.getIntValue();
|
bytecode[7] = GET_HEALTH.getIntValue();
|
||||||
|
|
||||||
VirtualMachine vm = new VirtualMachine();
|
var vm = new VirtualMachine();
|
||||||
vm.execute(bytecode);
|
vm.execute(bytecode);
|
||||||
|
|
||||||
assertEquals(Integer.valueOf(50), vm.getStack().pop());
|
assertEquals(Integer.valueOf(50), vm.getStack().pop());
|
||||||
@ -116,13 +116,13 @@ public class VirtualMachineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPlaySound() {
|
public void testPlaySound() {
|
||||||
int wizardNumber = 0;
|
var wizardNumber = 0;
|
||||||
int[] bytecode = new int[3];
|
var bytecode = new int[3];
|
||||||
bytecode[0] = LITERAL.getIntValue();
|
bytecode[0] = LITERAL.getIntValue();
|
||||||
bytecode[1] = wizardNumber;
|
bytecode[1] = wizardNumber;
|
||||||
bytecode[2] = PLAY_SOUND.getIntValue();
|
bytecode[2] = PLAY_SOUND.getIntValue();
|
||||||
|
|
||||||
VirtualMachine vm = new VirtualMachine();
|
var vm = new VirtualMachine();
|
||||||
vm.execute(bytecode);
|
vm.execute(bytecode);
|
||||||
|
|
||||||
assertEquals(0, vm.getStack().size());
|
assertEquals(0, vm.getStack().size());
|
||||||
@ -131,13 +131,13 @@ public class VirtualMachineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSpawnParticles() {
|
public void testSpawnParticles() {
|
||||||
int wizardNumber = 0;
|
var wizardNumber = 0;
|
||||||
int[] bytecode = new int[3];
|
var bytecode = new int[3];
|
||||||
bytecode[0] = LITERAL.getIntValue();
|
bytecode[0] = LITERAL.getIntValue();
|
||||||
bytecode[1] = wizardNumber;
|
bytecode[1] = wizardNumber;
|
||||||
bytecode[2] = SPAWN_PARTICLES.getIntValue();
|
bytecode[2] = SPAWN_PARTICLES.getIntValue();
|
||||||
|
|
||||||
VirtualMachine vm = new VirtualMachine();
|
var vm = new VirtualMachine();
|
||||||
vm.execute(bytecode);
|
vm.execute(bytecode);
|
||||||
|
|
||||||
assertEquals(0, vm.getStack().size());
|
assertEquals(0, vm.getStack().size());
|
||||||
@ -146,9 +146,9 @@ public class VirtualMachineTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidInstruction() {
|
public void testInvalidInstruction() {
|
||||||
int[] bytecode = new int[1];
|
var bytecode = new int[1];
|
||||||
bytecode[0] = 999;
|
bytecode[0] = 999;
|
||||||
VirtualMachine vm = new VirtualMachine();
|
var vm = new VirtualMachine();
|
||||||
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> vm.execute(bytecode));
|
assertThrows(IllegalArgumentException.class, () -> vm.execute(bytecode));
|
||||||
}
|
}
|
||||||
|
@ -34,19 +34,19 @@ import org.junit.jupiter.api.Test;
|
|||||||
public class InstructionConverterUtilTest {
|
public class InstructionConverterUtilTest {
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyInstruction() {
|
public void testEmptyInstruction() {
|
||||||
String instruction = "";
|
var instruction = "";
|
||||||
|
|
||||||
int[] bytecode = InstructionConverterUtil.convertToByteCode(instruction);
|
var bytecode = InstructionConverterUtil.convertToByteCode(instruction);
|
||||||
|
|
||||||
Assertions.assertEquals(0, bytecode.length);
|
Assertions.assertEquals(0, bytecode.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInstructions() {
|
public void testInstructions() {
|
||||||
String instructions =
|
var instructions = "LITERAL 35 SET_HEALTH SET_WISDOM SET_AGILITY PLAY_SOUND"
|
||||||
"LITERAL 35 SET_HEALTH SET_WISDOM SET_AGILITY PLAY_SOUND SPAWN_PARTICLES GET_HEALTH ADD DIVIDE";
|
+ " SPAWN_PARTICLES GET_HEALTH ADD DIVIDE";
|
||||||
|
|
||||||
int[] bytecode = InstructionConverterUtil.convertToByteCode(instructions);
|
var bytecode = InstructionConverterUtil.convertToByteCode(instructions);
|
||||||
|
|
||||||
Assertions.assertEquals(10, bytecode.length);
|
Assertions.assertEquals(10, bytecode.length);
|
||||||
Assertions.assertEquals(Instruction.LITERAL.getIntValue(), bytecode[0]);
|
Assertions.assertEquals(Instruction.LITERAL.getIntValue(), bytecode[0]);
|
||||||
|
@ -76,7 +76,7 @@ public class App {
|
|||||||
// true to run the tests with MongoDB (provided that MongoDB is
|
// true to run the tests with MongoDB (provided that MongoDB is
|
||||||
// installed and socket connection is open).
|
// installed and socket connection is open).
|
||||||
AppManager.initCacheCapacity(3);
|
AppManager.initCacheCapacity(3);
|
||||||
App app = new App();
|
var app = new App();
|
||||||
app.useReadAndWriteThroughStrategy();
|
app.useReadAndWriteThroughStrategy();
|
||||||
app.useReadThroughAndWriteAroundStrategy();
|
app.useReadThroughAndWriteAroundStrategy();
|
||||||
app.useReadThroughAndWriteBehindStrategy();
|
app.useReadThroughAndWriteBehindStrategy();
|
||||||
@ -90,7 +90,7 @@ public class App {
|
|||||||
LOGGER.info("# CachingPolicy.THROUGH");
|
LOGGER.info("# CachingPolicy.THROUGH");
|
||||||
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
|
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
|
||||||
|
|
||||||
UserAccount userAccount1 = new UserAccount("001", "John", "He is a boy.");
|
var userAccount1 = new UserAccount("001", "John", "He is a boy.");
|
||||||
|
|
||||||
AppManager.save(userAccount1);
|
AppManager.save(userAccount1);
|
||||||
LOGGER.info(AppManager.printCacheContent());
|
LOGGER.info(AppManager.printCacheContent());
|
||||||
@ -105,7 +105,7 @@ public class App {
|
|||||||
LOGGER.info("# CachingPolicy.AROUND");
|
LOGGER.info("# CachingPolicy.AROUND");
|
||||||
AppManager.initCachingPolicy(CachingPolicy.AROUND);
|
AppManager.initCachingPolicy(CachingPolicy.AROUND);
|
||||||
|
|
||||||
UserAccount userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
|
var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
|
||||||
|
|
||||||
AppManager.save(userAccount2);
|
AppManager.save(userAccount2);
|
||||||
LOGGER.info(AppManager.printCacheContent());
|
LOGGER.info(AppManager.printCacheContent());
|
||||||
@ -127,9 +127,9 @@ public class App {
|
|||||||
LOGGER.info("# CachingPolicy.BEHIND");
|
LOGGER.info("# CachingPolicy.BEHIND");
|
||||||
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
|
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
|
||||||
|
|
||||||
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
||||||
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
||||||
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
||||||
|
|
||||||
AppManager.save(userAccount3);
|
AppManager.save(userAccount3);
|
||||||
AppManager.save(userAccount4);
|
AppManager.save(userAccount4);
|
||||||
@ -152,9 +152,9 @@ public class App {
|
|||||||
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
|
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
|
||||||
LOGGER.info(AppManager.printCacheContent());
|
LOGGER.info(AppManager.printCacheContent());
|
||||||
|
|
||||||
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
||||||
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
||||||
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
||||||
AppManager.save(userAccount3);
|
AppManager.save(userAccount3);
|
||||||
AppManager.save(userAccount4);
|
AppManager.save(userAccount4);
|
||||||
AppManager.save(userAccount5);
|
AppManager.save(userAccount5);
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
package com.iluwatar.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AppManager helps to bridge the gap in communication between the main class and the application's
|
* AppManager helps to bridge the gap in communication between the main class and the application's
|
||||||
@ -116,16 +117,12 @@ public final class AppManager {
|
|||||||
* Cache-Aside find user account helper.
|
* Cache-Aside find user account helper.
|
||||||
*/
|
*/
|
||||||
private static UserAccount findAside(String userId) {
|
private static UserAccount findAside(String userId) {
|
||||||
UserAccount userAccount = CacheStore.get(userId);
|
return Optional.ofNullable(CacheStore.get(userId))
|
||||||
if (userAccount != null) {
|
.or(() -> {
|
||||||
return userAccount;
|
Optional<UserAccount> userAccount = Optional.ofNullable(DbManager.readFromDb(userId));
|
||||||
}
|
userAccount.ifPresent(account -> CacheStore.set(userId, account));
|
||||||
|
return userAccount;
|
||||||
userAccount = DbManager.readFromDb(userId);
|
})
|
||||||
if (userAccount != null) {
|
.orElse(null);
|
||||||
CacheStore.set(userId, userAccount);
|
|
||||||
}
|
|
||||||
|
|
||||||
return userAccount;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,8 @@
|
|||||||
package com.iluwatar.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -34,7 +36,7 @@ public class CacheStore {
|
|||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(CacheStore.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(CacheStore.class);
|
||||||
|
|
||||||
static LruCache cache;
|
private static LruCache cache;
|
||||||
|
|
||||||
private CacheStore() {
|
private CacheStore() {
|
||||||
}
|
}
|
||||||
@ -134,27 +136,22 @@ public class CacheStore {
|
|||||||
*/
|
*/
|
||||||
public static void flushCache() {
|
public static void flushCache() {
|
||||||
LOGGER.info("# flushCache...");
|
LOGGER.info("# flushCache...");
|
||||||
if (null == cache) {
|
Optional.ofNullable(cache)
|
||||||
return;
|
.map(LruCache::getCacheDataInListForm)
|
||||||
}
|
.orElse(List.of())
|
||||||
List<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
.forEach(DbManager::updateDb);
|
||||||
for (UserAccount userAccount : listOfUserAccounts) {
|
|
||||||
DbManager.upsertDb(userAccount);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print user accounts.
|
* Print user accounts.
|
||||||
*/
|
*/
|
||||||
public static String print() {
|
public static String print() {
|
||||||
List<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
return Optional.ofNullable(cache)
|
||||||
StringBuilder sb = new StringBuilder();
|
.map(LruCache::getCacheDataInListForm)
|
||||||
sb.append("\n--CACHE CONTENT--\n");
|
.orElse(List.of())
|
||||||
for (UserAccount userAccount : listOfUserAccounts) {
|
.stream()
|
||||||
sb.append(userAccount.toString() + "\n");
|
.map(userAccount -> userAccount.toString() + "\n")
|
||||||
}
|
.collect(Collectors.joining("", "\n--CACHE CONTENT--\n", "----\n"));
|
||||||
sb.append("----\n");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,7 +25,6 @@ package com.iluwatar.caching;
|
|||||||
|
|
||||||
import com.iluwatar.caching.constants.CachingConstants;
|
import com.iluwatar.caching.constants.CachingConstants;
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
import com.mongodb.client.FindIterable;
|
|
||||||
import com.mongodb.client.MongoDatabase;
|
import com.mongodb.client.MongoDatabase;
|
||||||
import com.mongodb.client.model.UpdateOptions;
|
import com.mongodb.client.model.UpdateOptions;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
@ -87,7 +86,7 @@ public final class DbManager {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FindIterable<Document> iterable = db
|
var iterable = db
|
||||||
.getCollection(CachingConstants.USER_ACCOUNT)
|
.getCollection(CachingConstants.USER_ACCOUNT)
|
||||||
.find(new Document(CachingConstants.USER_ID, userId));
|
.find(new Document(CachingConstants.USER_ID, userId));
|
||||||
if (iterable == null) {
|
if (iterable == null) {
|
||||||
|
@ -67,7 +67,7 @@ public class LruCache {
|
|||||||
*/
|
*/
|
||||||
public UserAccount get(String userId) {
|
public UserAccount get(String userId) {
|
||||||
if (cache.containsKey(userId)) {
|
if (cache.containsKey(userId)) {
|
||||||
Node node = cache.get(userId);
|
var node = cache.get(userId);
|
||||||
remove(node);
|
remove(node);
|
||||||
setHead(node);
|
setHead(node);
|
||||||
return node.userAccount;
|
return node.userAccount;
|
||||||
@ -111,12 +111,12 @@ public class LruCache {
|
|||||||
*/
|
*/
|
||||||
public void set(String userId, UserAccount userAccount) {
|
public void set(String userId, UserAccount userAccount) {
|
||||||
if (cache.containsKey(userId)) {
|
if (cache.containsKey(userId)) {
|
||||||
Node old = cache.get(userId);
|
var old = cache.get(userId);
|
||||||
old.userAccount = userAccount;
|
old.userAccount = userAccount;
|
||||||
remove(old);
|
remove(old);
|
||||||
setHead(old);
|
setHead(old);
|
||||||
} else {
|
} else {
|
||||||
Node newNode = new Node(userId, userAccount);
|
var newNode = new Node(userId, userAccount);
|
||||||
if (cache.size() >= capacity) {
|
if (cache.size() >= capacity) {
|
||||||
LOGGER.info("# Cache is FULL! Removing {} from cache...", end.userId);
|
LOGGER.info("# Cache is FULL! Removing {} from cache...", end.userId);
|
||||||
cache.remove(end.userId); // remove LRU data from cache.
|
cache.remove(end.userId); // remove LRU data from cache.
|
||||||
@ -137,7 +137,7 @@ public class LruCache {
|
|||||||
* Invalidate cache for user.
|
* Invalidate cache for user.
|
||||||
*/
|
*/
|
||||||
public void invalidate(String userId) {
|
public void invalidate(String userId) {
|
||||||
Node toBeRemoved = cache.remove(userId);
|
var toBeRemoved = cache.remove(userId);
|
||||||
if (toBeRemoved != null) {
|
if (toBeRemoved != null) {
|
||||||
LOGGER.info("# {} has been updated! Removing older version from cache...", userId);
|
LOGGER.info("# {} has been updated! Removing older version from cache...", userId);
|
||||||
remove(toBeRemoved);
|
remove(toBeRemoved);
|
||||||
@ -165,8 +165,8 @@ public class LruCache {
|
|||||||
* Returns cache data in list form.
|
* Returns cache data in list form.
|
||||||
*/
|
*/
|
||||||
public List<UserAccount> getCacheDataInListForm() {
|
public List<UserAccount> getCacheDataInListForm() {
|
||||||
List<UserAccount> listOfCacheData = new ArrayList<>();
|
var listOfCacheData = new ArrayList<UserAccount>();
|
||||||
Node temp = head;
|
var temp = head;
|
||||||
while (temp != null) {
|
while (temp != null) {
|
||||||
listOfCacheData.add(temp.userAccount);
|
listOfCacheData.add(temp.userAccount);
|
||||||
temp = temp.next;
|
temp = temp.next;
|
||||||
|
@ -32,8 +32,7 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
@Test
|
@Test
|
||||||
public void test() throws IOException {
|
public void test() {
|
||||||
String[] args = {};
|
App.main(new String[]{});
|
||||||
App.main(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,22 +27,21 @@ import org.junit.jupiter.api.BeforeEach;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Application test
|
* Application test
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class CachingTest {
|
public class CachingTest {
|
||||||
App app;
|
private App app;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup of application test includes: initializing DB connection and cache size/capacity.
|
* Setup of application test includes: initializing DB connection and cache size/capacity.
|
||||||
*/
|
*/
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
AppManager.initDb(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
|
// VirtualDB (instead of MongoDB) was used in running the JUnit tests
|
||||||
// to avoid Maven compilation errors. Set flag to true to run the
|
// to avoid Maven compilation errors. Set flag to true to run the
|
||||||
// tests with MongoDB (provided that MongoDB is installed and socket
|
// tests with MongoDB (provided that MongoDB is installed and socket
|
||||||
// connection is open).
|
// connection is open).
|
||||||
|
AppManager.initDb(false);
|
||||||
AppManager.initCacheCapacity(3);
|
AppManager.initCacheCapacity(3);
|
||||||
app = new App();
|
app = new App();
|
||||||
}
|
}
|
||||||
|
@ -43,8 +43,7 @@ public final class App {
|
|||||||
* Program entry point.
|
* Program entry point.
|
||||||
*/
|
*/
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
Task task = new SimpleTask();
|
var task = new SimpleTask();
|
||||||
Callback callback = () -> LOGGER.info("I'm done now.");
|
task.executeWith(() -> LOGGER.info("I'm done now."));
|
||||||
task.executeWith(callback);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,8 +42,7 @@ public final class LambdasApp {
|
|||||||
* Program entry point.
|
* Program entry point.
|
||||||
*/
|
*/
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
Task task = new SimpleTask();
|
var task = new SimpleTask();
|
||||||
Callback c = () -> LOGGER.info("I'm done now.");
|
task.executeWith(() -> LOGGER.info("I'm done now."));
|
||||||
task.executeWith(c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
package com.iluwatar.callback;
|
package com.iluwatar.callback;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Template-method class for callback hook execution.
|
* Template-method class for callback hook execution.
|
||||||
*/
|
*/
|
||||||
@ -33,9 +35,7 @@ public abstract class Task {
|
|||||||
*/
|
*/
|
||||||
final void executeWith(final Callback callback) {
|
final void executeWith(final Callback callback) {
|
||||||
execute();
|
execute();
|
||||||
if (callback != null) {
|
Optional.ofNullable(callback).ifPresent(Callback::call);
|
||||||
callback.call();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void execute();
|
public abstract void execute();
|
||||||
|
@ -25,15 +25,12 @@ package com.iluwatar.callback;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that Callback example runs without errors.
|
* Tests that Callback example runs without errors.
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
@Test
|
@Test
|
||||||
public void test() throws IOException {
|
public void test() {
|
||||||
String[] args = {};
|
App.main(new String[]{});
|
||||||
App.main(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,14 +23,14 @@
|
|||||||
|
|
||||||
package com.iluwatar.callback;
|
package com.iluwatar.callback;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a field as a counter. Every time the callback method is called increment this field. Unit
|
* Add a field as a counter. Every time the callback method is called increment this field. Unit
|
||||||
* test checks that the field is being incremented.
|
* test checks that the field is being incremented.
|
||||||
*
|
* <p>
|
||||||
* Could be done with mock objects as well where the call method call is verified.
|
* Could be done with mock objects as well where the call method call is verified.
|
||||||
*/
|
*/
|
||||||
public class CallbackTest {
|
public class CallbackTest {
|
||||||
@ -41,17 +41,17 @@ public class CallbackTest {
|
|||||||
public void test() {
|
public void test() {
|
||||||
Callback callback = () -> callingCount++;
|
Callback callback = () -> callingCount++;
|
||||||
|
|
||||||
Task task = new SimpleTask();
|
var task = new SimpleTask();
|
||||||
|
|
||||||
assertEquals(new Integer(0), callingCount, "Initial calling count of 0");
|
assertEquals(Integer.valueOf(0), callingCount, "Initial calling count of 0");
|
||||||
|
|
||||||
task.executeWith(callback);
|
task.executeWith(callback);
|
||||||
|
|
||||||
assertEquals(new Integer(1), callingCount, "Callback called once");
|
assertEquals(Integer.valueOf(1), callingCount, "Callback called once");
|
||||||
|
|
||||||
task.executeWith(callback);
|
task.executeWith(callback);
|
||||||
|
|
||||||
assertEquals(new Integer(2), callingCount, "Callback called twice");
|
assertEquals(Integer.valueOf(2), callingCount, "Callback called twice");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ public class App {
|
|||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
OrcKing king = new OrcKing();
|
var king = new OrcKing();
|
||||||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
||||||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
|
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
|
||||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||||
|
@ -28,7 +28,7 @@ package com.iluwatar.chain;
|
|||||||
*/
|
*/
|
||||||
public class OrcKing {
|
public class OrcKing {
|
||||||
|
|
||||||
RequestHandler chain;
|
private RequestHandler chain;
|
||||||
|
|
||||||
public OrcKing() {
|
public OrcKing() {
|
||||||
buildChain();
|
buildChain();
|
||||||
|
@ -26,15 +26,12 @@ package com.iluwatar.chain;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Application test
|
* Application test
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
App.main(new String[]{});
|
||||||
App.main(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,10 +23,11 @@
|
|||||||
|
|
||||||
package com.iluwatar.chain;
|
package com.iluwatar.chain;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Date: 12/6/15 - 9:29 PM
|
* Date: 12/6/15 - 9:29 PM
|
||||||
*
|
*
|
||||||
@ -37,24 +38,23 @@ public class OrcKingTest {
|
|||||||
/**
|
/**
|
||||||
* All possible requests
|
* All possible requests
|
||||||
*/
|
*/
|
||||||
private static final Request[] REQUESTS = new Request[]{
|
private static final List<Request> REQUESTS = List.of(
|
||||||
new Request(RequestType.DEFEND_CASTLE, "Don't let the barbarians enter my castle!!"),
|
new Request(RequestType.DEFEND_CASTLE, "Don't let the barbarians enter my castle!!"),
|
||||||
new Request(RequestType.TORTURE_PRISONER, "Don't just stand there, tickle him!"),
|
new Request(RequestType.TORTURE_PRISONER, "Don't just stand there, tickle him!"),
|
||||||
new Request(RequestType.COLLECT_TAX, "Don't steal, the King hates competition ..."),
|
new Request(RequestType.COLLECT_TAX, "Don't steal, the King hates competition ...")
|
||||||
};
|
);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMakeRequest() {
|
public void testMakeRequest() {
|
||||||
final OrcKing king = new OrcKing();
|
final var king = new OrcKing();
|
||||||
|
|
||||||
for (final Request request : REQUESTS) {
|
REQUESTS.forEach(request -> {
|
||||||
king.makeRequest(request);
|
king.makeRequest(request);
|
||||||
assertTrue(
|
assertTrue(
|
||||||
request.isHandled(),
|
request.isHandled(),
|
||||||
"Expected all requests from King to be handled, but [" + request + "] was not!"
|
"Expected all requests from King to be handled, but [" + request + "] was not!"
|
||||||
);
|
);
|
||||||
}
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -24,19 +24,18 @@
|
|||||||
package com.iluwatar.circuitbreaker;
|
package com.iluwatar.circuitbreaker;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Circuit Breaker test
|
* Circuit Breaker test
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class CircuitBreakerTest {
|
public class CircuitBreakerTest {
|
||||||
|
|
||||||
//long timeout, int failureThreshold, long retryTimePeriod
|
//long timeout, int failureThreshold, long retryTimePeriod
|
||||||
@Test
|
@Test
|
||||||
public void testSetState() {
|
public void testSetState() {
|
||||||
var circuitBreaker = new CircuitBreaker(1,1,100);
|
var circuitBreaker = new CircuitBreaker(1, 1, 100);
|
||||||
//Right now, failureCount<failureThreshold, so state should be closed
|
//Right now, failureCount<failureThreshold, so state should be closed
|
||||||
assertEquals(circuitBreaker.getState(), "CLOSED");
|
assertEquals(circuitBreaker.getState(), "CLOSED");
|
||||||
circuitBreaker.failureCount = 4;
|
circuitBreaker.failureCount = 4;
|
||||||
@ -55,23 +54,24 @@ public class CircuitBreakerTest {
|
|||||||
circuitBreaker.setState();
|
circuitBreaker.setState();
|
||||||
assertEquals(circuitBreaker.getState(), "CLOSED");
|
assertEquals(circuitBreaker.getState(), "CLOSED");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetStateForBypass() {
|
public void testSetStateForBypass() {
|
||||||
var circuitBreaker = new CircuitBreaker(1,1,100);
|
var circuitBreaker = new CircuitBreaker(1, 1, 100);
|
||||||
//Right now, failureCount<failureThreshold, so state should be closed
|
//Right now, failureCount<failureThreshold, so state should be closed
|
||||||
//Bypass it and set it to open
|
//Bypass it and set it to open
|
||||||
circuitBreaker.setStateForBypass(State.OPEN);
|
circuitBreaker.setStateForBypass(State.OPEN);
|
||||||
assertEquals(circuitBreaker.getState(), "OPEN");
|
assertEquals(circuitBreaker.getState(), "OPEN");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testApiResponses() {
|
public void testApiResponses() {
|
||||||
var circuitBreaker = new CircuitBreaker(1,1,100);
|
var circuitBreaker = new CircuitBreaker(1, 1, 100);
|
||||||
try {
|
try {
|
||||||
//Call with the paramater start_time set to huge amount of time in past so that service
|
//Call with the paramater start_time set to huge amount of time in past so that service
|
||||||
//replies with "Ok". Also, state is CLOSED in start
|
//replies with "Ok". Also, state is CLOSED in start
|
||||||
var response = circuitBreaker.call("delayedService", System.nanoTime() - 60 * 1000 * 1000 * 1000);
|
var serviceStartTime = System.nanoTime() - 60 * 1000 * 1000 * 1000;
|
||||||
|
var response = circuitBreaker.call("delayedService", serviceStartTime);
|
||||||
assertEquals(response, "Delayed service is working");
|
assertEquals(response, "Delayed service is working");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
|
@ -24,12 +24,11 @@
|
|||||||
package com.iluwatar.circuitbreaker;
|
package com.iluwatar.circuitbreaker;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Monitoring Service test
|
* Monitoring Service test
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class DelayedServiceTest {
|
public class DelayedServiceTest {
|
||||||
|
|
||||||
|
@ -24,12 +24,11 @@
|
|||||||
package com.iluwatar.circuitbreaker;
|
package com.iluwatar.circuitbreaker;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Monitoring Service test
|
* Monitoring Service test
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class MonitoringServiceTest {
|
public class MonitoringServiceTest {
|
||||||
|
|
||||||
@ -40,24 +39,24 @@ public class MonitoringServiceTest {
|
|||||||
var response = monitoringService.localResourceResponse();
|
var response = monitoringService.localResourceResponse();
|
||||||
assertEquals(response, "Local Service is working");
|
assertEquals(response, "Local Service is working");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRemoteResponse() {
|
public void testRemoteResponse() {
|
||||||
var monitoringService = new MonitoringService();
|
var monitoringService = new MonitoringService();
|
||||||
var circuitBreaker = new CircuitBreaker(1,1,100);
|
var circuitBreaker = new CircuitBreaker(1, 1, 100);
|
||||||
//Set time in past to make the server work
|
//Set time in past to make the server work
|
||||||
var serverStartTime = System.nanoTime() / 10;
|
var serverStartTime = System.nanoTime() / 10;
|
||||||
var response = monitoringService.remoteResourceResponse(circuitBreaker, serverStartTime);
|
var response = monitoringService.remoteResourceResponse(circuitBreaker, serverStartTime);
|
||||||
assertEquals(response, "Delayed service is working");
|
assertEquals(response, "Delayed service is working");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRemoteResponse2() {
|
public void testRemoteResponse2() {
|
||||||
MonitoringService monitoringService = new MonitoringService();
|
var monitoringService = new MonitoringService();
|
||||||
CircuitBreaker circuitBreaker = new CircuitBreaker(1,1,100);
|
var circuitBreaker = new CircuitBreaker(1, 1, 100);
|
||||||
//Set time as current time as initially server fails
|
//Set time as current time as initially server fails
|
||||||
long serverStartTime = System.nanoTime();
|
var serverStartTime = System.nanoTime();
|
||||||
String response = monitoringService.remoteResourceResponse(circuitBreaker, serverStartTime);
|
var response = monitoringService.remoteResourceResponse(circuitBreaker, serverStartTime);
|
||||||
assertEquals(response, "Remote service not responding");
|
assertEquals(response, "Remote service not responding");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@
|
|||||||
package com.iluwatar.collectionpipeline;
|
package com.iluwatar.collectionpipeline;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -46,31 +45,26 @@ public class App {
|
|||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
var cars = CarFactory.createCars();
|
||||||
|
|
||||||
List<Car> cars = CarFactory.createCars();
|
var modelsImperative = ImperativeProgramming.getModelsAfter2000(cars);
|
||||||
|
|
||||||
List<String> modelsImperative = ImperativeProgramming.getModelsAfter2000(cars);
|
|
||||||
LOGGER.info(modelsImperative.toString());
|
LOGGER.info(modelsImperative.toString());
|
||||||
|
|
||||||
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
|
var modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
|
||||||
LOGGER.info(modelsFunctional.toString());
|
LOGGER.info(modelsFunctional.toString());
|
||||||
|
|
||||||
Map<Category, List<Car>> groupingByCategoryImperative =
|
var groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
||||||
ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
|
||||||
LOGGER.info(groupingByCategoryImperative.toString());
|
LOGGER.info(groupingByCategoryImperative.toString());
|
||||||
|
|
||||||
Map<Category, List<Car>> groupingByCategoryFunctional =
|
var groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
||||||
FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
|
||||||
LOGGER.info(groupingByCategoryFunctional.toString());
|
LOGGER.info(groupingByCategoryFunctional.toString());
|
||||||
|
|
||||||
Person john = new Person(cars);
|
var john = new Person(cars);
|
||||||
|
|
||||||
List<Car> sedansOwnedImperative =
|
var sedansOwnedImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
||||||
ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
|
||||||
LOGGER.info(sedansOwnedImperative.toString());
|
LOGGER.info(sedansOwnedImperative.toString());
|
||||||
|
|
||||||
List<Car> sedansOwnedFunctional =
|
var sedansOwnedFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
||||||
FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
|
||||||
LOGGER.info(sedansOwnedFunctional.toString());
|
LOGGER.info(sedansOwnedFunctional.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,12 +23,12 @@
|
|||||||
|
|
||||||
package com.iluwatar.collectionpipeline;
|
package com.iluwatar.collectionpipeline;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Imperative-style programming to iterate over the list and get the names of cars made later than
|
* Imperative-style programming to iterate over the list and get the names of cars made later than
|
||||||
@ -57,26 +57,11 @@ public class ImperativeProgramming {
|
|||||||
* @return {@link List} of {@link String} of car models built after year 2000
|
* @return {@link List} of {@link String} of car models built after year 2000
|
||||||
*/
|
*/
|
||||||
public static List<String> getModelsAfter2000(List<Car> cars) {
|
public static List<String> getModelsAfter2000(List<Car> cars) {
|
||||||
List<Car> carsSortedByYear = new ArrayList<>();
|
return cars.stream()
|
||||||
|
.filter(car -> car.getYear() > 2000)
|
||||||
for (Car car : cars) {
|
.sorted(Comparator.comparingInt(Car::getYear))
|
||||||
if (car.getYear() > 2000) {
|
.map(Car::getModel)
|
||||||
carsSortedByYear.add(car);
|
.collect(Collectors.toList());
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Collections.sort(carsSortedByYear, new Comparator<Car>() {
|
|
||||||
public int compare(Car car1, Car car2) {
|
|
||||||
return new Integer(car1.getYear()).compareTo(car2.getYear());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
List<String> models = new ArrayList<>();
|
|
||||||
for (Car car : carsSortedByYear) {
|
|
||||||
models.add(car.getModel());
|
|
||||||
}
|
|
||||||
|
|
||||||
return models;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -86,17 +71,7 @@ public class ImperativeProgramming {
|
|||||||
* @return {@link Map} with category as key and cars belonging to that category as value
|
* @return {@link Map} with category as key and cars belonging to that category as value
|
||||||
*/
|
*/
|
||||||
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
|
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
|
||||||
Map<Category, List<Car>> groupingByCategory = new HashMap<>();
|
return cars.stream().collect(Collectors.groupingBy(Car::getCategory));
|
||||||
for (Car car : cars) {
|
|
||||||
if (groupingByCategory.containsKey(car.getCategory())) {
|
|
||||||
groupingByCategory.get(car.getCategory()).add(car);
|
|
||||||
} else {
|
|
||||||
List<Car> categoryCars = new ArrayList<>();
|
|
||||||
categoryCars.add(car);
|
|
||||||
groupingByCategory.put(car.getCategory(), categoryCars);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return groupingByCategory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,25 +82,11 @@ public class ImperativeProgramming {
|
|||||||
* @return {@link List} of {@link Car} to belonging to the group
|
* @return {@link List} of {@link Car} to belonging to the group
|
||||||
*/
|
*/
|
||||||
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
|
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
|
||||||
List<Car> cars = new ArrayList<>();
|
return persons.stream()
|
||||||
for (Person person : persons) {
|
.map(Person::getCars)
|
||||||
cars.addAll(person.getCars());
|
.flatMap(Collection::stream)
|
||||||
}
|
.filter(car -> car.getCategory() == Category.SEDAN)
|
||||||
|
.sorted(Comparator.comparingInt(Car::getYear))
|
||||||
List<Car> sedanCars = new ArrayList<>();
|
.collect(Collectors.toList());
|
||||||
for (Car car : cars) {
|
|
||||||
if (Category.SEDAN.equals(car.getCategory())) {
|
|
||||||
sedanCars.add(car);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sedanCars.sort(new Comparator<Car>() {
|
|
||||||
@Override
|
|
||||||
public int compare(Car o1, Car o2) {
|
|
||||||
return o1.getYear() - o2.getYear();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return sedanCars;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,12 +54,18 @@ public class AppTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetGroupingOfCarsByCategory() {
|
public void testGetGroupingOfCarsByCategory() {
|
||||||
var modelsExpected = Map.of(
|
var modelsExpected = Map.of(
|
||||||
Category.CONVERTIBLE, List.of(new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE),
|
Category.CONVERTIBLE, List.of(
|
||||||
new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE)),
|
new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE),
|
||||||
Category.SEDAN, List.of(new Car("Dodge", "Avenger", 2010, Category.SEDAN),
|
new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE)
|
||||||
new Car("Ford", "Focus", 2012, Category.SEDAN)),
|
),
|
||||||
Category.JEEP, List.of(new Car("Jeep", "Wrangler", 2011, Category.JEEP),
|
Category.SEDAN, List.of(
|
||||||
new Car("Jeep", "Comanche", 1990, Category.JEEP)));
|
new Car("Dodge", "Avenger", 2010, Category.SEDAN),
|
||||||
|
new Car("Ford", "Focus", 2012, Category.SEDAN)
|
||||||
|
),
|
||||||
|
Category.JEEP, List.of(
|
||||||
|
new Car("Jeep", "Wrangler", 2011, Category.JEEP),
|
||||||
|
new Car("Jeep", "Comanche", 1990, Category.JEEP))
|
||||||
|
);
|
||||||
var modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
var modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
||||||
var modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
var modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
||||||
LOGGER.info("Category " + modelsFunctional);
|
LOGGER.info("Category " + modelsFunctional);
|
||||||
@ -70,8 +76,10 @@ public class AppTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetSedanCarsOwnedSortedByDate() {
|
public void testGetSedanCarsOwnedSortedByDate() {
|
||||||
var john = new Person(cars);
|
var john = new Person(cars);
|
||||||
var modelsExpected = List.of(new Car("Dodge", "Avenger", 2010, Category.SEDAN),
|
var modelsExpected = List.of(
|
||||||
new Car("Ford", "Focus", 2012, Category.SEDAN));
|
new Car("Dodge", "Avenger", 2010, Category.SEDAN),
|
||||||
|
new Car("Ford", "Focus", 2012, Category.SEDAN)
|
||||||
|
);
|
||||||
var modelsFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
var modelsFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
||||||
var modelsImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
var modelsImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
||||||
assertEquals(modelsExpected, modelsFunctional);
|
assertEquals(modelsExpected, modelsFunctional);
|
||||||
|
@ -49,8 +49,8 @@ public class App {
|
|||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Wizard wizard = new Wizard();
|
var wizard = new Wizard();
|
||||||
Goblin goblin = new Goblin();
|
var goblin = new Goblin();
|
||||||
|
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ public class ShrinkSpell extends Command {
|
|||||||
@Override
|
@Override
|
||||||
public void undo() {
|
public void undo() {
|
||||||
if (oldSize != null && target != null) {
|
if (oldSize != null && target != null) {
|
||||||
Size temp = target.getSize();
|
var temp = target.getSize();
|
||||||
target.setSize(oldSize);
|
target.setSize(oldSize);
|
||||||
oldSize = temp;
|
oldSize = temp;
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ public class Wizard {
|
|||||||
*/
|
*/
|
||||||
public void undoLastSpell() {
|
public void undoLastSpell() {
|
||||||
if (!undoStack.isEmpty()) {
|
if (!undoStack.isEmpty()) {
|
||||||
Command previousSpell = undoStack.pollLast();
|
var previousSpell = undoStack.pollLast();
|
||||||
redoStack.offerLast(previousSpell);
|
redoStack.offerLast(previousSpell);
|
||||||
LOGGER.info("{} undoes {}", this, previousSpell);
|
LOGGER.info("{} undoes {}", this, previousSpell);
|
||||||
previousSpell.undo();
|
previousSpell.undo();
|
||||||
@ -68,7 +68,7 @@ public class Wizard {
|
|||||||
*/
|
*/
|
||||||
public void redoLastSpell() {
|
public void redoLastSpell() {
|
||||||
if (!redoStack.isEmpty()) {
|
if (!redoStack.isEmpty()) {
|
||||||
Command previousSpell = redoStack.pollLast();
|
var previousSpell = redoStack.pollLast();
|
||||||
undoStack.offerLast(previousSpell);
|
undoStack.offerLast(previousSpell);
|
||||||
LOGGER.info("{} redoes {}", this, previousSpell);
|
LOGGER.info("{} redoes {}", this, previousSpell);
|
||||||
previousSpell.redo();
|
previousSpell.redo();
|
||||||
|
@ -25,15 +25,12 @@ package com.iluwatar.command;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that Command example runs without errors.
|
* Tests that Command example runs without errors.
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
@Test
|
@Test
|
||||||
public void test() throws IOException {
|
public void test() {
|
||||||
String[] args = {};
|
App.main(new String[]{});
|
||||||
App.main(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,23 +23,23 @@
|
|||||||
|
|
||||||
package com.iluwatar.command;
|
package com.iluwatar.command;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all
|
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all
|
||||||
* information needed to perform an action or trigger an event at a later time. This information
|
* information needed to perform an action or trigger an event at a later time. This information
|
||||||
* includes the method name, the object that owns the method and values for the method parameters.
|
* includes the method name, the object that owns the method and values for the method parameters.
|
||||||
*
|
*
|
||||||
* <p>Four terms always associated with the command pattern are command, receiver, invoker and
|
* <p>Four terms always associated with the command pattern are command, receiver, invoker and
|
||||||
* client. A command object (spell) knows about the receiver (target) and invokes a method of
|
* client. A command object (spell) knows about the receiver (target) and invokes a method of the
|
||||||
* the receiver.Values for parameters of the receiver method are stored in the command. The receiver
|
* receiver.Values for parameters of the receiver method are stored in the command. The receiver
|
||||||
* then does the work. An invoker object (wizard) knows how to execute a command, and optionally
|
* then does the work. An invoker object (wizard) knows how to execute a command, and optionally
|
||||||
* does bookkeeping about the command execution. The invoker does not know anything about a
|
* does bookkeeping about the command execution. The invoker does not know anything about a concrete
|
||||||
* concrete command, it knows only about command interface. Both an invoker object and several
|
* command, it knows only about command interface. Both an invoker object and several command
|
||||||
* command objects are held by a client object (app). The client decides which commands to execute
|
* objects are held by a client object (app). The client decides which commands to execute at which
|
||||||
* at which points. To execute a command, it passes the command object to the invoker object.
|
* points. To execute a command, it passes the command object to the invoker object.
|
||||||
*/
|
*/
|
||||||
public class CommandTest {
|
public class CommandTest {
|
||||||
|
|
||||||
@ -53,8 +53,8 @@ public class CommandTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testCommand() {
|
public void testCommand() {
|
||||||
|
|
||||||
Wizard wizard = new Wizard();
|
var wizard = new Wizard();
|
||||||
Goblin goblin = new Goblin();
|
var goblin = new Goblin();
|
||||||
|
|
||||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||||
verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.VISIBLE);
|
verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.VISIBLE);
|
||||||
@ -79,16 +79,17 @@ public class CommandTest {
|
|||||||
* This method asserts that the passed goblin object has the name as expectedName, size as
|
* This method asserts that the passed goblin object has the name as expectedName, size as
|
||||||
* expectedSize and visibility as expectedVisibility.
|
* expectedSize and visibility as expectedVisibility.
|
||||||
*
|
*
|
||||||
* @param goblin a goblin object whose state is to be verified against other parameters
|
* @param goblin a goblin object whose state is to be verified against other
|
||||||
* @param expectedName expectedName of the goblin
|
* parameters
|
||||||
* @param expectedSize expected size of the goblin
|
* @param expectedName expectedName of the goblin
|
||||||
|
* @param expectedSize expected size of the goblin
|
||||||
* @param expectedVisibility expected visibility of the goblin
|
* @param expectedVisibility expected visibility of the goblin
|
||||||
*/
|
*/
|
||||||
private void verifyGoblin(Goblin goblin, String expectedName, Size expectedSize,
|
private void verifyGoblin(Goblin goblin, String expectedName, Size expectedSize,
|
||||||
Visibility expectedVisibility) {
|
Visibility expectedVisibility) {
|
||||||
assertEquals(expectedName, goblin.toString(), "Goblin's name must be same as expectedName");
|
assertEquals(expectedName, goblin.toString(), "Goblin's name must be same as expectedName");
|
||||||
assertEquals(expectedSize, goblin.getSize(), "Goblin's size must be same as expectedSize");
|
assertEquals(expectedSize, goblin.getSize(), "Goblin's size must be same as expectedSize");
|
||||||
assertEquals(expectedVisibility, goblin.getVisibility(),
|
assertEquals(expectedVisibility, goblin.getVisibility(),
|
||||||
"Goblin's visibility must be same as expectedVisibility");
|
"Goblin's visibility must be same as expectedVisibility");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,51 +40,47 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
|||||||
* available/unavailable.
|
* available/unavailable.
|
||||||
*/
|
*/
|
||||||
public class AppEmployeeDbFailCases {
|
public class AppEmployeeDbFailCases {
|
||||||
final int numOfRetries = 3;
|
private final int numOfRetries = 3;
|
||||||
final long retryDuration = 30000;
|
private final long retryDuration = 30000;
|
||||||
final long queueTime = 240000; //4 mins
|
private final long queueTime = 240000; //4 mins
|
||||||
final long queueTaskTime = 60000; //1 min
|
private final long queueTaskTime = 60000; //1 min
|
||||||
final long paymentTime = 120000; //2 mins
|
private final long paymentTime = 120000; //2 mins
|
||||||
final long messageTime = 150000; //2.5 mins
|
private final long messageTime = 150000; //2.5 mins
|
||||||
final long employeeTime = 240000; //4 mins
|
private final long employeeTime = 240000; //4 mins
|
||||||
|
|
||||||
void employeeDatabaseUnavailableCase() throws Exception {
|
void employeeDatabaseUnavailableCase() throws Exception {
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ms = new MessagingService(new MessagingDatabase());
|
||||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||||
EmployeeHandle eh =
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
var qdb =
|
||||||
new DatabaseUnavailableException());
|
|
||||||
QueueDatabase qdb =
|
|
||||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void employeeDbSuccessCase() throws Exception {
|
void employeeDbSuccessCase() throws Exception {
|
||||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
var ps = new PaymentService(new PaymentDatabase());
|
||||||
ShippingService ss =
|
var ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||||
new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
var ms = new MessagingService(new MessagingDatabase());
|
||||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||||
EmployeeHandle eh =
|
new DatabaseUnavailableException());
|
||||||
new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
var qdb = new QueueDatabase();
|
||||||
new DatabaseUnavailableException());
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
QueueDatabase qdb = new QueueDatabase();
|
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +91,7 @@ public class AppEmployeeDbFailCases {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
AppEmployeeDbFailCases aefc = new AppEmployeeDbFailCases();
|
var aefc = new AppEmployeeDbFailCases();
|
||||||
//aefc.employeeDatabaseUnavailableCase();
|
//aefc.employeeDatabaseUnavailableCase();
|
||||||
aefc.employeeDbSuccessCase();
|
aefc.employeeDbSuccessCase();
|
||||||
}
|
}
|
||||||
|
@ -40,102 +40,95 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class AppMessagingFailCases {
|
public class AppMessagingFailCases {
|
||||||
final int numOfRetries = 3;
|
private final int numOfRetries = 3;
|
||||||
final long retryDuration = 30000;
|
private final long retryDuration = 30000;
|
||||||
final long queueTime = 240000; //4 mins
|
private final long queueTime = 240000; //4 mins
|
||||||
final long queueTaskTime = 60000; //1 min
|
private final long queueTaskTime = 60000; //1 min
|
||||||
final long paymentTime = 120000; //2 mins
|
private final long paymentTime = 120000; //2 mins
|
||||||
final long messageTime = 150000; //2.5 mins
|
private final long messageTime = 150000; //2.5 mins
|
||||||
final long employeeTime = 240000; //4 mins
|
private final long employeeTime = 240000; //4 mins
|
||||||
|
|
||||||
void messagingDatabaseUnavailableCasePaymentSuccess() throws Exception {
|
void messagingDatabaseUnavailableCasePaymentSuccess() throws Exception {
|
||||||
//rest is successful
|
//rest is successful
|
||||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
var ps = new PaymentService(new PaymentDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
MessagingService ms =
|
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var qdb = new QueueDatabase();
|
||||||
QueueDatabase qdb = new QueueDatabase();
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void messagingDatabaseUnavailableCasePaymentError() throws Exception {
|
void messagingDatabaseUnavailableCasePaymentError() throws Exception {
|
||||||
//rest is successful
|
//rest is successful
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||||
MessagingService ms =
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
new DatabaseUnavailableException());
|
var qdb = new QueueDatabase();
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
QueueDatabase qdb = new QueueDatabase();
|
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void messagingDatabaseUnavailableCasePaymentFailure() throws Exception {
|
void messagingDatabaseUnavailableCasePaymentFailure() throws Exception {
|
||||||
//rest is successful
|
//rest is successful
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||||
MessagingService ms =
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
new DatabaseUnavailableException());
|
var qdb =
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
|
||||||
QueueDatabase qdb =
|
|
||||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||||
Commander c =
|
var c =
|
||||||
new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration, queueTime, queueTaskTime,
|
new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration, queueTime, queueTaskTime,
|
||||||
paymentTime, messageTime, employeeTime);
|
paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void messagingSuccessCase() throws Exception {
|
void messagingSuccessCase() throws Exception {
|
||||||
//done here
|
//done here
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||||
MessagingService ms =
|
new DatabaseUnavailableException());
|
||||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
new DatabaseUnavailableException());
|
var qdb = new QueueDatabase();
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
QueueDatabase qdb = new QueueDatabase();
|
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +139,7 @@ public class AppMessagingFailCases {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
AppMessagingFailCases amfc = new AppMessagingFailCases();
|
var amfc = new AppMessagingFailCases();
|
||||||
//amfc.messagingDatabaseUnavailableCasePaymentSuccess();
|
//amfc.messagingDatabaseUnavailableCasePaymentSuccess();
|
||||||
//amfc.messagingDatabaseUnavailableCasePaymentError();
|
//amfc.messagingDatabaseUnavailableCasePaymentError();
|
||||||
//amfc.messagingDatabaseUnavailableCasePaymentFailure();
|
//amfc.messagingDatabaseUnavailableCasePaymentFailure();
|
||||||
|
@ -40,62 +40,58 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class AppPaymentFailCases {
|
public class AppPaymentFailCases {
|
||||||
final int numOfRetries = 3;
|
private final int numOfRetries = 3;
|
||||||
final long retryDuration = 30000;
|
private final long retryDuration = 30000;
|
||||||
final long queueTime = 240000; //4 mins
|
private final long queueTime = 240000; //4 mins
|
||||||
final long queueTaskTime = 60000; //1 min
|
private final long queueTaskTime = 60000; //1 min
|
||||||
final long paymentTime = 120000; //2 mins
|
private final long paymentTime = 120000; //2 mins
|
||||||
final long messageTime = 150000; //2.5 mins
|
private final long messageTime = 150000; //2.5 mins
|
||||||
final long employeeTime = 240000; //4 mins
|
private final long employeeTime = 240000; //4 mins
|
||||||
|
|
||||||
void paymentNotPossibleCase() throws Exception {
|
void paymentNotPossibleCase() throws Exception {
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
new PaymentDetailsErrorException());
|
||||||
new PaymentDetailsErrorException());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||||
MessagingService ms =
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
var qdb = new QueueDatabase(new DatabaseUnavailableException());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException());
|
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void paymentDatabaseUnavailableCase() throws Exception {
|
void paymentDatabaseUnavailableCase() throws Exception {
|
||||||
//rest is successful
|
//rest is successful
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ms = new MessagingService(new MessagingDatabase());
|
||||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var qdb = new QueueDatabase();
|
||||||
QueueDatabase qdb = new QueueDatabase();
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void paymentSuccessCase() throws Exception {
|
void paymentSuccessCase() throws Exception {
|
||||||
//goes to message after 2 retries maybe - rest is successful for now
|
//goes to message after 2 retries maybe - rest is successful for now
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ms =
|
||||||
MessagingService ms =
|
|
||||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException());
|
var qdb = new QueueDatabase(new DatabaseUnavailableException());
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +102,7 @@ public class AppPaymentFailCases {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
AppPaymentFailCases apfc = new AppPaymentFailCases();
|
var apfc = new AppPaymentFailCases();
|
||||||
//apfc.paymentNotPossibleCase();
|
//apfc.paymentNotPossibleCase();
|
||||||
//apfc.paymentDatabaseUnavailableCase();
|
//apfc.paymentDatabaseUnavailableCase();
|
||||||
apfc.paymentSuccessCase();
|
apfc.paymentSuccessCase();
|
||||||
|
@ -40,98 +40,93 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class AppQueueFailCases {
|
public class AppQueueFailCases {
|
||||||
final int numOfRetries = 3;
|
private final int numOfRetries = 3;
|
||||||
final long retryDuration = 30000;
|
private final long retryDuration = 30000;
|
||||||
final long queueTime = 240000; //4 mins
|
private final long queueTime = 240000; //4 mins
|
||||||
final long queueTaskTime = 60000; //1 min
|
private final long queueTaskTime = 60000; //1 min
|
||||||
final long paymentTime = 120000; //2 mins
|
private final long paymentTime = 120000; //2 mins
|
||||||
final long messageTime = 150000; //2.5 mins
|
private final long messageTime = 150000; //2.5 mins
|
||||||
final long employeeTime = 240000; //4 mins
|
private final long employeeTime = 240000; //4 mins
|
||||||
|
|
||||||
void queuePaymentTaskDatabaseUnavailableCase() throws Exception {
|
void queuePaymentTaskDatabaseUnavailableCase() throws Exception {
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ms = new MessagingService(new MessagingDatabase());
|
||||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var qdb =
|
||||||
QueueDatabase qdb =
|
|
||||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void queueMessageTaskDatabaseUnavailableCase() throws Exception {
|
void queueMessageTaskDatabaseUnavailableCase() throws Exception {
|
||||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
var ps = new PaymentService(new PaymentDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
MessagingService ms =
|
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var qdb =
|
||||||
QueueDatabase qdb =
|
|
||||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void queueEmployeeDbTaskDatabaseUnavailableCase() throws Exception {
|
void queueEmployeeDbTaskDatabaseUnavailableCase() throws Exception {
|
||||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
var ps = new PaymentService(new PaymentDatabase());
|
||||||
ShippingService ss =
|
var ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||||
new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
var ms = new MessagingService(new MessagingDatabase());
|
||||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||||
EmployeeHandle eh =
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
var qdb =
|
||||||
new DatabaseUnavailableException());
|
|
||||||
QueueDatabase qdb =
|
|
||||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void queueSuccessCase() throws Exception {
|
void queueSuccessCase() throws Exception {
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var ss = new ShippingService(new ShippingDatabase());
|
||||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
var ms =
|
||||||
MessagingService ms =
|
|
||||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException());
|
new DatabaseUnavailableException());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
QueueDatabase qdb =
|
var qdb =
|
||||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,7 +137,7 @@ public class AppQueueFailCases {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
AppQueueFailCases aqfc = new AppQueueFailCases();
|
var aqfc = new AppQueueFailCases();
|
||||||
//aqfc.queuePaymentTaskDatabaseUnavailableCase();
|
//aqfc.queuePaymentTaskDatabaseUnavailableCase();
|
||||||
//aqfc.queueMessageTaskDatabaseUnavailableCase();
|
//aqfc.queueMessageTaskDatabaseUnavailableCase();
|
||||||
//aqfc.queueEmployeeDbTaskDatabaseUnavailableCase();
|
//aqfc.queueEmployeeDbTaskDatabaseUnavailableCase();
|
||||||
|
@ -42,75 +42,69 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class AppShippingFailCases {
|
public class AppShippingFailCases {
|
||||||
final int numOfRetries = 3;
|
private final int numOfRetries = 3;
|
||||||
final long retryDuration = 30000;
|
private final long retryDuration = 30000;
|
||||||
final long queueTime = 240000; //4 mins
|
private final long queueTime = 240000; //4 mins
|
||||||
final long queueTaskTime = 60000; //1 min
|
private final long queueTaskTime = 60000; //1 min
|
||||||
final long paymentTime = 120000; //2 mins
|
private final long paymentTime = 120000; //2 mins
|
||||||
final long messageTime = 150000; //2.5 mins
|
private final long messageTime = 150000; //2.5 mins
|
||||||
final long employeeTime = 240000; //4 mins
|
private final long employeeTime = 240000; //4 mins
|
||||||
|
|
||||||
void itemUnavailableCase() throws Exception {
|
void itemUnavailableCase() throws Exception {
|
||||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
var ps = new PaymentService(new PaymentDatabase());
|
||||||
ShippingService ss =
|
var ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||||
new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
var ms = new MessagingService(new MessagingDatabase());
|
||||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var qdb = new QueueDatabase();
|
||||||
QueueDatabase qdb = new QueueDatabase();
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shippingNotPossibleCase() throws Exception {
|
void shippingNotPossibleCase() throws Exception {
|
||||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
var ps = new PaymentService(new PaymentDatabase());
|
||||||
ShippingService ss =
|
var ss = new ShippingService(new ShippingDatabase(), new ShippingNotPossibleException());
|
||||||
new ShippingService(new ShippingDatabase(), new ShippingNotPossibleException());
|
var ms = new MessagingService(new MessagingDatabase());
|
||||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var qdb = new QueueDatabase();
|
||||||
QueueDatabase qdb = new QueueDatabase();
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shippingDatabaseUnavailableCase() throws Exception {
|
void shippingDatabaseUnavailableCase() throws Exception {
|
||||||
//rest is successful
|
//rest is successful
|
||||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
var ps = new PaymentService(new PaymentDatabase());
|
||||||
ShippingService ss =
|
var ss = new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||||
new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var ms = new MessagingService(new MessagingDatabase());
|
||||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
var qdb = new QueueDatabase();
|
||||||
QueueDatabase qdb = new QueueDatabase();
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shippingSuccessCase() throws Exception {
|
void shippingSuccessCase() throws Exception {
|
||||||
//goes to payment after 2 retries maybe - rest is successful for now
|
//goes to payment after 2 retries maybe - rest is successful for now
|
||||||
PaymentService ps =
|
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException());
|
||||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException());
|
var ss = new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||||
ShippingService ss =
|
new DatabaseUnavailableException());
|
||||||
new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||||
new DatabaseUnavailableException());
|
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||||
MessagingService ms =
|
var qdb = new QueueDatabase();
|
||||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
|
||||||
QueueDatabase qdb = new QueueDatabase();
|
|
||||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
|
||||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
c.placeOrder(order);
|
c.placeOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +115,7 @@ public class AppShippingFailCases {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
AppShippingFailCases asfc = new AppShippingFailCases();
|
var asfc = new AppShippingFailCases();
|
||||||
//asfc.itemUnavailableCase();
|
//asfc.itemUnavailableCase();
|
||||||
//asfc.shippingNotPossibleCase();
|
//asfc.shippingNotPossibleCase();
|
||||||
//asfc.shippingDatabaseUnavailableCase();
|
//asfc.shippingDatabaseUnavailableCase();
|
||||||
|
@ -36,7 +36,6 @@ import com.iluwatar.commander.queue.QueueDatabase;
|
|||||||
import com.iluwatar.commander.queue.QueueTask;
|
import com.iluwatar.commander.queue.QueueTask;
|
||||||
import com.iluwatar.commander.queue.QueueTask.TaskType;
|
import com.iluwatar.commander.queue.QueueTask.TaskType;
|
||||||
import com.iluwatar.commander.shippingservice.ShippingService;
|
import com.iluwatar.commander.shippingservice.ShippingService;
|
||||||
import java.util.ArrayList;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -86,7 +85,7 @@ public class Commander {
|
|||||||
private final long messageTime;
|
private final long messageTime;
|
||||||
private final long employeeTime;
|
private final long employeeTime;
|
||||||
private boolean finalSiteMsgShown;
|
private boolean finalSiteMsgShown;
|
||||||
static final Logger LOG = LoggerFactory.getLogger(Commander.class);
|
private static final Logger LOG = LoggerFactory.getLogger(Commander.class);
|
||||||
//we could also have another db where it stores all orders
|
//we could also have another db where it stores all orders
|
||||||
|
|
||||||
Commander(EmployeeHandle empDb, PaymentService paymentService, ShippingService shippingService,
|
Commander(EmployeeHandle empDb, PaymentService paymentService, ShippingService shippingService,
|
||||||
@ -113,7 +112,7 @@ public class Commander {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendShippingRequest(Order order) throws Exception {
|
private void sendShippingRequest(Order order) throws Exception {
|
||||||
ArrayList<Exception> list = shippingService.exceptionsList;
|
var list = shippingService.exceptionsList;
|
||||||
Retry.Operation op = (l) -> {
|
Retry.Operation op = (l) -> {
|
||||||
if (!l.isEmpty()) {
|
if (!l.isEmpty()) {
|
||||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||||
@ -131,7 +130,6 @@ public class Commander {
|
|||||||
LOG.info("Order has been placed and will be shipped to you. Please wait while we make your"
|
LOG.info("Order has been placed and will be shipped to you. Please wait while we make your"
|
||||||
+ " payment... ");
|
+ " payment... ");
|
||||||
sendPaymentRequest(order);
|
sendPaymentRequest(order);
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||||
if (ShippingNotPossibleException.class.isAssignableFrom(err.getClass())) {
|
if (ShippingNotPossibleException.class.isAssignableFrom(err.getClass())) {
|
||||||
@ -153,9 +151,8 @@ public class Commander {
|
|||||||
LOG.error("Order " + order.id + ": Shipping service unavailable, order not placed..");
|
LOG.error("Order " + order.id + ": Shipping service unavailable, order not placed..");
|
||||||
finalSiteMsgShown = true;
|
finalSiteMsgShown = true;
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
r.perform(list, order);
|
r.perform(list, order);
|
||||||
}
|
}
|
||||||
@ -169,79 +166,69 @@ public class Commander {
|
|||||||
} //if succeeded or failed, would have been dequeued, no attempt to make payment
|
} //if succeeded or failed, would have been dequeued, no attempt to make payment
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<Exception> list = paymentService.exceptionsList;
|
var list = paymentService.exceptionsList;
|
||||||
Thread t = new Thread(new Runnable() {
|
var t = new Thread(() -> {
|
||||||
@Override
|
Retry.Operation op = (l) -> {
|
||||||
public void run() {
|
if (!l.isEmpty()) {
|
||||||
Retry.Operation op = (l) -> {
|
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||||
if (!l.isEmpty()) {
|
LOG.debug("Order " + order.id + ": Error in connecting to payment service,"
|
||||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
+ " trying again..");
|
||||||
LOG.debug("Order " + order.id + ": Error in connecting to payment service,"
|
|
||||||
+ " trying again..");
|
|
||||||
} else {
|
|
||||||
LOG.debug("Order " + order.id + ": Error in creating payment request..");
|
|
||||||
}
|
|
||||||
throw l.remove(0);
|
|
||||||
}
|
|
||||||
if (order.paid.equals(PaymentStatus.Trying)) {
|
|
||||||
String transactionId = paymentService.receiveRequest(order.price);
|
|
||||||
order.paid = PaymentStatus.Done;
|
|
||||||
LOG.info("Order " + order.id + ": Payment successful, transaction Id: "
|
|
||||||
+ transactionId);
|
|
||||||
if (!finalSiteMsgShown) {
|
|
||||||
LOG.info("Payment made successfully, thank you for shopping with us!!");
|
|
||||||
finalSiteMsgShown = true;
|
|
||||||
}
|
|
||||||
sendSuccessMessage(order);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
|
||||||
if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())) {
|
|
||||||
if (!finalSiteMsgShown) {
|
|
||||||
LOG.info("There was an error in payment. Your account/card details "
|
|
||||||
+ "may have been incorrect. "
|
|
||||||
+ "Meanwhile, your order has been converted to COD and will be shipped.");
|
|
||||||
finalSiteMsgShown = true;
|
|
||||||
}
|
|
||||||
LOG.error("Order " + order.id + ": Payment details incorrect, failed..");
|
|
||||||
o.paid = PaymentStatus.NotDone;
|
|
||||||
sendPaymentFailureMessage(o);
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
LOG.debug("Order " + order.id + ": Error in creating payment request..");
|
||||||
if (o.messageSent.equals(MessageSent.NoneSent)) {
|
|
||||||
if (!finalSiteMsgShown) {
|
|
||||||
LOG.info("There was an error in payment. We are on it, and will get back to you "
|
|
||||||
+ "asap. Don't worry, your order has been placed and will be shipped.");
|
|
||||||
finalSiteMsgShown = true;
|
|
||||||
}
|
|
||||||
LOG.warn("Order " + order.id + ": Payment error, going to queue..");
|
|
||||||
sendPaymentPossibleErrorMsg(o);
|
|
||||||
}
|
|
||||||
if (o.paid.equals(PaymentStatus.Trying) && System
|
|
||||||
.currentTimeMillis() - o.createdTime < paymentTime) {
|
|
||||||
QueueTask qt = new QueueTask(o, TaskType.Payment, -1);
|
|
||||||
updateQueue(qt);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return;
|
throw l.remove(0);
|
||||||
};
|
|
||||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
|
||||||
try {
|
|
||||||
r.perform(list, order);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
if (order.paid.equals(PaymentStatus.Trying)) {
|
||||||
|
var transactionId = paymentService.receiveRequest(order.price);
|
||||||
|
order.paid = PaymentStatus.Done;
|
||||||
|
LOG.info("Order " + order.id + ": Payment successful, transaction Id: " + transactionId);
|
||||||
|
if (!finalSiteMsgShown) {
|
||||||
|
LOG.info("Payment made successfully, thank you for shopping with us!!");
|
||||||
|
finalSiteMsgShown = true;
|
||||||
|
}
|
||||||
|
sendSuccessMessage(order);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||||
|
if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())) {
|
||||||
|
if (!finalSiteMsgShown) {
|
||||||
|
LOG.info("There was an error in payment. Your account/card details "
|
||||||
|
+ "may have been incorrect. "
|
||||||
|
+ "Meanwhile, your order has been converted to COD and will be shipped.");
|
||||||
|
finalSiteMsgShown = true;
|
||||||
|
}
|
||||||
|
LOG.error("Order " + order.id + ": Payment details incorrect, failed..");
|
||||||
|
o.paid = PaymentStatus.NotDone;
|
||||||
|
sendPaymentFailureMessage(o);
|
||||||
|
} else {
|
||||||
|
if (o.messageSent.equals(MessageSent.NoneSent)) {
|
||||||
|
if (!finalSiteMsgShown) {
|
||||||
|
LOG.info("There was an error in payment. We are on it, and will get back to you "
|
||||||
|
+ "asap. Don't worry, your order has been placed and will be shipped.");
|
||||||
|
finalSiteMsgShown = true;
|
||||||
|
}
|
||||||
|
LOG.warn("Order " + order.id + ": Payment error, going to queue..");
|
||||||
|
sendPaymentPossibleErrorMsg(o);
|
||||||
|
}
|
||||||
|
if (o.paid.equals(PaymentStatus.Trying) && System
|
||||||
|
.currentTimeMillis() - o.createdTime < paymentTime) {
|
||||||
|
var qt = new QueueTask(o, TaskType.Payment, -1);
|
||||||
|
updateQueue(qt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||||
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
|
try {
|
||||||
|
r.perform(list, order);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateQueue(QueueTask qt) throws InterruptedException {
|
private void updateQueue(QueueTask qt) {
|
||||||
if (System.currentTimeMillis() - qt.order.createdTime >= this.queueTime) {
|
if (System.currentTimeMillis() - qt.order.createdTime >= this.queueTime) {
|
||||||
// since payment time is lesser than queuetime it would have already failed..
|
// since payment time is lesser than queuetime it would have already failed..
|
||||||
// additional check not needed
|
// additional check not needed
|
||||||
@ -256,97 +243,82 @@ public class Commander {
|
|||||||
LOG.trace("Order " + qt.order.id + ": Not queueing task since task already done..");
|
LOG.trace("Order " + qt.order.id + ": Not queueing task since task already done..");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<Exception> list = queue.exceptionsList;
|
var list = queue.exceptionsList;
|
||||||
Thread t = new Thread(new Runnable() {
|
Thread t = new Thread(() -> {
|
||||||
@Override
|
Retry.Operation op = (list1) -> {
|
||||||
public void run() {
|
if (!list1.isEmpty()) {
|
||||||
Retry.Operation op = (list) -> {
|
LOG.warn("Order " + qt.order.id + ": Error in connecting to queue db, trying again..");
|
||||||
if (!list.isEmpty()) {
|
throw list1.remove(0);
|
||||||
LOG.warn("Order " + qt.order.id + ": Error in connecting to queue db, trying again..");
|
|
||||||
throw list.remove(0);
|
|
||||||
}
|
|
||||||
queue.add(qt);
|
|
||||||
queueItems++;
|
|
||||||
LOG.info("Order " + qt.order.id + ": " + qt.getType() + " task enqueued..");
|
|
||||||
tryDoingTasksInQueue();
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry.HandleErrorIssue<QueueTask> handleError = (qt, err) -> {
|
|
||||||
if (qt.taskType.equals(TaskType.Payment)) {
|
|
||||||
qt.order.paid = PaymentStatus.NotDone;
|
|
||||||
sendPaymentFailureMessage(qt.order);
|
|
||||||
LOG.error("Order " + qt.order.id + ": Unable to enqueue payment task,"
|
|
||||||
+ " payment failed..");
|
|
||||||
}
|
|
||||||
LOG.error("Order " + qt.order.id + ": Unable to enqueue task of type " + qt.getType()
|
|
||||||
+ ", trying to add to employee handle..");
|
|
||||||
employeeHandleIssue(qt.order);
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry<QueueTask> r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
|
||||||
try {
|
|
||||||
r.perform(list, qt);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
queue.add(qt);
|
||||||
|
queueItems++;
|
||||||
|
LOG.info("Order " + qt.order.id + ": " + qt.getType() + " task enqueued..");
|
||||||
|
tryDoingTasksInQueue();
|
||||||
|
};
|
||||||
|
Retry.HandleErrorIssue<QueueTask> handleError = (qt1, err) -> {
|
||||||
|
if (qt1.taskType.equals(TaskType.Payment)) {
|
||||||
|
qt1.order.paid = PaymentStatus.NotDone;
|
||||||
|
sendPaymentFailureMessage(qt1.order);
|
||||||
|
LOG.error("Order " + qt1.order.id + ": Unable to enqueue payment task,"
|
||||||
|
+ " payment failed..");
|
||||||
|
}
|
||||||
|
LOG.error("Order " + qt1.order.id + ": Unable to enqueue task of type " + qt1.getType()
|
||||||
|
+ ", trying to add to employee handle..");
|
||||||
|
employeeHandleIssue(qt1.order);
|
||||||
|
};
|
||||||
|
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||||
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
|
try {
|
||||||
|
r.perform(list, qt);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryDoingTasksInQueue() { //commander controls operations done to queue
|
private void tryDoingTasksInQueue() { //commander controls operations done to queue
|
||||||
ArrayList<Exception> list = queue.exceptionsList;
|
var list = queue.exceptionsList;
|
||||||
Thread t2 = new Thread(new Runnable() {
|
var t2 = new Thread(() -> {
|
||||||
@Override
|
Retry.Operation op = (list1) -> {
|
||||||
public void run() {
|
if (!list1.isEmpty()) {
|
||||||
Retry.Operation op = (list) -> {
|
LOG.warn("Error in accessing queue db to do tasks, trying again..");
|
||||||
if (!list.isEmpty()) {
|
throw list1.remove(0);
|
||||||
LOG.warn("Error in accessing queue db to do tasks, trying again..");
|
|
||||||
throw list.remove(0);
|
|
||||||
}
|
|
||||||
doTasksInQueue();
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry<QueueTask> r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
|
||||||
try {
|
|
||||||
r.perform(list, null);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
doTasksInQueue();
|
||||||
|
};
|
||||||
|
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
||||||
|
};
|
||||||
|
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||||
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
|
try {
|
||||||
|
r.perform(list, null);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
t2.start();
|
t2.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryDequeue() {
|
private void tryDequeue() {
|
||||||
ArrayList<Exception> list = queue.exceptionsList;
|
var list = queue.exceptionsList;
|
||||||
Thread t3 = new Thread(new Runnable() {
|
var t3 = new Thread(() -> {
|
||||||
@Override
|
Retry.Operation op = (list1) -> {
|
||||||
public void run() {
|
if (!list1.isEmpty()) {
|
||||||
Retry.Operation op = (list) -> {
|
LOG.warn("Error in accessing queue db to dequeue task, trying again..");
|
||||||
if (!list.isEmpty()) {
|
throw list1.remove(0);
|
||||||
LOG.warn("Error in accessing queue db to dequeue task, trying again..");
|
|
||||||
throw list.remove(0);
|
|
||||||
}
|
|
||||||
queue.dequeue();
|
|
||||||
queueItems--;
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry<QueueTask> r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
|
||||||
try {
|
|
||||||
r.perform(list, null);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
queue.dequeue();
|
||||||
|
queueItems--;
|
||||||
|
};
|
||||||
|
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
||||||
|
};
|
||||||
|
var r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
||||||
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
|
try {
|
||||||
|
r.perform(list, null);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
t3.start();
|
t3.start();
|
||||||
@ -357,53 +329,44 @@ public class Commander {
|
|||||||
LOG.trace("Order " + order.id + ": Message time for order over, returning..");
|
LOG.trace("Order " + order.id + ": Message time for order over, returning..");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<Exception> list = messagingService.exceptionsList;
|
var list = messagingService.exceptionsList;
|
||||||
Thread t = new Thread(new Runnable() {
|
Thread t = new Thread(() -> {
|
||||||
@Override
|
Retry.Operation op = (l) -> {
|
||||||
public void run() {
|
if (!l.isEmpty()) {
|
||||||
Retry.Operation op = (l) -> {
|
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||||
if (!l.isEmpty()) {
|
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
+ "(Payment Success msg), trying again..");
|
||||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
} else {
|
||||||
+ "(Payment Success msg), trying again..");
|
LOG.debug("Order " + order.id + ": Error in creating Payment Success"
|
||||||
} else {
|
+ " messaging request..");
|
||||||
LOG.debug("Order " + order.id + ": Error in creating Payment Success"
|
|
||||||
+ " messaging request..");
|
|
||||||
}
|
|
||||||
throw l.remove(0);
|
|
||||||
}
|
}
|
||||||
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
throw l.remove(0);
|
||||||
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
|
||||||
String requestId = messagingService.receiveRequest(2);
|
|
||||||
order.messageSent = MessageSent.PaymentSuccessful;
|
|
||||||
LOG.info("Order " + order.id + ": Payment Success message sent,"
|
|
||||||
+ " request Id: " + requestId);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
|
||||||
try {
|
|
||||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
|
||||||
.equals(MessageSent.PaymentTrying))
|
|
||||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
|
||||||
QueueTask qt = new QueueTask(order, TaskType.Messaging, 2);
|
|
||||||
updateQueue(qt);
|
|
||||||
LOG.info("Order " + order.id + ": Error in sending Payment Success message, trying to"
|
|
||||||
+ " queue task and add to employee handle..");
|
|
||||||
employeeHandleIssue(order);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
|
||||||
try {
|
|
||||||
r.perform(list, order);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
||||||
|
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
||||||
|
var requestId = messagingService.receiveRequest(2);
|
||||||
|
order.messageSent = MessageSent.PaymentSuccessful;
|
||||||
|
LOG.info("Order " + order.id + ": Payment Success message sent,"
|
||||||
|
+ " request Id: " + requestId);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||||
|
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
||||||
|
.equals(MessageSent.PaymentTrying))
|
||||||
|
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||||
|
var qt = new QueueTask(order, TaskType.Messaging, 2);
|
||||||
|
updateQueue(qt);
|
||||||
|
LOG.info("Order " + order.id + ": Error in sending Payment Success message, trying to"
|
||||||
|
+ " queue task and add to employee handle..");
|
||||||
|
employeeHandleIssue(order);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||||
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
|
try {
|
||||||
|
r.perform(list, order);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
t.start();
|
t.start();
|
||||||
@ -414,53 +377,44 @@ public class Commander {
|
|||||||
LOG.trace("Order " + order.id + ": Message time for order over, returning..");
|
LOG.trace("Order " + order.id + ": Message time for order over, returning..");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<Exception> list = messagingService.exceptionsList;
|
var list = messagingService.exceptionsList;
|
||||||
Thread t = new Thread(new Runnable() {
|
var t = new Thread(() -> {
|
||||||
@Override
|
Retry.Operation op = (l) -> {
|
||||||
public void run() {
|
if (!l.isEmpty()) {
|
||||||
Retry.Operation op = (l) -> {
|
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||||
if (!l.isEmpty()) {
|
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
+ "(Payment Failure msg), trying again..");
|
||||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
} else {
|
||||||
+ "(Payment Failure msg), trying again..");
|
LOG.debug("Order " + order.id + ": Error in creating Payment Failure"
|
||||||
} else {
|
+ " message request..");
|
||||||
LOG.debug("Order " + order.id + ": Error in creating Payment Failure"
|
|
||||||
+ " message request..");
|
|
||||||
}
|
|
||||||
throw l.remove(0);
|
|
||||||
}
|
}
|
||||||
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
throw l.remove(0);
|
||||||
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
|
||||||
String requestId = messagingService.receiveRequest(0);
|
|
||||||
order.messageSent = MessageSent.PaymentFail;
|
|
||||||
LOG.info("Order " + order.id + ": Payment Failure message sent successfully,"
|
|
||||||
+ " request Id: " + requestId);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
|
||||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
|
||||||
.equals(MessageSent.PaymentTrying))
|
|
||||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
|
||||||
try {
|
|
||||||
QueueTask qt = new QueueTask(order, TaskType.Messaging, 0);
|
|
||||||
updateQueue(qt);
|
|
||||||
LOG.warn("Order " + order.id + ": Error in sending Payment Failure message, "
|
|
||||||
+ "trying to queue task and add to employee handle..");
|
|
||||||
employeeHandleIssue(o);
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
|
||||||
try {
|
|
||||||
r.perform(list, order);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
||||||
|
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
||||||
|
var requestId = messagingService.receiveRequest(0);
|
||||||
|
order.messageSent = MessageSent.PaymentFail;
|
||||||
|
LOG.info("Order " + order.id + ": Payment Failure message sent successfully,"
|
||||||
|
+ " request Id: " + requestId);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||||
|
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
||||||
|
.equals(MessageSent.PaymentTrying))
|
||||||
|
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||||
|
var qt = new QueueTask(order, TaskType.Messaging, 0);
|
||||||
|
updateQueue(qt);
|
||||||
|
LOG.warn("Order " + order.id + ": Error in sending Payment Failure message, "
|
||||||
|
+ "trying to queue task and add to employee handle..");
|
||||||
|
employeeHandleIssue(o);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||||
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
|
try {
|
||||||
|
r.perform(list, order);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
t.start();
|
t.start();
|
||||||
@ -471,53 +425,44 @@ public class Commander {
|
|||||||
LOG.trace("Message time for order over, returning..");
|
LOG.trace("Message time for order over, returning..");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<Exception> list = messagingService.exceptionsList;
|
var list = messagingService.exceptionsList;
|
||||||
Thread t = new Thread(new Runnable() {
|
var t = new Thread(() -> {
|
||||||
@Override
|
Retry.Operation op = (l) -> {
|
||||||
public void run() {
|
if (!l.isEmpty()) {
|
||||||
Retry.Operation op = (l) -> {
|
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||||
if (!l.isEmpty()) {
|
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
+ "(Payment Error msg), trying again..");
|
||||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
} else {
|
||||||
+ "(Payment Error msg), trying again..");
|
LOG.debug("Order " + order.id + ": Error in creating Payment Error"
|
||||||
} else {
|
+ " messaging request..");
|
||||||
LOG.debug("Order " + order.id + ": Error in creating Payment Error"
|
|
||||||
+ " messaging request..");
|
|
||||||
}
|
|
||||||
throw l.remove(0);
|
|
||||||
}
|
}
|
||||||
if (order.paid.equals(PaymentStatus.Trying) && order.messageSent
|
throw l.remove(0);
|
||||||
.equals(MessageSent.NoneSent)) {
|
|
||||||
String requestId = messagingService.receiveRequest(1);
|
|
||||||
order.messageSent = MessageSent.PaymentTrying;
|
|
||||||
LOG.info("Order " + order.id + ": Payment Error message sent successfully,"
|
|
||||||
+ " request Id: " + requestId);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
|
||||||
try {
|
|
||||||
if (o.messageSent.equals(MessageSent.NoneSent) && order.paid
|
|
||||||
.equals(PaymentStatus.Trying)
|
|
||||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
|
||||||
QueueTask qt = new QueueTask(order, TaskType.Messaging, 1);
|
|
||||||
updateQueue(qt);
|
|
||||||
LOG.warn("Order " + order.id + ": Error in sending Payment Error message, "
|
|
||||||
+ "trying to queue task and add to employee handle..");
|
|
||||||
employeeHandleIssue(o);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
|
||||||
try {
|
|
||||||
r.perform(list, order);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
if (order.paid.equals(PaymentStatus.Trying) && order.messageSent
|
||||||
|
.equals(MessageSent.NoneSent)) {
|
||||||
|
var requestId = messagingService.receiveRequest(1);
|
||||||
|
order.messageSent = MessageSent.PaymentTrying;
|
||||||
|
LOG.info("Order " + order.id + ": Payment Error message sent successfully,"
|
||||||
|
+ " request Id: " + requestId);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||||
|
if (o.messageSent.equals(MessageSent.NoneSent) && order.paid
|
||||||
|
.equals(PaymentStatus.Trying)
|
||||||
|
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||||
|
var qt = new QueueTask(order, TaskType.Messaging, 1);
|
||||||
|
updateQueue(qt);
|
||||||
|
LOG.warn("Order " + order.id + ": Error in sending Payment Error message, "
|
||||||
|
+ "trying to queue task and add to employee handle..");
|
||||||
|
employeeHandleIssue(o);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||||
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
|
try {
|
||||||
|
r.perform(list, order);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
t.start();
|
t.start();
|
||||||
@ -528,45 +473,35 @@ public class Commander {
|
|||||||
LOG.trace("Order " + order.id + ": Employee handle time for order over, returning..");
|
LOG.trace("Order " + order.id + ": Employee handle time for order over, returning..");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ArrayList<Exception> list = employeeDb.exceptionsList;
|
var list = employeeDb.exceptionsList;
|
||||||
Thread t = new Thread(new Runnable() {
|
var t = new Thread(() -> {
|
||||||
@Override
|
Retry.Operation op = (l) -> {
|
||||||
public void run() {
|
if (!l.isEmpty()) {
|
||||||
Retry.Operation op = (l) -> {
|
LOG.warn("Order " + order.id + ": Error in connecting to employee handle,"
|
||||||
if (!l.isEmpty()) {
|
+ " trying again..");
|
||||||
LOG.warn("Order " + order.id + ": Error in connecting to employee handle,"
|
throw l.remove(0);
|
||||||
+ " trying again..");
|
|
||||||
throw l.remove(0);
|
|
||||||
}
|
|
||||||
if (!order.addedToEmployeeHandle) {
|
|
||||||
employeeDb.receiveRequest(order);
|
|
||||||
order.addedToEmployeeHandle = true;
|
|
||||||
LOG.info("Order " + order.id + ": Added order to employee database");
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
|
||||||
try {
|
|
||||||
if (!o.addedToEmployeeHandle && System
|
|
||||||
.currentTimeMillis() - order.createdTime < employeeTime) {
|
|
||||||
QueueTask qt = new QueueTask(order, TaskType.EmployeeDb, -1);
|
|
||||||
updateQueue(qt);
|
|
||||||
LOG.warn("Order " + order.id + ": Error in adding to employee db,"
|
|
||||||
+ " trying to queue task..");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
|
||||||
try {
|
|
||||||
r.perform(list, order);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
if (!order.addedToEmployeeHandle) {
|
||||||
|
employeeDb.receiveRequest(order);
|
||||||
|
order.addedToEmployeeHandle = true;
|
||||||
|
LOG.info("Order " + order.id + ": Added order to employee database");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||||
|
if (!o.addedToEmployeeHandle && System
|
||||||
|
.currentTimeMillis() - order.createdTime < employeeTime) {
|
||||||
|
var qt = new QueueTask(order, TaskType.EmployeeDb, -1);
|
||||||
|
updateQueue(qt);
|
||||||
|
LOG.warn("Order " + order.id + ": Error in adding to employee db,"
|
||||||
|
+ " trying to queue task..");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||||
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
|
try {
|
||||||
|
r.perform(list, order);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
t.start();
|
t.start();
|
||||||
@ -574,7 +509,7 @@ public class Commander {
|
|||||||
|
|
||||||
private void doTasksInQueue() throws Exception {
|
private void doTasksInQueue() throws Exception {
|
||||||
if (queueItems != 0) {
|
if (queueItems != 0) {
|
||||||
QueueTask qt = queue.peek(); //this should probably be cloned here
|
var qt = queue.peek(); //this should probably be cloned here
|
||||||
//this is why we have retry for doTasksInQueue
|
//this is why we have retry for doTasksInQueue
|
||||||
LOG.trace("Order " + qt.order.id + ": Started doing task of type " + qt.getType());
|
LOG.trace("Order " + qt.order.id + ": Started doing task of type " + qt.getType());
|
||||||
if (qt.firstAttemptTime == -1) {
|
if (qt.firstAttemptTime == -1) {
|
||||||
|
@ -36,14 +36,10 @@ public class Order { //can store all transactions ids also
|
|||||||
NotDone, Trying, Done
|
NotDone, Trying, Done
|
||||||
}
|
}
|
||||||
|
|
||||||
;
|
|
||||||
|
|
||||||
enum MessageSent {
|
enum MessageSent {
|
||||||
NoneSent, PaymentFail, PaymentTrying, PaymentSuccessful
|
NoneSent, PaymentFail, PaymentTrying, PaymentSuccessful
|
||||||
}
|
}
|
||||||
|
|
||||||
;
|
|
||||||
|
|
||||||
final User user;
|
final User user;
|
||||||
final String item;
|
final String item;
|
||||||
public final String id;
|
public final String id;
|
||||||
@ -74,7 +70,7 @@ public class Order { //can store all transactions ids also
|
|||||||
this.addedToEmployeeHandle = false;
|
this.addedToEmployeeHandle = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String createUniqueId() {
|
private String createUniqueId() {
|
||||||
StringBuilder random = new StringBuilder();
|
StringBuilder random = new StringBuilder();
|
||||||
while (random.length() < 12) { // length of the random string.
|
while (random.length() < 12) { // length of the random string.
|
||||||
int index = (int) (RANDOM.nextFloat() * ALL_CHARS.length());
|
int index = (int) (RANDOM.nextFloat() * ALL_CHARS.length());
|
||||||
|
@ -25,6 +25,7 @@ package com.iluwatar.commander;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
@ -42,7 +43,7 @@ public class Retry<T> {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public interface Operation {
|
public interface Operation {
|
||||||
void operation(ArrayList<Exception> list) throws Exception;
|
void operation(List<Exception> list) throws Exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -63,9 +64,9 @@ public class Retry<T> {
|
|||||||
private final long maxDelay;
|
private final long maxDelay;
|
||||||
private final AtomicInteger attempts;
|
private final AtomicInteger attempts;
|
||||||
private final Predicate<Exception> test;
|
private final Predicate<Exception> test;
|
||||||
private final ArrayList<Exception> errors;
|
private final List<Exception> errors;
|
||||||
|
|
||||||
Retry(Operation op, HandleErrorIssue handleError, int maxAttempts,
|
Retry(Operation op, HandleErrorIssue<T> handleError, int maxAttempts,
|
||||||
long maxDelay, Predicate<Exception>... ignoreTests) {
|
long maxDelay, Predicate<Exception>... ignoreTests) {
|
||||||
this.op = op;
|
this.op = op;
|
||||||
this.handleError = handleError;
|
this.handleError = handleError;
|
||||||
@ -83,7 +84,7 @@ public class Retry<T> {
|
|||||||
* @param obj is the parameter to be passed into handleIsuue method
|
* @param obj is the parameter to be passed into handleIsuue method
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public void perform(ArrayList<Exception> list, T obj) throws Exception {
|
public void perform(List<Exception> list, T obj) {
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
op.operation(list);
|
op.operation(list);
|
||||||
@ -97,7 +98,7 @@ public class Retry<T> {
|
|||||||
try {
|
try {
|
||||||
long testDelay =
|
long testDelay =
|
||||||
(long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
|
(long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
|
||||||
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
long delay = Math.min(testDelay, this.maxDelay);
|
||||||
Thread.sleep(delay);
|
Thread.sleep(delay);
|
||||||
} catch (InterruptedException f) {
|
} catch (InterruptedException f) {
|
||||||
//ignore
|
//ignore
|
||||||
|
@ -36,7 +36,7 @@ public class EmployeeDatabase extends Database<Order> {
|
|||||||
private Hashtable<String, Order> data;
|
private Hashtable<String, Order> data;
|
||||||
|
|
||||||
public EmployeeDatabase() {
|
public EmployeeDatabase() {
|
||||||
this.data = new Hashtable<String, Order>();
|
this.data = new Hashtable<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -28,8 +28,8 @@ import com.iluwatar.commander.Service;
|
|||||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The EmployeeHandle class is the middle-man between {@link Commander} and {@link
|
* The EmployeeHandle class is the middle-man between {@link com.iluwatar.commander.Commander} and
|
||||||
* EmployeeDatabase}.
|
* {@link EmployeeDatabase}.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class EmployeeHandle extends Service {
|
public class EmployeeHandle extends Service {
|
||||||
@ -39,11 +39,11 @@ public class EmployeeHandle extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||||
return updateDb((Order) parameters[0]);
|
return updateDb(parameters[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||||
Order o = (Order) parameters[0];
|
var o = (Order) parameters[0];
|
||||||
if (database.get(o.id) == null) {
|
if (database.get(o.id) == null) {
|
||||||
database.add(o);
|
database.add(o);
|
||||||
return o.id; //true rcvd - change addedToEmployeeHandle to true else dont do anything
|
return o.id; //true rcvd - change addedToEmployeeHandle to true else dont do anything
|
||||||
|
@ -36,16 +36,16 @@ public class MessagingDatabase extends Database<MessageRequest> {
|
|||||||
private Hashtable<String, MessageRequest> data;
|
private Hashtable<String, MessageRequest> data;
|
||||||
|
|
||||||
public MessagingDatabase() {
|
public MessagingDatabase() {
|
||||||
this.data = new Hashtable<String, MessageRequest>();
|
this.data = new Hashtable<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MessageRequest add(MessageRequest r) throws DatabaseUnavailableException {
|
public MessageRequest add(MessageRequest r) {
|
||||||
return data.put(r.reqId, r);
|
return data.put(r.reqId, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MessageRequest get(String requestId) throws DatabaseUnavailableException {
|
public MessageRequest get(String requestId) {
|
||||||
return data.get(requestId);
|
return data.get(requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
/**
|
/**
|
||||||
* The MessagingService is used to send messages to user regarding their order and payment status.
|
* The MessagingService is used to send messages to user regarding their order and payment status.
|
||||||
* In case an error is encountered in payment and this service is found to be unavailable, the order
|
* In case an error is encountered in payment and this service is found to be unavailable, the order
|
||||||
* is added to the {@link EmployeeDatabase}.
|
* is added to the {@link com.iluwatar.commander.employeehandle.EmployeeDatabase}.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class MessagingService extends Service {
|
public class MessagingService extends Service {
|
||||||
@ -41,8 +41,6 @@ public class MessagingService extends Service {
|
|||||||
PaymentFail, PaymentTrying, PaymentSuccessful
|
PaymentFail, PaymentTrying, PaymentSuccessful
|
||||||
}
|
}
|
||||||
|
|
||||||
;
|
|
||||||
|
|
||||||
class MessageRequest {
|
class MessageRequest {
|
||||||
String reqId;
|
String reqId;
|
||||||
MessageToSend msg;
|
MessageToSend msg;
|
||||||
@ -58,12 +56,12 @@ public class MessagingService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public method which will receive request from {@link Commander}.
|
* Public method which will receive request from {@link com.iluwatar.commander.Commander}.
|
||||||
*/
|
*/
|
||||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||||
int messageToSend = (int) parameters[0];
|
var messageToSend = (int) parameters[0];
|
||||||
String id = generateId();
|
var id = generateId();
|
||||||
MessageToSend msg = null;
|
MessageToSend msg;
|
||||||
if (messageToSend == 0) {
|
if (messageToSend == 0) {
|
||||||
msg = MessageToSend.PaymentFail;
|
msg = MessageToSend.PaymentFail;
|
||||||
} else if (messageToSend == 1) {
|
} else if (messageToSend == 1) {
|
||||||
@ -71,12 +69,12 @@ public class MessagingService extends Service {
|
|||||||
} else { //messageToSend == 2
|
} else { //messageToSend == 2
|
||||||
msg = MessageToSend.PaymentSuccessful;
|
msg = MessageToSend.PaymentSuccessful;
|
||||||
}
|
}
|
||||||
MessageRequest req = new MessageRequest(id, msg);
|
var req = new MessageRequest(id, msg);
|
||||||
return updateDb(req);
|
return updateDb(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||||
MessageRequest req = (MessageRequest) parameters[0];
|
var req = (MessageRequest) parameters[0];
|
||||||
if (this.database.get(req.reqId) == null) { //idempotence, in case db fails here
|
if (this.database.get(req.reqId) == null) { //idempotence, in case db fails here
|
||||||
database.add(req); //if successful:
|
database.add(req); //if successful:
|
||||||
LOGGER.info(sendMessage(req.msg));
|
LOGGER.info(sendMessage(req.msg));
|
||||||
|
@ -37,17 +37,17 @@ public class PaymentDatabase extends Database<PaymentRequest> {
|
|||||||
private Hashtable<String, PaymentRequest> data;
|
private Hashtable<String, PaymentRequest> data;
|
||||||
|
|
||||||
public PaymentDatabase() {
|
public PaymentDatabase() {
|
||||||
this.data = new Hashtable<String, PaymentRequest>();
|
this.data = new Hashtable<>();
|
||||||
//0-fail, 1-error, 2-success
|
//0-fail, 1-error, 2-success
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PaymentRequest add(PaymentRequest r) throws DatabaseUnavailableException {
|
public PaymentRequest add(PaymentRequest r) {
|
||||||
return data.put(r.transactionId, r);
|
return data.put(r.transactionId, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PaymentRequest get(String requestId) throws DatabaseUnavailableException {
|
public PaymentRequest get(String requestId) {
|
||||||
return data.get(requestId);
|
return data.get(requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ import com.iluwatar.commander.Service;
|
|||||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The PaymentService class receives request from the {@link Commander} and adds to the {@link
|
* The PaymentService class receives request from the {@link com.iluwatar.commander.Commander} and
|
||||||
* PaymentDatabase}.
|
* adds to the {@link PaymentDatabase}.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class PaymentService extends Service {
|
public class PaymentService extends Service {
|
||||||
@ -50,18 +50,18 @@ public class PaymentService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public method which will receive request from {@link Commander}.
|
* Public method which will receive request from {@link com.iluwatar.commander.Commander}.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||||
//it could also be sending a userid, payment details here or something, not added here
|
//it could also be sending a userid, payment details here or something, not added here
|
||||||
String id = generateId();
|
var id = generateId();
|
||||||
PaymentRequest req = new PaymentRequest(id, (float) parameters[0]);
|
var req = new PaymentRequest(id, (float) parameters[0]);
|
||||||
return updateDb(req);
|
return updateDb(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||||
PaymentRequest req = (PaymentRequest) parameters[0];
|
var req = (PaymentRequest) parameters[0];
|
||||||
if (database.get(req.transactionId) == null || !req.paid) {
|
if (database.get(req.transactionId) == null || !req.paid) {
|
||||||
database.add(req);
|
database.add(req);
|
||||||
req.paid = true;
|
req.paid = true;
|
||||||
|
@ -33,15 +33,15 @@ import com.iluwatar.commander.exceptions.IsEmptyException;
|
|||||||
|
|
||||||
public class Queue<T> {
|
public class Queue<T> {
|
||||||
|
|
||||||
Node<T> front;
|
private Node<T> front;
|
||||||
Node<T> rear;
|
private Node<T> rear;
|
||||||
public int size = 0;
|
private int size;
|
||||||
|
|
||||||
class Node<T> {
|
static class Node<V> {
|
||||||
T value;
|
V value;
|
||||||
Node<T> next;
|
Node<V> next;
|
||||||
|
|
||||||
Node(T obj, Node<T> b) {
|
Node(V obj, Node<V> b) {
|
||||||
value = obj;
|
value = obj;
|
||||||
next = b;
|
next = b;
|
||||||
}
|
}
|
||||||
@ -57,19 +57,15 @@ public class Queue<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean isEmpty() {
|
boolean isEmpty() {
|
||||||
if (size == 0) {
|
return size == 0;
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void enqueue(T obj) {
|
void enqueue(T obj) {
|
||||||
if (front == null) {
|
if (front == null) {
|
||||||
front = new Node(obj, null);
|
front = new Node<>(obj, null);
|
||||||
rear = front;
|
rear = front;
|
||||||
} else {
|
} else {
|
||||||
Node temp = new Node(obj, null);
|
var temp = new Node<>(obj, null);
|
||||||
rear.next = temp;
|
rear.next = temp;
|
||||||
rear = temp;
|
rear = temp;
|
||||||
}
|
}
|
||||||
@ -80,10 +76,10 @@ public class Queue<T> {
|
|||||||
if (isEmpty()) {
|
if (isEmpty()) {
|
||||||
throw new IsEmptyException();
|
throw new IsEmptyException();
|
||||||
} else {
|
} else {
|
||||||
Node temp = front;
|
var temp = front;
|
||||||
front = front.next;
|
front = front.next;
|
||||||
size = size - 1;
|
size = size - 1;
|
||||||
return (T) temp.value;
|
return temp.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +87,7 @@ public class Queue<T> {
|
|||||||
if (isEmpty()) {
|
if (isEmpty()) {
|
||||||
throw new IsEmptyException();
|
throw new IsEmptyException();
|
||||||
} else {
|
} else {
|
||||||
return (T) front.value;
|
return front.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ import java.util.List;
|
|||||||
public class QueueDatabase extends Database<QueueTask> {
|
public class QueueDatabase extends Database<QueueTask> {
|
||||||
|
|
||||||
private Queue<QueueTask> data;
|
private Queue<QueueTask> data;
|
||||||
public ArrayList<Exception> exceptionsList;
|
public List<Exception> exceptionsList;
|
||||||
|
|
||||||
public QueueDatabase(Exception... exc) {
|
public QueueDatabase(Exception... exc) {
|
||||||
this.data = new Queue<>();
|
this.data = new Queue<>();
|
||||||
@ -44,7 +44,7 @@ public class QueueDatabase extends Database<QueueTask> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public QueueTask add(QueueTask t) throws DatabaseUnavailableException {
|
public QueueTask add(QueueTask t) {
|
||||||
data.enqueue(t);
|
data.enqueue(t);
|
||||||
return t;
|
return t;
|
||||||
//even if same thing queued twice, it is taken care of in other dbs
|
//even if same thing queued twice, it is taken care of in other dbs
|
||||||
@ -55,12 +55,10 @@ public class QueueDatabase extends Database<QueueTask> {
|
|||||||
*
|
*
|
||||||
* @return object at front of queue
|
* @return object at front of queue
|
||||||
* @throws IsEmptyException if queue is empty
|
* @throws IsEmptyException if queue is empty
|
||||||
* @throws DatabaseUnavailableException if queue db is unavailable
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public QueueTask peek() throws IsEmptyException, DatabaseUnavailableException {
|
public QueueTask peek() throws IsEmptyException {
|
||||||
QueueTask qt = this.data.peek();
|
return this.data.peek();
|
||||||
return qt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,16 +66,14 @@ public class QueueDatabase extends Database<QueueTask> {
|
|||||||
*
|
*
|
||||||
* @return object at front of queue
|
* @return object at front of queue
|
||||||
* @throws IsEmptyException if queue is empty
|
* @throws IsEmptyException if queue is empty
|
||||||
* @throws DatabaseUnavailableException if queue db is unavailable
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public QueueTask dequeue() throws IsEmptyException, DatabaseUnavailableException {
|
public QueueTask dequeue() throws IsEmptyException {
|
||||||
QueueTask qt = this.data.dequeue();
|
return this.data.dequeue();
|
||||||
return qt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public QueueTask get(String taskId) throws DatabaseUnavailableException {
|
public QueueTask get(String taskId) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,8 +39,6 @@ public class QueueTask {
|
|||||||
Messaging, Payment, EmployeeDb
|
Messaging, Payment, EmployeeDb
|
||||||
}
|
}
|
||||||
|
|
||||||
;
|
|
||||||
|
|
||||||
public Order order;
|
public Order order;
|
||||||
public TaskType taskType;
|
public TaskType taskType;
|
||||||
public int messageType; //0-fail, 1-error, 2-success
|
public int messageType; //0-fail, 1-error, 2-success
|
||||||
@ -69,7 +67,6 @@ public class QueueTask {
|
|||||||
*
|
*
|
||||||
* @return String representing type of task
|
* @return String representing type of task
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public String getType() {
|
public String getType() {
|
||||||
if (!this.taskType.equals(TaskType.Messaging)) {
|
if (!this.taskType.equals(TaskType.Messaging)) {
|
||||||
return this.taskType.toString();
|
return this.taskType.toString();
|
||||||
|
@ -37,15 +37,15 @@ public class ShippingDatabase extends Database<ShippingRequest> {
|
|||||||
private Hashtable<String, ShippingRequest> data;
|
private Hashtable<String, ShippingRequest> data;
|
||||||
|
|
||||||
public ShippingDatabase() {
|
public ShippingDatabase() {
|
||||||
this.data = new Hashtable<String, ShippingRequest>();
|
this.data = new Hashtable<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ShippingRequest add(ShippingRequest r) throws DatabaseUnavailableException {
|
public ShippingRequest add(ShippingRequest r) {
|
||||||
return data.put(r.transactionId, r);
|
return data.put(r.transactionId, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ShippingRequest get(String trasnactionId) throws DatabaseUnavailableException {
|
public ShippingRequest get(String trasnactionId) {
|
||||||
return data.get(trasnactionId);
|
return data.get(trasnactionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,13 +27,13 @@ import com.iluwatar.commander.Service;
|
|||||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ShippingService class receives request from {@link Commander} class and adds it to the {@link
|
* ShippingService class receives request from {@link com.iluwatar.commander.Commander} class and
|
||||||
* ShippingDatabase}.
|
* adds it to the {@link ShippingDatabase}.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class ShippingService extends Service {
|
public class ShippingService extends Service {
|
||||||
|
|
||||||
class ShippingRequest {
|
static class ShippingRequest {
|
||||||
String transactionId;
|
String transactionId;
|
||||||
String item;
|
String item;
|
||||||
String address;
|
String address;
|
||||||
@ -50,18 +50,19 @@ public class ShippingService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public method which will receive request from {@link Commander}.
|
* Public method which will receive request from {@link com.iluwatar.commander.Commander}.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||||
String id = generateId();
|
var id = generateId();
|
||||||
ShippingRequest req =
|
var item = (String) parameters[0];
|
||||||
new ShippingRequest(id, (String) parameters[0] /*item*/, (String) parameters[1]/*address*/);
|
var address = (String) parameters[1];
|
||||||
|
var req = new ShippingRequest(id, item, address);
|
||||||
return updateDb(req);
|
return updateDb(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||||
ShippingRequest req = (ShippingRequest) parameters[0];
|
var req = (ShippingRequest) parameters[0];
|
||||||
if (this.database.get(req.transactionId) == null) {
|
if (this.database.get(req.transactionId) == null) {
|
||||||
database.add(req);
|
database.add(req);
|
||||||
return req.transactionId;
|
return req.transactionId;
|
||||||
|
@ -23,43 +23,38 @@
|
|||||||
|
|
||||||
package com.iluwatar.commander;
|
package com.iluwatar.commander;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||||
import com.iluwatar.commander.exceptions.ItemUnavailableException;
|
import com.iluwatar.commander.exceptions.ItemUnavailableException;
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
class RetryTest {
|
class RetryTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void performTest() {
|
void performTest() {
|
||||||
Retry.Operation op = (l) -> {
|
Retry.Operation op = (l) -> {
|
||||||
if (!l.isEmpty()) {
|
if (!l.isEmpty()) {
|
||||||
throw l.remove(0);
|
throw l.remove(0);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
Retry.HandleErrorIssue<Order> handleError = (o,e) -> {
|
Retry.HandleErrorIssue<Order> handleError = (o, e) -> {
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
Retry<Order> r1 = new Retry<>(op, handleError, 3, 30000,
|
var r1 = new Retry<>(op, handleError, 3, 30000,
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
Retry<Order> r2 = new Retry<>(op, handleError, 3, 30000,
|
var r2 = new Retry<>(op, handleError, 3, 30000,
|
||||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||||
User user = new User("Jim", "ABCD");
|
var user = new User("Jim", "ABCD");
|
||||||
Order order = new Order(user, "book", 10f);
|
var order = new Order(user, "book", 10f);
|
||||||
ArrayList<Exception> arr1 = new ArrayList<>(List.of(
|
var arr1 = new ArrayList<>(List.of(new ItemUnavailableException(), new DatabaseUnavailableException()));
|
||||||
new ItemUnavailableException(), new DatabaseUnavailableException()));
|
|
||||||
try {
|
try {
|
||||||
r1.perform(arr1, order);
|
r1.perform(arr1, order);
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
ArrayList<Exception> arr2 = new ArrayList<>(List.of(
|
var arr2 = new ArrayList<>(List.of(new DatabaseUnavailableException(), new ItemUnavailableException()));
|
||||||
new DatabaseUnavailableException(), new ItemUnavailableException()));
|
|
||||||
try {
|
try {
|
||||||
r2.perform(arr2, order);
|
r2.perform(arr2, order);
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
|
@ -49,12 +49,12 @@ public class App {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
LOGGER.info("Message from the orcs: ");
|
LOGGER.info("Message from the orcs: ");
|
||||||
|
|
||||||
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
var orcMessage = new Messenger().messageFromOrcs();
|
||||||
orcMessage.print();
|
orcMessage.print();
|
||||||
|
|
||||||
LOGGER.info("\nMessage from the elves: ");
|
LOGGER.info("\nMessage from the elves: ");
|
||||||
|
|
||||||
LetterComposite elfMessage = new Messenger().messageFromElves();
|
var elfMessage = new Messenger().messageFromElves();
|
||||||
elfMessage.print();
|
elfMessage.print();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,9 +52,7 @@ public abstract class LetterComposite {
|
|||||||
*/
|
*/
|
||||||
public void print() {
|
public void print() {
|
||||||
printThisBefore();
|
printThisBefore();
|
||||||
for (LetterComposite letter : children) {
|
children.forEach(LetterComposite::print);
|
||||||
letter.print();
|
|
||||||
}
|
|
||||||
printThisAfter();
|
printThisAfter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,20 +32,17 @@ public class Messenger {
|
|||||||
|
|
||||||
LetterComposite messageFromOrcs() {
|
LetterComposite messageFromOrcs() {
|
||||||
|
|
||||||
List<Word> words = List.of(
|
var words = List.of(
|
||||||
new Word(List.of(new Letter('W'), new Letter('h'), new Letter('e'), new Letter(
|
new Word('W', 'h', 'e', 'r', 'e'),
|
||||||
'r'), new Letter('e'))),
|
new Word('t', 'h', 'e', 'r', 'e'),
|
||||||
new Word(List.of(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
|
new Word('i', 's'),
|
||||||
'r'), new Letter('e'))),
|
new Word('a'),
|
||||||
new Word(List.of(new Letter('i'), new Letter('s'))),
|
new Word('w', 'h', 'i', 'p'),
|
||||||
new Word(List.of(new Letter('a'))),
|
new Word('t', 'h', 'e', 'r', 'e'),
|
||||||
new Word(List.of(new Letter('w'), new Letter('h'), new Letter('i'), new Letter(
|
new Word('i', 's'),
|
||||||
'p'))),
|
new Word('a'),
|
||||||
new Word(List.of(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
|
new Word('w', 'a', 'y')
|
||||||
'r'), new Letter('e'))),
|
);
|
||||||
new Word(List.of(new Letter('i'), new Letter('s'))),
|
|
||||||
new Word(List.of(new Letter('a'))),
|
|
||||||
new Word(List.of(new Letter('w'), new Letter('a'), new Letter('y'))));
|
|
||||||
|
|
||||||
return new Sentence(words);
|
return new Sentence(words);
|
||||||
|
|
||||||
@ -53,15 +50,14 @@ public class Messenger {
|
|||||||
|
|
||||||
LetterComposite messageFromElves() {
|
LetterComposite messageFromElves() {
|
||||||
|
|
||||||
List<Word> words = List.of(
|
var words = List.of(
|
||||||
new Word(List.of(new Letter('M'), new Letter('u'), new Letter('c'), new Letter('h'))),
|
new Word('M', 'u', 'c', 'h'),
|
||||||
new Word(List.of(new Letter('w'), new Letter('i'), new Letter('n'), new Letter('d'))),
|
new Word('w', 'i', 'n', 'd'),
|
||||||
new Word(List.of(new Letter('p'), new Letter('o'), new Letter('u'), new Letter('r'),
|
new Word('p', 'o', 'u', 'r', 's'),
|
||||||
new Letter('s'))),
|
new Word('f', 'r', 'o', 'm'),
|
||||||
new Word(List.of(new Letter('f'), new Letter('r'), new Letter('o'), new Letter('m'))),
|
new Word('y', 'o', 'u', 'r'),
|
||||||
new Word(List.of(new Letter('y'), new Letter('o'), new Letter('u'), new Letter('r'))),
|
new Word('m', 'o', 'u', 't', 'h')
|
||||||
new Word(List.of(new Letter('m'), new Letter('o'), new Letter('u'), new Letter('t'),
|
);
|
||||||
new Letter('h'))));
|
|
||||||
|
|
||||||
return new Sentence(words);
|
return new Sentence(words);
|
||||||
|
|
||||||
|
@ -34,9 +34,7 @@ public class Sentence extends LetterComposite {
|
|||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public Sentence(List<Word> words) {
|
public Sentence(List<Word> words) {
|
||||||
for (Word w : words) {
|
words.forEach(this::add);
|
||||||
this.add(w);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -34,8 +34,12 @@ public class Word extends LetterComposite {
|
|||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public Word(List<Letter> letters) {
|
public Word(List<Letter> letters) {
|
||||||
for (Letter l : letters) {
|
letters.forEach(this::add);
|
||||||
this.add(l);
|
}
|
||||||
|
|
||||||
|
public Word(char... letters) {
|
||||||
|
for (char letter : letters) {
|
||||||
|
this.add(new Letter(letter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,15 +26,12 @@ package com.iluwatar.composite;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Application test
|
* Application test
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
App.main(new String[]{});
|
||||||
App.main(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ public class MessengerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testMessageFromOrcs() {
|
public void testMessageFromOrcs() {
|
||||||
final Messenger messenger = new Messenger();
|
final var messenger = new Messenger();
|
||||||
testMessage(
|
testMessage(
|
||||||
messenger.messageFromOrcs(),
|
messenger.messageFromOrcs(),
|
||||||
"Where there is a whip there is a way."
|
"Where there is a whip there is a way."
|
||||||
@ -84,7 +84,7 @@ public class MessengerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testMessageFromElves() {
|
public void testMessageFromElves() {
|
||||||
final Messenger messenger = new Messenger();
|
final var messenger = new Messenger();
|
||||||
testMessage(
|
testMessage(
|
||||||
messenger.messageFromElves(),
|
messenger.messageFromElves(),
|
||||||
"Much wind pours from your mouth."
|
"Much wind pours from your mouth."
|
||||||
@ -99,7 +99,7 @@ public class MessengerTest {
|
|||||||
*/
|
*/
|
||||||
private void testMessage(final LetterComposite composedMessage, final String message) {
|
private void testMessage(final LetterComposite composedMessage, final String message) {
|
||||||
// Test is the composed message has the correct number of words
|
// Test is the composed message has the correct number of words
|
||||||
final String[] words = message.split(" ");
|
final var words = message.split(" ");
|
||||||
assertNotNull(composedMessage);
|
assertNotNull(composedMessage);
|
||||||
assertEquals(words.length, composedMessage.count());
|
assertEquals(words.length, composedMessage.count());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user