Skip to content

Commit

Permalink
devonfw#1454 made first test functional
Browse files Browse the repository at this point in the history
added first WIP implementation of installTemplateSets
  • Loading branch information
jan-vcapgemini committed Mar 3, 2023
1 parent 7e43176 commit cce187d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 23 deletions.
7 changes: 6 additions & 1 deletion cobigen/gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@
<artifactId>system-lambda</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>core-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,6 +33,8 @@
*/
public class DetailsController implements Initializable {

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

// TODO: getIncrements()
private List<String> INCREMENTS = new ArrayList<>();

Expand Down Expand Up @@ -110,29 +117,40 @@ public void showTreeView(TreeView<String> 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<MavenCoordinate> 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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {

Expand All @@ -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
Expand Down

0 comments on commit cce187d

Please sign in to comment.