Skip to content

Commit

Permalink
devonfw#1454 connect ArtifactRetriever with GUI
Browse files Browse the repository at this point in the history
changed setupHeadlessMode from BeforeClass to Before
moved temporary test folder to TestFXBase
added first ArtifactRetriever logic to MenuController
changed JavaFX version from 18 to 19
  • Loading branch information
jan-vcapgemini committed Mar 15, 2023
1 parent 19992cc commit d5b63dd
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.devonfw.cobigen.retriever;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.api.util.TemplatesJarUtil;
import com.devonfw.cobigen.impl.config.entity.io.TemplateSetConfiguration;
import com.devonfw.cobigen.impl.config.reader.TemplateSetConfigurationReader;
import com.devonfw.cobigen.retriever.mavensearch.MavenSearchArtifactRetriever;
Expand All @@ -33,6 +33,10 @@ public class ArtifactRetriever {

private static final Logger LOG = LoggerFactory.getLogger(ArtifactRetriever.class);

/** Path to artifact cache folder **/
private static Path artifactCachePath = CobiGenPaths.getTemplateSetsFolderPath()
.resolve(ConfigurationConstants.TEMPLATE_SET_ARTIFACT_CACHE_FOLDER);

/**
* Retrieves a list of maven artifact download URLs
*
Expand All @@ -41,7 +45,7 @@ public class ArtifactRetriever {
* @return list of maven artifact download URLs
*
*/
protected static List<URL> retrieveTemplateSetXmlDownloadLinks(List<String> groupIdsList, String mavenSettings) {
public static List<URL> retrieveTemplateSetXmlDownloadLinks(List<String> groupIdsList, String mavenSettings) {

List<URL> downloadLinks = new ArrayList<>();

Expand Down Expand Up @@ -73,6 +77,23 @@ protected static List<URL> retrieveTemplateSetXmlDownloadLinks(List<String> grou

}

/**
* Downloads template set artifacts from given URLs
*
* @param artifactUrls List of URLs
* @return List of artifact Paths
*/
public static List<Path> downloadArtifactsFromUrls(List<URL> artifactUrls) {

List<Path> artifactPaths = new ArrayList<>();
for (URL url : artifactUrls) {
artifactPaths.add(TemplatesJarUtil.downloadFile(url.toString(), artifactCachePath));

}

return artifactPaths;
}

/**
* Retrieves {@link TemplateSetArtifactReader}s taken from template-set files providing human readable data only
*
Expand All @@ -99,30 +120,25 @@ public static List<TemplateSet> retrieveTemplateSetData(List<Path> templateSetFi
/**
* Retrieves a list of {@link TemplateSetConfiguration} from the template set artifact cache
*
* @param cachedArtifacts List of template set artifact paths
*
* @return List of {@link TemplateSetConfiguration}
*/
public static List<TemplateSetConfiguration> retrieveArtifactsFromCache() {

Path templateSetFolder = CobiGenPaths.getTemplateSetsFolderPath();

Path artifactCacheFolder = templateSetFolder.resolve(ConfigurationConstants.TEMPLATE_SET_ARTIFACT_CACHE_FOLDER);
public static List<TemplateSetConfiguration> retrieveArtifactsFromCache(List<Path> cachedArtifacts) {

List<TemplateSetConfiguration> templateSetConfigurations = new ArrayList<>();
if (Files.exists(artifactCacheFolder)) {
for (File file : artifactCacheFolder.toFile().listFiles()) {
TemplateSetConfigurationReader reader = new TemplateSetConfigurationReader();
reader.readConfiguration(file.toPath());

TemplateSetConfiguration templateSetConfiguration = reader.getTemplateSetConfiguration();
templateSetConfigurations.add(templateSetConfiguration);
}
} else {
try {
Files.createDirectory(artifactCacheFolder);
} catch (IOException e) {
throw new CobiGenRuntimeException("Could not create artifact cache folder!", e);
}
if (cachedArtifacts == null) {
List<File> artfactList = Arrays.asList(CobiGenPaths.getTemplateSetsFolderPath()
.resolve(ConfigurationConstants.TEMPLATE_SET_ARTIFACT_CACHE_FOLDER).toFile().listFiles());
}

for (Path file : cachedArtifacts) {
TemplateSetConfigurationReader reader = new TemplateSetConfigurationReader();
reader.readConfiguration(file);

TemplateSetConfiguration templateSetConfiguration = reader.getTemplateSetConfiguration();
templateSetConfigurations.add(templateSetConfiguration);
}

return templateSetConfigurations;
Expand Down
2 changes: 1 addition & 1 deletion cobigen/gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<java.version>11</java.version>
<javafx.version>18</javafx.version>
<javafx.version>19</javafx.version>
<javafx.maven.plugin.version>0.0.6</javafx.maven.plugin.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.api.util.MavenUtil;
import com.devonfw.cobigen.gui.Controller;
import com.devonfw.cobigen.gui.model.TemplateSetModel;
import com.devonfw.cobigen.gui.services.TemplateSetCell;
import com.devonfw.cobigen.impl.config.entity.io.TemplateSetConfiguration;
import com.devonfw.cobigen.retriever.ArtifactRetriever;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXML;
Expand Down Expand Up @@ -46,7 +55,6 @@ public class MenuController implements Initializable {
*/
public MenuController() {

// TemplateSetModel.getInstance().loadAllAvailableTemplateSets();
// Where do we need tags
// List<TemplateSetTag> tagsList = new ArrayList<>();
// tagsList.addAll(templateSet.getTemplateSetConfiguration().getContextConfiguration().getTags().getTagsList());
Expand Down Expand Up @@ -77,8 +85,35 @@ public void initialize(URL location, ResourceBundle resources) {
}
});

Path artifactCachePath = CobiGenPaths.getTemplateSetsFolderPath()
.resolve(ConfigurationConstants.TEMPLATE_SET_ARTIFACT_CACHE_FOLDER);

if (!Files.exists(artifactCachePath)) {
try {
Files.createDirectory(artifactCachePath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// Initialize template set artifacts
// TODO: read CobiGen properties
List<String> groupIds = Arrays.asList(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID);
List<URL> urlList = ArtifactRetriever.retrieveTemplateSetXmlDownloadLinks(groupIds,
MavenUtil.determineMavenSettings());

List<Path> downloadedArtifacts = ArtifactRetriever.downloadArtifactsFromUrls(urlList);

List<TemplateSetConfiguration> templateSetConfigurations = ArtifactRetriever
.retrieveArtifactsFromCache(downloadedArtifacts);

ObservableList<TemplateSetConfiguration> observableList = FXCollections.observableArrayList();

observableList.addAll(templateSetConfigurations);

// binds the List with model
this.searchResultsView.setItems(TemplateSetModel.getInstance().getTemplateSetObservableList());
this.searchResultsView.setItems(observableList);

// Load increments of selected template set
// call back functions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.devonfw.cobigen.gui.model;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.impl.config.entity.io.TemplateSetConfiguration;

import javafx.collections.FXCollections;
Expand Down Expand Up @@ -46,19 +39,14 @@ public ObservableList<TemplateSetConfiguration> getTemplateSetObservableList() {

public void loadAllAvailableTemplateSets() {

// Load all the paths of template set from cobigen home folder
// all templates in template-set-list folder
File templatesetFolder = new File(CobiGenPaths.getCobiGenHomePath().resolve("template-set-list").toString());
File[] templatesetFileslist = templatesetFolder.listFiles();
List<Path> templateSetFiles = new ArrayList<>();
for (File file : templatesetFileslist) {
templateSetFiles.add(Paths.get(file.getPath()));
}
// Load all template set artifacts
// List<TemplateSetConfiguration> templateSetConfigurations = ArtifactRetriever.retrieveArtifactsFromCache();

// List<com.devonfw.cobigen.impl.config.entity.io.TemplateSetConfiguration> templateSets = ArtifactRetriever
// .retrieveTemplateSetData(templateSetFiles);
// pass TemplateSetConfigurations to GUI
// for (TemplateSetConfiguration configuration : templateSetConfigurations) {
// this.templateSetObservableList.addAll(configuration);
// }

// this.templateSetObservableList.addAll(templateSets);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;

import org.junit.Rule;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.testfx.util.WaitForAsyncUtils;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.api.util.MavenUtil;
import com.devonfw.cobigen.impl.config.entity.io.TemplateSetConfiguration;
import com.devonfw.cobigen.retriever.ArtifactRetriever;

Expand All @@ -28,10 +31,6 @@
*/
public class ProcessTemplateSetTest extends TestFXBase {

/** Temporary files rule to create temporary folders or files */
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();

/**
* Root path to all resources used in this test case
*/
Expand All @@ -57,12 +56,14 @@ public void testGetAllTemplateSetsAdapted() throws Exception {
public void testInstallTemplateSet() throws Exception {

// preparation
File userHome = this.tmpFolder.newFolder("UserHome");
// File userHome = this.tmpFolder.newFolder("UserHome");
File downloaded = this.tmpFolder.newFolder("UserHome", ConfigurationConstants.TEMPLATE_SETS_FOLDER,
ConfigurationConstants.DOWNLOADED_FOLDER);

// simulate template-set-list folder for downloaded template-set.xml files to be used in GUI
File artifactCacheFolder = this.tmpFolder.newFolder("UserHome", "template-sets", "template-set-list");
// Path artifactCacheFolder = userHome.toPath().resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER)
// .resolve(ConfigurationConstants.TEMPLATE_SET_ARTIFACT_CACHE_FOLDER);

Path templateSetXmlFile1 = TEST_FILE_ROOT_PATH.resolve("crud-java-server-app-2021.12.007-template-set.xml");
Files.copy(templateSetXmlFile1,
Expand All @@ -73,41 +74,68 @@ public void testInstallTemplateSet() throws Exception {
artifactCacheFolder.toPath().resolve("crud-openapi-server-app-2021.12.007-template-set.xml"),
StandardCopyOption.REPLACE_EXISTING);

withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, userHome.getAbsolutePath()).execute(() -> {
List<TemplateSetConfiguration> templateSetConfigurations = ArtifactRetriever.retrieveArtifactsFromCache();
// List<TemplateSetConfiguration> templateSetConfigurations = ArtifactRetriever.retrieveArtifactsFromCache();

// pass TemplateSetConfigurations to GUI
this.templateSetObservableList = FXCollections.observableArrayList();
for (TemplateSetConfiguration configuration : templateSetConfigurations) {
this.templateSetObservableList.addAll(configuration);
}
// pass TemplateSetConfigurations to GUI
// this.templateSetObservableList = FXCollections.observableArrayList();
// for (TemplateSetConfiguration configuration : templateSetConfigurations) {
// this.templateSetObservableList.addAll(configuration);
// }
//
// this.searchResultsView.setItems(this.templateSetObservableList);

this.searchResultsView.setItems(this.templateSetObservableList);
Button installButton = find("#installButton");
String installButtonText = installButton.getText();

Button installButton = find("#installButton");
String installButtonText = installButton.getText();
sleep(1000);

sleep(1000);
// clicks on first element of searchResultsView
clickOn(this.searchResultsView.getItems().get(0).getContextConfiguration().getTrigger().get(0).getId());

// clicks on first element of searchResultsView
clickOn(this.searchResultsView.getItems().get(0).getContextConfiguration().getTrigger().get(0).getId());
sleep(1000);

sleep(1000);
clickOn("Install");

clickOn("Install");
WaitForAsyncUtils.waitForFxEvents();

WaitForAsyncUtils.waitForFxEvents();
assertThat(downloaded.toPath().resolve("crud-java-server-app-2021.12.007.jar")).exists();

assertThat(downloaded.toPath().resolve("crud-java-server-app-2021.12.007.jar")).exists();
assertThat(installButtonText).isEqualTo("Installed");

assertThat(installButtonText).isEqualTo("Installed");
});
}

public void testAll() {

List<String> groupIds = Arrays.asList(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID);
List<URL> urlList = ArtifactRetriever.retrieveTemplateSetXmlDownloadLinks(groupIds,
MavenUtil.determineMavenSettings());

List<Path> downloadedArtifacts = ArtifactRetriever.downloadArtifactsFromUrls(urlList);

List<TemplateSetConfiguration> templateSetConfigurations = ArtifactRetriever
.retrieveArtifactsFromCache(downloadedArtifacts);

this.templateSetObservableList = FXCollections.observableArrayList();
for (TemplateSetConfiguration configuration : templateSetConfigurations) {
this.templateSetObservableList.addAll(configuration);
}
}

@Test
public void testGetAllTemplateSetsDownloaded() throws Exception {

// preparation
File userHome = this.tmpFolder.newFolder("UserHome");

Path templateSetPath = TEST_FILE_ROOT_PATH.resolve("downloaded_template_sets/template-sets");

FileUtils.copyDirectory(templateSetPath.toFile(), userHome.toPath().resolve("template-sets").toFile());

withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, userHome.getAbsolutePath()).execute(() -> {
Path downloadedPath = CobiGenPaths.getTemplateSetsFolderPath().resolve(ConfigurationConstants.DOWNLOADED_FOLDER);

});

}

@Test
Expand Down
Loading

0 comments on commit d5b63dd

Please sign in to comment.