2020package com .amazonaws .athena .connectors .vertica ;
2121
2222import com .amazonaws .athena .connector .lambda .domain .TableName ;
23- import org .apache .arrow .vector .types .pojo .Field ;
2423import org .apache .arrow .vector .types .pojo .Schema ;
25- import org .junit .Assert ;
2624import org .junit .Before ;
2725import org .junit .Test ;
26+ import org .junit .runner .RunWith ;
27+ import org .mockito .Mock ;
2828import org .mockito .Mockito ;
29- import org .slf4j .Logger ;
30- import org .slf4j .LoggerFactory ;
31-
32- import java .sql .*;
29+ import org .mockito .junit .MockitoJUnitRunner ;
30+
31+ import java .lang .reflect .Method ;
32+ import java .security .cert .Certificate ;
33+ import java .security .cert .X509Certificate ;
34+ import java .sql .Types ;
35+ import java .sql .ResultSet ;
36+ import java .sql .Connection ;
37+ import java .sql .DatabaseMetaData ;
38+ import java .sql .SQLException ;
39+ import java .util .Base64 ;
3340import java .util .concurrent .atomic .AtomicInteger ;
3441
35- import static org .mockito .ArgumentMatchers .nullable ;
42+ import static org .junit .Assert .assertEquals ;
43+ import static org .junit .Assert .assertTrue ;
44+ import static org .junit .Assert .fail ;
45+ import static org .mockito .Mockito .when ;
3646
47+ @ RunWith (MockitoJUnitRunner .class )
3748public class VerticaSchemaUtilsTest extends TestBase
3849{
39- private static final Logger logger = LoggerFactory .getLogger (VerticaSchemaUtils .class );
4050 private Connection connection ;
4151 private DatabaseMetaData databaseMetaData ;
4252 private TableName tableName ;
4353
54+ @ Mock
55+ private X509Certificate mockCertificate ;
4456
4557 @ Before
4658 public void setUp () throws SQLException
@@ -60,11 +72,27 @@ public void buildTableSchema() throws SQLException
6072 int [] types = {Types.INTEGER, Types.INTEGER};*/
6173
6274 String [] schema = {"TABLE_SCHEM" , "TABLE_NAME" , "COLUMN_NAME" , "TYPE_NAME" };
63- Object [][] values = {{"testSchema" , "testTable1" , "id" , "bigint" }, {"testSchema" , "testTable1" , "date" , "timestamp" },
64- {"testSchema" , "testTable1" , "orders" , "integer" }, {"testSchema" , "testTable1" , "price" , "float4" },
65- {"testSchema" , "testTable1" , "shop" , "varchar" }
66- };
67- int [] types = {Types .BIGINT , Types .TIMESTAMP , Types .INTEGER ,Types .FLOAT , Types .VARCHAR , Types .VARCHAR };
75+ Object [][] values = {
76+ {"testSchema" , "testTable1" , "bit_col" , "BIT" },
77+ {"testSchema" , "testTable1" , "tinyint_col" , "TINYINT" },
78+ {"testSchema" , "testTable1" , "smallint_col" , "SMALLINT" },
79+ {"testSchema" , "testTable1" , "integer_col" , "INTEGER" },
80+ {"testSchema" , "testTable1" , "bigint_col" , "BIGINT" },
81+ {"testSchema" , "testTable1" , "float4_col" , "FLOAT4" },
82+ {"testSchema" , "testTable1" , "float8_col" , "FLOAT8" },
83+ {"testSchema" , "testTable1" , "numeric_col" , "NUMERIC" },
84+ {"testSchema" , "testTable1" , "boolean_col" , "BOOLEAN" },
85+ {"testSchema" , "testTable1" , "varchar_col" , "VARCHAR" },
86+ {"testSchema" , "testTable1" , "timestamp_col" , "TIMESTAMP" },
87+ {"testSchema" , "testTable1" , "timestamptz_col" , "TIMESTAMPTZ" },
88+ {"testSchema" , "testTable1" , "datetime_col" , "DATETIME" },
89+ {"testSchema" , "testTable1" , "unknown_col" , "UNKNOWN" }
90+ };
91+ int [] types = {
92+ Types .BIT , Types .TINYINT , Types .SMALLINT , Types .INTEGER , Types .BIGINT ,
93+ Types .FLOAT , Types .DOUBLE , Types .NUMERIC , Types .BOOLEAN , Types .VARCHAR ,
94+ Types .TIMESTAMP , Types .TIMESTAMP , Types .DATE , Types .OTHER
95+ };
6896
6997 AtomicInteger rowNumber = new AtomicInteger (-1 );
7098 ResultSet resultSet = mockResultSet (schema , types , values , rowNumber );
@@ -75,12 +103,52 @@ public void buildTableSchema() throws SQLException
75103 VerticaSchemaUtils verticaSchemaUtils = new VerticaSchemaUtils ();
76104 Schema mockSchema = verticaSchemaUtils .buildTableSchema (this .connection , tableName );
77105
78- Field testDateField = mockSchema .findField ("date" );
79- Assert .assertEquals ("Utf8" , testDateField .getType ().toString ());
106+ assertEquals ("Bool" , mockSchema .findField ("bit_col" ).getType ().toString ());
107+ assertEquals ("Int(8, true)" , mockSchema .findField ("tinyint_col" ).getType ().toString ());
108+ assertEquals ("Int(16, true)" , mockSchema .findField ("smallint_col" ).getType ().toString ());
109+ assertEquals ("Int(64, true)" , mockSchema .findField ("integer_col" ).getType ().toString ());
110+ assertEquals ("Int(64, true)" , mockSchema .findField ("bigint_col" ).getType ().toString ());
111+ assertEquals ("FloatingPoint(SINGLE)" , mockSchema .findField ("float4_col" ).getType ().toString ());
112+ assertEquals ("FloatingPoint(DOUBLE)" , mockSchema .findField ("float8_col" ).getType ().toString ());
113+ assertEquals ("Decimal(10, 2, 128)" , mockSchema .findField ("numeric_col" ).getType ().toString ());
114+ assertEquals ("Utf8" , mockSchema .findField ("boolean_col" ).getType ().toString ());
115+ assertEquals ("Utf8" , mockSchema .findField ("varchar_col" ).getType ().toString ());
116+ assertEquals ("Utf8" , mockSchema .findField ("timestamp_col" ).getType ().toString ());
117+ assertEquals ("Utf8" , mockSchema .findField ("timestamptz_col" ).getType ().toString ());
118+ assertEquals ("Date(DAY)" , mockSchema .findField ("datetime_col" ).getType ().toString ());
119+ assertEquals ("Utf8" , mockSchema .findField ("unknown_col" ).getType ().toString ());
120+ }
80121
81- Field testPriceField = mockSchema .findField ("price" );
82- Assert .assertEquals ("FloatingPoint(SINGLE)" , testPriceField .getType ().toString ());
122+ @ Test
123+ public void buildTableSchemaSQLException () throws SQLException
124+ {
125+ when (databaseMetaData .getColumns (null , tableName .getSchemaName (), tableName .getTableName (), null ))
126+ .thenThrow (new SQLException ("Database error" ));
83127
128+ VerticaSchemaUtils verticaSchemaUtils = new VerticaSchemaUtils ();
129+ try {
130+ verticaSchemaUtils .buildTableSchema (connection , tableName );
131+ fail ("Expected RuntimeException" );
132+ } catch (RuntimeException e ) {
133+ assertTrue (e .getMessage ().contains ("Error in building the table schema" ));
134+ assertTrue (e .getCause () instanceof SQLException );
135+ }
136+ }
84137
138+ @ Test
139+ public void formatCrtFileContents () throws Exception
140+ {
141+ when (mockCertificate .getEncoded ()).thenReturn ("test-cert" .getBytes ());
142+
143+ Method formatMethod = VerticaSchemaUtils .class .getDeclaredMethod ("formatCrtFileContents" , Certificate .class );
144+ formatMethod .setAccessible (true );
145+ String result = (String ) formatMethod .invoke (null , mockCertificate );
146+
147+ String expectedEncoded = Base64 .getMimeEncoder (64 , System .lineSeparator ().getBytes ())
148+ .encodeToString ("test-cert" .getBytes ());
149+ String expected = "-----BEGIN CERTIFICATE-----" + System .lineSeparator () +
150+ expectedEncoded + System .lineSeparator () + "-----END CERTIFICATE-----" ;
151+ assertEquals (expected , result );
85152 }
86- }
153+
154+ }
0 commit comments