tpalfy commented on code in PR #6401:
URL: https://github.com/apache/nifi/pull/6401#discussion_r971116583


##########
nifi-nar-bundles/nifi-dropbox-bundle/nifi-dropbox-processors/src/test/java/org/apache/nifi/processors/dropbox/FetchDropboxTest.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.dropbox;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static 
org.apache.nifi.services.dropbox.StandardDropboxCredentialService.ACCESS_TOKEN;
+import static 
org.apache.nifi.services.dropbox.StandardDropboxCredentialService.APP_KEY;
+import static 
org.apache.nifi.services.dropbox.StandardDropboxCredentialService.APP_SECRET;
+import static 
org.apache.nifi.services.dropbox.StandardDropboxCredentialService.REFRESH_TOKEN;
+import static org.mockito.Mockito.when;
+
+import com.dropbox.core.DbxDownloader;
+import com.dropbox.core.DbxException;
+import com.dropbox.core.v2.DbxClientV2;
+import com.dropbox.core.v2.files.DbxUserFilesRequests;
+import com.dropbox.core.v2.files.FileMetadata;
+import java.io.ByteArrayInputStream;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.proxy.ProxyConfiguration;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.services.dropbox.StandardDropboxCredentialService;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class FetchDropboxTest {
+
+    public static final String FILE_ID_1 = "id:odTlUvbpIEAAAAAAAAAGGQ";
+    public static final String FILE_ID_2 = "id:odTlUvbpIEBBBBBBBBBGGQ";
+    public static final String FILENAME = "file_name";
+    public static final String FOLDER = "/testFolder";
+    public static final String SIZE = "125";
+    public static final String CREATED_TIME = "1659707000";
+    public static final String REVISION = "5e4ddb1320676a5c29261";
+
+    private TestRunner testRunner;
+
+    @Mock
+    private DbxClientV2 mockDropboxClient;
+
+    @Mock
+    private StandardDropboxCredentialService credentialService;
+
+    @Mock
+    private DbxUserFilesRequests mockDbxUserFilesRequest;
+
+
+    @Mock
+    private DbxDownloader<FileMetadata> mockDbxDownloader;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        FetchDropbox testSubject = new FetchDropbox() {
+            @Override
+            public DbxClientV2 getDropboxApiClient(ProcessContext context, 
ProxyConfiguration proxyConfiguration) {
+                return mockDropboxClient;
+            }
+        };
+
+        testRunner = TestRunners.newTestRunner(testSubject);
+
+        when(mockDropboxClient.files()).thenReturn(mockDbxUserFilesRequest);
+
+        mockStandardDropboxCredentialService();
+    }
+
+    @Test
+    void testFileIsDownloadedById() throws Exception {
+
+        testRunner.setProperty(FetchDropbox.FILE, "${dropbox.id}");
+
+        
when(mockDbxUserFilesRequest.download(FILE_ID_1)).thenReturn(mockDbxDownloader);
+        when(mockDbxDownloader.getInputStream()).thenReturn(new 
ByteArrayInputStream("content".getBytes(UTF_8)));
+
+        MockFlowFile inputFlowFile = getMockFlowFile(FILE_ID_1);
+        testRunner.enqueue(inputFlowFile);
+        testRunner.run();
+
+        testRunner.assertAllFlowFilesTransferred(FetchDropbox.REL_SUCCESS, 1);
+        List<MockFlowFile> flowFiles = 
testRunner.getFlowFilesForRelationship(FetchDropbox.REL_SUCCESS);
+        MockFlowFile ff0 = flowFiles.get(0);
+        ff0.assertContentEquals("content");
+        assertOutFlowFileAttributes(ff0, FILE_ID_1);
+    }
+
+    @Test
+    void testFileIsDownloadedByPath() throws Exception {
+        testRunner.setProperty(FetchDropbox.FILE, "${path}/${filename}");
+
+        when(mockDbxUserFilesRequest.download(FOLDER + "/" + 
FILENAME)).thenReturn(mockDbxDownloader);
+        when(mockDbxDownloader.getInputStream()).thenReturn(new 
ByteArrayInputStream("contentByPath".getBytes(UTF_8)));
+
+        MockFlowFile inputFlowFile = getMockFlowFile(FILE_ID_1);
+        testRunner.enqueue(inputFlowFile);
+        testRunner.run();
+
+        testRunner.assertAllFlowFilesTransferred(FetchDropbox.REL_SUCCESS, 1);
+        List<MockFlowFile> flowFiles = 
testRunner.getFlowFilesForRelationship(FetchDropbox.REL_SUCCESS);
+        MockFlowFile ff0 = flowFiles.get(0);
+        ff0.assertContentEquals("contentByPath");
+        assertOutFlowFileAttributes(ff0, FILE_ID_1);
+    }
+
+    @Test
+    void testFetchFails() throws Exception {
+        testRunner.setProperty(FetchDropbox.FILE, "${dropbox.id}");
+
+        when(mockDbxUserFilesRequest.download(FILE_ID_2)).thenThrow(new 
DbxException("Error in Dropbox"));
+
+        MockFlowFile inputFlowFile = getMockFlowFile(FILE_ID_2);
+        testRunner.enqueue(inputFlowFile);
+        testRunner.run();
+
+        testRunner.assertAllFlowFilesTransferred(FetchDropbox.REL_FAILURE, 1);
+        List<MockFlowFile> flowFiles = 
testRunner.getFlowFilesForRelationship(FetchDropbox.REL_FAILURE);
+        MockFlowFile ff0 = flowFiles.get(0);
+        ff0.assertAttributeEquals("error.message", "Error in Dropbox");
+        assertOutFlowFileAttributes(ff0, FILE_ID_2);
+    }
+
+    private void mockStandardDropboxCredentialService() throws 
InitializationException {
+        String credentialServiceId = "dropbox_credentials";
+        
when(credentialService.getIdentifier()).thenReturn(credentialServiceId);
+        testRunner.addControllerService(credentialServiceId, 
credentialService);
+        testRunner.setProperty(credentialService, APP_KEY, "appKey");
+        testRunner.setProperty(credentialService, APP_SECRET, "appSecret");
+        testRunner.setProperty(credentialService, ACCESS_TOKEN, "accessToken");
+        testRunner.setProperty(credentialService, REFRESH_TOKEN, 
"refreshToken");
+        testRunner.enableControllerService(credentialService);
+        testRunner.setProperty(FetchDropbox.CREDENTIAL_SERVICE, 
credentialServiceId);
+    }

