Skip to content

Commit

Permalink
Feature/sftp folder bug (#1576)
Browse files Browse the repository at this point in the history
* Submit fix for issue #1575 with code coverage to assert fix
  • Loading branch information
badvision committed Nov 20, 2018
1 parent 979121c commit 5972bec
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Hashtable;
import java.util.Objects;
import java.util.Vector;
import java.util.logging.Level;
Expand Down Expand Up @@ -277,7 +276,7 @@ class SftpHierarchicalElement implements HierarchicalElement {
this.path = this.uri.getPath();
}

private SftpHierarchicalElement(String uri, ChannelSftp channel, boolean holdOpen) throws URISyntaxException, JSchException {
SftpHierarchicalElement(String uri, ChannelSftp channel, boolean holdOpen) throws URISyntaxException, JSchException {
this(uri);
this.channel = channel;
this.keepChannelOpen = holdOpen;
Expand Down Expand Up @@ -329,12 +328,16 @@ private void retrieveDetails() throws URISyntaxException, JSchException, SftpExc
if (!retrieved) {
openChannel();
SftpATTRS attributes = channel.lstat(path);
isFile = !attributes.isDir();
size = attributes.getSize();
processAttrs(attributes);
if (!keepChannelOpen) {
closeChannel();
}
}
}

private void processAttrs(SftpATTRS attrs) {
isFile = !attrs.isDir();
size = attrs.getSize();
retrieved = true;
}

Expand Down Expand Up @@ -366,7 +369,10 @@ public Stream<HierarchicalElement> getChildren() {
try {
openChannel();
Vector<ChannelSftp.LsEntry> children = channel.ls(path);
return children.stream().map(this::getChildFromEntry).filter(Objects::nonNull);
return children.stream()
.filter(this::isNotDotFolder)
.map(this::getChildFromEntry)
.filter(Objects::nonNull);
} catch (URISyntaxException | JSchException | SftpException ex) {
Logger.getLogger(FileAssetIngestor.class.getName()).log(Level.SEVERE, null, ex);
return Stream.empty();
Expand All @@ -377,10 +383,16 @@ public Stream<HierarchicalElement> getChildren() {
}
}

private boolean isNotDotFolder(ChannelSftp.LsEntry entry) {
return !(".".equals(entry.getFilename()) || "..".equals(entry.getFilename()));
}

private HierarchicalElement getChildFromEntry(ChannelSftp.LsEntry entry) {
try {
String childPath = getSourcePath() + "/" + entry.getFilename();
return (HierarchicalElement) new SftpHierarchicalElement(childPath, channel, true);
SftpHierarchicalElement child = new SftpHierarchicalElement(childPath, channel, true);
child.processAttrs(entry.getAttrs());
return child;
} catch (URISyntaxException | JSchException ex) {
Logger.getLogger(FileAssetIngestor.class.getName()).log(Level.SEVERE, null, ex);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
*/
package com.adobe.acs.commons.mcp.impl.processes.asset;

import com.adobe.acs.commons.mcp.impl.processes.asset.FileAssetIngestor;
import com.adobe.acs.commons.fam.ActionManager;
import com.adobe.acs.commons.functions.CheckedConsumer;
import com.day.cq.dam.api.AssetManager;
import com.google.common.base.Function;
import com.google.common.io.Files;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.JcrConstants;
import org.apache.sling.api.resource.PersistenceException;
Expand All @@ -52,6 +54,8 @@
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;
import java.util.stream.Collectors;

import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
Expand Down Expand Up @@ -285,11 +289,36 @@ public void testSftpStructures() throws URISyntaxException, JSchException {
assertEquals("/content/dam", elem2.getNodePath());
assertEquals("path", elem2.getNodeName());
}

@Test
public void testSftpRecursion() throws URISyntaxException, JSchException, SftpException {
String baseUri = "sftp://user:password@host/test/path";
ingestor.fileBasePath = baseUri;
ChannelSftp channel = mock(ChannelSftp.class);
when(channel.isConnected()).thenReturn(true);
when(channel.getSession()).thenReturn(mock(Session.class));

Vector<ChannelSftp.LsEntry> entries = (new MockDirectoryBuilder())
.addDirectory(".")
.addDirectory("..")
.addFile("file1.png", 1234L)
.addFile("file2.png", 4567L)
.asVector();
when(channel.ls(anyObject())).thenReturn(entries);

FileAssetIngestor.SftpHierarchicalElement elem1 = ingestor.new SftpHierarchicalElement(baseUri, channel, false);
int count = 0;
for (HierarchicalElement e : elem1.getChildren().collect(Collectors.toList())) {
count++;
assertTrue("Expected file name", e.getName().equals("file1.png") || e.getName().equals("file2.png"));
assertTrue("Expected isFile for " + e.getName(), e.isFile());
}
assertEquals("Expected only two files", 2, count);
}

private File addFile(File dir, String name, String resourcePath) throws IOException {
File newFile = new File(dir, name);
FileUtils.copyInputStreamToFile(getClass().getResourceAsStream(resourcePath), newFile);
return newFile;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2018 Adobe
* %%
* Licensed 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.
* #L%
*/
package com.adobe.acs.commons.mcp.impl.processes.asset;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.SftpATTRS;
import java.util.ArrayList;
import java.util.Vector;

import static org.mockito.Mockito.*;

/**
* Useful in building up a mock directory for SFTP mock testing
*/
public class MockDirectoryBuilder {

ArrayList<LsEntry> directory = new ArrayList<>();

ChannelSftp sftp = new ChannelSftp();

String currentDirectory = "";

public MockDirectoryBuilder atFolder(String base) {
currentDirectory = base;
return this;
}

public MockDirectoryBuilder addFile(String name, Long size) {
String longName = currentDirectory + "/" + name;

SftpATTRS attrs = mock(SftpATTRS.class);
when(attrs.isDir()).thenReturn(false);
when(attrs.getSize()).thenReturn(size);
LsEntry entry = mock(LsEntry.class);
when(entry.getFilename()).thenReturn(name);
when(entry.getLongname()).thenReturn(longName);
when(entry.getAttrs()).thenReturn(attrs);
directory.add(entry);

return this;
}

public MockDirectoryBuilder addDirectory(String name) {
String longName = currentDirectory + "/" + name;

SftpATTRS attrs = mock(SftpATTRS.class);
when(attrs.isDir()).thenReturn(true);
LsEntry entry = mock(LsEntry.class);
when(entry.getFilename()).thenReturn(name);
when(entry.getLongname()).thenReturn(longName);
when(entry.getAttrs()).thenReturn(attrs);
directory.add(entry);

return this;
}

public Vector<LsEntry> asVector() {
return new Vector<>(directory);
}
}

0 comments on commit 5972bec

Please sign in to comment.