|
| 1 | +package com.intuit.payment.services; |
| 2 | + |
| 3 | +import com.intuit.payment.config.RequestContext; |
| 4 | +import com.intuit.payment.data.BankAccount; |
| 5 | +import com.intuit.payment.data.Card; |
| 6 | +import com.intuit.payment.data.Token; |
| 7 | +import com.intuit.payment.exception.BaseException; |
| 8 | +import com.intuit.payment.http.Request; |
| 9 | +import com.intuit.payment.http.Response; |
| 10 | +import com.intuit.payment.services.base.ServiceBase; |
| 11 | +import com.intuit.payment.util.JsonUtil; |
| 12 | +import mockit.Expectations; |
| 13 | +import mockit.Mocked; |
| 14 | +import org.testng.Assert; |
| 15 | +import org.testng.annotations.Test; |
| 16 | + |
| 17 | +public class TokenServiceTest { |
| 18 | + |
| 19 | + @Mocked |
| 20 | + private ServiceBase serviceBase; |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testTokenServiceCreation() { |
| 24 | + TokenService tokenService = new TokenService(); |
| 25 | + Assert.assertNull(tokenService.getRequestContext()); |
| 26 | + } |
| 27 | + @Test |
| 28 | + public void testTokenServiceRequestContext() { |
| 29 | + RequestContext requestContext = new RequestContext(); |
| 30 | + requestContext.setBaseUrl("fakeBaseUrl"); |
| 31 | + TokenService tokenService = new TokenService(requestContext); |
| 32 | + Assert.assertEquals(requestContext, tokenService.getRequestContext()); |
| 33 | + |
| 34 | + RequestContext secondRequestContext = new RequestContext(); |
| 35 | + secondRequestContext.setBaseUrl("secondBaseUrl"); |
| 36 | + tokenService.setRequestContext(secondRequestContext); |
| 37 | + Assert.assertEquals(secondRequestContext, tokenService.getRequestContext()); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testCreateToken() throws BaseException { |
| 43 | + Token expectedToken = new Token.Builder() |
| 44 | + .card(new Card.Builder().name("cardName").build()) |
| 45 | + .bankAccount(new BankAccount.Builder().id("fakeID").build()) |
| 46 | + .value("fake value") |
| 47 | + .build(); |
| 48 | + final String tokenString = JsonUtil.serialize(expectedToken); |
| 49 | + final Response mockedResponse = new Response(200, tokenString, "fjkdlsfd"); |
| 50 | + mockedResponse.setResponseObject(expectedToken); |
| 51 | + |
| 52 | + new Expectations() {{ |
| 53 | + serviceBase.sendRequest((Request) any); result = mockedResponse; |
| 54 | + }}; |
| 55 | + |
| 56 | + RequestContext requestContext = new RequestContext(); |
| 57 | + requestContext.setBaseUrl("fakeBaseUrl"); |
| 58 | + TokenService tokenService = new TokenService(requestContext); |
| 59 | + |
| 60 | + try { |
| 61 | + Token token = new Token(); |
| 62 | + Token tokenGenerated = tokenService.createToken(token); |
| 63 | + Assert.assertEquals(expectedToken, tokenGenerated); |
| 64 | + |
| 65 | + } catch (BaseException e) { |
| 66 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testCreateTokenBadResponse() throws BaseException { |
| 72 | + final Response mockedResponse = new Response(500, "", "fjkdlsfd"); |
| 73 | + new Expectations() {{ |
| 74 | + serviceBase.sendRequest((Request) any); result = mockedResponse; |
| 75 | + }}; |
| 76 | + |
| 77 | + RequestContext requestContext = new RequestContext(); |
| 78 | + requestContext.setBaseUrl("fakeBaseUrl"); |
| 79 | + TokenService tokenService = new TokenService(requestContext); |
| 80 | + |
| 81 | + try { |
| 82 | + Token token = new Token(); |
| 83 | + Token tokenGenerated = tokenService.createToken(token); |
| 84 | + Assert.assertNull(tokenGenerated); |
| 85 | + |
| 86 | + } catch (BaseException e) { |
| 87 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments