diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java index 42b09d617560e85da972039a501416fe40c7d26f..e2877b6836f32c9c48d601defa892380e6015533 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java @@ -74,6 +74,7 @@ public class RemoteService implements RemoteServiceInterface { } catch (InterruptedException e) { LOGGER.error("Thread sleep state interrupted", e); } - return waitTime <= THRESHOLD ? value * 10 : RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue(); + return waitTime <= THRESHOLD ? value * 10 + : RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue(); } } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceStatus.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceStatus.java index f9faebd0ed69a45095ea556cef6164664f8154cc..5d830e0fc6e987ae1fee023eab49bd78d149bbe0 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceStatus.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceStatus.java @@ -1,23 +1,48 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + package com.iluwatar.ambassador; /** * Holds information regarding the status of the Remote Service. * - * !Attention - This Enum replaces the integer value previously stored in {@link RemoteServiceInterface} - * as SonarCloud was identifying it as an issue. All test cases have been checked after changes, without failures. + *

This Enum replaces the integer value previously + * stored in {@link RemoteServiceInterface} as SonarCloud was identifying + * it as an issue. All test cases have been checked after changes, + * without failures.

*/ public enum RemoteServiceStatus { - FAILURE(-1) - ; + FAILURE(-1) + ; - private final long remoteServiceStatusValue; + private final long remoteServiceStatusValue; - RemoteServiceStatus(long remoteServiceStatusValue) { - this.remoteServiceStatusValue = remoteServiceStatusValue; - } + RemoteServiceStatus(long remoteServiceStatusValue) { + this.remoteServiceStatusValue = remoteServiceStatusValue; + } - public long getRemoteServiceStatusValue() { - return remoteServiceStatusValue; - } + public long getRemoteServiceStatusValue() { + return remoteServiceStatusValue; + } } diff --git a/commander/src/main/java/com/iluwatar/commander/Commander.java b/commander/src/main/java/com/iluwatar/commander/Commander.java index c2e124663cf1d472c17dad0a126b62e2e9098b34..2909f9304071df3738366fae0d45f21792b55da5 100644 --- a/commander/src/main/java/com/iluwatar/commander/Commander.java +++ b/commander/src/main/java/com/iluwatar/commander/Commander.java @@ -36,10 +36,10 @@ import com.iluwatar.commander.queue.QueueDatabase; import com.iluwatar.commander.queue.QueueTask; import com.iluwatar.commander.queue.QueueTask.TaskType; import com.iluwatar.commander.shippingservice.ShippingService; +import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; /** *

Commander pattern is used to handle all issues that can come up while making a @@ -171,10 +171,53 @@ public class Commander { var list = paymentService.exceptionsList; var t = new Thread(() -> { Retry.Operation op = (l) -> { - handlePaymentRetryOperation(order, l); + if (!l.isEmpty()) { + if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) { + 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)) { + 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 handleError = (o, err) -> { - handlePaymentErrorIssue(order, 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.NOT_DONE; + sendPaymentFailureMessage(o); + } else { + if (o.messageSent.equals(MessageSent.NONE_SENT)) { + 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())); @@ -187,58 +230,6 @@ public class Commander { t.start(); } - private void handlePaymentRetryOperation(Order order, List l) throws Exception { - if (!l.isEmpty()) { - if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) { - 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)) { - 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); - } - } - - private void handlePaymentErrorIssue(Order order, Order o, Exception 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.NOT_DONE; - sendPaymentFailureMessage(o); - } else { - if (o.messageSent.equals(MessageSent.NONE_SENT)) { - 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); - } - } - } - private void updateQueue(QueueTask qt) { if (System.currentTimeMillis() - qt.order.createdTime >= this.queueTime) { // since payment time is lesser than queuetime it would have already failed.. @@ -371,24 +362,24 @@ public class Commander { private Retry.Operation handleSuccessMessageRetryOperation(Order order) { return (l) -> { - if (!l.isEmpty()) { - if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) { - LOG.debug("Order " + order.id + ": Error in connecting to messaging service " - + "(Payment Success msg), trying again.."); - } else { - LOG.debug("Order " + order.id + ": Error in creating Payment Success" - + " messaging request.."); - } - throw l.remove(0); - } - if (!order.messageSent.equals(MessageSent.PAYMENT_FAIL) - && !order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) { - var requestId = messagingService.receiveRequest(2); - order.messageSent = MessageSent.PAYMENT_SUCCESSFUL; - LOG.info("Order " + order.id + ": Payment Success message sent," - + " request Id: " + requestId); + if (!l.isEmpty()) { + if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) { + LOG.debug("Order " + order.id + ": Error in connecting to messaging service " + + "(Payment Success msg), trying again.."); + } else { + LOG.debug("Order " + order.id + ": Error in creating Payment Success" + + " messaging request.."); } - }; + throw l.remove(0); + } + if (!order.messageSent.equals(MessageSent.PAYMENT_FAIL) + && !order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) { + var requestId = messagingService.receiveRequest(2); + order.messageSent = MessageSent.PAYMENT_SUCCESSFUL; + LOG.info("Order " + order.id + ": Payment Success message sent," + + " request Id: " + requestId); + } + }; } private void sendPaymentFailureMessage(Order order) { @@ -483,7 +474,8 @@ public class Commander { } } - private void handlePaymentPossibleErrorMsgRetryOperation(Order order, List l) throws Exception { + private void handlePaymentPossibleErrorMsgRetryOperation(Order order, List l) + throws Exception { if (!l.isEmpty()) { if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) { LOG.debug("Order " + order.id + ": Error in connecting to messaging service " diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java index 1baf99a3a5f4368df040e6f195a12925ee93231c..c85bd1e68ad30dfb3b5bbf3f99fee617e0613cdf 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/AiComponentManager.java @@ -40,7 +40,7 @@ public class AiComponentManager { private final int numEntities; - private final Component[] AI_COMPONENTS = new AiComponent[MAX_ENTITIES]; + private final Component[] aiComponents = new AiComponent[MAX_ENTITIES]; public AiComponentManager(int numEntities) { this.numEntities = numEntities; @@ -51,7 +51,7 @@ public class AiComponentManager { */ public void start() { LOGGER.info("Start AI Game Component"); - IntStream.range(0, numEntities).forEach(i -> AI_COMPONENTS[i] = new AiComponent()); + IntStream.range(0, numEntities).forEach(i -> aiComponents[i] = new AiComponent()); } /** @@ -60,7 +60,7 @@ public class AiComponentManager { public void update() { LOGGER.info("Update AI Game Component"); IntStream.range(0, numEntities) - .filter(i -> AI_COMPONENTS.length > i && AI_COMPONENTS[i] != null) - .forEach(i -> AI_COMPONENTS[i].update()); + .filter(i -> aiComponents.length > i && aiComponents[i] != null) + .forEach(i -> aiComponents[i].update()); } } diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java index e5917979cee6e005032af9e0d95b06eeb12a9b82..155793c887e7025acd6dc4097ecd1efca33ed9ac 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/PhysicsComponentManager.java @@ -40,7 +40,7 @@ public class PhysicsComponentManager { private final int numEntities; - private final Component[] PHYSICS_COMPONENTS = new PhysicsComponent[MAX_ENTITIES]; + private final Component[] physicsComponents = new PhysicsComponent[MAX_ENTITIES]; public PhysicsComponentManager(int numEntities) { this.numEntities = numEntities; @@ -51,7 +51,7 @@ public class PhysicsComponentManager { */ public void start() { LOGGER.info("Start Physics Game Component "); - IntStream.range(0, numEntities).forEach(i -> PHYSICS_COMPONENTS[i] = new PhysicsComponent()); + IntStream.range(0, numEntities).forEach(i -> physicsComponents[i] = new PhysicsComponent()); } @@ -62,7 +62,7 @@ public class PhysicsComponentManager { LOGGER.info("Update Physics Game Component "); // Process physics. IntStream.range(0, numEntities) - .filter(i -> PHYSICS_COMPONENTS.length > i && PHYSICS_COMPONENTS[i] != null) - .forEach(i -> PHYSICS_COMPONENTS[i].update()); + .filter(i -> physicsComponents.length > i && physicsComponents[i] != null) + .forEach(i -> physicsComponents[i].update()); } } diff --git a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java index b3522f90890e5ec0c565ca3448651ec1fc224f93..be1d3c2e95f04962a7bca1ac8bae39e9a2195dda 100644 --- a/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java +++ b/data-locality/src/main/java/com/iluwatar/data/locality/game/component/manager/RenderComponentManager.java @@ -40,7 +40,7 @@ public class RenderComponentManager { private final int numEntities; - private final Component[] RENDER_COMPONENTS = new RenderComponent[MAX_ENTITIES]; + private final Component[] renderComponents = new RenderComponent[MAX_ENTITIES]; public RenderComponentManager(int numEntities) { this.numEntities = numEntities; @@ -51,7 +51,7 @@ public class RenderComponentManager { */ public void start() { LOGGER.info("Start Render Game Component "); - IntStream.range(0, numEntities).forEach(i -> RENDER_COMPONENTS[i] = new RenderComponent()); + IntStream.range(0, numEntities).forEach(i -> renderComponents[i] = new RenderComponent()); } @@ -62,7 +62,7 @@ public class RenderComponentManager { LOGGER.info("Update Render Game Component "); // Process Render. IntStream.range(0, numEntities) - .filter(i -> RENDER_COMPONENTS.length > i && RENDER_COMPONENTS[i] != null) - .forEach(i -> RENDER_COMPONENTS[i].render()); + .filter(i -> renderComponents.length > i && renderComponents[i] != null) + .forEach(i -> renderComponents[i].render()); } } diff --git a/unit-of-work/pom.xml b/unit-of-work/pom.xml index 1fab101863eb1f075ffca9143d98ce4b5771abf8..0f8f0b21af41361dd905275eb9e32a2161c4383d 100644 --- a/unit-of-work/pom.xml +++ b/unit-of-work/pom.xml @@ -47,6 +47,7 @@ org.junit.jupiter junit-jupiter-engine + 5.0.0 test diff --git a/unit-of-work/src/main/java/com/iluwatar/unitofwork/App.java b/unit-of-work/src/main/java/com/iluwatar/unitofwork/App.java index a9e41dd7c16f3f2356e976a62de2764fa555920d..6caf1de498bc343e829e4833b29377483c6799bb 100644 --- a/unit-of-work/src/main/java/com/iluwatar/unitofwork/App.java +++ b/unit-of-work/src/main/java/com/iluwatar/unitofwork/App.java @@ -35,6 +35,7 @@ public class App { * * @param args no argument sent */ + public static void main(String[] args) { var ram = new Student(1, "Ram", "Street 9, Cupertino"); var shyam = new Student(2, "Shyam", "Z bridge, Pune"); diff --git a/unit-of-work/src/main/java/com/iluwatar/unitofwork/UnitActions.java b/unit-of-work/src/main/java/com/iluwatar/unitofwork/UnitActions.java index b00d015a3d7fcfea2c237d2e7d2615d5a7d6eff3..36e7386260ce9d13691a7728a344d92d9c1c3d2e 100644 --- a/unit-of-work/src/main/java/com/iluwatar/unitofwork/UnitActions.java +++ b/unit-of-work/src/main/java/com/iluwatar/unitofwork/UnitActions.java @@ -1,18 +1,40 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + package com.iluwatar.unitofwork; public enum UnitActions { - INSERT("INSERT"), - DELETE("DELETE"), - MODIFY("MODIFY") - ; + INSERT("INSERT"), + DELETE("DELETE"), + MODIFY("MODIFY"); - private final String actionValue; + private final String actionValue; - UnitActions(String actionValue) { - this.actionValue = actionValue; - } + UnitActions(String actionValue) { + this.actionValue = actionValue; + } - public String getActionValue() { - return actionValue; - } + public String getActionValue() { + return actionValue; + } } diff --git a/unit-of-work/src/test/java/com/iluwatar/unitofwork/AppTest.java b/unit-of-work/src/test/java/com/iluwatar/unitofwork/AppTest.java index 2fbe37ccf903af769ad7bfb6dbbf41cc8e563d1d..630bb1392c30b1d29e84e0f2b6f627883ccd664b 100644 --- a/unit-of-work/src/test/java/com/iluwatar/unitofwork/AppTest.java +++ b/unit-of-work/src/test/java/com/iluwatar/unitofwork/AppTest.java @@ -31,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; * AppTest */ public class AppTest { + @Test public void shouldExecuteWithoutException() { assertDoesNotThrow(() -> App.main(new String[]{}));