diff --git a/cobigen/gui/pom.xml b/cobigen/gui/pom.xml index da477acf9..73b9da41b 100644 --- a/cobigen/gui/pom.xml +++ b/cobigen/gui/pom.xml @@ -77,7 +77,12 @@ system-lambda 1.2.0 test - + + + com.devonfw.cobigen + core-test + test + diff --git a/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/DetailsController.java b/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/DetailsController.java index f40f0de9a..45a2ba117 100644 --- a/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/DetailsController.java +++ b/cobigen/gui/src/main/java/com/devonfw/cobigen/gui/controllers/DetailsController.java @@ -1,15 +1,20 @@ package com.devonfw.cobigen.gui.controllers; -import java.io.IOException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; 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; +import com.devonfw.cobigen.api.util.TemplatesJarUtil; +import com.devonfw.cobigen.retriever.ArtifactRetriever; import com.devonfw.cobigen.retriever.reader.to.model.TemplateSet; import javafx.fxml.FXML; @@ -28,6 +33,8 @@ */ public class DetailsController implements Initializable { + private static final Logger LOG = LoggerFactory.getLogger(ArtifactRetriever.class); + // TODO: getIncrements() private List INCREMENTS = new ArrayList<>(); @@ -110,29 +117,40 @@ public void showTreeView(TreeView treeView) { } /** - * @param actionEvent - * @throws IOException + * Installs a template set into the template-sets/downloaded folder + * + * @param actionEvent the action event */ @FXML - public void installTemplateSet(javafx.event.ActionEvent actionEvent) throws IOException { + public void installTemplateSet(javafx.event.ActionEvent actionEvent) { - // Retrieving trigger information and move the template set file to user folder + // Retrieve template set name information from trigger String triggerName = this.templateSet.getTemplateSetConfiguration().getContextConfiguration().getTriggers().getId(); - String FileName = triggerName.replace("_", "-") + "-" + this.templateSet.getTemplateSetVersion() - + "-template-set.xml"; - Path cobigenHome = CobiGenPaths.getCobiGenHomePath(); - Path sourceFilePath = cobigenHome.resolve("template-set-list").resolve(FileName); - String destinationPath = "C:\\Users\\alsaad\\template-set-installed\\" + FileName; - Path destinationFilePath = Paths.get(destinationPath); - if (!Files.exists(destinationFilePath)) { - Files.copy(sourceFilePath, destinationFilePath); - System.out.println(sourceFilePath.toString()); - System.out.println(destinationFilePath); + String mavenArtfifactId = triggerName.replace("_", "-"); + String templateSetVersion = this.templateSet.getTemplateSetVersion(); + + // Adjust file name + String FileName = mavenArtfifactId + "-" + templateSetVersion + ".jar"; + + // prepare MavenCoordinate list for download + MavenCoordinate mavenCoordinate = new MavenCoordinate( + ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID, mavenArtfifactId, templateSetVersion); + List mavenCoordinateList = new ArrayList<>(); + mavenCoordinateList.add(mavenCoordinate); + + Path templateSetsPath = CobiGenPaths.getTemplateSetsFolderPath(); + + Path destinationFilePath = templateSetsPath.resolve(ConfigurationConstants.DOWNLOADED_FOLDER).resolve(FileName); + + if (!Files.exists(destinationFilePath.resolve(FileName))) { + // Download template set class file into downloaded folder + TemplatesJarUtil.downloadTemplatesByMavenCoordinates( + templateSetsPath.resolve(ConfigurationConstants.DOWNLOADED_FOLDER), mavenCoordinateList); } else { // Alert window if the file is already installed Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("Confirmation"); - alert.setHeaderText("the selected template-set is already installed"); + alert.setHeaderText("The selected template-set is already installed!"); alert.show(); } } 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 041b19f78..b8c3cbc3a 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,20 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; -import org.apache.commons.io.FileUtils; import org.junit.Rule; 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.retriever.reader.TemplateSetArtifactReader; +import com.devonfw.cobigen.retriever.reader.to.model.TemplateSet; + +import javafx.collections.FXCollections; +import javafx.scene.text.Text; /** * TODO @@ -22,6 +29,9 @@ public class ProcessTemplateSetTest extends TestFXBase { @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); + /** Test data root path */ + private static final String testdataRoot = "src/test/resources/testdata/integrationtests/ProcessTemplateSetTest"; + @Test public void testGetAllTemplateSetsAdapted() throws Exception { @@ -34,17 +44,47 @@ public void testGetAllTemplateSetsAdapted() throws Exception { @Test public void testGetAllTemplateSetsDownloaded() throws Exception { + // preparation File userHome = this.tmpFolder.newFolder("UserHome"); - File templateSets = this.tmpFolder.newFolder("UserHome", ConfigurationConstants.TEMPLATE_SETS_FOLDER); File downloaded = this.tmpFolder.newFolder("UserHome", ConfigurationConstants.TEMPLATE_SETS_FOLDER, ConfigurationConstants.DOWNLOADED_FOLDER); - // TODO: Create Dummy jar in resources - File jar = new File(""); - FileUtils.copyFile(jar, downloaded.toPath().resolve("template-test.jar").toFile()); + + // simulate template-set-list folder for downloaded template-set.xml files to be used in GUI + this.tmpFolder.newFolder("UserHome", "template-set-list"); + + Path templateSetXmlFile = Paths.get(testdataRoot).resolve("crud-java-server-app-2021.12.007-template-set.xml"); + + TemplateSetArtifactReader artifactReader = new TemplateSetArtifactReader(); + + TemplateSet templateSet = artifactReader.retrieveTemplateSet(templateSetXmlFile); + + // adds template set to GUI + this.templateSetObservableList = FXCollections.observableArrayList(); + this.templateSetObservableList.addAll(templateSet); + + this.searchResultsView.setItems(this.templateSetObservableList); + + Text installStatustext = find("#installStatusText"); + + sleep(1000); withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, userHome.getAbsolutePath()).execute(() -> { + // clicks on first element of searchResultsView + clickOn(this.searchResultsView.getItems().get(0).getTemplateSetConfiguration().getContextConfiguration() + .getTriggers().getId()); + + sleep(1000); + + clickOn("Install"); + + WaitForAsyncUtils.waitForFxEvents(); + + assertThat(downloaded.toPath().resolve("crud-java-server-app-2021.12.007.jar")).exists(); + + assertThat(installStatustext.getText()).contains("INSTALLED"); }); + } @Test