Skip to content

Commit

Permalink
devonfw#1454 made refresh button functional
Browse files Browse the repository at this point in the history
added click on refresh button in installTemplateSetTest
cleaned up exception messages
  • Loading branch information
jan-vcapgemini committed Mar 15, 2023
1 parent 48671ff commit 781727d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.devonfw.cobigen.retriever;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -117,6 +119,38 @@ public static List<TemplateSet> retrieveTemplateSetData(List<Path> templateSetFi
return templateSetList;
}

/**
* Retrieves the artifact cache path
*
* @return Path to artifact cache folder
*/
public static Path retrieveArtifactCachePath() {

Path artifactCacheFolder = CobiGenPaths.getTemplateSetsFolderPath()
.resolve(ConfigurationConstants.TEMPLATE_SET_ARTIFACT_CACHE_FOLDER);
return artifactCacheFolder;
}

/**
* Checks is a directory is empty
*
* @param path directory to check
* @return true if empty, false if not
* @throws IOException
*/
private static boolean isEmpty(Path path) throws IOException {

if (Files.isDirectory(path)) {
try (DirectoryStream<Path> directory = Files.newDirectoryStream(path)) {
return !directory.iterator().hasNext();
} catch (IOException e) {
LOG.debug("An error occurred while checking if the directory {} was empty", path, e);
}
}

return false;
}

/**
* Retrieves a list of {@link TemplateSetConfiguration} from the template set artifact cache
*
Expand All @@ -129,8 +163,26 @@ public static List<TemplateSetConfiguration> retrieveArtifactsFromCache(List<Pat
List<TemplateSetConfiguration> templateSetConfigurations = new ArrayList<>();

if (cachedArtifacts == null) {
List<File> artfactList = Arrays.asList(CobiGenPaths.getTemplateSetsFolderPath()
.resolve(ConfigurationConstants.TEMPLATE_SET_ARTIFACT_CACHE_FOLDER).toFile().listFiles());

Path artifactCacheFolder = retrieveArtifactCachePath();
try {
if (!Files.exists(artifactCacheFolder) || isEmpty(artifactCacheFolder)) {
return null;
}
} catch (IOException e) {
LOG.error("An error occurred while checking the artifact cache directory {}", artifactCacheFolder, e);
return null;
}

List<File> artfactList = Arrays.asList(artifactCacheFolder.toFile().listFiles());
for (File file : artfactList) {
TemplateSetConfigurationReader reader = new TemplateSetConfigurationReader();
reader.readConfiguration(file.toPath());

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

for (Path file : cachedArtifacts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import java.util.ListIterator;
import java.util.ResourceBundle;

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

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.api.util.MavenCoordinate;
Expand All @@ -36,6 +39,9 @@
*/
public class MenuController implements Initializable {

/** Logger instance */
private static final Logger LOG = LoggerFactory.getLogger(MenuController.class);

@FXML
private Controller controller;

Expand Down Expand Up @@ -85,7 +91,7 @@ public void initialize(URL location, ResourceBundle resources) {
try {
this.controller.loadHome(event);
} catch (IOException e) {
e.printStackTrace();
LOG.error("An error occurred while loading the home page", e);
}
});

Expand All @@ -96,8 +102,7 @@ public void initialize(URL location, ResourceBundle resources) {
try {
Files.createDirectory(artifactCachePath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
LOG.error("An error occurred while creating the artifact cache directory", e);
}
} else {

Expand All @@ -116,7 +121,7 @@ public void initialize(URL location, ResourceBundle resources) {
try {
MenuController.this.controller.loadDetails();
} catch (IOException e) {
e.printStackTrace();
LOG.error("An error occurred while loading the details page", e);
}

});
Expand Down Expand Up @@ -167,8 +172,7 @@ public void refresh() {
try {
Files.createDirectory(artifactCachePath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
LOG.error("An error occurred while creating the artifact cache directory", e);
}
} else {

Expand Down Expand Up @@ -213,13 +217,18 @@ public void refresh() {

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

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

List<TemplateSetConfiguration> templateSetConfigurations;
if (downloadedArtifacts.isEmpty()) {
templateSetConfigurations = ArtifactRetriever.retrieveArtifactsFromCache(null);
} else {
templateSetConfigurations = ArtifactRetriever.retrieveArtifactsFromCache(downloadedArtifacts);
}
ObservableList<TemplateSetConfiguration> observableList = FXCollections.observableArrayList();

observableList.addAll(templateSetConfigurations);
this.searchResultsView.setItems(observableList);
if (templateSetConfigurations != null) {
observableList.addAll(templateSetConfigurations);
this.searchResultsView.setItems(observableList);
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@
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.apache.commons.io.FileUtils;
import org.junit.Test;
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;

import javafx.collections.FXCollections;
import javafx.scene.control.Button;

/**
Expand Down Expand Up @@ -56,14 +49,11 @@ public void testGetAllTemplateSetsAdapted() throws Exception {
public void testInstallTemplateSet() throws Exception {

// preparation
// 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 @@ -74,24 +64,17 @@ public void testInstallTemplateSet() throws Exception {
artifactCacheFolder.toPath().resolve("crud-openapi-server-app-2021.12.007-template-set.xml"),
StandardCopyOption.REPLACE_EXISTING);

// List<TemplateSetConfiguration> templateSetConfigurations = ArtifactRetriever.retrieveArtifactsFromCache();
Button refreshButton = find("#refreshButton");
clickOn(refreshButton);

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

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

sleep(1000);
WaitForAsyncUtils.waitForFxEvents();

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

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

sleep(1000);

clickOn("Install");
Expand All @@ -104,23 +87,6 @@ public void testInstallTemplateSet() throws Exception {

}

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 {

Expand Down

0 comments on commit 781727d

Please sign in to comment.