|
9 | 9 | * Licensed under the Apache License, Version 2.0 (the "License");
|
10 | 10 | * you may not use this file except in compliance with the License.
|
11 | 11 | * You may obtain a copy of the License at
|
12 |
| - * |
| 12 | + * |
13 | 13 | * http://www.apache.org/licenses/LICENSE-2.0
|
14 |
| - * |
| 14 | + * |
15 | 15 | * Unless required by applicable law or agreed to in writing, software
|
16 | 16 | * distributed under the License is distributed on an "AS IS" BASIS,
|
17 | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21 | 21 | */
|
22 | 22 |
|
23 | 23 |
|
24 |
| -import com.unzer.payment.PaymentError; |
25 |
| -import com.unzer.payment.PaymentException; |
26 |
| -import com.unzer.payment.TestData; |
| 24 | +import com.unzer.payment.*; |
| 25 | +import com.unzer.payment.business.AbstractPaymentTest; |
27 | 26 | import com.unzer.payment.communication.json.JsonCharge;
|
28 | 27 | import com.unzer.payment.communication.json.JsonErrorObject;
|
29 | 28 | import org.junit.Test;
|
30 | 29 |
|
31 |
| -import static org.junit.Assert.assertEquals; |
32 |
| -import static org.junit.Assert.fail; |
33 |
| - |
34 |
| -public class JsonParserTest { |
35 |
| - |
36 |
| - @Test |
37 |
| - public void given_an_error_message_then_payment_exception_is_thrown() { |
38 |
| - try { |
39 |
| - JsonParser parser = new JsonParser(); |
40 |
| - parser.fromJson(TestData.errorJson(), JsonCharge.class); |
41 |
| - |
42 |
| - fail("Expected PaymentException"); |
43 |
| - } catch (PaymentException exception) { |
44 |
| - assertBasicErrorAttributes(exception.getId(), exception.getUrl(), exception.getTimestamp()); |
45 |
| - assertPaymentError(exception.getPaymentErrorList().get(0)); |
46 |
| - } |
47 |
| - } |
48 |
| - |
49 |
| - @Test |
50 |
| - public void given_an_error_json_then_fromJson_returnes_jsonerrorobject() { |
51 |
| - assertJsonError(new JsonParser().fromJson(TestData.errorJson(), JsonErrorObject.class)); |
52 |
| - } |
53 |
| - |
54 |
| - private void assertJsonError(JsonErrorObject expectedError) { |
55 |
| - assertBasicErrorAttributes(expectedError.getId(), expectedError.getUrl(), expectedError.getTimestamp()); |
56 |
| - assertEquals(1, expectedError.getErrors().size()); |
57 |
| - assertPaymentError(expectedError.getErrors().get(0)); |
58 |
| - } |
59 |
| - |
60 |
| - private void assertBasicErrorAttributes(String id, String url, String timestamp) { |
61 |
| - assertEquals("s-err-f2ea241e5e8e4eb3b1513fab12c", id); |
62 |
| - assertEquals("https://api.unzer.com/v1/payments/charges", url); |
63 |
| - assertEquals("2019-01-09 15:42:24", timestamp); |
64 |
| - |
65 |
| - } |
66 |
| - |
67 |
| - private void assertPaymentError(PaymentError error) { |
68 |
| - assertEquals("COR.400.100.101", error.getCode()); |
69 |
| - assertEquals("Address untraceable", error.getMerchantMessage()); |
70 |
| - assertEquals("The provided address is invalid. Please check your input and try agian.", |
71 |
| - error.getCustomerMessage()); |
72 |
| - } |
| 30 | +import static org.junit.Assert.*; |
| 31 | + |
| 32 | +public class JsonParserTest extends AbstractPaymentTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void given_an_error_message_then_payment_exception_is_thrown() { |
| 36 | + try { |
| 37 | + JsonParser parser = new JsonParser(); |
| 38 | + parser.fromJson(TestData.errorJson(), JsonCharge.class); |
| 39 | + |
| 40 | + fail("Expected PaymentException"); |
| 41 | + } catch (PaymentException exception) { |
| 42 | + assertBasicErrorAttributes(exception.getId(), exception.getUrl(), exception.getTimestamp()); |
| 43 | + assertPaymentError(exception.getPaymentErrorList().get(0)); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void given_an_error_json_then_fromJson_returnes_jsonerrorobject() { |
| 49 | + assertJsonError(new JsonParser().fromJson(TestData.errorJson(), JsonErrorObject.class)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testValidJson() { |
| 54 | + assertTrue(new JsonParser().isJsonValid("{\"name\": \"value\"}")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testInvalidJson() { |
| 59 | + assertFalse(new JsonParser().isJsonValid("This is an error message!")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test(expected = IllegalArgumentException.class) |
| 63 | + public void testNullJson() { |
| 64 | + new JsonParser().fromJson(null, Payment.class); |
| 65 | + } |
| 66 | + |
| 67 | + @Test(expected = IllegalArgumentException.class) |
| 68 | + public void testNullClass() { |
| 69 | + new JsonParser().fromJson("{\"name\": \"value\"}", null); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testNullValidJson() { |
| 74 | + assertFalse(new JsonParser().isJsonValid(null)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test(expected = IllegalArgumentException.class) |
| 78 | + public void testFromInvalidJson() { |
| 79 | + new JsonParser().fromJson("This is an error message!", Payment.class); |
| 80 | + } |
| 81 | + |
| 82 | + private void assertJsonError(JsonErrorObject expectedError) { |
| 83 | + assertBasicErrorAttributes(expectedError.getId(), expectedError.getUrl(), expectedError.getTimestamp()); |
| 84 | + assertEquals(1, expectedError.getErrors().size()); |
| 85 | + assertPaymentError(expectedError.getErrors().get(0)); |
| 86 | + } |
| 87 | + |
| 88 | + private void assertBasicErrorAttributes(String id, String url, String timestamp) { |
| 89 | + assertEquals("s-err-f2ea241e5e8e4eb3b1513fab12c", id); |
| 90 | + assertEquals("https://api.unzer.com/v1/payments/charges", url); |
| 91 | + assertEquals("2019-01-09 15:42:24", timestamp); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + private void assertPaymentError(PaymentError error) { |
| 96 | + assertEquals("COR.400.100.101", error.getCode()); |
| 97 | + assertEquals("Address untraceable", error.getMerchantMessage()); |
| 98 | + assertEquals("The provided address is invalid. Please check your input and try agian.", |
| 99 | + error.getCustomerMessage()); |
| 100 | + } |
73 | 101 | }
|
0 commit comments