1+ package uk .gov .pay .ledger .transaction .resource ;
2+
3+ import com .google .common .collect .ImmutableMap ;
4+ import org .apache .commons .csv .CSVParser ;
5+ import org .apache .commons .csv .CSVRecord ;
6+ import org .junit .Before ;
7+ import org .junit .ClassRule ;
8+ import org .junit .Test ;
9+ import uk .gov .pay .ledger .metadatakey .dao .MetadataKeyDao ;
10+ import uk .gov .pay .ledger .rule .AppWithPostgresAndSqsRule ;
11+ import uk .gov .pay .ledger .transaction .state .TransactionState ;
12+ import uk .gov .pay .ledger .transactionmetadata .dao .TransactionMetadataDao ;
13+ import uk .gov .pay .ledger .util .fixture .TransactionFixture ;
14+
15+ import javax .ws .rs .core .Response ;
16+ import java .io .IOException ;
17+ import java .io .InputStream ;
18+ import java .time .ZonedDateTime ;
19+ import java .util .List ;
20+
21+ import static io .restassured .RestAssured .given ;
22+ import static java .nio .charset .StandardCharsets .UTF_8 ;
23+ import static org .apache .commons .csv .CSVFormat .DEFAULT ;
24+ import static org .apache .commons .csv .CSVFormat .RFC4180 ;
25+ import static org .hamcrest .CoreMatchers .is ;
26+ import static org .hamcrest .MatcherAssert .assertThat ;
27+ import static uk .gov .pay .ledger .util .DatabaseTestHelper .aDatabaseTestHelper ;
28+ import static uk .gov .pay .ledger .util .fixture .TransactionFixture .aTransactionFixture ;
29+
30+ public class TransactionResourceCsvIT {
31+
32+ @ ClassRule
33+ public static AppWithPostgresAndSqsRule rule = new AppWithPostgresAndSqsRule ();
34+
35+ private Integer port = rule .getAppRule ().getLocalPort ();
36+ private MetadataKeyDao metadataKeyDao ;
37+ private TransactionMetadataDao transactionMetadataDao ;
38+
39+ @ Before
40+ public void setUp () {
41+ transactionMetadataDao = new TransactionMetadataDao (rule .getJdbi ());
42+ metadataKeyDao = rule .getJdbi ().onDemand (MetadataKeyDao .class );
43+ aDatabaseTestHelper (rule .getJdbi ()).truncateAllData ();
44+ }
45+
46+ @ Test
47+ public void shouldGetAllTransactionsAsCSVWithAcceptType () throws IOException {
48+ String targetGatewayAccountId = "123" ;
49+ String otherGatewayAccountId = "456" ;
50+
51+ TransactionFixture transactionFixture = aTransactionFixture ()
52+ .withTransactionType ("PAYMENT" )
53+ .withState (TransactionState .ERROR_GATEWAY )
54+ .withAmount (123L )
55+ .withTotalAmount (123L )
56+ .withCorporateCardSurcharge (5L )
57+ .withCreatedDate (ZonedDateTime .parse ("2018-03-12T16:25:01.123456Z" ))
58+ .withGatewayAccountId (targetGatewayAccountId )
59+ .withLastDigitsCardNumber ("1234" )
60+ .withCardBrandLabel ("Diners Club" )
61+ .withDefaultCardDetails ().withCardholderName ("J Doe" )
62+ .withGatewayTransactionId ("gateway-transaction-id" )
63+ .withExternalMetadata (ImmutableMap .of ("test-key-1" , "value1" ))
64+ .withDefaultTransactionDetails ()
65+ .insert (rule .getJdbi ());
66+
67+ aTransactionFixture ()
68+ .withTransactionType ("PAYMENT" )
69+ .withState (TransactionState .SUBMITTED )
70+ .withGatewayAccountId (otherGatewayAccountId )
71+ .insert (rule .getJdbi ());
72+
73+ aTransactionFixture ()
74+ .withTransactionType ("REFUND" )
75+ .withParentExternalId (transactionFixture .getExternalId ())
76+ .withGatewayAccountId (transactionFixture .getGatewayAccountId ())
77+ .withRefundedByUserEmail ("refund-by-user-email@example.org" )
78+ .withCreatedDate (ZonedDateTime .parse ("2018-03-12T16:24:01.123456Z" ))
79+ .withAmount (100L )
80+ .withTotalAmount (100L )
81+ .withState (TransactionState .ERROR_GATEWAY )
82+ .withDefaultTransactionDetails ()
83+ .insert (rule .getJdbi ());
84+
85+ metadataKeyDao .insertIfNotExist ("test-key-1" );
86+ metadataKeyDao .insertIfNotExist ("test-key-2" );
87+
88+ transactionMetadataDao .insertIfNotExist (transactionFixture .getId (), "test-key-1" );
89+
90+ InputStream csvResponseStream = given ().port (port )
91+ .accept ("text/csv" )
92+ .get ("/v1/transaction/?" +
93+ "account_id=" + targetGatewayAccountId +
94+ "&page=1" +
95+ "&display_size=5"
96+ )
97+ .then ()
98+ .statusCode (Response .Status .OK .getStatusCode ())
99+ .contentType ("text/csv" )
100+ .extract ().asInputStream ();
101+
102+ List <CSVRecord > csvRecords = CSVParser .parse (csvResponseStream , UTF_8 , RFC4180 .withFirstRecordAsHeader ()).getRecords ();
103+
104+ assertThat (csvRecords .size (), is (2 ));
105+
106+ CSVRecord paymentRecord = csvRecords .get (0 );
107+ assertThat (paymentRecord .size (), is (22 ));
108+ assertPaymentTransactionDetails (paymentRecord , transactionFixture );
109+ assertThat (paymentRecord .get ("Amount" ), is ("1.23" ));
110+ assertThat (paymentRecord .get ("State" ), is ("Error" ));
111+ assertThat (paymentRecord .get ("Finished" ), is ("true" ));
112+ assertThat (paymentRecord .get ("Error Code" ), is ("P0050" ));
113+ assertThat (paymentRecord .get ("Error Message" ), is ("Payment provider returned an error" ));
114+ assertThat (paymentRecord .get ("Date Created" ), is ("12 Mar 2018" ));
115+ assertThat (paymentRecord .get ("Time Created" ), is ("16:25:01" ));
116+ assertThat (paymentRecord .get ("Corporate Card Surcharge" ), is ("0.05" ));
117+ assertThat (paymentRecord .get ("Total Amount" ), is ("1.23" ));
118+ assertThat (paymentRecord .get ("test-key-1 (metadata)" ), is ("value1" ));
119+ assertThat (paymentRecord .get ("Wallet Type" ), is ("" ));
120+ assertThat (paymentRecord .isMapped ("Net" ), is (false ));
121+ assertThat (paymentRecord .isMapped ("Fee" ), is (false ));
122+ assertThat (paymentRecord .isMapped ("MOTO" ), is (false ));
123+
124+ CSVRecord refundRecord = csvRecords .get (1 );
125+ assertPaymentTransactionDetails (refundRecord , transactionFixture );
126+ assertThat (refundRecord .get ("Amount" ), is ("-1.00" ));
127+ assertThat (refundRecord .get ("State" ), is ("Refund error" ));
128+ assertThat (refundRecord .get ("Finished" ), is ("true" ));
129+ assertThat (refundRecord .get ("Error Code" ), is ("P0050" ));
130+ assertThat (refundRecord .get ("Error Message" ), is ("Payment provider returned an error" ));
131+ assertThat (refundRecord .get ("Date Created" ), is ("12 Mar 2018" ));
132+ assertThat (refundRecord .get ("Time Created" ), is ("16:24:01" ));
133+ assertThat (refundRecord .get ("Corporate Card Surcharge" ), is ("0.00" ));
134+ assertThat (refundRecord .get ("Total Amount" ), is ("-1.00" ));
135+ assertThat (refundRecord .get ("Wallet Type" ), is ("" ));
136+ assertThat (refundRecord .get ("Issued By" ), is ("refund-by-user-email@example.org" ));
137+ }
138+
139+ @ Test
140+ public void shouldGetAllTransactionsAsCSVWithAcceptTypeWithFeeHeaders () throws IOException {
141+ String gatewayAccountId = "123" ;
142+
143+ aTransactionFixture ()
144+ .withGatewayAccountId (gatewayAccountId )
145+ .withTransactionType ("PAYMENT" )
146+ .withFee (100 )
147+ .withNetAmount (1100 )
148+ .insert (rule .getJdbi ());
149+
150+ InputStream csvResponseStream = given ().port (port )
151+ .accept ("text/csv" )
152+ .get ("/v1/transaction/?" +
153+ "account_id=" + gatewayAccountId +
154+ "&fee_headers=true" +
155+ "&page=1" +
156+ "&display_size=5"
157+ )
158+ .then ()
159+ .statusCode (Response .Status .OK .getStatusCode ())
160+ .contentType ("text/csv" )
161+ .extract ().asInputStream ();
162+
163+ List <CSVRecord > csvRecords = CSVParser .parse (csvResponseStream , UTF_8 , RFC4180 .withFirstRecordAsHeader ()).getRecords ();
164+
165+ assertThat (csvRecords .size (), is (1 ));
166+
167+ CSVRecord paymentRecord = csvRecords .get (0 );
168+ assertThat (paymentRecord .size (), is (23 ));
169+ assertThat (paymentRecord .get ("Net" ), is ("11.00" ));
170+ assertThat (paymentRecord .get ("Fee" ), is ("1.00" ));
171+ }
172+
173+ @ Test
174+ public void shouldGetAllTransactionsAsCSVWithAcceptTypeWithMotoHeader () throws IOException {
175+ String gatewayAccountId = "123" ;
176+
177+ aTransactionFixture ()
178+ .withGatewayAccountId (gatewayAccountId )
179+ .withTransactionType ("PAYMENT" )
180+ .withMoto (true )
181+ .insert (rule .getJdbi ());
182+
183+ InputStream csvResponseStream = given ().port (port )
184+ .accept ("text/csv" )
185+ .get ("/v1/transaction/?" +
186+ "account_id=" + gatewayAccountId +
187+ "&moto_header=true" +
188+ "&page=1" +
189+ "&display_size=5"
190+ )
191+ .then ()
192+ .statusCode (Response .Status .OK .getStatusCode ())
193+ .contentType ("text/csv" )
194+ .extract ().asInputStream ();
195+
196+ List <CSVRecord > csvRecords = CSVParser .parse (csvResponseStream , UTF_8 , RFC4180 .withFirstRecordAsHeader ()).getRecords ();
197+
198+ assertThat (csvRecords .size (), is (1 ));
199+
200+ CSVRecord paymentRecord = csvRecords .get (0 );
201+ assertThat (paymentRecord .size (), is (22 ));
202+ assertThat (paymentRecord .get ("MOTO" ), is ("true" ));
203+ }
204+
205+ @ Test
206+ public void shouldReturnCSVHeadersInCorrectOrder () throws IOException {
207+ String targetGatewayAccountId = "123" ;
208+
209+ String metadataKey = "a-metadata-key" ;
210+ TransactionFixture transactionFixture = aTransactionFixture ()
211+ .withTransactionType ("PAYMENT" )
212+ .withGatewayAccountId (targetGatewayAccountId )
213+ .withExternalMetadata (ImmutableMap .of (metadataKey , "value1" ))
214+ .withFee (100 )
215+ .withNetAmount (1100 )
216+ .withDefaultTransactionDetails ()
217+ .insert (rule .getJdbi ());
218+
219+ metadataKeyDao .insertIfNotExist (metadataKey );
220+ transactionMetadataDao .insertIfNotExist (transactionFixture .getId (), metadataKey );
221+
222+ InputStream csvResponseStream = given ().port (port )
223+ .accept ("text/csv" )
224+ .get ("/v1/transaction?" +
225+ "account_id=" + targetGatewayAccountId +
226+ "&fee_headers=true" +
227+ "&moto_header=true" +
228+ "&page=1" +
229+ "&display_size=5"
230+ )
231+ .then ()
232+ .statusCode (Response .Status .OK .getStatusCode ())
233+ .contentType ("text/csv" )
234+ .extract ().asInputStream ();
235+
236+ List <CSVRecord > csvRecords = CSVParser .parse (csvResponseStream , UTF_8 , DEFAULT ).getRecords ();
237+
238+ CSVRecord header = csvRecords .get (0 );
239+ assertThat (header .size (), is (25 ));
240+ assertThat (header .get (0 ), is ("Reference" ));
241+ assertThat (header .get (1 ), is ("Description" ));
242+ assertThat (header .get (2 ), is ("Email" ));
243+ assertThat (header .get (3 ), is ("Amount" ));
244+ assertThat (header .get (4 ), is ("Card Brand" ));
245+ assertThat (header .get (5 ), is ("Cardholder Name" ));
246+ assertThat (header .get (6 ), is ("Card Expiry Date" ));
247+ assertThat (header .get (7 ), is ("Card Number" ));
248+ assertThat (header .get (8 ), is ("State" ));
249+ assertThat (header .get (9 ), is ("Finished" ));
250+ assertThat (header .get (10 ), is ("Error Code" ));
251+ assertThat (header .get (11 ), is ("Error Message" ));
252+ assertThat (header .get (12 ), is ("Provider ID" ));
253+ assertThat (header .get (13 ), is ("GOV.UK Payment ID" ));
254+ assertThat (header .get (14 ), is ("Issued By" ));
255+ assertThat (header .get (15 ), is ("Date Created" ));
256+ assertThat (header .get (16 ), is ("Time Created" ));
257+ assertThat (header .get (17 ), is ("Corporate Card Surcharge" ));
258+ assertThat (header .get (18 ), is ("Total Amount" ));
259+ assertThat (header .get (19 ), is ("Wallet Type" ));
260+ assertThat (header .get (20 ), is ("Fee" ));
261+ assertThat (header .get (21 ), is ("Net" ));
262+ assertThat (header .get (22 ), is ("Card Type" ));
263+ assertThat (header .get (23 ), is ("MOTO" ));
264+ assertThat (header .get (24 ), is ("a-metadata-key (metadata)" ));
265+ }
266+
267+ private void assertPaymentTransactionDetails (CSVRecord csvRecord , TransactionFixture transactionFixture ) {
268+ assertThat (csvRecord .get ("Reference" ), is (transactionFixture .getReference ()));
269+ assertThat (csvRecord .get ("Description" ), is (transactionFixture .getDescription ()));
270+ assertThat (csvRecord .get ("Email" ), is ("someone@example.org" ));
271+ assertThat (csvRecord .get ("Card Brand" ), is ("Diners Club" ));
272+ assertThat (csvRecord .get ("Cardholder Name" ), is ("J Doe" ));
273+ assertThat (csvRecord .get ("Card Expiry Date" ), is ("10/21" ));
274+ assertThat (csvRecord .get ("Card Number" ), is ("1234" ));
275+ assertThat (csvRecord .get ("Provider ID" ), is ("gateway-transaction-id" ));
276+ assertThat (csvRecord .get ("GOV.UK Payment ID" ), is (transactionFixture .getExternalId ()));
277+ assertThat (csvRecord .get ("Card Type" ), is ("credit" ));
278+ }
279+ }
0 commit comments