1616
1717package io .cdap .plugin .gcp .bigquery .action ;
1818
19+ import com .google .cloud .bigquery .Field ;
20+ import com .google .cloud .bigquery .FieldList ;
21+ import com .google .cloud .bigquery .LegacySQLTypeName ;
22+ import com .google .common .collect .ImmutableList ;
1923import io .cdap .cdap .etl .api .validation .CauseAttributes ;
2024import io .cdap .cdap .etl .api .validation .ValidationException ;
2125import io .cdap .cdap .etl .api .validation .ValidationFailure ;
2226import io .cdap .cdap .etl .mock .validation .MockFailureCollector ;
2327import org .junit .Assert ;
28+ import org .junit .Before ;
2429import org .junit .Test ;
2530
31+ import java .util .List ;
32+
2633public class BigQueryArgumentSetterConfigTest {
2734
2835 private static final String VALID_DATASET = "dataset" ;
2936 private static final String VALID_DATASET_PROJECT = "datasetProject" ;
3037 private static final String VALID_TABLE = "table" ;
3138 private static final String VALID_ARGUMENT_SELECTION_CONDITIONS = "feed=10;id=0" ;
3239 private static final String VALID_ARGUMENT_COLUMN = "name" ;
40+ private MockFailureCollector mockFailureCollector ;
41+
42+ @ Before
43+ public void setUp () {
44+ mockFailureCollector = new MockFailureCollector ();
45+ }
3346
3447 @ Test
3548 public void testValidateMissingArgumentSelectionConditions () {
@@ -43,6 +56,47 @@ public void testValidateMissingArgumentColumns() {
4356 validateConfigValidationFail (config , BigQueryArgumentSetterConfig .NAME_ARGUMENTS_COLUMNS );
4457 }
4558
59+ @ Test
60+ public void testCheckIfArgumentsColumnsListExistsInSourceMatchOne () {
61+ List <String > argumentsColumnsList = ImmutableList .of ("name" );
62+ FieldList fields = FieldList .of (Field .newBuilder ("name" , LegacySQLTypeName .STRING ).build ());
63+ BigQueryArgumentSetterConfig .checkIfArgumentsColumnsListExistsInSource (argumentsColumnsList , fields ,
64+ mockFailureCollector );
65+ Assert .assertEquals (0 , mockFailureCollector .getValidationFailures ().size ());
66+ }
67+
68+ @ Test
69+ public void testCheckIfArgumentsColumnsListExistsInSourceMatchSome () {
70+ List <String > argumentsColumnsList = ImmutableList .of ("name" );
71+ FieldList fields = FieldList .of (Field .newBuilder ("name" , LegacySQLTypeName .STRING ).build (),
72+ Field .newBuilder ("age" , LegacySQLTypeName .INTEGER ).build ());
73+ BigQueryArgumentSetterConfig .checkIfArgumentsColumnsListExistsInSource (argumentsColumnsList , fields ,
74+ mockFailureCollector );
75+ Assert .assertEquals (0 , mockFailureCollector .getValidationFailures ().size ());
76+ }
77+
78+ @ Test
79+ public void testCheckIfArgumentsColumnsListExistsInSourceFailedMatchOne () {
80+ List <String > argumentsColumnsList = ImmutableList .of ("name" );
81+ FieldList fields = FieldList .of (Field .newBuilder ("age" , LegacySQLTypeName .INTEGER ).build ());
82+ BigQueryArgumentSetterConfig .checkIfArgumentsColumnsListExistsInSource (argumentsColumnsList , fields ,
83+ mockFailureCollector );
84+ Assert .assertEquals (1 , mockFailureCollector .getValidationFailures ().size ());
85+ Assert .assertEquals ("Column: \" name\" does not exist in table. Argument columns must exist in table." ,
86+ mockFailureCollector .getValidationFailures ().get (0 ).getMessage ());
87+ }
88+
89+ @ Test
90+ public void testCheckIfArgumentsColumnsListExistsInSourceFailedMatchSome () {
91+ List <String > argumentsColumnsList = ImmutableList .of ("name" , "city" );
92+ FieldList fields = FieldList .of (Field .newBuilder ("age" , LegacySQLTypeName .INTEGER ).build ());
93+ BigQueryArgumentSetterConfig .checkIfArgumentsColumnsListExistsInSource (argumentsColumnsList , fields ,
94+ mockFailureCollector );
95+ Assert .assertEquals (1 , mockFailureCollector .getValidationFailures ().size ());
96+ Assert .assertEquals ("Columns: \" name ,city\" do not exist in table. Argument columns must exist in table." ,
97+ mockFailureCollector .getValidationFailures ().get (0 ).getMessage ());
98+ }
99+
46100 private static BigQueryArgumentSetterConfig .Builder getBuilder () {
47101 return BigQueryArgumentSetterConfig .builder ()
48102 .setDatasetProject (VALID_DATASET_PROJECT )
0 commit comments