11package storefileblob ;
22
33import org .junit .Test ;
4- import org .junit .jupiter .api .MethodOrderer ;
5- import org .junit .jupiter .api .Order ;
6- import org .junit .jupiter .api .TestMethodOrder ;
7-
4+ import org .junit .jupiter .api .BeforeAll ;
5+ import org .junit .jupiter .params .ParameterizedTest ;
6+ import org .junit .jupiter .params .provider .CsvSource ;
87import java .io .IOException ;
98import java .sql .SQLException ;
10-
9+ import static org . junit . jupiter . api . Assertions . assertArrayEquals ;
1110import static org .junit .jupiter .api .Assertions .assertTrue ;
1211
13- @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
1412public class JdbcConnectionUnitTest {
1513
14+ private static final String TEST_FILE_PATH = "warehouse.png" ;
15+ private static final String RETRIEVED_FILE_PATH = "retrieve-warehouse.png" ;
16+ private static JdbcConnection jdbcConnection ;
1617 String selectBlob = """
1718 SELECT picture FROM warehouses WHERE id=?
1819 """ ;
1920
21+ @ BeforeAll
22+ public static void setUp () throws SQLException {
23+ jdbcConnection = new JdbcConnection ();
24+ jdbcConnection .createSchema ();
25+ }
26+
2027 @ Test
21- @ Order (1 )
2228 public void givenDatabaseSchema_whenJdbcConnectionIsEstablished_thenCreateSchema () throws SQLException {
23- JdbcConnection jdbcConnection = new JdbcConnection ();
2429 assertTrue (jdbcConnection .createSchema ());
2530 }
2631
27- @ Test
28- @ Order (3 )
29- public void givenBlobFile_whenInsertingTheBlobFileIntoTheWarehouseTable_thenSuccessful () throws SQLException , IOException {
30- JdbcConnection jdbcConnection = new JdbcConnection ();
31- jdbcConnection .createSchema ();
32- assertTrue (jdbcConnection .insertFile (4 , "Liu" , 3000 , "warehouse.png" ));
32+ @ ParameterizedTest
33+ @ CsvSource ({ "1, 'Liu', 3000" , "2, 'Walmart', 5000" })
34+ public void givenBlobFile_whenInsertingTheBlobFileAsByteArray_thenSuccessful (int id , String name , int capacity ) throws SQLException , IOException {
35+ boolean result = jdbcConnection .insertFile (id , name , capacity , TEST_FILE_PATH );
36+ assertTrue (result );
3337 }
3438
35- @ Test
36- @ Order (2 )
37- public void givenBlobFile_whenInsertingTheFileIntoTheDatabaseAsStream_thenSuccessful () throws SQLException , IOException {
38- JdbcConnection jdbcConnection = new JdbcConnection ();
39- jdbcConnection .createSchema ();
40- assertTrue (jdbcConnection .insertFileAsStream (6 , "Liu" , 3000 , "warehouse.png" ));
39+ @ ParameterizedTest
40+ @ CsvSource ({ "7, 'Dylan', 8000" , "8, 'Jumia', 1000" })
41+ public void givenBlobFile_whenInsertingTheFileIntoTheDatabaseInChunks_thenSuccessful (int id , String name , int capacity ) throws SQLException , IOException {
42+ boolean result = jdbcConnection .insertFileAsStream (id , name , capacity , TEST_FILE_PATH );
43+ assertTrue (result );
4144 }
4245
4346 @ Test
44- @ Order (4 )
4547 public void givenAnEntityWithBlob_whenReadingBlobAndConvertingItToFile_thenSuccessful () throws SQLException , IOException {
46- JdbcConnection jdbcConnection = new JdbcConnection ();
47- assertTrue (JdbcConnection .writeBlobToFile (selectBlob , 1 , 6 , "retrieve-warehouse.png" ));
48+ boolean result = JdbcConnection .writeBlobToFile (selectBlob , 1 , 2 , RETRIEVED_FILE_PATH );
49+ byte [] expectedBytes = JdbcConnection .convertFileToByteArray (TEST_FILE_PATH );
50+ byte [] retrievedBytes = JdbcConnection .convertFileToByteArray (RETRIEVED_FILE_PATH );
51+ assertArrayEquals (expectedBytes , retrievedBytes );
4852 }
4953
5054}
0 commit comments