/
MonetDBBulkLoaderMetaTest.java
264 lines (208 loc) · 10 KB
/
MonetDBBulkLoaderMetaTest.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
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2016 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.trans.steps.monetdbbulkloader;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.plugins.StepPluginType;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.row.value.ValueMetaPluginType;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.StepInjectionMetaEntry;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.pentaho.di.trans.steps.loadsave.LoadSaveTester;
import org.pentaho.di.trans.steps.loadsave.initializer.InitializerInterface;
import org.pentaho.di.trans.steps.loadsave.validator.ArrayLoadSaveValidator;
import org.pentaho.di.trans.steps.loadsave.validator.BooleanLoadSaveValidator;
import org.pentaho.di.trans.steps.loadsave.validator.FieldLoadSaveValidator;
import org.pentaho.di.trans.steps.loadsave.validator.PrimitiveBooleanArrayLoadSaveValidator;
import org.pentaho.di.trans.steps.loadsave.validator.StringLoadSaveValidator;
/**
* Created by gmoran on 2/25/14.
*/
public class MonetDBBulkLoaderMetaTest implements InitializerInterface<StepMetaInterface> {
private StepMeta stepMeta;
private MonetDBBulkLoader loader;
private MonetDBBulkLoaderData ld;
private MonetDBBulkLoaderMeta lm;
LoadSaveTester loadSaveTester;
Class<MonetDBBulkLoaderMeta> testMetaClass = MonetDBBulkLoaderMeta.class;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
PluginRegistry.addPluginType( ValueMetaPluginType.getInstance() );
PluginRegistry.init( true );
}
@Before
public void setUp() throws Exception {
KettleEnvironment.init();
PluginRegistry.init( true );
TransMeta transMeta = new TransMeta();
transMeta.setName( "loader" );
lm = new MonetDBBulkLoaderMeta();
ld = new MonetDBBulkLoaderData();
PluginRegistry plugReg = PluginRegistry.getInstance();
String loaderPid = plugReg.getPluginId( StepPluginType.class, lm );
stepMeta = new StepMeta( loaderPid, "loader", lm );
Trans trans = new Trans( transMeta );
transMeta.addStep( stepMeta );
loader = new MonetDBBulkLoader( stepMeta, ld, 1, transMeta, trans );
List<String> attributes =
Arrays.asList( "dbConnectionName", "schemaName", "tableName", "logFile", "fieldSeparator", "fieldEnclosure",
"NULLrepresentation", "encoding", "truncate", "fullyQuoteSQL", "autoSchema", "autoStringWidths", "fieldTable", "fieldStream",
"fieldFormatOk" );
// Important note - the "databaseMeta" is not tested here as it's tied to the dbConnectionName. Since the loader
// has assymetry here, we have to not test the databaseMeta, or we have to do surgery on the MonetDBBulkLoaderMeta
// so that it's symmetric (and has no dependent variables like this).
// MB -5/2016
FieldLoadSaveValidator<String[]> stringArrayLoadSaveValidator =
new ArrayLoadSaveValidator<String>( new StringLoadSaveValidator(), 5 );
Map<String, FieldLoadSaveValidator<?>> attrValidatorMap = new HashMap<String, FieldLoadSaveValidator<?>>();
attrValidatorMap.put( "fieldTable", stringArrayLoadSaveValidator );
attrValidatorMap.put( "fieldStream", stringArrayLoadSaveValidator );
attrValidatorMap.put( "fieldFormatOk",
new PrimitiveBooleanArrayLoadSaveValidator( new BooleanLoadSaveValidator(), 5 ) );
Map<String, FieldLoadSaveValidator<?>> typeValidatorMap = new HashMap<String, FieldLoadSaveValidator<?>>();
loadSaveTester =
new LoadSaveTester( testMetaClass, attributes, new ArrayList<String>(), new ArrayList<String>(),
new HashMap<String, String>(), new HashMap<String, String>(), attrValidatorMap, typeValidatorMap, this );
}
// Call the allocate method on the LoadSaveTester meta class
@Override
public void modify( StepMetaInterface someMeta ) {
if ( someMeta instanceof MonetDBBulkLoaderMeta ) {
( (MonetDBBulkLoaderMeta) someMeta ).allocate( 5 );
}
}
@Test
public void testSerialization() throws KettleException {
loadSaveTester.testSerialization();
}
@Test
public void testTopLevelMetadataEntries() {
try {
List<StepInjectionMetaEntry> entries =
loader.getStepMeta().getStepMetaInterface().getStepMetaInjectionInterface().getStepInjectionMetadataEntries();
String masterKeys = "SCHEMA TABLE LOGFILE FIELD_SEPARATOR FIELD_ENCLOSURE NULL_REPRESENTATION ENCODING TRUNCATE "
+ "FULLY_QUOTE_SQL BUFFER_SIZE MAPPINGS ";
for ( StepInjectionMetaEntry entry : entries ) {
String key = entry.getKey();
assertTrue( masterKeys.contains( key ) );
masterKeys = masterKeys.replace( key, "" );
}
assertTrue( masterKeys.trim().length() == 0 );
} catch ( KettleException e ) {
fail( e.getMessage() );
}
}
@Test
public void testChildLevelMetadataEntries() {
try {
List<StepInjectionMetaEntry> entries =
loader.getStepMeta().getStepMetaInterface().getStepMetaInjectionInterface().getStepInjectionMetadataEntries();
String childKeys = "STREAMNAME FIELDNAME FIELD_FORMAT_OK ";
StepInjectionMetaEntry mappingEntry = null;
for ( StepInjectionMetaEntry entry : entries ) {
String key = entry.getKey();
if ( key.equals( "MAPPINGS" ) ) {
mappingEntry = entry;
break;
}
}
assertNotNull( mappingEntry );
List<StepInjectionMetaEntry> fieldAttributes = mappingEntry.getDetails().get( 0 ).getDetails();
for ( StepInjectionMetaEntry attribute : fieldAttributes ) {
String key = attribute.getKey();
assertTrue( childKeys.contains( key ) );
childKeys = childKeys.replace( key, "" );
}
assertTrue( childKeys.trim().length() == 0 );
} catch ( KettleException e ) {
fail( e.getMessage() );
}
}
// Note - the cloneTest() was removed since it's being covered by the load/save tester now.
@Test
public void testInjection() {
try {
List<StepInjectionMetaEntry> entries =
loader.getStepMeta().getStepMetaInterface().getStepMetaInjectionInterface().getStepInjectionMetadataEntries();
for ( StepInjectionMetaEntry entry : entries ) {
entry.setValueType( lm.findAttribute( entry.getKey() ).getType() );
switch ( entry.getValueType() ) {
case ValueMetaInterface.TYPE_STRING:
entry.setValue( "new_".concat( entry.getKey() ) );
break;
case ValueMetaInterface.TYPE_BOOLEAN:
entry.setValue( Boolean.TRUE );
break;
default:
break;
}
if ( !entry.getDetails().isEmpty() ) {
List<StepInjectionMetaEntry> childEntries = entry.getDetails().get( 0 ).getDetails();
for ( StepInjectionMetaEntry childEntry : childEntries ) {
switch ( childEntry.getValueType() ) {
case ValueMetaInterface.TYPE_STRING:
childEntry.setValue( "new_".concat( childEntry.getKey() ) );
break;
case ValueMetaInterface.TYPE_BOOLEAN:
childEntry.setValue( Boolean.TRUE );
break;
default:
break;
}
}
}
}
loader.getStepMeta().getStepMetaInterface().getStepMetaInjectionInterface().injectStepMetadataEntries( entries );
assertEquals( "Schema name not properly injected... ", "new_SCHEMA", lm.getSchemaName() );
assertEquals( "Table name not properly injected... ", "new_TABLE", lm.getTableName() );
assertEquals( "Logfile not properly injected... ", "new_LOGFILE", lm.getLogFile() );
assertEquals( "Field separator not properly injected... ", "new_FIELD_SEPARATOR", lm.getFieldSeparator() );
assertEquals( "Field enclosure not properly injected... ", "new_FIELD_ENCLOSURE", lm.getFieldEnclosure() );
assertEquals( "Null representation not properly injected... ", "new_NULL_REPRESENTATION", lm.getNULLrepresentation() );
assertEquals( "Encoding path not properly injected... ", "new_ENCODING", lm.getEncoding() );
assertEquals( "Buffer size not properly injected... ", "new_BUFFER_SIZE", lm.getBufferSize() );
assertEquals( "Truncate not properly injected... ", Boolean.TRUE, lm.isTruncate() );
assertEquals( "Fully Quote SQL not properly injected... ", Boolean.TRUE, lm.isFullyQuoteSQL() );
assertEquals( "Field name not properly injected... ", "new_FIELDNAME", lm.getFieldTable()[0] );
assertEquals( "Stream name not properly injected... ", "new_STREAMNAME", lm.getFieldStream()[0] );
assertEquals( "Field Format not properly injected... ", Boolean.TRUE, lm.getFieldFormatOk()[0] );
} catch ( KettleException e ) {
fail( e.getMessage() );
}
}
}