Review Comment:
   ```suggestion
       private void mockDropboxCredentialService() throws 
InitializationException {
           String credentialServiceId = "dropbox_credentials";
           
when(credentialService.getIdentifier()).thenReturn(credentialServiceId);
           testRunner.addControllerService(credentialServiceId, 
credentialService);
           testRunner.enableControllerService(credentialService);
           testRunner.setProperty(FetchDropbox.CREDENTIAL_SERVICE, 
credentialServiceId);
       }
   ```



##########
nifi-nar-bundles/nifi-dropbox-bundle/nifi-dropbox-processors/src/main/java/org/apache/nifi/processors/dropbox/FetchDropbox.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.dropbox;
+
+import static java.lang.String.format;
+
+import com.dropbox.core.DbxException;
+import com.dropbox.core.DbxRequestConfig;
+import com.dropbox.core.oauth.DbxCredential;
+import com.dropbox.core.v2.DbxClientV2;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Pattern;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.SeeAlso;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnScheduled;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.dropbox.credentials.service.DropboxCredentialDetails;
+import org.apache.nifi.dropbox.credentials.service.DropboxCredentialService;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+
+@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED)
+@Tags({"dropbox", "storage", "fetch"})
+@CapabilityDescription("Fetches files from Dropbox. Designed to be used in 
tandem with ListDropbox.")
+@WritesAttribute(attribute = "error.message", description = "When a FlowFile 
is routed to 'failure', this attribute is added indicating why the file could "
+        + "not be fetched from Dropbox.")
+@SeeAlso(ListDropbox.class)
+@WritesAttributes(
+        @WritesAttribute(attribute = FetchDropbox.ERROR_MESSAGE_ATTRIBUTE, 
description = "The error message returned by Dropbox when the fetch of a file 
fails."))
+public class FetchDropbox extends AbstractProcessor {
+
+    public static final String ERROR_MESSAGE_ATTRIBUTE = "error.message";
+
+    public static final PropertyDescriptor FILE = new PropertyDescriptor
+            .Builder().name("file")
+            .displayName("File")
+            .description("The Dropbox identifier or path of the Dropbox file 
to fetch." +
+                    " The 'File' should match the following regular expression 
pattern: /.*|id:.* ." +
+                    " When ListDropbox is used for input, either 
'${dropbox.id}' (identify file by Dropbox id)" +
+                    " or '${path}/${filename}' (identify file by path) can be 
used as 'File' value.")

Review Comment:
   ```suggestion
                       " When ListDropbox is used for input, either 
'${dropbox.id}' (identifying files by Dropbox id)" +
                       " or '${path}/${filename}' (identifying files by path) 
can be used as 'File' value.")
   ```



##########
nifi-nar-bundles/nifi-dropbox-bundle/nifi-dropbox-processors/src/main/java/org/apache/nifi/processors/dropbox/FetchDropbox.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.dropbox;
+
+import static java.lang.String.format;
+
+import com.dropbox.core.DbxException;
+import com.dropbox.core.DbxRequestConfig;
+import com.dropbox.core.http.StandardHttpRequestor;
+import com.dropbox.core.oauth.DbxCredential;
+import com.dropbox.core.v2.DbxClientV2;
+import java.io.InputStream;
+import java.net.Proxy;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Pattern;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.SeeAlso;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnScheduled;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.dropbox.credentials.service.DropboxCredentialDetails;
+import org.apache.nifi.dropbox.credentials.service.DropboxCredentialService;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.proxy.ProxyConfiguration;
+import org.apache.nifi.proxy.ProxySpec;
+
+@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED)
+@Tags({"dropbox", "storage", "fetch"})
+@CapabilityDescription("Fetches files from Dropbox. Designed to be used in 
tandem with ListDropbox.")
+@WritesAttribute(attribute = "error.message", description = "When a FlowFile 
is routed to 'failure', this attribute is added indicating why the file could "
+        + "not be fetched from Dropbox.")
+@SeeAlso(ListDropbox.class)
+@WritesAttributes(
+        @WritesAttribute(attribute = FetchDropbox.ERROR_MESSAGE_ATTRIBUTE, 
description = "The error message returned by Dropbox when the fetch of a file 
fails."))
+public class FetchDropbox extends AbstractProcessor {
+
+    public static final String ERROR_MESSAGE_ATTRIBUTE = "error.message";
+
+    public static final PropertyDescriptor FILE = new PropertyDescriptor
+            .Builder().name("file")
+            .displayName("File")
+            .description("The Dropbox identifier or path of the Dropbox file 
to fetch." +
+                    " The 'File' should match the following regular expression 
pattern: /.*|id:.* ." +
+                    " When ListDropbox is used for input, either 
'${dropbox.id}' (identify file by Dropbox id)" +
+                    " or '${path}/${filename}' (identify file by path) can be 
used as 'File' value.")
+            .required(true)
+            .defaultValue("${dropbox.id}")
+            
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
+            .addValidator(StandardValidators.createRegexMatchingValidator(
+                    Pattern.compile("/.*|id:.*")))
+            .build();
+    public static final PropertyDescriptor CREDENTIAL_SERVICE = new 
PropertyDescriptor.Builder()
+            .name("dropbox-credential-service")
+            .displayName("Dropbox Credential Service")
+            .description("Controller Service used to obtain Dropbox 
credentials." +
+                    " See controller service's usage documentation for more 
details.")
+            .identifiesControllerService(DropboxCredentialService.class)
+            .required(true)
+            .build();
+    public static final Relationship REL_SUCCESS =
+            new Relationship.Builder()
+                    .name("success")
+                    .description("A FlowFile will be routed here for each 
successfully fetched File.")
+                    .build();
+    public static final Relationship REL_FAILURE =
+            new Relationship.Builder().name("failure")
+                    .description("A FlowFile will be routed here for each File 
for which fetch was attempted but failed.")
+                    .build();
+    public static final Set<Relationship> relationships = 
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
+            REL_SUCCESS,
+            REL_FAILURE
+    )));
+
+    private static final List<PropertyDescriptor> PROPERTIES = 
Collections.unmodifiableList(Arrays.asList(
+            CREDENTIAL_SERVICE,
+            FILE,
+            ProxyConfiguration.createProxyConfigPropertyDescriptor(false, 
ProxySpec.HTTP_AUTH)
+    ));
+
+    private DbxClientV2 dropboxApiClient;
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return relationships;
+    }
+
+    @OnScheduled
+    public void onScheduled(final ProcessContext context) {
+        final ProxyConfiguration proxyConfiguration = 
ProxyConfiguration.getConfiguration(context);
+        dropboxApiClient = getDropboxApiClient(context, proxyConfiguration);
+    }
+
+    @Override
+    public void onTrigger(ProcessContext context, ProcessSession session) 
throws ProcessException {
+        FlowFile flowFile = session.get();
+        if (flowFile == null) {
+            return;
+        }
+
+        String fileIdentifier = 
context.getProperty(FILE).evaluateAttributeExpressions(flowFile).getValue();
+        fileIdentifier = correctFilePath(fileIdentifier);
+
+        FlowFile outFlowFile = flowFile;
+        try {
+            fetchFile(fileIdentifier, session, outFlowFile);
+            session.transfer(outFlowFile, REL_SUCCESS);
+        } catch (Exception e) {
+            handleError(session, flowFile, fileIdentifier, e);
+        }
+    }
+
+    @Override
+    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {

Review Comment:
   I'd put this after `getRelationships()`



##########
nifi-nar-bundles/nifi-dropbox-bundle/nifi-dropbox-processors/src/test/java/org/apache/nifi/processors/dropbox/FetchDropboxTest.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.dropbox;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static 
org.apache.nifi.services.dropbox.StandardDropboxCredentialService.ACCESS_TOKEN;
+import static 
org.apache.nifi.services.dropbox.StandardDropboxCredentialService.APP_KEY;
+import static 
org.apache.nifi.services.dropbox.StandardDropboxCredentialService.APP_SECRET;
+import static 
org.apache.nifi.services.dropbox.StandardDropboxCredentialService.REFRESH_TOKEN;
+import static org.mockito.Mockito.when;
+
+import com.dropbox.core.DbxDownloader;
+import com.dropbox.core.DbxException;
+import com.dropbox.core.v2.DbxClientV2;
+import com.dropbox.core.v2.files.DbxUserFilesRequests;
+import com.dropbox.core.v2.files.FileMetadata;
+import java.io.ByteArrayInputStream;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.proxy.ProxyConfiguration;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.services.dropbox.StandardDropboxCredentialService;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class FetchDropboxTest {
+
+    public static final String FILE_ID_1 = "id:odTlUvbpIEAAAAAAAAAGGQ";
+    public static final String FILE_ID_2 = "id:odTlUvbpIEBBBBBBBBBGGQ";
+    public static final String FILENAME = "file_name";
+    public static final String FOLDER = "/testFolder";
+    public static final String SIZE = "125";
+    public static final String CREATED_TIME = "1659707000";
+    public static final String REVISION = "5e4ddb1320676a5c29261";
+
+    private TestRunner testRunner;
+
+    @Mock
+    private DbxClientV2 mockDropboxClient;
+
+    @Mock
+    private StandardDropboxCredentialService credentialService;

Review Comment:
   ```suggestion
       private DropboxCredentialService credentialService;
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to