1+ package org .databunker ;
2+
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import org .apache .http .HttpEntity ;
5+ import org .apache .http .client .methods .CloseableHttpResponse ;
6+ import org .apache .http .client .methods .HttpGet ;
7+ import org .apache .http .impl .client .CloseableHttpClient ;
8+ import org .apache .http .impl .client .HttpClients ;
9+ import org .apache .http .util .EntityUtils ;
10+ import org .junit .After ;
11+ import org .junit .Before ;
12+ import org .junit .BeforeClass ;
13+ import org .junit .Test ;
14+
15+ import java .io .IOException ;
16+ import java .util .HashMap ;
17+ import java .util .List ;
18+ import java .util .Map ;
19+
20+ import static org .junit .Assert .*;
21+
22+ public class DatabunkerproApiPciTest {
23+ private DatabunkerproApi api ;
24+ private static final String API_URL = "https://pro.databunker.org" ;
25+ private static String tenantName ;
26+ private static String apiToken ;
27+ private static boolean serverAvailable = false ;
28+
29+ @ BeforeClass
30+ public static void setUpBeforeClass () throws IOException {
31+ // Fetch tenant credentials from DatabunkerPro test environment
32+ try (CloseableHttpClient httpClient = HttpClients .createDefault ()) {
33+ HttpGet request = new HttpGet ("https://databunker.org/api/newtenant.php" );
34+ try (CloseableHttpResponse response = httpClient .execute (request )) {
35+ HttpEntity entity = response .getEntity ();
36+ String responseString = EntityUtils .toString (entity );
37+ ObjectMapper mapper = new ObjectMapper ();
38+ Map <String , Object > data = mapper .readValue (responseString , Map .class );
39+
40+ if (data != null && "ok" .equals (data .get ("status" ))) {
41+ tenantName = (String ) data .get ("tenantname" );
42+ apiToken = (String ) data .get ("xtoken" );
43+
44+ // Test connection with new credentials
45+ try (DatabunkerproApi testApi = new DatabunkerproApi (API_URL , apiToken , tenantName )) {
46+ Map <String , Object > result = testApi .getSystemStats (null );
47+ serverAvailable = result != null && "ok" .equals (result .get ("status" ));
48+ if (serverAvailable ) {
49+ System .out .println ("\n Successfully connected to DatabunkerPro server" );
50+ System .out .println ("Tenant: " + tenantName );
51+ System .out .println ("API URL: " + API_URL );
52+ }
53+ }
54+ }
55+ }
56+ } catch (Exception e ) {
57+ serverAvailable = false ;
58+ System .out .println ("\n Failed to connect to DatabunkerPro server: " + e .getMessage ());
59+ }
60+ }
61+
62+ @ Before
63+ public void setUp () throws IOException {
64+ if (!serverAvailable ) {
65+ throw new RuntimeException ("DatabunkerPro server is not available or credentials could not be obtained" );
66+ }
67+ api = new DatabunkerproApi (API_URL , apiToken , tenantName );
68+ }
69+
70+ @ After
71+ public void tearDown () throws IOException {
72+ if (api != null ) {
73+ api .close ();
74+ }
75+ }
76+
77+ @ Test
78+ public void testCreditCardTokenization () throws IOException {
79+ System .out .println ("\n Testing credit card tokenization..." );
80+
81+ // Step 1: Tokenize a credit card number
82+ String creditCardNumber = "5467047429390590" ;
83+ Map <String , Object > options = new HashMap <>();
84+ options .put ("slidingtime" , "1d" );
85+ options .put ("finaltime" , "12m" );
86+ options .put ("unique" , true );
87+
88+ Map <String , Object > tokenResult = api .createToken ("creditcard" , creditCardNumber , options , null );
89+ assertNotNull (tokenResult );
90+ assertEquals ("ok" , tokenResult .get ("status" ));
91+ assertNotNull (tokenResult .get ("tokenuuid" ));
92+ assertNotNull (tokenResult .get ("tokenbase" ));
93+ System .out .println ("Created token for credit card: " + creditCardNumber );
94+
95+ // Step 2: Retrieve tokenized data using both token types
96+ Map <String , Object > tokenDataByUUID = api .getToken ((String ) tokenResult .get ("tokenuuid" ), null );
97+ assertNotNull (tokenDataByUUID );
98+ assertEquals ("ok" , tokenDataByUUID .get ("status" ));
99+ assertEquals (creditCardNumber , tokenDataByUUID .get ("record" ));
100+
101+ Map <String , Object > tokenDataByBase = api .getToken ((String ) tokenResult .get ("tokenbase" ), null );
102+ assertNotNull (tokenDataByBase );
103+ assertEquals ("ok" , tokenDataByBase .get ("status" ));
104+ assertEquals (creditCardNumber , tokenDataByBase .get ("record" ));
105+ System .out .println ("Successfully retrieved original credit card number using both token types" );
106+
107+ // Step 3: Delete token and verify deletion
108+ Map <String , Object > deleteResult = api .deleteToken ((String ) tokenResult .get ("tokenuuid" ), null );
109+ assertNotNull (deleteResult );
110+ assertEquals ("ok" , deleteResult .get ("status" ));
111+
112+ // Verify both token types are deleted
113+ try {
114+ Map <String , Object > response = api .getToken ((String ) tokenResult .get ("tokenuuid" ), null );
115+ if (response != null && "error" .equals (response .get ("status" ))) {
116+ // Expected error response
117+ assertNotNull (response .get ("message" ));
118+ System .out .println ("Token deletion verified (UUID): " + response .get ("message" ));
119+ } else {
120+ fail ("Token should be deleted" );
121+ }
122+ } catch (Exception e ) {
123+ // Expected exception
124+ System .out .println ("Token deletion verified (UUID): " + e .getMessage ());
125+ }
126+
127+ try {
128+ Map <String , Object > response = api .getToken ((String ) tokenResult .get ("tokenbase" ), null );
129+ if (response != null && "error" .equals (response .get ("status" ))) {
130+ // Expected error response
131+ assertNotNull (response .get ("message" ));
132+ System .out .println ("Token deletion verified (base): " + response .get ("message" ));
133+ } else {
134+ fail ("Token should be deleted" );
135+ }
136+ } catch (Exception e ) {
137+ // Expected exception
138+ System .out .println ("Token deletion verified (base): " + e .getMessage ());
139+ }
140+ System .out .println ("Successfully deleted token and verified deletion" );
141+ }
142+
143+ @ Test
144+ public void testBulkCreditCardTokenization () throws IOException {
145+ System .out .println ("\n Testing bulk credit card tokenization..." );
146+
147+ // Step 1: Create multiple tokens in bulk
148+ String [] creditCards = {
149+ "5467047429390590" ,
150+ "4532015112830366" ,
151+ "4716182333661234"
152+ };
153+
154+ Object [] records = new Object [creditCards .length ];
155+ for (int i = 0 ; i < creditCards .length ; i ++) {
156+ Map <String , Object > record = new HashMap <>();
157+ record .put ("tokentype" , "creditcard" );
158+ record .put ("record" , creditCards [i ]);
159+ records [i ] = record ;
160+ }
161+
162+ Map <String , Object > options = new HashMap <>();
163+ options .put ("slidingtime" , "30d" );
164+ options .put ("finaltime" , "12m" );
165+ options .put ("unique" , true );
166+
167+ Map <String , Object > bulkTokens = api .createTokensBulk (records , options , null );
168+ System .out .println ("Bulk tokens: " + bulkTokens );
169+ assertNotNull (bulkTokens );
170+ assertEquals ("ok" , bulkTokens .get ("status" ));
171+ assertNotNull (bulkTokens .get ("created" ));
172+ assertEquals (3 , bulkTokens .get ("num" ));
173+
174+ // Verify summary
175+ Map <String , Object > summary = (Map <String , Object >) bulkTokens .get ("summary" );
176+ assertNotNull (summary );
177+ assertEquals (3 , summary .get ("created" ));
178+ assertEquals (0 , summary .get ("duplicates" ));
179+ assertEquals (0 , summary .get ("errors" ));
180+ assertEquals (3 , summary .get ("total" ));
181+
182+ System .out .println ("Created bulk tokens for " + creditCards .length + " credit cards" );
183+
184+ // Step 2: Retrieve multiple tokenized records in bulk
185+ String [] tokenList = new String [creditCards .length ];
186+ List <Map <String , Object >> createdTokens = (List <Map <String , Object >>) bulkTokens .get ("created" );
187+ for (int i = 0 ; i < creditCards .length ; i ++) {
188+ tokenList [i ] = (String ) createdTokens .get (i ).get ("tokenbase" );
189+ }
190+
191+ Map <String , Object > bulkData = api .listTokensBulk (tokenList , null );
192+ System .out .println ("Bulk data: " + bulkData );
193+ assertNotNull (bulkData );
194+ assertEquals ("ok" , bulkData .get ("status" ));
195+ assertNotNull (bulkData .get ("rows" ));
196+ assertEquals (3 , bulkData .get ("total" ));
197+
198+ // Verify rows
199+ List <Map <String , Object >> rows = (List <Map <String , Object >>) bulkData .get ("rows" );
200+ assertEquals (creditCards .length , rows .size ());
201+
202+ // Verify each row contains expected fields
203+ for (Map <String , Object > row : rows ) {
204+ assertNotNull (row .get ("tokenuuid" ));
205+ assertNotNull (row .get ("tokenbase" ));
206+ assertNotNull (row .get ("record" ));
207+ assertEquals ("creditcard" , row .get ("tokentype" ));
208+ }
209+
210+ System .out .println ("Successfully retrieved all tokenized records in bulk" );
211+
212+ // Step 3: Delete multiple tokens in bulk
213+ Map <String , Object > deleteBulkResult = api .deleteTokensBulk (tokenList , null );
214+ assertNotNull (deleteBulkResult );
215+ assertEquals ("ok" , deleteBulkResult .get ("status" ));
216+
217+ // Verify all tokens are deleted
218+ Map <String , Object > remainingTokens = api .listTokensBulk (tokenList , null );
219+ assertNotNull (remainingTokens );
220+ assertEquals ("ok" , remainingTokens .get ("status" ));
221+ assertEquals (0 , ((List <?>) remainingTokens .get ("rows" )).size ());
222+ System .out .println ("Successfully deleted all tokens in bulk" );
223+
224+ // Additional error handling tests
225+ try {
226+ // Test with invalid token
227+ Map <String , Object > invalidTokenResponse = api .getToken ("invalid-token" , null );
228+ if (invalidTokenResponse != null ) {
229+ assertEquals ("error" , invalidTokenResponse .get ("status" ));
230+ assertNotNull (invalidTokenResponse .get ("message" ));
231+ System .out .println ("Invalid token test passed: " + invalidTokenResponse .get ("message" ));
232+ }
233+ } catch (Exception e ) {
234+ System .out .println ("Invalid token test passed with exception: " + e .getMessage ());
235+ }
236+
237+ try {
238+ // Test with empty records array
239+ Map <String , Object > emptyRecordsResponse = api .createTokensBulk (new Object [0 ], options , null );
240+ if (emptyRecordsResponse != null ) {
241+ assertEquals ("error" , emptyRecordsResponse .get ("status" ));
242+ assertNotNull (emptyRecordsResponse .get ("message" ));
243+ System .out .println ("Empty records test passed: " + emptyRecordsResponse .get ("message" ));
244+ }
245+ } catch (Exception e ) {
246+ System .out .println ("Empty records test passed with exception: " + e .getMessage ());
247+ }
248+ }
249+ }
0 commit comments