Skip to content

Commit 8841d27

Browse files
committed
NIFI-15523: Fix EventIterator of factory not returning items
An EventIterator created using the `of` factory would return Optional.empty() when there are actually items in the internal iterator. Flipping the ternary result expressions fixes this issue. Added some basic tests to exercise the two factories and the default filter implementation.
1 parent 2aa11aa commit 8841d27

File tree

2 files changed

+68
-5
lines changed
  • nifi-framework-bundle/nifi-framework-extensions/nifi-provenance-repository-bundle/nifi-persistent-provenance-repository/src

2 files changed

+68
-5
lines changed

nifi-framework-bundle/nifi-framework-extensions/nifi-provenance-repository-bundle/nifi-persistent-provenance-repository/src/main/java/org/apache/nifi/provenance/store/iterator/EventIterator.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public interface EventIterator extends Closeable {
3333

3434
EventIterator EMPTY = new EventIterator() {
3535
@Override
36-
public void close() throws IOException {
36+
public void close() {
3737
}
3838

3939
@Override
@@ -46,17 +46,16 @@ static EventIterator of(final ProvenanceEventRecord... events) {
4646
final Iterator<ProvenanceEventRecord> itr = Arrays.asList(events).iterator();
4747
return new EventIterator() {
4848
@Override
49-
public void close() throws IOException {
49+
public void close() {
5050
}
5151

5252
@Override
5353
public Optional<ProvenanceEventRecord> nextEvent() {
54-
return itr.hasNext() ? Optional.empty() : Optional.of(itr.next());
54+
return itr.hasNext() ? Optional.of(itr.next()) : Optional.empty();
5555
}
5656
};
5757
}
5858

59-
6059
default EventIterator filter(Predicate<ProvenanceEventRecord> predicate) {
6160
final EventIterator self = this;
6261

@@ -70,7 +69,7 @@ public void close() throws IOException {
7069
public Optional<ProvenanceEventRecord> nextEvent() throws IOException {
7170
while (true) {
7271
Optional<ProvenanceEventRecord> next = self.nextEvent();
73-
if (!next.isPresent()) {
72+
if (next.isEmpty()) {
7473
return next;
7574
}
7675

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.nifi.provenance.store.iterator;
19+
20+
import org.apache.nifi.provenance.TestUtil;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.io.IOException;
24+
import java.util.Objects;
25+
26+
import static org.junit.jupiter.api.Assertions.*;
27+
28+
class EventIteratorTest {
29+
30+
@Test
31+
void testCanCreateAndRetrieveUsingOfFactory() throws IOException{
32+
var event = TestUtil.createEvent();
33+
var eventIterator = EventIterator.of(event);
34+
var foundEvent = eventIterator.nextEvent();
35+
assertTrue(foundEvent.isPresent());
36+
assertEquals(foundEvent.get().getAttribute("uuid"), event.getAttribute("uuid"));
37+
}
38+
39+
@Test
40+
void testEmptyReturnedWhenExhausted() throws IOException {
41+
var eventIterator = EventIterator.of(TestUtil.createEvent());
42+
assertTrue(eventIterator.nextEvent().isPresent());
43+
assertTrue(eventIterator.nextEvent().isEmpty());
44+
}
45+
46+
@Test
47+
void testCanFilterEvents() throws IOException{
48+
var eventOne = TestUtil.createEvent();
49+
var eventTwo = TestUtil.createEvent();
50+
51+
var eventIterator = EventIterator.of(eventOne, eventTwo);
52+
// Filter out the first event
53+
var filteredIterator = eventIterator.filter((e) -> !Objects.equals(e.getAttribute("uuid"), eventOne.getAttribute("uuid")));
54+
55+
var foundEvent = filteredIterator.nextEvent().orElseThrow();
56+
assertEquals(foundEvent.getAttribute("uuid"), eventTwo.getAttribute("uuid"));
57+
}
58+
59+
@Test
60+
void testEmptyFactoryIsEmpty() throws IOException {
61+
var eventIterator = EventIterator.EMPTY;
62+
assertTrue(eventIterator.nextEvent().isEmpty());
63+
}
64+
}

0 commit comments

Comments
 (0)