This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
TransUnitTest.java
473 lines (407 loc) · 12.5 KB
/
TransUnitTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2017 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.di.dataset;
import org.apache.commons.lang.StringUtils;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.dataset.util.DataSetConst;
import org.pentaho.di.dataset.util.FactoriesHierarchy;
import org.pentaho.metastore.persist.MetaStoreAttribute;
import org.pentaho.metastore.persist.MetaStoreElementType;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* This class describes a test-case where a transformation output is verified against golden data.
*
* @author matt
*/
@MetaStoreElementType(
name = "Kettle Transformation Unit Test",
description = "This describes a golden data unit test for a transformation with defined input data sets" )
public class TransUnitTest {
private String name;
@MetaStoreAttribute( key = "description" )
private String description;
@MetaStoreAttribute( key = "transformation_rep_object_id" )
protected String transObjectId; // rep: by reference (1st priority)
@MetaStoreAttribute( key = "transformation_rep_path" )
protected String transRepositoryPath; // rep: by name (2nd priority)
@MetaStoreAttribute( key = "transformation_filename" )
protected String transFilename; // file (3rd priority)
@MetaStoreAttribute( key = "input_data_sets" )
protected List<TransUnitTestSetLocation> inputDataSets;
@MetaStoreAttribute( key = "golden_data_sets" )
protected List<TransUnitTestSetLocation> goldenDataSets;
@MetaStoreAttribute( key = "trans_test_tweaks" )
protected List<TransUnitTestTweak> tweaks;
@MetaStoreAttribute( key = "test_type" )
protected TestType type;
@MetaStoreAttribute( key = "persist_filename" )
protected String filename;
@MetaStoreAttribute
protected String basePath;
@MetaStoreAttribute( key = "database_replacements" )
protected List<TransUnitTestDatabaseReplacement> databaseReplacements;
@MetaStoreAttribute
protected List<VariableValue> variableValues;
@MetaStoreAttribute
protected boolean autoOpening;
public TransUnitTest() {
inputDataSets = new ArrayList<TransUnitTestSetLocation>();
goldenDataSets = new ArrayList<TransUnitTestSetLocation>();
tweaks = new ArrayList<TransUnitTestTweak>();
type = TestType.NONE;
databaseReplacements = new ArrayList<TransUnitTestDatabaseReplacement>();
variableValues = new ArrayList<>();
basePath = null;
autoOpening = false;
}
public TransUnitTest( String name, String description,
String transObjectId, String transRepositoryPath, String transFilename,
List<TransUnitTestSetLocation> inputDataSets,
List<TransUnitTestSetLocation> goldenDataSets,
List<TransUnitTestTweak> tweaks,
TestType type,
String filename,
List<TransUnitTestDatabaseReplacement> databaseReplacements,
boolean autoOpening ) {
this();
this.name = name;
this.description = description;
this.transObjectId = transObjectId;
this.transRepositoryPath = transRepositoryPath;
this.transFilename = transFilename;
this.inputDataSets = inputDataSets;
this.goldenDataSets = goldenDataSets;
this.tweaks = tweaks;
this.type = type;
this.filename = filename;
this.databaseReplacements = databaseReplacements;
this.autoOpening = autoOpening;
}
@Override
public boolean equals( Object obj ) {
if ( obj == this ) {
return true;
}
if ( !( obj instanceof TransUnitTest ) ) {
return false;
}
return ( (TransUnitTest) obj ).name.equalsIgnoreCase( name );
}
@Override
public int hashCode() {
return name.hashCode();
}
public TransUnitTestSetLocation findGoldenLocation( String stepName ) {
for ( TransUnitTestSetLocation location : goldenDataSets ) {
if ( stepName.equalsIgnoreCase( location.getStepname() ) ) {
return location;
}
}
return null;
}
public TransUnitTestSetLocation findInputLocation( String stepName ) {
for ( TransUnitTestSetLocation location : inputDataSets ) {
if ( stepName.equalsIgnoreCase( location.getStepname() ) ) {
return location;
}
}
return null;
}
/**
* Retrieve the golden data set for the specified location
*
* @param log the logging channel to log to
* @param hierarchy The factories to load sets with
* @param location the location where we want to check against golden rows
* @return The golden data set
* @throws KettleException
*/
public DataSet getGoldenDataSet( LogChannelInterface log, FactoriesHierarchy hierarchy, TransUnitTestSetLocation location ) throws KettleException {
String stepName = location.getStepname();
String goldenDataSetName = location.getDataSetName();
try {
// Look in the golden data sets list for the mentioned step name
//
if ( goldenDataSetName == null ) {
throw new KettleException( "Unable to find golden data set for step '" + stepName + "'" );
}
DataSet goldenDataSet = hierarchy.getSetFactory().loadElement( goldenDataSetName );
if ( goldenDataSet == null ) {
throw new KettleException( "Unable to find golden data set '" + goldenDataSetName + "' for step '" + stepName + "'" );
}
return goldenDataSet;
} catch ( Exception e ) {
throw new KettleException( "Unable to retrieve sorted golden row data set '" + stepName + "'", e );
}
}
/**
* Find the first tweak for a certain step
*
* @param stepname the name of the step on which a tweak is put
* @return the first tweak for a certain step or null if nothing was found
*/
public TransUnitTestTweak findTweak( String stepname ) {
for ( TransUnitTestTweak tweak : tweaks ) {
if ( tweak.getStepName() != null && tweak.getStepName().equalsIgnoreCase( stepname ) ) {
return tweak;
}
}
return null;
}
/**
* Remove all input and golden data sets on the step with the provided name
*
* @param stepname the name of the step for which we need to clear out all input and golden data sets
*/
public void removeInputAndGoldenDataSets( String stepname ) {
for ( Iterator<TransUnitTestSetLocation> iterator = inputDataSets.iterator(); iterator.hasNext(); ) {
TransUnitTestSetLocation inputLocation = iterator.next();
if ( inputLocation.getStepname().equalsIgnoreCase( stepname ) ) {
iterator.remove();
}
}
for ( Iterator<TransUnitTestSetLocation> iterator = goldenDataSets.iterator(); iterator.hasNext(); ) {
TransUnitTestSetLocation goldenLocation = iterator.next();
if ( goldenLocation.getStepname().equalsIgnoreCase( stepname ) ) {
iterator.remove();
}
}
}
public String calculateCompleteFilename( VariableSpace space ) {
String baseFilePath = space.environmentSubstitute( basePath );
if ( StringUtils.isEmpty( baseFilePath ) ) {
// See if the base path environment variable is set
//
baseFilePath = space.getVariable( DataSetConst.VARIABLE_UNIT_TESTS_BASE_PATH );
}
if ( StringUtils.isEmpty( baseFilePath ) ) {
baseFilePath = "";
}
if ( StringUtils.isNotEmpty( baseFilePath ) ) {
if ( !baseFilePath.endsWith( File.separator ) ) {
baseFilePath += File.separator;
}
}
return baseFilePath + transFilename;
}
/**
* Gets name
*
* @return value of name
*/
public String getName() {
return name;
}
/**
* @param name The name to set
*/
public void setName( String name ) {
this.name = name;
}
/**
* Gets description
*
* @return value of description
*/
public String getDescription() {
return description;
}
/**
* @param description The description to set
*/
public void setDescription( String description ) {
this.description = description;
}
/**
* Gets transObjectId
*
* @return value of transObjectId
*/
public String getTransObjectId() {
return transObjectId;
}
/**
* @param transObjectId The transObjectId to set
*/
public void setTransObjectId( String transObjectId ) {
this.transObjectId = transObjectId;
}
/**
* Gets transRepositoryPath
*
* @return value of transRepositoryPath
*/
public String getTransRepositoryPath() {
return transRepositoryPath;
}
/**
* @param transRepositoryPath The transRepositoryPath to set
*/
public void setTransRepositoryPath( String transRepositoryPath ) {
this.transRepositoryPath = transRepositoryPath;
}
/**
* Gets transFilename
*
* @return value of transFilename
*/
public String getTransFilename() {
return transFilename;
}
/**
* @param transFilename The transFilename to set
*/
public void setTransFilename( String transFilename ) {
this.transFilename = transFilename;
}
/**
* Gets inputDataSets
*
* @return value of inputDataSets
*/
public List<TransUnitTestSetLocation> getInputDataSets() {
return inputDataSets;
}
/**
* @param inputDataSets The inputDataSets to set
*/
public void setInputDataSets( List<TransUnitTestSetLocation> inputDataSets ) {
this.inputDataSets = inputDataSets;
}
/**
* Gets goldenDataSets
*
* @return value of goldenDataSets
*/
public List<TransUnitTestSetLocation> getGoldenDataSets() {
return goldenDataSets;
}
/**
* @param goldenDataSets The goldenDataSets to set
*/
public void setGoldenDataSets( List<TransUnitTestSetLocation> goldenDataSets ) {
this.goldenDataSets = goldenDataSets;
}
/**
* Gets tweaks
*
* @return value of tweaks
*/
public List<TransUnitTestTweak> getTweaks() {
return tweaks;
}
/**
* @param tweaks The tweaks to set
*/
public void setTweaks( List<TransUnitTestTweak> tweaks ) {
this.tweaks = tweaks;
}
/**
* Gets type
*
* @return value of type
*/
public TestType getType() {
return type;
}
/**
* @param type The type to set
*/
public void setType( TestType type ) {
this.type = type;
}
/**
* Gets filename
*
* @return value of filename
*/
public String getFilename() {
return filename;
}
/**
* @param filename The filename to set
*/
public void setFilename( String filename ) {
this.filename = filename;
}
/**
* Gets basePath
*
* @return value of basePath
*/
public String getBasePath() {
return basePath;
}
/**
* @param basePath The basePath to set
*/
public void setBasePath( String basePath ) {
this.basePath = basePath;
}
/**
* Gets databaseReplacements
*
* @return value of databaseReplacements
*/
public List<TransUnitTestDatabaseReplacement> getDatabaseReplacements() {
return databaseReplacements;
}
/**
* @param databaseReplacements The databaseReplacements to set
*/
public void setDatabaseReplacements( List<TransUnitTestDatabaseReplacement> databaseReplacements ) {
this.databaseReplacements = databaseReplacements;
}
/**
* Gets variableValues
*
* @return value of variableValues
*/
public List<VariableValue> getVariableValues() {
return variableValues;
}
/**
* @param variableValues The variableValues to set
*/
public void setVariableValues( List<VariableValue> variableValues ) {
this.variableValues = variableValues;
}
/**
* Gets autoOpening
*
* @return value of autoOpening
*/
public boolean isAutoOpening() {
return autoOpening;
}
/**
* @param autoOpening The autoOpening to set
*/
public void setAutoOpening( boolean autoOpening ) {
this.autoOpening = autoOpening;
}
}