|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014-2016 Ilkka Sepp�l� |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package com.iluwatar.commander; |
| 25 | + |
| 26 | +import com.iluwatar.commander.employeehandle.EmployeeDatabase; |
| 27 | +import com.iluwatar.commander.employeehandle.EmployeeHandle; |
| 28 | +import com.iluwatar.commander.exceptions.DatabaseUnavailableException; |
| 29 | +import com.iluwatar.commander.messagingservice.MessagingDatabase; |
| 30 | +import com.iluwatar.commander.messagingservice.MessagingService; |
| 31 | +import com.iluwatar.commander.paymentservice.PaymentDatabase; |
| 32 | +import com.iluwatar.commander.paymentservice.PaymentService; |
| 33 | +import com.iluwatar.commander.shippingservice.ShippingDatabase; |
| 34 | +import com.iluwatar.commander.shippingservice.ShippingService; |
| 35 | +import com.iluwatar.commander.queue.QueueDatabase; |
| 36 | + |
| 37 | +/** |
| 38 | + * AppMessagingFailCases class looks at possible cases when Messaging service is |
| 39 | + * available/unavailable. |
| 40 | + */ |
| 41 | + |
| 42 | +public class AppMessagingFailCases { |
| 43 | + final int numOfRetries = 3; |
| 44 | + final long retryDuration = 30000; |
| 45 | + final long queueTime = 240000; //4 mins |
| 46 | + final long queueTaskTime = 60000; //1 min |
| 47 | + final long paymentTime = 120000; //2 mins |
| 48 | + final long messageTime = 150000; //2.5 mins |
| 49 | + final long employeeTime = 240000; //4 mins |
| 50 | + |
| 51 | + void messagingDatabaseUnavailableCasePaymentSuccess() throws Exception { |
| 52 | + //rest is successful |
| 53 | + PaymentService ps = new PaymentService(new PaymentDatabase()); |
| 54 | + ShippingService ss = new ShippingService(new ShippingDatabase()); |
| 55 | + MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(), |
| 56 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 57 | + new DatabaseUnavailableException(), new DatabaseUnavailableException()); |
| 58 | + EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase()); |
| 59 | + QueueDatabase qdb = new QueueDatabase(); |
| 60 | + Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration, |
| 61 | + queueTime,queueTaskTime,paymentTime,messageTime,employeeTime); |
| 62 | + User user = new User("Jim", "ABCD"); |
| 63 | + Order order = new Order(user, "book", 10f); |
| 64 | + c.placeOrder(order); |
| 65 | + } |
| 66 | + |
| 67 | + void messagingDatabaseUnavailableCasePaymentError() throws Exception { |
| 68 | + //rest is successful |
| 69 | + PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(), |
| 70 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 71 | + new DatabaseUnavailableException(), new DatabaseUnavailableException()); |
| 72 | + ShippingService ss = new ShippingService(new ShippingDatabase()); |
| 73 | + MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(), |
| 74 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 75 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 76 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 77 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 78 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException()); |
| 79 | + EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase()); |
| 80 | + QueueDatabase qdb = new QueueDatabase(); |
| 81 | + Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration, |
| 82 | + queueTime,queueTaskTime,paymentTime,messageTime,employeeTime); |
| 83 | + User user = new User("Jim", "ABCD"); |
| 84 | + Order order = new Order(user, "book", 10f); |
| 85 | + c.placeOrder(order); |
| 86 | + } |
| 87 | + |
| 88 | + void messagingDatabaseUnavailableCasePaymentFailure() throws Exception { |
| 89 | + //rest is successful |
| 90 | + PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(), |
| 91 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 92 | + new DatabaseUnavailableException(), new DatabaseUnavailableException()); |
| 93 | + ShippingService ss = new ShippingService(new ShippingDatabase()); |
| 94 | + MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(), |
| 95 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 96 | + new DatabaseUnavailableException(), new DatabaseUnavailableException()); |
| 97 | + EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase()); |
| 98 | + QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 99 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 100 | + new DatabaseUnavailableException()); |
| 101 | + Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,queueTime,queueTaskTime, |
| 102 | + paymentTime,messageTime,employeeTime); |
| 103 | + User user = new User("Jim", "ABCD"); |
| 104 | + Order order = new Order(user, "book", 10f); |
| 105 | + c.placeOrder(order); |
| 106 | + } |
| 107 | + |
| 108 | + void messagingSuccessCase() throws Exception { |
| 109 | + //done here |
| 110 | + PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(), |
| 111 | + new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(), |
| 112 | + new DatabaseUnavailableException(), new DatabaseUnavailableException()); |
| 113 | + ShippingService ss = new ShippingService(new ShippingDatabase()); |
| 114 | + MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(), |
| 115 | + new DatabaseUnavailableException()); |
| 116 | + EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase()); |
| 117 | + QueueDatabase qdb = new QueueDatabase(); |
| 118 | + Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration, |
| 119 | + queueTime,queueTaskTime,paymentTime,messageTime,employeeTime); |
| 120 | + User user = new User("Jim", "ABCD"); |
| 121 | + Order order = new Order(user, "book", 10f); |
| 122 | + c.placeOrder(order); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Program entry point. |
| 127 | + * |
| 128 | + * @param args command line args |
| 129 | + */ |
| 130 | + |
| 131 | + public static void main(String[] args) throws Exception { |
| 132 | + AppMessagingFailCases amfc = new AppMessagingFailCases(); |
| 133 | + //amfc.messagingDatabaseUnavailableCasePaymentSuccess(); |
| 134 | + //amfc.messagingDatabaseUnavailableCasePaymentError(); |
| 135 | + //amfc.messagingDatabaseUnavailableCasePaymentFailure(); |
| 136 | + amfc.messagingSuccessCase(); |
| 137 | + } |
| 138 | +} |
0 commit comments