|
| 1 | +package co.omise; |
| 2 | + |
| 3 | +import co.omise.models.*; |
| 4 | +import co.omise.requests.Request; |
| 5 | +import co.omise.requests.Requester; |
| 6 | +import co.omise.requests.ResponseType; |
| 7 | +import co.omise.models.schedules.Schedule; |
| 8 | +import co.omise.models.schedules.Occurrence; |
| 9 | +import okhttp3.OkHttpClient; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.io.ByteArrayOutputStream; |
| 13 | +import java.io.PrintStream; |
| 14 | +import java.lang.reflect.Method; |
| 15 | +import java.lang.reflect.ParameterizedType; |
| 16 | +import java.lang.reflect.Type; |
| 17 | +import java.util.Collections; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertNotNull; |
| 20 | + |
| 21 | +public class ExampleTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void coversAllExampleScenarios() throws Exception { |
| 25 | + Client stubClient = new Client(new MockRequester()); |
| 26 | + Example example = new Example(stubClient); |
| 27 | + |
| 28 | + PrintStream originalOut = System.out; |
| 29 | + ByteArrayOutputStream sink = new ByteArrayOutputStream(); |
| 30 | + System.setOut(new PrintStream(sink)); |
| 31 | + try { |
| 32 | + example.retrieveAccount(); |
| 33 | + example.retrieveBalance(); |
| 34 | + example.destroyCard(); |
| 35 | + example.listCards(); |
| 36 | + example.retrieveCard(); |
| 37 | + example.updateCard(); |
| 38 | + example.captureCharge(); |
| 39 | + example.partialCaptureCharge(); |
| 40 | + example.chargeWithCard(); |
| 41 | + example.chargeWithAuthentication(AuthenticationType.THREE_DS); |
| 42 | + example.chargeWithCustomer(); |
| 43 | + example.chargeWithToken(); |
| 44 | + example.createPartialCaptureCharge(); |
| 45 | + example.listCharges(); |
| 46 | + example.retrieveCharge(); |
| 47 | + example.reverseCharge(); |
| 48 | + example.updateCharge(); |
| 49 | + example.attachCardToCustomer(); |
| 50 | + example.createCustomerSimple(); |
| 51 | + example.updateCustomer(); |
| 52 | + example.destroyCustomer(); |
| 53 | + example.listAllDisputes(); |
| 54 | + example.listClosedDiputes(); |
| 55 | + example.listOpenDiputes(); |
| 56 | + example.listPendingDiputes(); |
| 57 | + example.retrieveDispute(); |
| 58 | + example.updateDispute(); |
| 59 | + example.listEvents(); |
| 60 | + example.retrieveEvent(); |
| 61 | + example.retrieveCustomer(); |
| 62 | + example.listCustomers(); |
| 63 | + example.createTransfer(); |
| 64 | + example.createTransferWithRecipient(); |
| 65 | + example.destroyTransfer(); |
| 66 | + example.listTransfers(); |
| 67 | + example.retrieveTransfer(); |
| 68 | + example.updateTransfer(); |
| 69 | + example.createRecipient(); |
| 70 | + example.destroyRecipient(); |
| 71 | + example.listRecipients(); |
| 72 | + example.retrieveRecipient(); |
| 73 | + example.updateRecipient(); |
| 74 | + example.createRefund(); |
| 75 | + example.listRefunds(); |
| 76 | + example.retrieveRefund(); |
| 77 | + example.createToken(); |
| 78 | + example.retrieveToken(); |
| 79 | + example.listTransactions(); |
| 80 | + example.retrieveTransaction(); |
| 81 | + example.createLink(); |
| 82 | + example.retrieveLink(); |
| 83 | + example.listLinks(); |
| 84 | + example.createSource(); |
| 85 | + example.createSourceInstallment(); |
| 86 | + example.retrieveSearch(); |
| 87 | + example.retrieveSchedule(); |
| 88 | + example.listSchedule(); |
| 89 | + example.listChargeSchedule(); |
| 90 | + example.listCustomerSchedule(); |
| 91 | + example.listTransferSchedule(); |
| 92 | + example.listRecipientSchedule(); |
| 93 | + example.createSchedule(); |
| 94 | + example.destroySchedule(); |
| 95 | + example.retrieveOccurrence(); |
| 96 | + example.listOccurrence(); |
| 97 | + example.retrieveReceipt(); |
| 98 | + example.listReceipt(); |
| 99 | + example.getForex(); |
| 100 | + example.getCapapabilities(); |
| 101 | + } finally { |
| 102 | + System.setOut(originalOut); |
| 103 | + } |
| 104 | + |
| 105 | + Example realExample = new Example(); |
| 106 | + Method clientMethod = Example.class.getDeclaredMethod("client"); |
| 107 | + clientMethod.setAccessible(true); |
| 108 | + Client actualClient = (Client) clientMethod.invoke(realExample); |
| 109 | + assertNotNull(actualClient); |
| 110 | + } |
| 111 | + |
| 112 | + private static final class MockRequester implements Requester { |
| 113 | + private final OkHttpClient httpClient = new OkHttpClient(); |
| 114 | + |
| 115 | + @Override |
| 116 | + public <T extends OmiseObjectBase, R extends Request<T>> T sendRequest(R request) { |
| 117 | + ResponseType<T> responseType = request.getType(); |
| 118 | + |
| 119 | + if (responseType.isClassType()) { |
| 120 | + return responseType.getClassType().cast(createStubForClass(responseType.getClassType())); |
| 121 | + } |
| 122 | + |
| 123 | + if (responseType.isTypeReference()) { |
| 124 | + return castFromType(responseType.getTypeReference().getType()); |
| 125 | + } |
| 126 | + |
| 127 | + throw new UnsupportedOperationException("Unhandled response type: " + responseType); |
| 128 | + } |
| 129 | + |
| 130 | + @SuppressWarnings("unchecked") |
| 131 | + private <T extends OmiseObjectBase> T castFromType(Type type) { |
| 132 | + Object stub = createStubForType(type); |
| 133 | + return (T) stub; |
| 134 | + } |
| 135 | + |
| 136 | + private OmiseObjectBase createStubForClass(Class<?> clazz) { |
| 137 | + if (clazz.equals(Account.class)) { |
| 138 | + return withId(new Account(), "acct_test"); |
| 139 | + } |
| 140 | + if (clazz.equals(Balance.class)) { |
| 141 | + Balance balance = withId(new Balance(), "bal_test"); |
| 142 | + balance.setTransferable(12345L); |
| 143 | + return balance; |
| 144 | + } |
| 145 | + if (clazz.equals(Card.class)) { |
| 146 | + Card card = withId(new Card(), "card_test"); |
| 147 | + card.setLastDigits("4242"); |
| 148 | + return card; |
| 149 | + } |
| 150 | + if (clazz.equals(Charge.class)) { |
| 151 | + Charge charge = withId(new Charge(), "chrg_test"); |
| 152 | + charge.setAmount(1000L); |
| 153 | + charge.setDescription("desc"); |
| 154 | + charge.setReversed(true); |
| 155 | + charge.setAuthorizeUri("https://example.com/auth"); |
| 156 | + return charge; |
| 157 | + } |
| 158 | + if (clazz.equals(Token.class)) { |
| 159 | + Token token = withId(new Token(), "tokn_test"); |
| 160 | + Card card = withId(new Card(), "card_token"); |
| 161 | + card.setLastDigits("1111"); |
| 162 | + token.setCard(card); |
| 163 | + return token; |
| 164 | + } |
| 165 | + if (clazz.equals(Customer.class)) { |
| 166 | + Customer customer = withId(new Customer(), "cust_test"); |
| 167 | + customer.setEmail("user@example.com"); |
| 168 | + return customer; |
| 169 | + } |
| 170 | + if (clazz.equals(Dispute.class)) { |
| 171 | + Dispute dispute = withId(new Dispute(), "dspt_test"); |
| 172 | + dispute.setAmount(5000L); |
| 173 | + dispute.setMessage("message"); |
| 174 | + return dispute; |
| 175 | + } |
| 176 | + if (clazz.equals(Event.class)) { |
| 177 | + Event event = withId(new Event(), "evnt_test"); |
| 178 | + event.setKey("charge.completed"); |
| 179 | + return event; |
| 180 | + } |
| 181 | + if (clazz.equals(Transfer.class)) { |
| 182 | + Transfer transfer = withId(new Transfer(), "trsf_test"); |
| 183 | + transfer.setAmount(2000L); |
| 184 | + return transfer; |
| 185 | + } |
| 186 | + if (clazz.equals(Recipient.class)) { |
| 187 | + Recipient recipient = withId(new Recipient(), "recp_test"); |
| 188 | + recipient.setEmail("recipient@example.com"); |
| 189 | + return recipient; |
| 190 | + } |
| 191 | + if (clazz.equals(Refund.class)) { |
| 192 | + Refund refund = withId(new Refund(), "rfnd_test"); |
| 193 | + refund.setAmount(3000L); |
| 194 | + return refund; |
| 195 | + } |
| 196 | + if (clazz.equals(Transaction.class)) { |
| 197 | + Transaction transaction = withId(new Transaction(), "trxn_test"); |
| 198 | + transaction.setAmount(4000L); |
| 199 | + return transaction; |
| 200 | + } |
| 201 | + if (clazz.equals(Link.class)) { |
| 202 | + return withId(new Link(), "link_test"); |
| 203 | + } |
| 204 | + if (clazz.equals(Source.class)) { |
| 205 | + return withId(new Source(), "src_test"); |
| 206 | + } |
| 207 | + if (clazz.equals(Schedule.class)) { |
| 208 | + return withId(new Schedule(), "schd_test"); |
| 209 | + } |
| 210 | + if (clazz.equals(Occurrence.class)) { |
| 211 | + return withId(new Occurrence(), "occu_test"); |
| 212 | + } |
| 213 | + if (clazz.equals(Receipt.class)) { |
| 214 | + return withId(new Receipt(), "rcpt_test"); |
| 215 | + } |
| 216 | + if (clazz.equals(Forex.class)) { |
| 217 | + Forex forex = withId(new Forex(), "forx_test"); |
| 218 | + forex.setRate(1.23d); |
| 219 | + return forex; |
| 220 | + } |
| 221 | + if (clazz.equals(Capability.class)) { |
| 222 | + Capability capability = withId(new Capability(), "cap_test"); |
| 223 | + capability.setZeroInterestInstallments(true); |
| 224 | + return capability; |
| 225 | + } |
| 226 | + |
| 227 | + throw new UnsupportedOperationException("Unhandled class: " + clazz.getName()); |
| 228 | + } |
| 229 | + |
| 230 | + private OmiseObjectBase createStubForType(Type type) { |
| 231 | + if (type instanceof ParameterizedType) { |
| 232 | + ParameterizedType parameterizedType = (ParameterizedType) type; |
| 233 | + Type rawType = parameterizedType.getRawType(); |
| 234 | + |
| 235 | + if (rawType == ScopedList.class) { |
| 236 | + Type elementType = parameterizedType.getActualTypeArguments()[0]; |
| 237 | + Class<?> element = elementType instanceof Class |
| 238 | + ? (Class<?>) elementType |
| 239 | + : Charge.class; |
| 240 | + ScopedList<Model> scopedList = new ScopedList<>(); |
| 241 | + scopedList.setTotal(1); |
| 242 | + scopedList.setData(Collections.singletonList((Model) createStubForClass(element))); |
| 243 | + return scopedList; |
| 244 | + } |
| 245 | + |
| 246 | + if (rawType == SearchResult.class) { |
| 247 | + SearchResult<Model> searchResult = new SearchResult<>(); |
| 248 | + searchResult.setTotal(1); |
| 249 | + searchResult.setData(Collections.singletonList((Model) createStubForClass(Charge.class))); |
| 250 | + return searchResult; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + throw new UnsupportedOperationException("Unhandled type: " + type.getTypeName()); |
| 255 | + } |
| 256 | + |
| 257 | + private <T extends Model> T withId(T model, String id) { |
| 258 | + model.setId(id); |
| 259 | + return model; |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public OkHttpClient getHttpClient() { |
| 264 | + return httpClient; |
| 265 | + } |
| 266 | + } |
| 267 | +} |
0 commit comments