Skip to content

Commit

Permalink
Fixes #432 (more or less)
Browse files Browse the repository at this point in the history
  • Loading branch information
Riduidel committed May 16, 2024
1 parent fef9e08 commit 5be8208
Show file tree
Hide file tree
Showing 19 changed files with 352 additions and 56 deletions.
3 changes: 2 additions & 1 deletion architecture-documentation/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.Riduidel.aadarchi</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.commons.vfs2.FileSelector;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.ndx.aadarchi.base.enhancers.ModelElementKeys;
import org.ndx.aadarchi.base.enhancers.scm.SCMHandler;
import org.ndx.aadarchi.base.utils.StructurizrUtils;
Expand All @@ -38,6 +39,8 @@ public class FileObjectDetector {
@Inject FileSystemManager fileSystemManager;
@Inject Instance<SCMHandler> scmHandlers;

@Inject FileSystemOptions fileSystemOptions;

/**
* Perform the given success operation when file is detected.
*
Expand Down Expand Up @@ -74,7 +77,7 @@ public <Returned> Optional<Returned> whenFileDetected(Element element,
String localPath = properties.get(ModelElementKeys.ConfigProperties.BasePath.NAME);
try {
analyzedPath = Optional.ofNullable(
fileSystemManager.resolveFile(localPath));
fileSystemManager.resolveFile(localPath, fileSystemOptions));
} catch (FileSystemException e) {
logger.log(Level.SEVERE, String.format("Unable to resolve path to %s", localPath), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.ndx.aadarchi.base.utils.commonsvfs;

import java.util.function.Consumer;

import org.apache.commons.vfs2.FileSystemOptions;

@FunctionalInterface
public interface FileSystemOptionsConfigurer extends Consumer<FileSystemOptions>{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.ndx.aadarchi.base.utils.commonsvfs;

import org.apache.commons.vfs2.FileSystemOptions;

import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

public class FileSystemOptionsProducer {

@Inject Instance<FileSystemOptionsConfigurer> configurers;
@Produces @Singleton FileSystemOptions createOptions(Instance<FileSystemOptions> updaters) {
FileSystemOptions globalOptions = new FileSystemOptions();
configurers.stream()
.forEach(configurer -> configurer.accept(globalOptions));
return globalOptions;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import org.ndx.aadarchi.base.enhancers.scm.SCMHandler;
import org.ndx.aadarchi.base.utils.FileContentCache;
import org.ndx.aadarchi.base.utils.icon.FontIcon;
import org.ndx.aadarchi.github.vfs.GitHubFileSystemProvider;
import org.ndx.aadarchi.github.vfs.GitHubFileSystemOptionsConfigurer;
import org.ndx.aadarchi.github.vfs.GitHubRootProvider;
import org.ndx.aadarchi.gitlab.GitOperator;

import com.structurizr.annotation.Component;
Expand All @@ -33,7 +34,8 @@ public class GithubSCMHandler implements SCMHandler {
@Inject FileContentCache fileCache;
@Inject @Named("github") Instance<GitOperator> cloner;
@Inject @FontIcon(name="github") String githubIcon;
@Inject GitHubFileSystemProvider gitHubFileSystem;
@Inject GitHubFileSystemOptionsConfigurer gitHubFileSystem;
@Inject GitHubRootProvider githubRootProvider;
@Override
public boolean canHandle(String project) {
return Constants.isGitHubProject(project);
Expand Down Expand Up @@ -71,7 +73,7 @@ public void checkout(String projectUrl, File checkoutLocation) throws IOExceptio
@Override
public FileObject getProjectRoot(String project) {
try {
return gitHubFileSystem.getProjectRoot(project);
return githubRootProvider.getProjectRoot(project);
} catch (FileSystemException e) {
throw new GitHubHandlerException("Unable to obtain VFS", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
import org.ndx.aadarchi.base.utils.commonsvfs.FileSystemOptionsConfigurer;
import org.ndx.aadarchi.cdi.deltaspike.ConfigProperty;
import org.ndx.aadarchi.vfs.github.GitHubFileProvider;

Expand All @@ -18,19 +19,14 @@
* @author nicolas-delsaux
*
*/
public class GitHubFileSystemProvider {
public class GitHubFileSystemOptionsConfigurer implements FileSystemOptionsConfigurer {
@Inject FileSystemManager fileSystemManager;
private FileSystemOptions authenticationOptions;
@Inject @ConfigProperty(name=CONFIG_GITHUB_TOKEN) String token;

@Inject
public void initializeAuthentication(@ConfigProperty(name=CONFIG_GITHUB_TOKEN) String token) {
StaticUserAuthenticator auth = new StaticUserAuthenticator("github.com",
null, token);
authenticationOptions = new FileSystemOptions();
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(authenticationOptions, auth);
}
public FileObject getProjectRoot(String project) throws FileSystemException {
return fileSystemManager.resolveFile(GitHubFileProvider.urlFor(project), authenticationOptions);
@Override
public void accept(FileSystemOptions opts) {
StaticUserAuthenticator auth = new StaticUserAuthenticator("github.com", null, token);
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.ndx.aadarchi.github.vfs;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.ndx.aadarchi.vfs.github.GitHubFileProvider;

import jakarta.inject.Inject;

public class GitHubRootProvider {
@Inject FileSystemManager fileSystemManager;
@Inject FileSystemOptions options;
public FileObject getProjectRoot(String project) throws FileSystemException {
return fileSystemManager.resolveFile(GitHubFileProvider.urlFor(project), options);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,57 @@
package org.ndx.aadarchi.github.vfs;

import jakarta.inject.Inject;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.filter.RegexFileFilter;
import org.assertj.core.api.Assertions;
import org.jboss.weld.junit5.EnableWeld;
import org.jboss.weld.junit5.WeldInitiator;
import org.jboss.weld.junit5.WeldSetup;
import org.junit.jupiter.api.Test;
import org.ndx.aadarchi.base.enhancers.ModelElementKeys;
import org.ndx.aadarchi.base.utils.commonsvfs.FileObjectDetector;

import com.structurizr.Workspace;
import com.structurizr.model.SoftwareSystem;

import jakarta.inject.Inject;

@EnableWeld
class GitHubFileSystemProviderTest {
@WeldSetup
public WeldInitiator weld = WeldInitiator.performDefaultDiscovery();

@Inject GitHubFileSystemProvider gitHubFileSystem;
@Inject GitHubFileSystemOptionsConfigurer gitHubFileSystem;
@Inject FileSystemManager fileSystemManager;
@Inject GitHubRootProvider gitHubRootProvider;
@Inject FileObjectDetector detector;

@Test
void test() throws FileSystemException {
// Given
FileObject repositoryRoot = gitHubFileSystem.getProjectRoot("Riduidel/aadarchi");
FileObject repositoryRoot = gitHubRootProvider.getProjectRoot("Riduidel/aadarchi");
// When
FileObject readme = repositoryRoot.getChild("README.md");
// Then
Assertions.assertThat((Object) readme).isNotNull();
Assertions.assertThat(readme.getContent().getSize()).isGreaterThan(100);
}


@Test
void bug_432_is_resolved() throws FileSystemException {
// Given
var w = new Workspace(getClass().getName(), "a test workspace");
SoftwareSystem system = w.getModel().addSoftwareSystem("The system which has an associated file to read");
system.addProperty(ModelElementKeys.ConfigProperties.BasePath.NAME, "github://github.com/Riduidel/aadarchi");
// When
detector.whenFileDetected(system,
new RegexFileFilter("(readme|README)\\.(adoc|md)"),
// Then
elementRoot -> { Assertions.fail("We should detect one readme file"); },
(elementRoot, readme) -> Assertions.assertThat((Object) readme).isNotNull(),
(elementRoot, files) -> Assertions.fail("We should detect one readme file"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestFor345_Fail_When_Using_Repository_Path {
}

@Test
void can_have_file_object_when_repo_path_is_given() throws FileSystemException {
void can_get_file_when_calling_vfs() throws FileSystemException {
FileObject rootFile = VFS.getManager().resolveFile(GitHubFileProvider.urlFor("https://github.com/Riduidel/aadarchi.git"), authenticationOptions);
Assertions.assertThat((Object) rootFile).isNotNull();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import org.gitlab4j.api.GitLabApiException;
import org.ndx.aadarchi.base.enhancers.scm.SCMHandler;
import org.ndx.aadarchi.base.utils.icon.FontIcon;
import org.ndx.aadarchi.gitlab.vfs.GitLabFileSystemProvider;
import org.ndx.aadarchi.gitlab.vfs.GitLabFileSystemOptionsConfigurer;
import org.ndx.aadarchi.gitlab.vfs.GitLabRootProvider;

import com.structurizr.annotation.Component;

Expand All @@ -22,10 +23,12 @@ public class GitlabSCMHandler implements SCMHandler {
private @Inject GitLabContainer gitlab;
@Inject @Named("gitlab") Instance<GitOperator> cloner;
@Inject
GitLabFileSystemProvider gitlabFileSystem;
GitLabFileSystemOptionsConfigurer gitlabFileSystem;
@Inject
@FontIcon(name = "gitlab")
String gitlabIcon;
@Inject
private GitLabRootProvider gitlabRootProvider;

@Override
public boolean canHandle(String project) {
Expand Down Expand Up @@ -57,7 +60,7 @@ public void checkout(String projectUrl, File checkoutLocation) throws IOExceptio
@Override
public FileObject getProjectRoot(String project) {
try {
return gitlabFileSystem.getProjectRoot(project);
return gitlabRootProvider.getProjectRoot(project);
} catch (FileSystemException e) {
throw new GitLabHandlerException("Unable to obtain VFS", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,36 @@
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
import org.ndx.aadarchi.base.utils.commonsvfs.FileSystemOptionsConfigurer;
import org.ndx.aadarchi.cdi.deltaspike.ConfigProperty;
import org.ndx.aadarchi.gitlab.Constants;
import org.ndx.aadarchi.vfs.gitlab.GitLabFileProvider;

import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import jakarta.inject.Named;

/**
* Load a pseudo-http file system dedicated to access to GitLab (by using our GitLab API)
* Load a pseudo-http file system dedicated to access to GitLab (by using our
* GitLab API)
*
* @author nicolas-delsaux
*
*/
public class GitLabFileSystemProvider {
@Inject FileSystemManager fileSystemManager;
private FileSystemOptions authenticationOptions;
private String gitlabServer;

public class GitLabFileSystemOptionsConfigurer implements FileSystemOptionsConfigurer {
@Inject
public void initializeAuthentication(
@ConfigProperty(name = Constants.CONFIG_GITLAB_TOKEN) String token,
@ConfigProperty(name = Constants.CONFIG_GITLAB_URL, defaultValue = "gitlab.com") String gitlabUrl) {
StaticUserAuthenticator auth = new StaticUserAuthenticator(gitlabUrl,
null, token);
authenticationOptions = new FileSystemOptions();
this.gitlabServer = gitlabUrl;
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(authenticationOptions, auth);
}
@ConfigProperty(name = Constants.CONFIG_GITLAB_TOKEN)
String token;
@Inject
@ConfigProperty(name = Constants.CONFIG_GITLAB_URL, defaultValue = "gitlab.com")
String gitlabUrl;
@Inject
FileSystemManager fileSystemManager;

public FileObject getProjectRoot(String project) throws FileSystemException {
return fileSystemManager.resolveFile(GitLabFileProvider.urlFor(gitlabServer, project), authenticationOptions);
@Override
public void accept(FileSystemOptions opts) {
StaticUserAuthenticator auth = new StaticUserAuthenticator(gitlabUrl, null, token);
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.ndx.aadarchi.gitlab.vfs;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.ndx.aadarchi.cdi.deltaspike.ConfigProperty;
import org.ndx.aadarchi.gitlab.Constants;
import org.ndx.aadarchi.vfs.gitlab.GitLabFileProvider;

import jakarta.inject.Inject;

public class GitLabRootProvider {
@Inject FileSystemManager fileSystemManager;
@Inject FileSystemOptions fileSystemOptions;
@Inject @ConfigProperty(name = Constants.CONFIG_GITLAB_URL, defaultValue = "gitlab.com") String gitlabUrl;

public FileObject getProjectRoot(String project) throws FileSystemException {
return fileSystemManager.resolveFile(GitLabFileProvider.urlFor(gitlabUrl, project), fileSystemOptions);
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package org.ndx.aadarchi.gitlab.vfs;

import jakarta.inject.Inject;

import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.vfs2.FileFilterSelector;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.filter.RegexFileFilter;
import org.assertj.core.api.Assertions;
import org.jboss.weld.junit5.EnableWeld;
import org.jboss.weld.junit5.WeldInitiator;
import org.jboss.weld.junit5.WeldSetup;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.ndx.aadarchi.base.enhancers.ModelElementKeys;
import org.ndx.aadarchi.base.utils.commonsvfs.FileObjectDetector;
import org.ndx.aadarchi.gitlab.Constants;
import org.ndx.aadarchi.gitlab.GitlabSCMHandler;

import com.structurizr.Workspace;
import com.structurizr.model.SoftwareSystem;

import jakarta.inject.Inject;

@EnableWeld
class GitLabFileSystemProviderTest {
@WeldSetup
public WeldInitiator weld = WeldInitiator.performDefaultDiscovery();

@Inject GitLabFileSystemProvider gitLabFileSystem;
@Inject GitLabRootProvider gitLabRootProvider;
@Inject FileObjectDetector detector;

@BeforeAll public static void setGitlabServer() {
System.setProperty(Constants.CONFIG_GITLAB_URL, "framagit.org");
Expand All @@ -29,12 +37,27 @@ class GitLabFileSystemProviderTest {
@Test
void can_get_readme_from_gitlab_repo() throws FileSystemException {
// Given
FileObject repositoryRoot = gitLabFileSystem.getProjectRoot("Riduidel/codingame-maven-plugins");
FileObject repositoryRoot = gitLabRootProvider.getProjectRoot("Riduidel/codingame-maven-plugins");
// When
FileObject readme = repositoryRoot.getChild("README.adoc");
// Then
Assertions.assertThat((Object) readme).isNotNull();
Assertions.assertThat(readme.getContent().getSize()).isGreaterThan(100);
}

@Test
void bug_432_is_resolved() throws FileSystemException {
// Given
var w = new Workspace(getClass().getName(), "a test workspace");
SoftwareSystem system = w.getModel().addSoftwareSystem("The system which has an associated file to read");
system.addProperty(ModelElementKeys.ConfigProperties.BasePath.NAME, "gitlab://framagit.org/Riduidel/aadarchi");
// When
detector.whenFileDetected(system,
new RegexFileFilter("(readme|README)\\.(adoc|md)"),
// Then
elementRoot -> { Assertions.fail("We should detect one readme file"); },
(elementRoot, readme) -> Assertions.assertThat((Object) readme).isNotNull(),
(elementRoot, files) -> Assertions.fail("We should detect one readme file"));
}

}
Loading

0 comments on commit 5be8208

Please sign in to comment.