From 781727dce8c1199a534ffc6e4674c470c4b76d59 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 16 Mar 2023 00:08:05 +0100 Subject: [PATCH] #1454 made refresh button functional added click on refresh button in installTemplateSetTest cleaned up exception messages --- .../cobigen/retriever/ArtifactRetriever.java | 56 ++++++++++++++++++- .../gui/controllers/MenuController.java | 31 ++++++---- .../cobigen/gui/ProcessTemplateSetTest.java | 46 ++------------- 3 files changed, 80 insertions(+), 53 deletions(-) diff --git a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/ArtifactRetriever.java b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/ArtifactRetriever.java index 085fa4264..910edcb91 100644 --- a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/ArtifactRetriever.java +++ b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/ArtifactRetriever.java @@ -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; @@ -117,6 +119,38 @@ public static List retrieveTemplateSetData(List 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 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 * @@ -129,8 +163,26 @@ public static List retrieveArtifactsFromCache(List templateSetConfigurations = new ArrayList<>(); if (cachedArtifacts == null) { - List 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 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) { diff --git a/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/MenuController.java b/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/MenuController.java index 434656395..8d831a656 100644 --- a/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/MenuController.java +++ b/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/MenuController.java @@ -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; @@ -36,6 +39,9 @@ */ public class MenuController implements Initializable { + /** Logger instance */ + private static final Logger LOG = LoggerFactory.getLogger(MenuController.class); + @FXML private Controller controller; @@ -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); } }); @@ -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 { @@ -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); } }); @@ -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 { @@ -213,13 +217,18 @@ public void refresh() { List downloadedArtifacts = ArtifactRetriever.downloadArtifactsFromUrls(urlList); - List templateSetConfigurations = ArtifactRetriever - .retrieveArtifactsFromCache(downloadedArtifacts); - + List templateSetConfigurations; + if (downloadedArtifacts.isEmpty()) { + templateSetConfigurations = ArtifactRetriever.retrieveArtifactsFromCache(null); + } else { + templateSetConfigurations = ArtifactRetriever.retrieveArtifactsFromCache(downloadedArtifacts); + } ObservableList observableList = FXCollections.observableArrayList(); - observableList.addAll(templateSetConfigurations); - this.searchResultsView.setItems(observableList); + if (templateSetConfigurations != null) { + observableList.addAll(templateSetConfigurations); + this.searchResultsView.setItems(observableList); + } } diff --git a/cobigen/gui/src/test/java/com/devonfw/cobigen/gui/ProcessTemplateSetTest.java b/cobigen/gui/src/test/java/com/devonfw/cobigen/gui/ProcessTemplateSetTest.java index a3ceaf376..20b91375c 100644 --- a/cobigen/gui/src/test/java/com/devonfw/cobigen/gui/ProcessTemplateSetTest.java +++ b/cobigen/gui/src/test/java/com/devonfw/cobigen/gui/ProcessTemplateSetTest.java @@ -4,13 +4,10 @@ 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; @@ -18,11 +15,7 @@ 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; /** @@ -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, @@ -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 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"); @@ -104,23 +87,6 @@ public void testInstallTemplateSet() throws Exception { } - public void testAll() { - - List groupIds = Arrays.asList(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID); - List urlList = ArtifactRetriever.retrieveTemplateSetXmlDownloadLinks(groupIds, - MavenUtil.determineMavenSettings()); - - List downloadedArtifacts = ArtifactRetriever.downloadArtifactsFromUrls(urlList); - - List templateSetConfigurations = ArtifactRetriever - .retrieveArtifactsFromCache(downloadedArtifacts); - - this.templateSetObservableList = FXCollections.observableArrayList(); - for (TemplateSetConfiguration configuration : templateSetConfigurations) { - this.templateSetObservableList.addAll(configuration); - } - } - @Test public void testGetAllTemplateSetsDownloaded() throws Exception {