Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring #1960

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void testAcceptForDos() {
var mockVisitor = mock(ConfigureForDosVisitor.class);

hayes.accept(mockVisitor);
verify((HayesVisitor) mockVisitor).visit(eq(hayes));
verify((HayesVisitor) mockVisitor).visit(hayes);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void testAcceptForDos() {
var mockVisitor = mock(ConfigureForDosVisitor.class);

zoom.accept(mockVisitor);
verify((ZoomVisitor) mockVisitor).visit(eq(zoom));
verify((ZoomVisitor) mockVisitor).visit(zoom);
}

@Test
Expand All @@ -50,6 +50,6 @@ void testAcceptForUnix() {
var mockVisitor = mock(ConfigureForUnixVisitor.class);

zoom.accept(mockVisitor);
verify((ZoomVisitor) mockVisitor).visit(eq(zoom));
verify((ZoomVisitor) mockVisitor).visit(zoom);
}
}
76 changes: 38 additions & 38 deletions commander/src/main/java/com/iluwatar/commander/Commander.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,65 +167,39 @@ private void sendShippingRequest(Order order) throws Exception {
}

private void sendPaymentRequest(Order order) {
if (System.currentTimeMillis() - order.createdTime >= this.paymentTime) {
if (order.paid.equals(PaymentStatus.TRYING)) {
order.paid = PaymentStatus.NOT_DONE;
sendPaymentFailureMessage(order);
LOG.error(ORDER_ID + ": Payment time for order over, failed and returning..", order.id);
} //if succeeded or failed, would have been dequeued, no attempt to make payment
boolean timeRemaining = (System.currentTimeMillis() - order.createdTime >= this.paymentTime);
boolean paymentStatus = order.paid.equals(PaymentStatus.TRYING);
if (timeRemaining && paymentStatus) {
order.paid = PaymentStatus.NOT_DONE;
sendPaymentFailureMessage(order);
LOG.error(ORDER_ID + ": Payment time for order over, failed and returning..", order.id);
return;
//if succeeded or failed, would have been dequeued, no attempt to make payment
}
var list = paymentService.exceptionsList;
var t = new Thread(() -> {
Retry.Operation op = (l) -> {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug(ORDER_ID + ": Error in connecting to payment service,"
+ " trying again..", order.id);
} else {
LOG.debug(ORDER_ID + ": Error in creating payment request..", order.id);
}
throw l.remove(0);
}
if (order.paid.equals(PaymentStatus.TRYING)) {
var transactionId = paymentService.receiveRequest(order.price);
order.paid = PaymentStatus.DONE;
LOG.info(ORDER_ID + ": Payment successful, transaction Id: {}",
order.id, transactionId);
if (!finalSiteMsgShown) {
LOG.info("Payment made successfully, thank you for shopping with us!!");
finalSiteMsgShown = true;
}
sendSuccessMessage(order);
}
};
Retry.Operation op=utilFunc(order);

Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())) {
if (!finalSiteMsgShown) {
if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())&&!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_ID + ": Payment details incorrect, failed..", order.id);
o.paid = PaymentStatus.NOT_DONE;
sendPaymentFailureMessage(o);
} else {
if (o.messageSent.equals(MessageSent.NONE_SENT)) {
if (!finalSiteMsgShown) {
} else if (o.messageSent.equals(MessageSent.NONE_SENT)&&!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_ID + ": Payment error, going to queue..", order.id);
sendPaymentPossibleErrorMsg(o);
}
if (o.paid.equals(PaymentStatus.TRYING) && System
.currentTimeMillis() - o.createdTime < paymentTime) {
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()));
Expand All @@ -237,6 +211,31 @@ private void sendPaymentRequest(Order order) {
});
t.start();
}
private Retry.Operation utilFunc(Order order){
return (l) -> {
if (!l.isEmpty()&&DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug(ORDER_ID + ": Error in connecting to payment service," + " trying again..", order.id);
}
else {
LOG.debug(ORDER_ID + ": Error in creating payment request..", order.id);
}
if(!l.isEmpty()) {
throw l.remove(0);
}
if (order.paid.equals(PaymentStatus.TRYING)) {
var transactionId = paymentService.receiveRequest(order.price);
order.paid = PaymentStatus.DONE;
LOG.info(ORDER_ID + ": Payment successful, transaction Id: {}",
order.id, transactionId);
sendSuccessMessage(order);
}
if (!finalSiteMsgShown&&order.paid.equals(PaymentStatus.TRYING)) {
LOG.info("Payment made successfully, thank you for shopping with us!!");
finalSiteMsgShown = true;
}
};

}

private void updateQueue(QueueTask qt) {
if (System.currentTimeMillis() - qt.order.createdTime >= this.queueTime) {
Expand Down Expand Up @@ -604,3 +603,4 @@ private void doTasksInQueue() throws Exception {
}

}