Skip to content

Commit 2610c5c

Browse files
authored
NIFI-15523 Fixed Provenance EventIterator factory not returning items (#10837)
- EventIterator created using the `of` factory would return Optional.empty() when there are actually items in the internal iterator - Flipping the ternary result expressions fixed this issue Signed-off-by: David Handermann <exceptionfactory@apache.org>
1 parent ca768ef commit 2610c5c

File tree

2 files changed

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

2 files changed

+71
-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,67 @@
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.ProvenanceEventRecord;
21+
import org.apache.nifi.provenance.TestUtil;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.IOException;
25+
import java.util.Objects;
26+
import java.util.Optional;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
31+
class EventIteratorTest {
32+
33+
@Test
34+
void testCanCreateAndRetrieveUsingOfFactory() throws IOException {
35+
ProvenanceEventRecord event = TestUtil.createEvent();
36+
EventIterator eventIterator = EventIterator.of(event);
37+
Optional<ProvenanceEventRecord> foundEvent = eventIterator.nextEvent();
38+
assertTrue(foundEvent.isPresent());
39+
assertEquals(foundEvent.get().getAttribute("uuid"), event.getAttribute("uuid"));
40+
}
41+
42+
@Test
43+
void testEmptyReturnedWhenExhausted() throws IOException {
44+
EventIterator eventIterator = EventIterator.of(TestUtil.createEvent());
45+
assertTrue(eventIterator.nextEvent().isPresent());
46+
assertTrue(eventIterator.nextEvent().isEmpty());
47+
}
48+
49+
@Test
50+
void testCanFilterEvents() throws IOException {
51+
ProvenanceEventRecord eventOne = TestUtil.createEvent();
52+
ProvenanceEventRecord eventTwo = TestUtil.createEvent();
53+
54+
EventIterator eventIterator = EventIterator.of(eventOne, eventTwo);
55+
// Filter out the first event
56+
EventIterator filteredIterator = eventIterator.filter((e) -> !Objects.equals(e.getAttribute("uuid"), eventOne.getAttribute("uuid")));
57+
58+
ProvenanceEventRecord foundEvent = filteredIterator.nextEvent().orElseThrow();
59+
assertEquals(foundEvent.getAttribute("uuid"), eventTwo.getAttribute("uuid"));
60+
}
61+
62+
@Test
63+
void testEmptyFactoryIsEmpty() throws IOException {
64+
EventIterator eventIterator = EventIterator.EMPTY;
65+
assertTrue(eventIterator.nextEvent().isEmpty());
66+
}
67+
}

0 commit comments

Comments
 (0)