|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2017 Intuit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | +package com.intuit.payment.services; |
| 17 | + |
| 18 | +import com.intuit.payment.config.RequestContext; |
| 19 | +import com.intuit.payment.data.ECheck; |
| 20 | +import com.intuit.payment.data.Refund; |
| 21 | +import com.intuit.payment.exception.BaseException; |
| 22 | +import com.intuit.payment.http.Request; |
| 23 | +import com.intuit.payment.http.Response; |
| 24 | +import com.intuit.payment.services.base.ServiceBase; |
| 25 | +import com.intuit.payment.util.JsonUtil; |
| 26 | +import mockit.Expectations; |
| 27 | +import mockit.Mocked; |
| 28 | +import org.testng.Assert; |
| 29 | +import org.testng.annotations.Test; |
| 30 | + |
| 31 | +import java.math.BigDecimal; |
| 32 | + |
| 33 | +public class ECheckServiceTest { |
| 34 | + |
| 35 | + @Mocked |
| 36 | + private ServiceBase serviceBase; |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testECheckServiceCreation() { |
| 40 | + ECheckService eCheckService = new ECheckService(); |
| 41 | + Assert.assertNull(eCheckService.getRequestContext()); |
| 42 | + } |
| 43 | + @Test |
| 44 | + public void testECheckServiceRequestContext() { |
| 45 | + RequestContext requestContext = new RequestContext(); |
| 46 | + requestContext.setBaseUrl("fakeBaseUrl"); |
| 47 | + ECheckService eCheckService = new ECheckService(requestContext); |
| 48 | + Assert.assertEquals(requestContext, eCheckService.getRequestContext()); |
| 49 | + |
| 50 | + RequestContext secondRequestContext = new RequestContext(); |
| 51 | + secondRequestContext.setBaseUrl("secondBaseUrl"); |
| 52 | + eCheckService.setRequestContext(secondRequestContext); |
| 53 | + Assert.assertEquals(secondRequestContext, eCheckService.getRequestContext()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testCreateECheck() throws BaseException { |
| 58 | + ECheck expectedECheck = new ECheck.Builder().id("ijids").amount(new BigDecimal("50000")).build(); |
| 59 | + final String eCheckString = JsonUtil.serialize(expectedECheck); |
| 60 | + final Response mockedResponse = new Response(200, eCheckString, "fjkdlsfd"); |
| 61 | + mockedResponse.setResponseObject(expectedECheck); |
| 62 | + |
| 63 | + new Expectations() {{ |
| 64 | + serviceBase.sendRequest((Request) any); result = mockedResponse; |
| 65 | + }}; |
| 66 | + |
| 67 | + RequestContext requestContext = new RequestContext(); |
| 68 | + requestContext.setBaseUrl("fakeBaseUrl"); |
| 69 | + ECheckService eCheckService = new ECheckService(requestContext); |
| 70 | + |
| 71 | + try { |
| 72 | + ECheck eCheck = new ECheck(); |
| 73 | + ECheck eCheckGenerated = eCheckService.create(eCheck); |
| 74 | + Assert.assertEquals(expectedECheck, eCheckGenerated); |
| 75 | + |
| 76 | + } catch (BaseException e) { |
| 77 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testRetrieveECheck() throws BaseException { |
| 83 | + ECheck expectedECheck = new ECheck.Builder().id("ijids").amount(new BigDecimal("50000")).build(); |
| 84 | + final String eCheckString = JsonUtil.serialize(expectedECheck); |
| 85 | + final Response mockedResponse = new Response(200, eCheckString, "fjkdlsfd"); |
| 86 | + mockedResponse.setResponseObject(expectedECheck); |
| 87 | + |
| 88 | + new Expectations() {{ |
| 89 | + serviceBase.sendRequest((Request) any); result = mockedResponse; |
| 90 | + }}; |
| 91 | + |
| 92 | + RequestContext requestContext = new RequestContext(); |
| 93 | + requestContext.setBaseUrl("fakeBaseUrl"); |
| 94 | + ECheckService eCheckService = new ECheckService(requestContext); |
| 95 | + |
| 96 | + try { |
| 97 | + ECheck eCheckGenerated = eCheckService.retrieve("myECheckId"); |
| 98 | + Assert.assertEquals(expectedECheck, eCheckGenerated); |
| 99 | + |
| 100 | + } catch (BaseException e) { |
| 101 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testRetrieveECheckNullECheckId() { |
| 107 | + ECheckService eCheckService = new ECheckService(); |
| 108 | + try { |
| 109 | + eCheckService.retrieve(null); |
| 110 | + Assert.fail("Expected IllegalArgumentException thrown"); |
| 111 | + } catch (IllegalArgumentException e) { |
| 112 | + Assert.assertTrue(true, "Expected IllegalArgumentException was thrown"); |
| 113 | + } catch (BaseException e) { |
| 114 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 115 | + } |
| 116 | + try { |
| 117 | + eCheckService.retrieve(""); |
| 118 | + Assert.fail("Expected IllegalArgumentException thrown"); |
| 119 | + } catch (IllegalArgumentException e) { |
| 120 | + Assert.assertTrue(true, "Expected IllegalArgumentException was thrown"); |
| 121 | + } catch (BaseException e) { |
| 122 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testRefundECheck() throws BaseException { |
| 128 | + Refund expectedRefund = new Refund.Builder().id("ijids").amount(new BigDecimal("50000")).build(); |
| 129 | + final String eCheckString = JsonUtil.serialize(expectedRefund); |
| 130 | + final Response mockedResponse = new Response(200, eCheckString, "fjkdlsfd"); |
| 131 | + mockedResponse.setResponseObject(expectedRefund); |
| 132 | + |
| 133 | + new Expectations() {{ |
| 134 | + serviceBase.sendRequest((Request) any); result = mockedResponse; |
| 135 | + }}; |
| 136 | + |
| 137 | + RequestContext requestContext = new RequestContext(); |
| 138 | + requestContext.setBaseUrl("fakeBaseUrl"); |
| 139 | + ECheckService eCheckService = new ECheckService(requestContext); |
| 140 | + |
| 141 | + try { |
| 142 | + Refund refund = new Refund.Builder().id("refundId").type("type").build(); |
| 143 | + Refund refundGenerated = eCheckService.refund("myECheckId", refund); |
| 144 | + Assert.assertEquals(expectedRefund, refundGenerated); |
| 145 | + |
| 146 | + } catch (BaseException e) { |
| 147 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testRefundNullECheckId() { |
| 153 | + ECheckService eCheckService = new ECheckService(); |
| 154 | + Refund refund = new Refund(); |
| 155 | + try { |
| 156 | + eCheckService.refund(null, refund); |
| 157 | + Assert.fail("Expected IllegalArgumentException thrown"); |
| 158 | + } catch (IllegalArgumentException e) { |
| 159 | + Assert.assertTrue(true, "Expected IllegalArgumentException was thrown"); |
| 160 | + } catch (BaseException e) { |
| 161 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 162 | + } |
| 163 | + try { |
| 164 | + eCheckService.refund("", refund); |
| 165 | + Assert.fail("Expected IllegalArgumentException thrown"); |
| 166 | + } catch (IllegalArgumentException e) { |
| 167 | + Assert.assertTrue(true, "Expected IllegalArgumentException was thrown"); |
| 168 | + } catch (BaseException e) { |
| 169 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testGetRefund() throws BaseException { |
| 175 | + Refund expectedRefund = new Refund.Builder().id("ijids").amount(new BigDecimal("50000")).build(); |
| 176 | + final String eCheckString = JsonUtil.serialize(expectedRefund); |
| 177 | + final Response mockedResponse = new Response(200, eCheckString, "fjkdlsfd"); |
| 178 | + mockedResponse.setResponseObject(expectedRefund); |
| 179 | + |
| 180 | + new Expectations() {{ |
| 181 | + serviceBase.sendRequest((Request) any); result = mockedResponse; |
| 182 | + }}; |
| 183 | + |
| 184 | + RequestContext requestContext = new RequestContext(); |
| 185 | + requestContext.setBaseUrl("fakeBaseUrl"); |
| 186 | + ECheckService eCheckService = new ECheckService(requestContext); |
| 187 | + |
| 188 | + try { |
| 189 | + Refund refund = new Refund.Builder().id("refundId").type("type").build(); |
| 190 | + Refund refundGenerated = eCheckService.getRefund("myECheckId", "myRefundId"); |
| 191 | + Assert.assertEquals(expectedRefund, refundGenerated); |
| 192 | + |
| 193 | + } catch (BaseException e) { |
| 194 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + public void testRefundECheckNullCheckId() { |
| 200 | + ECheckService eCheckService = new ECheckService(); |
| 201 | + try { |
| 202 | + eCheckService.getRefund(null, "refundid"); |
| 203 | + Assert.fail("Expected IllegalArgumentException thrown"); |
| 204 | + } catch (IllegalArgumentException e) { |
| 205 | + Assert.assertTrue(true, "Expected IllegalArgumentException was thrown"); |
| 206 | + } catch (BaseException e) { |
| 207 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 208 | + } |
| 209 | + try { |
| 210 | + eCheckService.getRefund("", "refundid"); |
| 211 | + Assert.fail("Expected IllegalArgumentException thrown"); |
| 212 | + } catch (IllegalArgumentException e) { |
| 213 | + Assert.assertTrue(true, "Expected IllegalArgumentException was thrown"); |
| 214 | + } catch (BaseException e) { |
| 215 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + public void testRefundECheckNullRefundId() { |
| 221 | + ECheckService eCheckService = new ECheckService(); |
| 222 | + try { |
| 223 | + eCheckService.getRefund("echeckid", null); |
| 224 | + Assert.fail("Expected IllegalArgumentException thrown"); |
| 225 | + } catch (IllegalArgumentException e) { |
| 226 | + Assert.assertTrue(true, "Expected IllegalArgumentException was thrown"); |
| 227 | + } catch (BaseException e) { |
| 228 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 229 | + } |
| 230 | + try { |
| 231 | + eCheckService.getRefund("echeckid", ""); |
| 232 | + Assert.fail("Expected IllegalArgumentException thrown"); |
| 233 | + } catch (IllegalArgumentException e) { |
| 234 | + Assert.assertTrue(true, "Expected IllegalArgumentException was thrown"); |
| 235 | + } catch (BaseException e) { |
| 236 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + |
| 241 | +} |
0 commit comments