Skip to content

Commit 4ffe5ae

Browse files
ncover21pvillard31
authored andcommitted
FLOW-14362: Adding not.found relationship to ListBoxFileInfo processor
Signed-off-by: Pierre Villard <[email protected]> This closes #9802.
1 parent d46ee93 commit 4ffe5ae

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

nifi-extension-bundles/nifi-box-bundle/nifi-box-processors/src/main/java/org/apache/nifi/processors/box/ListBoxFileInfo.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,15 @@ public class ListBoxFileInfo extends AbstractProcessor {
123123
.description("A FlowFile will be routed here if there is an error fetching file metadata from the folder.")
124124
.build();
125125

126+
public static final Relationship REL_NOT_FOUND = new Relationship.Builder()
127+
.name("not.found")
128+
.description("FlowFiles for which the specified Box folder was not found will be routed to this relationship.")
129+
.build();
130+
126131
public static final Set<Relationship> RELATIONSHIPS = Set.of(
127132
REL_SUCCESS,
128-
REL_FAILURE
133+
REL_FAILURE,
134+
REL_NOT_FOUND
129135
);
130136

131137
private static final List<PropertyDescriptor> PROPERTY_DESCRIPTORS = List.of(
@@ -233,11 +239,16 @@ public void onTrigger(final ProcessContext context, final ProcessSession session
233239
}
234240

235241
} catch (final BoxAPIResponseException e) {
236-
getLogger().error("Couldn't fetch files from folder with id [{}]", folderId, e);
237242
flowFile = session.putAttribute(flowFile, ERROR_CODE, valueOf(e.getResponseCode()));
238243
flowFile = session.putAttribute(flowFile, ERROR_MESSAGE, e.getMessage());
239-
flowFile = session.penalize(flowFile);
240-
session.transfer(flowFile, REL_FAILURE);
244+
if (e.getResponseCode() == 404) {
245+
getLogger().warn("Box folder with ID {} was not found.", folderId);
246+
session.transfer(flowFile, REL_NOT_FOUND);
247+
} else {
248+
getLogger().error("Couldn't fetch files from folder with id [{}]", folderId, e);
249+
flowFile = session.penalize(flowFile);
250+
session.transfer(flowFile, REL_FAILURE);
251+
}
241252
}
242253
}
243254

nifi-extension-bundles/nifi-box-bundle/nifi-box-processors/src/test/java/org/apache/nifi/processors/box/ListBoxFileInfoTest.java

+33-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void testProcessingMultipleFiles() {
146146
void testBoxAPIResponseException() {
147147
testRunner.setProperty(ListBoxFileInfo.FOLDER_ID, TEST_FOLDER_ID);
148148

149-
final BoxAPIResponseException apiException = new BoxAPIResponseException("API Error", 404, "Not Found", null);
149+
final BoxAPIResponseException apiException = new BoxAPIResponseException("API Error", 500, "Internal Server Error", null);
150150
doThrow(apiException).when(mockBoxFolder).getChildren(
151151
"id",
152152
"name",
@@ -161,16 +161,46 @@ void testBoxAPIResponseException() {
161161
final MockFlowFile inputFlowFile = new MockFlowFile(0);
162162
testRunner.enqueue(inputFlowFile);
163163
testRunner.run();
164-
165164
testRunner.assertTransferCount(ListBoxFileInfo.REL_FAILURE, 1);
166165
testRunner.assertTransferCount(ListBoxFileInfo.REL_SUCCESS, 0);
166+
testRunner.assertTransferCount(ListBoxFileInfo.REL_NOT_FOUND, 0);
167167

168168
final List<MockFlowFile> failureFiles = testRunner.getFlowFilesForRelationship(ListBoxFileInfo.REL_FAILURE);
169169
final MockFlowFile failureFlowFile = failureFiles.getFirst();
170-
failureFlowFile.assertAttributeEquals(BoxFileAttributes.ERROR_CODE, "404");
170+
failureFlowFile.assertAttributeEquals(BoxFileAttributes.ERROR_CODE, "500");
171171
failureFlowFile.assertAttributeExists(ERROR_MESSAGE);
172172
}
173173

174+
@Test
175+
void testBoxAPIResponseExceptionNotFound() {
176+
testRunner.setProperty(ListBoxFileInfo.FOLDER_ID, TEST_FOLDER_ID);
177+
178+
final BoxAPIResponseException apiException = new BoxAPIResponseException("API Error", 404, "Not Found", null);
179+
doThrow(apiException).when(mockBoxFolder).getChildren(
180+
"id",
181+
"name",
182+
"item_status",
183+
"size",
184+
"created_at",
185+
"modified_at",
186+
"content_created_at",
187+
"content_modified_at",
188+
"path_collection");
189+
190+
final MockFlowFile inputFlowFile = new MockFlowFile(0);
191+
testRunner.enqueue(inputFlowFile);
192+
testRunner.run();
193+
194+
testRunner.assertTransferCount(ListBoxFileInfo.REL_FAILURE, 0);
195+
testRunner.assertTransferCount(ListBoxFileInfo.REL_SUCCESS, 0);
196+
testRunner.assertTransferCount(ListBoxFileInfo.REL_NOT_FOUND, 1);
197+
198+
final List<MockFlowFile> notFoundFiles = testRunner.getFlowFilesForRelationship(ListBoxFileInfo.REL_NOT_FOUND);
199+
final MockFlowFile notFoundFlowFile = notFoundFiles.getFirst();
200+
notFoundFlowFile.assertAttributeEquals(BoxFileAttributes.ERROR_CODE, "404");
201+
notFoundFlowFile.assertAttributeExists(ERROR_MESSAGE);
202+
}
203+
174204
private void mockMultipleFilesResponse() {
175205
List<String> pathParts = Arrays.asList("path", "to", "file");
176206

0 commit comments

Comments
 (0)