You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "ChrisSamo632 (via GitHub)" <gi...@apache.org> on 2023/04/12 12:02:14 UTC
[GitHub] [nifi] ChrisSamo632 commented on a diff in pull request #7163: NIFI-11430 - Fixed empty file being produced when grouping by query and fixed _source and _meta extraction by query
ChrisSamo632 commented on code in PR #7163:
URL: https://github.com/apache/nifi/pull/7163#discussion_r1163945827
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/main/java/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearch.java:
##########
@@ -264,14 +265,16 @@ private void combineHits(final List<Map<String, Object>> hits, final PaginatedJs
* SearchResponse is processed, i.e. this approach allows recursion for paginated queries, but is unnecessary for single-response queries.
*/
@Override
- List<FlowFile> handleHits(final List<Map<String, Object>> hits, final boolean newQuery, final PaginatedJsonQueryParameters paginatedJsonQueryParameters,
+ List<FlowFile> handleHits(List<Map<String, Object>> hits, final boolean newQuery, final PaginatedJsonQueryParameters paginatedJsonQueryParameters,
final ProcessSession session, final FlowFile parent, final Map<String, String> attributes,
final List<FlowFile> hitsFlowFiles, final String transitUri, final StopWatch stopWatch) throws IOException {
paginatedJsonQueryParameters.incrementPageCount();
attributes.put("page.number", Integer.toString(paginatedJsonQueryParameters.getPageCount()));
if (hitStrategy == ResultOutputStrategy.PER_QUERY) {
- combineHits(hits, paginatedJsonQueryParameters, session, parent, attributes, hitsFlowFiles);
+
+ hits = formatHits(hits);
+ combineHits(hits, paginatedJsonQueryParameters, session, parent, attributes, hitsFlowFiles, newQuery);
Review Comment:
```suggestion
final List<Map<String, Object>> formattedHits = formatHits(hits);
combineHits(formattedHits, paginatedJsonQueryParameters, session, parent, attributes, hitsFlowFiles, newQuery);
```
Prefer to leave the original input `hits` unchanged for clarity (same as done in `AbstractJsonQueryElasticsearch#handleHits`)
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -32,15 +32,15 @@ import static groovy.json.JsonOutput.toJson
import static org.hamcrest.CoreMatchers.equalTo
import static org.hamcrest.CoreMatchers.is
import static org.hamcrest.MatcherAssert.assertThat
-import static org.junit.jupiter.api.Assertions.assertThrows
+import static org.junit.jupiter.api.Assertions.*
Review Comment:
Don't use wildcard imports
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -117,13 +117,184 @@ abstract class AbstractPaginatedJsonQueryElasticsearchTest extends AbstractJsonQ
assertSendEvent(runner, input)
}
+ @Test
+ void testSourceExtraction() throws Exception {
+ for (ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
+ final TestRunner runner = createRunner(false)
+ runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]], "sort": [[message: [order: "asc"]]]])))
+
+ int flowFileCount
+ String hitsCount
+ boolean ndjson = false
+
+ switch (resultOutputStrategy) {
+ case ResultOutputStrategy.PER_QUERY:
+ flowFileCount = 1
+ hitsCount = "10"
+ ndjson = true
+ break
+ case ResultOutputStrategy.PER_HIT:
+ flowFileCount = 10
+ hitsCount = "1"
+ break
+ case ResultOutputStrategy.PER_RESPONSE:
+ flowFileCount = 1
+ hitsCount = "10"
+ break
+ }
Review Comment:
Should really have a `default` clause in a `switch` - would likely be a case of throwing an `IllegalArgumentException` or such here to indicate that we haven't got a test for the current `resultOutputStrategy` value (e.g. if a new one is added to the enum in future)
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -117,13 +117,184 @@ abstract class AbstractPaginatedJsonQueryElasticsearchTest extends AbstractJsonQ
assertSendEvent(runner, input)
}
+ @Test
+ void testSourceExtraction() throws Exception {
+ for (ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
+ final TestRunner runner = createRunner(false)
+ runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]], "sort": [[message: [order: "asc"]]]])))
+
+ int flowFileCount
+ String hitsCount
+ boolean ndjson = false
+
+ switch (resultOutputStrategy) {
+ case ResultOutputStrategy.PER_QUERY:
+ flowFileCount = 1
+ hitsCount = "10"
+ ndjson = true
+ break
+ case ResultOutputStrategy.PER_HIT:
+ flowFileCount = 10
+ hitsCount = "1"
+ break
+ case ResultOutputStrategy.PER_RESPONSE:
+ flowFileCount = 1
+ hitsCount = "10"
+ break
+ }
+
+ // test _source only format
+ runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, resultOutputStrategy.getValue())
+ runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_FORMAT, SearchResultsFormat.SOURCE_ONLY.getValue())
+
+
+ // Test against each pagination type
+ for (PaginationType paginationType : PaginationType.values()) {
+ runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, paginationType.getValue())
+
+ runOnce(runner)
+
+ // Test Relationship counts
+ testCounts(runner, isInput() ? 1 : 0, flowFileCount, 0, 0)
+
+ // Per response outputs an array of values
+ if (resultOutputStrategy.equals(ResultOutputStrategy.PER_RESPONSE)) {
+ runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).forEach({ hit ->
+ hit.assertAttributeEquals("hit.count", hitsCount)
+ assertOutputContent(hit.getContent(), hitsCount as int, ndjson)
+ OBJECT_MAPPER.readValue(hit.getContent(), ArrayList.class).forEach(h -> {
+ assertFalse(h.isEmpty())
+ assertFalse(h.containsKey("_source"))
+ assertFalse(h.containsKey("_index"))
+ // should be the _source content only
+ assertTrue(h.containsKey("msg"))
+
+ })
+ assertThat(
+ runner.getProvenanceEvents().stream().filter({ pe ->
+ pe.getEventType() == ProvenanceEventType.RECEIVE &&
+ pe.getAttribute("uuid") == hit.getAttribute("uuid")
+ }).count(),
+ is(1L)
+ )
+ })
+ } else {
+ runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).forEach({ hit ->
+ hit.assertAttributeEquals("hit.count", hitsCount)
+ assertOutputContent(hit.getContent(), hitsCount as int, ndjson)
+ final Map<String, Object> h = OBJECT_MAPPER.readValue(hit.getContent(), Map.class)
+ assertFalse(h.isEmpty())
+ assertFalse(h.containsKey("_source"))
+ assertFalse(h.containsKey("_index"))
+ // should be the _source content only
+ assertTrue(h.containsKey("msg"))
+
+ assertThat(
+ runner.getProvenanceEvents().stream().filter({ pe ->
+ pe.getEventType() == ProvenanceEventType.RECEIVE &&
+ pe.getAttribute("uuid") == hit.getAttribute("uuid")
+ }).count(),
+ is(1L)
+ )
+ })
+ }
+
+ reset(runner)
+ }
+ }
+ }
+
+ @Test
+ void testMetaExtraction() throws Exception {
+
+ // Test against each result strategy type
+ for (ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
Review Comment:
```suggestion
for (final ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
```
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -265,46 +436,35 @@ abstract class AbstractPaginatedJsonQueryElasticsearchTest extends AbstractJsonQ
is(1L)
)
} else {
- assertThat(runner.getProvenanceEvents().stream().filter({ pe -> pe.getEventType() == ProvenanceEventType.SEND}).count(), is(0L))
+ assertThat(runner.getProvenanceEvents().stream().filter({ pe -> pe.getEventType() == ProvenanceEventType.SEND }).count(), is(0L))
}
}
@Test
- void testNoHitsFlowFileIsProducedForEachResultSplitSetup() {
+ void testEmptyHitsFlowFileIsProducedForEachResultSplitSetup() {
final TestRunner runner = createRunner(false)
final TestElasticsearchClientService service = getService(runner)
- runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]]])))
+ runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]], "sort": [[message: [order: "asc"]]]])))
runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.OUTPUT_NO_HITS, "true")
service.setMaxPages(0)
- // test that an empty flow file is produced for a per query setup
- runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, ResultOutputStrategy.PER_QUERY.getValue())
- runOnce(runner)
- testCounts(runner, isInput() ? 1 : 0, 1, 0, 0)
- runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "0")
- runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "1")
- runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).getSize() == 0
- reset(runner)
+ for (PaginationType paginationType : PaginationType.values()) {
Review Comment:
```suggestion
for (final PaginationType paginationType : PaginationType.values()) {
```
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -117,13 +117,184 @@ abstract class AbstractPaginatedJsonQueryElasticsearchTest extends AbstractJsonQ
assertSendEvent(runner, input)
}
+ @Test
+ void testSourceExtraction() throws Exception {
+ for (ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
+ final TestRunner runner = createRunner(false)
+ runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]], "sort": [[message: [order: "asc"]]]])))
+
+ int flowFileCount
+ String hitsCount
+ boolean ndjson = false
+
+ switch (resultOutputStrategy) {
+ case ResultOutputStrategy.PER_QUERY:
+ flowFileCount = 1
+ hitsCount = "10"
+ ndjson = true
+ break
+ case ResultOutputStrategy.PER_HIT:
+ flowFileCount = 10
+ hitsCount = "1"
+ break
+ case ResultOutputStrategy.PER_RESPONSE:
+ flowFileCount = 1
+ hitsCount = "10"
+ break
+ }
+
+ // test _source only format
+ runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, resultOutputStrategy.getValue())
+ runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_FORMAT, SearchResultsFormat.SOURCE_ONLY.getValue())
+
+
+ // Test against each pagination type
+ for (PaginationType paginationType : PaginationType.values()) {
+ runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, paginationType.getValue())
+
+ runOnce(runner)
+
+ // Test Relationship counts
+ testCounts(runner, isInput() ? 1 : 0, flowFileCount, 0, 0)
+
+ // Per response outputs an array of values
+ if (resultOutputStrategy.equals(ResultOutputStrategy.PER_RESPONSE)) {
+ runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).forEach({ hit ->
+ hit.assertAttributeEquals("hit.count", hitsCount)
+ assertOutputContent(hit.getContent(), hitsCount as int, ndjson)
+ OBJECT_MAPPER.readValue(hit.getContent(), ArrayList.class).forEach(h -> {
+ assertFalse(h.isEmpty())
+ assertFalse(h.containsKey("_source"))
+ assertFalse(h.containsKey("_index"))
+ // should be the _source content only
+ assertTrue(h.containsKey("msg"))
+
+ })
+ assertThat(
+ runner.getProvenanceEvents().stream().filter({ pe ->
+ pe.getEventType() == ProvenanceEventType.RECEIVE &&
+ pe.getAttribute("uuid") == hit.getAttribute("uuid")
+ }).count(),
+ is(1L)
+ )
+ })
+ } else {
+ runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).forEach({ hit ->
+ hit.assertAttributeEquals("hit.count", hitsCount)
+ assertOutputContent(hit.getContent(), hitsCount as int, ndjson)
+ final Map<String, Object> h = OBJECT_MAPPER.readValue(hit.getContent(), Map.class)
+ assertFalse(h.isEmpty())
+ assertFalse(h.containsKey("_source"))
+ assertFalse(h.containsKey("_index"))
+ // should be the _source content only
+ assertTrue(h.containsKey("msg"))
+
+ assertThat(
+ runner.getProvenanceEvents().stream().filter({ pe ->
+ pe.getEventType() == ProvenanceEventType.RECEIVE &&
+ pe.getAttribute("uuid") == hit.getAttribute("uuid")
+ }).count(),
+ is(1L)
+ )
+ })
+ }
+
+ reset(runner)
+ }
+ }
+ }
+
+ @Test
+ void testMetaExtraction() throws Exception {
Review Comment:
There's a lot of repetition between this and the `testSourceExtraction` above, would it be better to try combining them and reducing the repetetive code, e.g.
```java
static void assertFormattedResult(final SearchResultsFormat searchResultsFormat, final Map<String, Object> hit) {
assertFalse(hit.isEmpty())
switch(searchResultsFormat) {
case SearchResultsFormat.SOURCE_ONLY:
assertFalse(hit.containsKey("_source"))
assertFalse(hit.containsKey("_index"))
assertTrue(hit.containsKey("msg"))
break
case SearchResultsFormat.METADATA_ONLY:
assertFalse(hit.containsKey("_source"))
assertTrue(hit.containsKey("_index"))
assertFalse(hit.containsKey("msg"))
break
case SearchResultsFormat.FULL:
assertTrue(hit.containsKey("_source"))
assertTrue(hit.containsKey("_index"))
assertFalse(hit.containsKey("msg"))
break
default:
throw new IllegalArgumentException("Unknown SearchResultsFormat value: " + searchResultsFormat.toString())
}
}
private void assertResultsFormat(final TestRunner runner, final ResultOutputStrategy resultOutputStrategy, final SearchResultsFormat searchResultsFormat) {
int flowFileCount
String hitsCount
boolean ndjson = false
switch (resultOutputStrategy) {
case ResultOutputStrategy.PER_QUERY:
flowFileCount = 1
hitsCount = "10"
ndjson = true
break
case ResultOutputStrategy.PER_HIT:
flowFileCount = 10
hitsCount = "1"
break
case ResultOutputStrategy.PER_RESPONSE:
flowFileCount = 1
hitsCount = "10"
break
default:
throw new IllegalArgumentException("Unknown ResultOutputStrategy value: " + resultOutputStrategy.toString())
}
// Test Relationship counts
testCounts(runner, isInput() ? 1 : 0, flowFileCount, 0, 0)
// Per response outputs an array of values
runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).forEach({ hit ->
hit.assertAttributeEquals("hit.count", hitsCount)
assertOutputContent(hit.getContent(), hitsCount as int, ndjson)
if (ResultOutputStrategy.PER_RESPONSE == resultOutputStrategy) {
OBJECT_MAPPER.readValue(hit.getContent(), ArrayList.class).forEach(h -> {
assertFormattedResult(searchResultsFormat, h as Map<String, Object>)
})
} else {
final Map<String, Object> h = OBJECT_MAPPER.readValue(hit.getContent(), Map.class)
assertFormattedResult(searchResultsFormat, h)
}
assertThat(
runner.getProvenanceEvents().stream().filter({ pe ->
pe.getEventType() == ProvenanceEventType.RECEIVE &&
pe.getAttribute("uuid") == hit.getAttribute("uuid")
}).count(),
is(1L)
)
})
}
@Test
void testResultsFormat() throws Exception {
for (final ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
final TestRunner runner = createRunner(false)
runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]], "sort": [[message: [order: "asc"]]]])))
runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, resultOutputStrategy.getValue())
// Test against each results format
for (final SearchResultsFormat searchResultsFormat : SearchResultsFormat.values()) {
runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_FORMAT, searchResultsFormat.getValue())
// Test against each pagination type
for (final PaginationType paginationType : PaginationType.values()) {
runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, paginationType.getValue())
runOnce(runner)
assertResultsFormat(runner, resultOutputStrategy, searchResultsFormat)
reset(runner)
}
}
}
}
```
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/main/java/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearch.java:
##########
@@ -264,14 +265,16 @@ private void combineHits(final List<Map<String, Object>> hits, final PaginatedJs
* SearchResponse is processed, i.e. this approach allows recursion for paginated queries, but is unnecessary for single-response queries.
*/
@Override
- List<FlowFile> handleHits(final List<Map<String, Object>> hits, final boolean newQuery, final PaginatedJsonQueryParameters paginatedJsonQueryParameters,
+ List<FlowFile> handleHits(List<Map<String, Object>> hits, final boolean newQuery, final PaginatedJsonQueryParameters paginatedJsonQueryParameters,
Review Comment:
```suggestion
List<FlowFile> handleHits(final List<Map<String, Object>> hits, final boolean newQuery, final PaginatedJsonQueryParameters paginatedJsonQueryParameters,
```
Better `hits` remains unchanged/`final`
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -117,13 +117,184 @@ abstract class AbstractPaginatedJsonQueryElasticsearchTest extends AbstractJsonQ
assertSendEvent(runner, input)
}
+ @Test
+ void testSourceExtraction() throws Exception {
+ for (ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
+ final TestRunner runner = createRunner(false)
+ runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]], "sort": [[message: [order: "asc"]]]])))
+
+ int flowFileCount
+ String hitsCount
+ boolean ndjson = false
+
+ switch (resultOutputStrategy) {
+ case ResultOutputStrategy.PER_QUERY:
+ flowFileCount = 1
+ hitsCount = "10"
+ ndjson = true
+ break
+ case ResultOutputStrategy.PER_HIT:
+ flowFileCount = 10
+ hitsCount = "1"
+ break
+ case ResultOutputStrategy.PER_RESPONSE:
+ flowFileCount = 1
+ hitsCount = "10"
+ break
+ }
+
+ // test _source only format
+ runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, resultOutputStrategy.getValue())
+ runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_FORMAT, SearchResultsFormat.SOURCE_ONLY.getValue())
+
+
+ // Test against each pagination type
+ for (PaginationType paginationType : PaginationType.values()) {
Review Comment:
```suggestion
for (final PaginationType paginationType : PaginationType.values()) {
```
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -265,46 +436,35 @@ abstract class AbstractPaginatedJsonQueryElasticsearchTest extends AbstractJsonQ
is(1L)
)
} else {
- assertThat(runner.getProvenanceEvents().stream().filter({ pe -> pe.getEventType() == ProvenanceEventType.SEND}).count(), is(0L))
+ assertThat(runner.getProvenanceEvents().stream().filter({ pe -> pe.getEventType() == ProvenanceEventType.SEND }).count(), is(0L))
}
}
@Test
- void testNoHitsFlowFileIsProducedForEachResultSplitSetup() {
+ void testEmptyHitsFlowFileIsProducedForEachResultSplitSetup() {
final TestRunner runner = createRunner(false)
final TestElasticsearchClientService service = getService(runner)
- runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]]])))
+ runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]], "sort": [[message: [order: "asc"]]]])))
runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.OUTPUT_NO_HITS, "true")
service.setMaxPages(0)
- // test that an empty flow file is produced for a per query setup
- runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, ResultOutputStrategy.PER_QUERY.getValue())
- runOnce(runner)
- testCounts(runner, isInput() ? 1 : 0, 1, 0, 0)
- runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "0")
- runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "1")
- runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).getSize() == 0
- reset(runner)
+ for (PaginationType paginationType : PaginationType.values()) {
+ runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, paginationType.getValue())
- // test that an empty flow file is produced for a per hit setup
- runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, ResultOutputStrategy.PER_HIT.getValue())
- runOnce(runner)
- testCounts(runner, isInput() ? 1 : 0, 1, 0, 0)
+ for (ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
Review Comment:
```suggestion
for (final ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
```
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -117,13 +117,184 @@ abstract class AbstractPaginatedJsonQueryElasticsearchTest extends AbstractJsonQ
assertSendEvent(runner, input)
}
+ @Test
+ void testSourceExtraction() throws Exception {
+ for (ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
Review Comment:
```suggestion
for (final ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
```
(I like `final`s 🙂 )
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -117,13 +117,184 @@ abstract class AbstractPaginatedJsonQueryElasticsearchTest extends AbstractJsonQ
assertSendEvent(runner, input)
}
+ @Test
+ void testSourceExtraction() throws Exception {
+ for (ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
+ final TestRunner runner = createRunner(false)
+ runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]], "sort": [[message: [order: "asc"]]]])))
+
+ int flowFileCount
+ String hitsCount
+ boolean ndjson = false
+
+ switch (resultOutputStrategy) {
+ case ResultOutputStrategy.PER_QUERY:
+ flowFileCount = 1
+ hitsCount = "10"
+ ndjson = true
+ break
+ case ResultOutputStrategy.PER_HIT:
+ flowFileCount = 10
+ hitsCount = "1"
+ break
+ case ResultOutputStrategy.PER_RESPONSE:
+ flowFileCount = 1
+ hitsCount = "10"
+ break
+ }
+
+ // test _source only format
+ runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, resultOutputStrategy.getValue())
+ runner.setProperty(AbstractJsonQueryElasticsearch.SEARCH_RESULTS_FORMAT, SearchResultsFormat.SOURCE_ONLY.getValue())
+
+
+ // Test against each pagination type
+ for (PaginationType paginationType : PaginationType.values()) {
+ runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, paginationType.getValue())
+
+ runOnce(runner)
+
+ // Test Relationship counts
+ testCounts(runner, isInput() ? 1 : 0, flowFileCount, 0, 0)
+
+ // Per response outputs an array of values
+ if (resultOutputStrategy.equals(ResultOutputStrategy.PER_RESPONSE)) {
+ runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).forEach({ hit ->
+ hit.assertAttributeEquals("hit.count", hitsCount)
+ assertOutputContent(hit.getContent(), hitsCount as int, ndjson)
+ OBJECT_MAPPER.readValue(hit.getContent(), ArrayList.class).forEach(h -> {
+ assertFalse(h.isEmpty())
+ assertFalse(h.containsKey("_source"))
+ assertFalse(h.containsKey("_index"))
+ // should be the _source content only
+ assertTrue(h.containsKey("msg"))
+
+ })
+ assertThat(
+ runner.getProvenanceEvents().stream().filter({ pe ->
+ pe.getEventType() == ProvenanceEventType.RECEIVE &&
+ pe.getAttribute("uuid") == hit.getAttribute("uuid")
+ }).count(),
+ is(1L)
+ )
+ })
+ } else {
+ runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).forEach({ hit ->
+ hit.assertAttributeEquals("hit.count", hitsCount)
+ assertOutputContent(hit.getContent(), hitsCount as int, ndjson)
+ final Map<String, Object> h = OBJECT_MAPPER.readValue(hit.getContent(), Map.class)
+ assertFalse(h.isEmpty())
+ assertFalse(h.containsKey("_source"))
+ assertFalse(h.containsKey("_index"))
+ // should be the _source content only
+ assertTrue(h.containsKey("msg"))
+
+ assertThat(
+ runner.getProvenanceEvents().stream().filter({ pe ->
+ pe.getEventType() == ProvenanceEventType.RECEIVE &&
+ pe.getAttribute("uuid") == hit.getAttribute("uuid")
+ }).count(),
+ is(1L)
+ )
+ })
+ }
+
+ reset(runner)
+ }
+ }
+ }
+
+ @Test
+ void testMetaExtraction() throws Exception {
+
+ // Test against each result strategy type
+ for (ResultOutputStrategy resultOutputStrategy : ResultOutputStrategy.values()) {
+ final TestRunner runner = createRunner(false)
+ runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, prettyPrint(toJson([query: [match_all: [:]], "sort": [[message: [order: "asc"]]]])))
+
+ int flowFileCount
+ String hitsCount
+ boolean ndjson = false
+
+ switch (resultOutputStrategy) {
+ case ResultOutputStrategy.PER_QUERY:
+ flowFileCount = 1
+ hitsCount = "10"
+ ndjson = true
+ break
+ case ResultOutputStrategy.PER_HIT:
+ flowFileCount = 10
+ hitsCount = "1"
+ break
+ case ResultOutputStrategy.PER_RESPONSE:
+ flowFileCount = 1
+ hitsCount = "10"
+ break
+ }
Review Comment:
Consider a `default` block, as above
##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/PaginatedJsonQueryElasticsearchTest.groovy:
##########
@@ -61,6 +61,21 @@ class PaginatedJsonQueryElasticsearchTest extends AbstractPaginatedJsonQueryElas
reset(runner)
+ // NIFI-11430 test that 1 flow file is output when hits output per response with output empty flow file set to true
+ runner.setProperty(AbstractJsonQueryElasticsearch.OUTPUT_NO_HITS, "true")
Review Comment:
Once a property has been set for the processor on the `runner`, it doesn't get reset/changed unless explicitly modified by the test code - the existing tests were presumably using the default of not outputting `no_hits`, the new tests are setting this to `true` and leaving it that way with little/no other assertion changes (i.e. we basically run the exact same test and aseertions twice)
Be sure to set `runner.setProperty(AbstractJsonQueryElasticsearch.OUTPUT_NO_HITS, "false")` for each of the existing sets of tests here before the `runOnce(runner)` calls
Even better, consider refactoring to loop through the different values (`OUTPUT_NO_HITS`, but possibly `SEARCH_RESULTS_SPLIT` too), similar to how you've done it in `AbstractPaginatedJsonQueryElasticsearchTest.groovy`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org