1717package io .cdap .plugin .db .batch .sink ;
1818
1919import com .google .common .collect .ImmutableList ;
20+ import com .mockrunner .mock .jdbc .MockResultSet ;
2021import com .mockrunner .mock .jdbc .MockResultSetMetaData ;
2122import io .cdap .plugin .db .ColumnType ;
2223import org .junit .Assert ;
2324import org .junit .Test ;
2425
2526import java .sql .SQLException ;
2627import java .util .ArrayList ;
28+ import java .util .HashSet ;
2729import java .util .List ;
30+ import java .util .Set ;
2831
2932/**
3033 * Test class for abstract sink.
3134 */
3235public class AbstractDBSinkTest {
3336
3437 @ Test
35- public void testGetMatchedColumnTypeList () throws SQLException {
38+ public void testGetMatchedColumnTypeList () throws Exception {
3639 List <String > columns = ImmutableList .of (
3740 "ID" ,
3841 "NAME" ,
@@ -45,20 +48,25 @@ public void testGetMatchedColumnTypeList() throws SQLException {
4548 resultSetMetaData .setColumnCount (columns .size ());
4649
4750 for (int i = 0 ; i < columns .size (); i ++) {
48- String name = columns .get (i );
4951 resultSetMetaData .setColumnName (i + 1 , columns .get (i ));
5052 resultSetMetaData .setColumnTypeName (i + 1 , "STRING" );
5153 resultSetMetaData .setColumnType (i + 1 , i );
52- expectedColumns .add (new ColumnType (name , "STRING" , i ));
54+ expectedColumns .add (new ColumnType (columns . get ( i ) , "STRING" , i ));
5355 }
5456
55- List <ColumnType > result = AbstractDBSink .getMatchedColumnTypeList (resultSetMetaData , columns );
57+ MockResultSet resultSet = new MockResultSet ("data" );
58+ Set <String > columnNamesSet = new HashSet <String >();
59+ columnNamesSet .addAll (columns );
60+ resultSet .addColumns (columnNamesSet );
61+ resultSet .setResultSetMetaData (resultSetMetaData );
62+
63+ List <ColumnType > result = AbstractDBSink .getMatchedColumnTypeList (resultSet , columns );
5664
5765 Assert .assertEquals (expectedColumns , result );
5866 }
5967
6068 @ Test
61- public void testGetMismatchColumnTypeList () throws SQLException {
69+ public void testGetMismatchColumnTypeList () throws Exception {
6270 List <String > wrongColumns = ImmutableList .of (
6371 "MY_ID" ,
6472 "NAME" ,
@@ -80,12 +88,119 @@ public void testGetMismatchColumnTypeList() throws SQLException {
8088 resultSetMetaData .setColumnType (i + 1 , i );
8189 }
8290
91+ MockResultSet resultSet = new MockResultSet ("data" );
92+ Set <String > columnNamesSet = new HashSet <String >();
93+ columnNamesSet .addAll (columns );
94+ resultSet .addColumns (columnNamesSet );
95+ resultSet .setResultSetMetaData (resultSetMetaData );
96+
8397 try {
84- AbstractDBSink .getMatchedColumnTypeList (resultSetMetaData , wrongColumns );
98+ AbstractDBSink .getMatchedColumnTypeList (resultSet , wrongColumns );
8599 Assert .fail (String .format ("Expected to throw %s" , IllegalArgumentException .class .getName ()));
86100 } catch (IllegalArgumentException e ) {
87101 String errorMessage = "Missing column 'MY_ID' in SQL table" ;
88102 Assert .assertEquals (errorMessage , e .getMessage ());
89103 }
90104 }
105+
106+ @ Test
107+ public void testDifferentOrderOfFieldsInResultSet () throws Exception {
108+ List <String > diffOrdCol = ImmutableList .of (
109+ "Name" ,
110+ "SCORE" ,
111+ "ID"
112+ );
113+
114+ List <String > columns = ImmutableList .of (
115+ "ID" ,
116+ "NAME" ,
117+ "SCORE"
118+ );
119+
120+ List <String > typeName = ImmutableList .of (
121+ "INT" ,
122+ "STRING" ,
123+ "DOUBLE"
124+ );
125+
126+ List <Integer > typeValue = ImmutableList .of (
127+ 1 ,
128+ 2 ,
129+ 3
130+ );
131+
132+ List <ColumnType > expectedColumns = new ArrayList <>();
133+ MockResultSetMetaData resultSetMetaData = new MockResultSetMetaData ();
134+ resultSetMetaData .setColumnCount (columns .size ());
135+
136+ for (int i = 0 ; i < columns .size (); i ++) {
137+ resultSetMetaData .setColumnName (i + 1 , columns .get (i ));
138+ resultSetMetaData .setColumnTypeName (i + 1 , typeName .get (i ));
139+ resultSetMetaData .setColumnType (i + 1 , typeValue .get (i ));
140+ expectedColumns .add (new ColumnType (columns .get (i ), typeName .get (i ), typeValue .get (i )));
141+ }
142+
143+ MockResultSet resultSet = new MockResultSet ("data" );
144+ Set <String > columnNamesSet = new HashSet <String >();
145+ columnNamesSet .addAll (columns );
146+ resultSet .addColumns (columnNamesSet );
147+ resultSet .setResultSetMetaData (resultSetMetaData );
148+
149+ List <ColumnType > actualColumns = AbstractDBSink .getMatchedColumnTypeList (resultSet , diffOrdCol );
150+
151+ // Assert that all expected fields are present in the actual fields
152+ for (ColumnType exColType : expectedColumns ) {
153+ Assert .assertTrue (actualColumns .contains (exColType ));
154+ }
155+ }
156+
157+ @ Test
158+ public void testSubsetColumnsInResultSet () throws Exception {
159+ List <String > subsetCol = ImmutableList .of (
160+ "SCORE" ,
161+ "ID"
162+ );
163+
164+ List <String > columns = ImmutableList .of (
165+ "ID" ,
166+ "NAME" ,
167+ "SCORE"
168+ );
169+
170+ List <String > typeName = ImmutableList .of (
171+ "INT" ,
172+ "STRING" ,
173+ "DOUBLE"
174+ );
175+
176+ List <Integer > typeValue = ImmutableList .of (
177+ 1 ,
178+ 2 ,
179+ 3
180+ );
181+
182+ List <ColumnType > expectedColumns = new ArrayList <>();
183+ MockResultSetMetaData resultSetMetaData = new MockResultSetMetaData ();
184+ resultSetMetaData .setColumnCount (columns .size ());
185+
186+ for (int i = 0 ; i < columns .size (); i ++) {
187+ resultSetMetaData .setColumnName (i + 1 , columns .get (i ));
188+ resultSetMetaData .setColumnTypeName (i + 1 , typeName .get (i ));
189+ resultSetMetaData .setColumnType (i + 1 , typeValue .get (i ));
190+ expectedColumns .add (new ColumnType (columns .get (i ), typeName .get (i ), typeValue .get (i )));
191+ }
192+
193+ MockResultSet resultSet = new MockResultSet ("data" );
194+ Set <String > columnNamesSet = new HashSet <String >();
195+ columnNamesSet .addAll (columns );
196+ resultSet .addColumns (columnNamesSet );
197+ resultSet .setResultSetMetaData (resultSetMetaData );
198+
199+ List <ColumnType > actualColumns = AbstractDBSink .getMatchedColumnTypeList (resultSet , subsetCol );
200+
201+ // Assert that all actual fields are present in the expected fields
202+ for (ColumnType acColType : actualColumns ) {
203+ Assert .assertTrue (expectedColumns .contains (acColType ));
204+ }
205+ }
91206}
0 commit comments