Skip to content

Commit c74519c

Browse files
authored
Merge pull request #76 from george0st/change
Unit test simplification & Add schema builder
2 parents 916e8a8 + 26651c3 commit c74519c

File tree

4 files changed

+95
-117
lines changed

4 files changed

+95
-117
lines changed

nifi/cql-processor/nifi-cql-processors/src/test/java/org/george0st/processors/cql/PutCQLTest.java

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -43,43 +43,57 @@ public class PutCQLTest {
4343

4444
// Helper
4545
// https://medium.com/@mr.sinchan.banerjee/nifi-custom-processor-series-part-3-junit-test-with-nifi-mock-a935a1a4e3e5
46-
private void addTestScope(TestRunner testRunner, CQLControllerService testService, String propertyFile) throws IOException {
47-
TestSetup itm;
46+
public PutCQLTest() throws IOException, InterruptedException, InitializationException {
47+
48+
// create TestSetup
49+
setups=CreateSetup();
4850

49-
itm = TestSetup.getInstance(testRunner, testService, propertyFile);
50-
if (itm!=null) {
51-
setups.add(itm);
52-
testRunner.getLogger().info("Test scope: '{}'", itm.name);
51+
// build schema, if needed
52+
TestRunner runner = TestRunners.newTestRunner(PutCQL.class);
53+
CQLControllerService service = new CQLControllerService();
54+
runner.addControllerService(PutCQL.SERVICE_CONTROLLER.getName(), service);
55+
for (TestSetup setup: setups) {
56+
setup.setProperty(runner, service);
57+
runner.enableControllerService(service);
58+
try (CqlSession session=service.getSession()) {
59+
CqlCreateSchema schema = new CqlCreateSchema(session, setup);
60+
schema.Create();
61+
}
62+
runner.disableControllerService(service);
5363
}
5464
}
5565

66+
private ArrayList<TestSetup> CreateSetup() throws IOException {
67+
ArrayList<TestSetup> setup= new ArrayList<TestSetup>();
68+
69+
addTestScope(setup,
70+
TestSetup.getTestPropertyFile("./src/test",
71+
new String[]{"test-cassandra.json", "test-properties.json"}));
72+
addTestScope(setup,
73+
TestSetup.getTestPropertyFile("./src/test",
74+
new String[]{"test-scylla.json", "test-properties.json"}));
75+
addTestScope(setup,
76+
TestSetup.getTestPropertyFile("./src/test",
77+
new String[]{"test-astra.json", "test-properties.json"}));
78+
return setup;
79+
}
80+
81+
private void addTestScope(List<TestSetup> setup, String propertyFile) throws IOException {
82+
TestSetup itm;
83+
84+
itm = TestSetup.getInstance(propertyFile);
85+
if (itm!=null) setup.add(itm);
86+
}
87+
5688
@BeforeEach
5789
public void init() throws IOException, InterruptedException, InitializationException {
5890
testRunner = TestRunners.newTestRunner(PutCQL.class);
5991
testService = new CQLControllerService();
6092
testRunner.addControllerService(PutCQL.SERVICE_CONTROLLER.getName(), testService);
6193

62-
if (setups == null) {
63-
setups = new ArrayList<TestSetup>();
64-
65-
addTestScope(testRunner, testService,
66-
TestSetup.getTestPropertyFile("./src/test",
67-
new String[]{"test-cassandra.json", "test-properties.json"}));
68-
addTestScope(testRunner, testService,
69-
TestSetup.getTestPropertyFile("./src/test",
70-
new String[]{"test-scylla.json", "test-properties.json"}));
71-
addTestScope(testRunner, testService,
72-
TestSetup.getTestPropertyFile("./src/test",
73-
new String[]{"test-astra.json", "test-properties.json"}));
94+
for (TestSetup setup: setups) {
95+
System.out.println(String.format("Test scope: '%s'", setup.name));
7496
}
75-
76-
// // build schema
77-
// for (TestSetup setup: setups) {
78-
// CqlSession session=null;
79-
// CqlCreateSchema schema = new CqlCreateSchema(session, setup);
80-
// schema.Create();
81-
// }
82-
8397
}
8498

8599
private FlowFile coreTest(){
@@ -128,7 +142,7 @@ public void testBasic() {
128142
attributes.put("CQLName", setup.name);
129143

130144
testRunner.enqueue(content, attributes);
131-
setup.setProperty();
145+
setup.setProperty(testRunner, testService);
132146
testRunner.enableControllerService(testService);
133147
result = coreTest();
134148
testRunner.disableControllerService(testService);
@@ -139,8 +153,7 @@ public void testBasic() {
139153
}
140154

141155
@Test
142-
public void testBatchTypes() {
143-
156+
public void testBatchLoggedTypes() {
144157
HashMap<String, String> attributes = new HashMap<String, String>();
145158

146159
String content = "\"colbigint\",\"colint\",\"coltext\",\"colfloat\",\"coldouble\",\"coldate\",\"coltime\",\"coltimestamp\",\"colboolean\",\"coluuid\",\"colsmallint\",\"coltinyint\",\"coltimeuuid\",\"colvarchar\"\n" +
@@ -155,19 +168,35 @@ public void testBatchTypes() {
155168

156169
// batch type LOGGED
157170
testRunner.enqueue(content, attributes);
158-
setup.setProperty();
159-
setup.setProperty(PutCQL.BATCH_TYPE, PutCQL.BT_LOGGED.getValue());
171+
setup.setProperty(testRunner, testService);
172+
setup.setProperty(testRunner, PutCQL.BATCH_TYPE, PutCQL.BT_LOGGED.getValue());
160173
testRunner.enableControllerService(testService);
161174
result = coreTest();
162175
testRunner.disableControllerService(testService);
163176
// check amount of write items
164177
assertNotNull(result, String.format("Issue with processing in '%s'", setup.name));
165178
assertEquals(4, Long.parseLong(result.getAttribute(PutCQL.ATTRIBUTE_COUNT)));
179+
}
180+
}
181+
182+
@Test
183+
public void testBatchUnLoggedTypes() {
184+
HashMap<String, String> attributes = new HashMap<String, String>();
185+
186+
String content = "\"colbigint\",\"colint\",\"coltext\",\"colfloat\",\"coldouble\",\"coldate\",\"coltime\",\"coltimestamp\",\"colboolean\",\"coluuid\",\"colsmallint\",\"coltinyint\",\"coltimeuuid\",\"colvarchar\"\n" +
187+
"\"0\",\"1064\",\"zeVOKGnORq\",\"627.6811\",\"395.8522407512559\",\"1971-11-12\",\"03:37:15\",\"2000-09-25T22:18:45Z\",\"false\",\"6080071f-4dd1-4ea5-b711-9ad0716e242a\",\"8966\",\"55\",\"f45e58f5-c3b7-11ef-8d19-97ae87be7c54\",\"Tzxsw\"\n" +
188+
"\"1\",\"1709\",\"7By0z5QEXh\",\"652.03955\",\"326.9081263857284\",\"2013-12-17\",\"08:43:09\",\"2010-04-27T07:02:27Z\",\"false\",\"7d511666-2f81-41c4-9d5c-a5fa87f7d1c3\",\"24399\",\"38\",\"f45e8006-c3b7-11ef-8d19-172ff8d0d752\",\"exAbN\"\n" +
189+
"\"2\",\"6249\",\"UYI6AgkcBt\",\"939.01556\",\"373.48559413289485\",\"1980-11-05\",\"15:44:43\",\"2023-11-24T05:59:12Z\",\"false\",\"dbd35d1b-38d0-49a4-8069-9efd68314dc5\",\"6918\",\"72\",\"f45e8007-c3b7-11ef-8d19-d784fa8af8e3\",\"IjnDb\"\n" +
190+
"\"3\",\"6998\",\"lXQ69C5HOZ\",\"715.1224\",\"236.7994939033784\",\"1992-02-01\",\"08:07:34\",\"2024-06-29T21:08:54.463Z\",\"true\",\"84a7395c-94fd-43f5-84c6-4152f0407e93\",\"22123\",\"39\",\"f45e8008-c3b7-11ef-8d19-0376318d55df\",\"jyZo8\"\n";
191+
FlowFile result;
192+
193+
for (TestSetup setup : setups) {
194+
attributes.put("CQLName", setup.name);
166195

167196
// batch type UNLOGGED
168197
testRunner.enqueue(content, attributes);
169-
setup.setProperty();
170-
setup.setProperty(PutCQL.BATCH_TYPE, PutCQL.BT_UNLOGGED.getValue());
198+
setup.setProperty(testRunner, testService);
199+
setup.setProperty(testRunner, PutCQL.BATCH_TYPE, PutCQL.BT_UNLOGGED.getValue());
171200
testRunner.enableControllerService(testService);
172201
result = coreTest();
173202
testRunner.disableControllerService(testService);
@@ -200,17 +229,16 @@ public void testMoreItems() {
200229
attributes.put("CQLName",setup.name);
201230

202231
testRunner.enqueue(content, attributes);
203-
setup.setProperty();
232+
setup.setProperty(testRunner, testService);
204233
testRunner.enableControllerService(testService);
205234
result = coreTest();
206235
testRunner.disableControllerService(testService);
207236
// check amount of write items
208237
assertNotNull(result, String.format("Issue with processing in '%s'", setup.name));
209238
assertEquals(5, Long.parseLong(result.getAttribute(PutCQL.ATTRIBUTE_COUNT)));
210239

211-
212240
testRunner.enqueue(content2, attributes);
213-
setup.setProperty();
241+
setup.setProperty(testRunner, testService);
214242
testRunner.enableControllerService(testService);
215243
result = coreTest();
216244
testRunner.disableControllerService(testService);
@@ -225,10 +253,6 @@ public void testNoQuotas() {
225253
HashMap<String, String> attributes = new HashMap<String, String>();
226254

227255
String content = "colbigint,colint,coltext,colfloat,coldouble,coldate,coltime,coltimestamp,colboolean,coluuid,colsmallint,coltinyint,coltimeuuid,colvarchar\n" +
228-
"0,1064,zeVOKGnORq,627.6811,395.8522407512559,1971-11-12,03:37:15,2000-09-25T22:18:45Z,false,6080071f-4dd1-4ea5-b711-9ad0716e242a,8966,55,f45e58f5-c3b7-11ef-8d19-97ae87be7c54,Tzxsw\n" +
229-
"1,1709,7By0z5QEXh,652.03955,326.9081263857284,2013-12-17,08:43:09,2010-04-27T07:02:27Z,false,7d511666-2f81-41c4-9d5c-a5fa87f7d1c3,24399,38,f45e8006-c3b7-11ef-8d19-172ff8d0d752,exAbN\n";
230-
231-
String content2 = "colbigint,colint,coltext,colfloat,coldouble,coldate,coltime,coltimestamp,colboolean,coluuid,colsmallint,coltinyint,coltimeuuid,colvarchar\n" +
232256
"10,1064,zeVOKGnORq,627.6811,395.8522407512559,1971-11-12,03:37:15,2000-09-25T22:18:45Z,false,6080071f-4dd1-4ea5-b711-9ad0716e242a,8966,55,f45e58f5-c3b7-11ef-8d19-97ae87be7c54,Tzxsw\n" +
233257
"11,1709,7By0z5QEXh,652.03955,326.9081263857284,2013-12-17,08:43:09,2010-04-27T07:02:27Z,false,7d511666-2f81-41c4-9d5c-a5fa87f7d1c3,24399,38,f45e8006-c3b7-11ef-8d19-172ff8d0d752,exAbN\n" +
234258
"12,6249,UYI6AgkcBt,939.01556,373.48559413289485,1980-11-05,15:44:43,2023-11-24T05:59:12Z,false,dbd35d1b-38d0-49a4-8069-9efd68314dc5,6918,72,f45e8007-c3b7-11ef-8d19-d784fa8af8e3,IjnDb\n" +
@@ -239,25 +263,7 @@ public void testNoQuotas() {
239263
attributes.put("CQLName", setup.name);
240264

241265
testRunner.enqueue(content, attributes);
242-
setup.setProperty();
243-
testRunner.enableControllerService(testService);
244-
result = coreTest();
245-
testRunner.disableControllerService(testService);
246-
// check amount of write items
247-
assertNotNull(result, String.format("Issue with processing in '%s'", setup.name));
248-
assertEquals(2, Long.parseLong(result.getAttribute(PutCQL.ATTRIBUTE_COUNT)));
249-
250-
testRunner.enqueue(content, attributes);
251-
setup.setProperty();
252-
testRunner.enableControllerService(testService);
253-
result = coreTest();
254-
testRunner.disableControllerService(testService);
255-
// check amount of write items
256-
assertNotNull(result, String.format("Issue with processing in '%s'", setup.name));
257-
assertEquals(2, Long.parseLong(result.getAttribute(PutCQL.ATTRIBUTE_COUNT)));
258-
259-
testRunner.enqueue(content2, attributes);
260-
setup.setProperty();
266+
setup.setProperty(testRunner, testService);
261267
testRunner.enableControllerService(testService);
262268
result = coreTest();
263269
testRunner.disableControllerService(testService);
@@ -277,7 +283,7 @@ public void testEmptyInput() {
277283
attributes.put("CQLName", setup.name);
278284

279285
testRunner.enqueue(content, attributes);
280-
setup.setProperty();
286+
setup.setProperty(testRunner, testService);
281287
testRunner.enableControllerService(testService);
282288
result = coreTest();
283289
testRunner.disableControllerService(testService);
@@ -297,7 +303,7 @@ public void testOnlyHeader() {
297303
attributes.put("CQLName", setup.name);
298304

299305
testRunner.enqueue(content, attributes);
300-
setup.setProperty();
306+
setup.setProperty(testRunner, testService);
301307
testRunner.enableControllerService(testService);
302308
result = coreTest();
303309
testRunner.disableControllerService(testService);
@@ -317,7 +323,7 @@ public void testOnlyHeaderNoQuotas() {
317323
attributes.put("CQLName", setup.name);
318324

319325
testRunner.enqueue(content, attributes);
320-
setup.setProperty();
326+
setup.setProperty(testRunner, testService);
321327
testRunner.enableControllerService(testService);
322328
result = coreTest();
323329
testRunner.disableControllerService(testService);
@@ -340,7 +346,7 @@ public void testBasicRepeat5() {
340346
for (TestSetup setup: setups) {
341347
attributes.put("CQLName", setup.name);
342348

343-
setup.setProperty();
349+
setup.setProperty(testRunner, testService);
344350
testRunner.enableControllerService(testService);
345351
for (int i = 0; i < 5; i++) {
346352
testRunner.enqueue(content, attributes);
@@ -367,7 +373,7 @@ public void testNoQuotas2() {
367373
attributes.put("CQLName", setup.name);
368374

369375
testRunner.enqueue(content, attributes);
370-
setup.setProperty();
376+
setup.setProperty(testRunner, testService);
371377
testRunner.enableControllerService(testService);
372378
result = coreTest();
373379
testRunner.disableControllerService(testService);

nifi/cql-processor/nifi-cql-processors/src/test/java/org/george0st/processors/cql/helper/TestSetup.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ public class TestSetup extends Setup {
2626
public String consistencyLevel;
2727
public String compaction;
2828

29-
protected TestRunner testRunner;
30-
protected CQLControllerService testService;
31-
3229
private TestSetup(){
3330
}
3431

@@ -41,7 +38,7 @@ public void setIPAddresses(String ipAddr) {
4138
}
4239
public String getIPAddresses() { return String.join(",",this.ipAddresses); }
4340

44-
public static TestSetup getInstance(TestRunner runner, CQLControllerService service, String propertyFile) throws IOException {
41+
public static TestSetup getInstance(String propertyFile) throws IOException {
4542
try (FileReader fileReader = new FileReader(propertyFile)) {
4643
TestSetup setup = (new Gson()).fromJson(fileReader, TestSetup.class);
4744

@@ -50,8 +47,6 @@ public static TestSetup getInstance(TestRunner runner, CQLControllerService serv
5047
// default setting
5148
if (setup.compaction == null)
5249
setup.compaction = "{'class':'SizeTieredCompactionStrategy'}";
53-
setup.testRunner = runner;
54-
setup.testService = service;
5550
return setup;
5651
}
5752
}
@@ -80,12 +75,12 @@ public static String getTestPropertyFile(String[] files){
8075
return null;
8176
}
8277

83-
public void setControllerProperty(PropertyDescriptor property, String propertyValue) {
78+
public void setControllerProperty(TestRunner testRunner, CQLControllerService testService, PropertyDescriptor property, String propertyValue) {
8479
//if (propertyValue != null)
8580
testRunner.setProperty(testService, property, propertyValue);
8681
}
8782

88-
public void setProperty(PropertyDescriptor property, String propertyValue) {
83+
public void setProperty(TestRunner testRunner, PropertyDescriptor property, String propertyValue) {
8984
if (propertyValue != null)
9085
testRunner.setProperty(property, propertyValue);
9186
}
@@ -110,28 +105,28 @@ private String getJson(String item) {
110105
/**
111106
* Setting test runner based on test setting
112107
*/
113-
public void setProperty() {
108+
public void setProperty(TestRunner testRunner, CQLControllerService testService) {
114109
// clear all properties before the setting
115110
testRunner.clearProperties();
116111

117112
// set CONTROLLER properties
118-
setControllerProperty(CQLControllerService.IP_ADDRESSES, ipAddresses!=null ? String.join(",", ipAddresses) : (String)null);
119-
setControllerProperty(CQLControllerService.PORT, String.valueOf(port));
120-
setControllerProperty(CQLControllerService.SECURE_CONNECTION_BUNDLE, secureConnectionBundle);
121-
setControllerProperty(CQLControllerService.USERNAME, getJson(username));
122-
setControllerProperty(CQLControllerService.PASSWORD, getJson(pwd));
123-
setControllerProperty(CQLControllerService.LOCAL_DC, localDC);
124-
setControllerProperty(CQLControllerService.CONNECTION_TIMEOUT, String.valueOf(connectionTimeout));
125-
setControllerProperty(CQLControllerService.REQUEST_TIMEOUT, String.valueOf(requestTimeout));
126-
setControllerProperty(CQLControllerService.CONSISTENCY_LEVEL, consistencyLevel);
113+
setControllerProperty(testRunner, testService, CQLControllerService.IP_ADDRESSES, ipAddresses!=null ? String.join(",", ipAddresses) : (String)null);
114+
setControllerProperty(testRunner, testService, CQLControllerService.PORT, String.valueOf(port));
115+
setControllerProperty(testRunner, testService, CQLControllerService.SECURE_CONNECTION_BUNDLE, secureConnectionBundle);
116+
setControllerProperty(testRunner, testService, CQLControllerService.USERNAME, getJson(username));
117+
setControllerProperty(testRunner, testService, CQLControllerService.PASSWORD, getJson(pwd));
118+
setControllerProperty(testRunner, testService, CQLControllerService.LOCAL_DC, localDC);
119+
setControllerProperty(testRunner, testService, CQLControllerService.CONNECTION_TIMEOUT, String.valueOf(connectionTimeout));
120+
setControllerProperty(testRunner, testService, CQLControllerService.REQUEST_TIMEOUT, String.valueOf(requestTimeout));
121+
setControllerProperty(testRunner, testService, CQLControllerService.CONSISTENCY_LEVEL, consistencyLevel);
127122

128123
// set PROCESSOR properties
129-
setProperty(PutCQL.SERVICE_CONTROLLER, PutCQL.SERVICE_CONTROLLER.getName());
130-
setProperty(PutCQL.WRITE_CONSISTENCY_LEVEL, writeConsistencyLevel);
131-
setProperty(PutCQL.BATCH_SIZE, String.valueOf(getBatchSize()));
132-
setProperty(PutCQL.BATCH_TYPE, batchType);
133-
setProperty(PutCQL.TABLE, table);
134-
setProperty(PutCQL.DRY_RUN, String.valueOf(dryRun));
124+
setProperty(testRunner, PutCQL.SERVICE_CONTROLLER, PutCQL.SERVICE_CONTROLLER.getName());
125+
setProperty(testRunner, PutCQL.WRITE_CONSISTENCY_LEVEL, writeConsistencyLevel);
126+
setProperty(testRunner, PutCQL.BATCH_SIZE, String.valueOf(getBatchSize()));
127+
setProperty(testRunner, PutCQL.BATCH_TYPE, batchType);
128+
setProperty(testRunner, PutCQL.TABLE, table);
129+
setProperty(testRunner, PutCQL.DRY_RUN, String.valueOf(dryRun));
135130

136131
testRunner.setValidateExpressionUsage(false);
137132
}

0 commit comments

Comments
 (0)