Skip to content

Commit

Permalink
devonfw#1509 implemented testAdaptTemplateSetsAndGenerate and modify …
Browse files Browse the repository at this point in the history
…testBasicOpenAPIGenerationWithAdaptMonolithicTemplates
  • Loading branch information
cedricarnauld123 committed Mar 27, 2023
1 parent 096f2ef commit 4c5cfab
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 105 deletions.
50 changes: 50 additions & 0 deletions cobigen-eclipse/cobigen-eclipse-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<properties>
<tycho.testArgLine.enriched>${surefireArgLineIntegration} -Xms512m -Xmx2048m -XX:+DisableExplicitGC</tycho.testArgLine.enriched>
<enforcer.skip>true</enforcer.skip>
<maven.test.AdaptTemplatesTest1>src\main\resources\AdaptTemplatesTest\template-sets\adapted\crud-java-server-app</maven.test.AdaptTemplatesTest1>
<maven.test.AdaptTemplatesTest2>src\main\resources\AdaptTemplatesTest\template-sets\adapted\crud-java-server-app-complex</maven.test.AdaptTemplatesTest2>

</properties>

<dependencies>
Expand Down Expand Up @@ -58,6 +61,53 @@
</execution>
</executions>
</plugin>

<!-- Building a Maven project that is used on the tests -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>Install test adaptTemplatesTests1</id>
<phase>test-compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<workingDirectory>${maven.test.AdaptTemplatesTest1}</workingDirectory>
<arguments>
<argument>install</argument>
<!-- https://stackoverflow.com/a/66801171 -->
<argument>-Djansi.force=true</argument>
<argument>-Djansi.passthrough=true</argument>
<argument>-B</argument>
<argument>-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>Install test adaptTemplatesTests2</id>
<phase>test-compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<workingDirectory>${maven.test.AdaptTemplatesTest2}</workingDirectory>
<arguments>
<argument>install</argument>
<!-- https://stackoverflow.com/a/66801171 -->
<argument>-Djansi.force=true</argument>
<argument>-Djansi.passthrough=true</argument>
<argument>-B</argument>
<argument>-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class AdaptTemplatesTest extends SystemTest {
/** Line separator, e.g. for windows '\r\n' */
public static final String LINE_SEPARATOR = java.lang.System.getProperty("line.separator");

/* Root path for templateSets jar */
private static final String testResource = "src/main/resources/AdaptTemplatesTest/template-sets/downloaded";

/**
* Setup workbench appropriately for tests
*
Expand Down Expand Up @@ -116,17 +119,25 @@ public void testAdaptTemplatesAndImportIntoEclipse() throws Exception {

EclipseCobiGenUtils.runAndCaptureAdaptTemplatesSets(bot);

EclipseUtils.openErrorsTreeInProblemsView(bot);
// EclipseUtils.updateMavenProject(bot, ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);

// expand the new file in the package explorer
SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES);
SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml");
javaClassItem.select();

// execute CobiGen
EclipseCobiGenUtils.processCobiGen(bot, javaClassItem, 25000, "CRUD devon4j Server>CRUD REST services");
bot.waitUntil(new AllJobsAreFinished(), 10000);
// increase timeout as the openAPI parser is slow on initialization
EclipseCobiGenUtils.confirmSuccessfullGeneration(bot, 40000);
bot.waitUntil(new AllJobsAreFinished(), 10000);

// EclipseUtils.openErrorsTreeInProblemsView(bot);

IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot()
.getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);
bot.waitUntil(new AllJobsAreFinished(), 10000000);
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(testProjName);
bot.waitUntil(new AllJobsAreFinished(), 10000);

Path adaptedFolder = Paths.get(generatorProjOfTempltesSets.getLocationURI())
.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED);
Expand All @@ -151,7 +162,11 @@ public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exce
project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
this.tmpMavenProjectRule.updateProject();

EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot);
// retrieve CobiGen home directory (overwritten through environment variables)
File templatesDirectory = CobiGenPaths.getTemplatesFolderPath().toFile();
// create templates directory as this should be present in this scenario
TemplatesJarUtil.downloadLatestDevon4jTemplates(true, templatesDirectory);
TemplatesJarUtil.downloadLatestDevon4jTemplates(false, templatesDirectory);
EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot);
EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME);

Expand Down Expand Up @@ -184,7 +199,7 @@ public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exce
@Test
public void testAdaptMonolithicTemplatesAndGenerate() throws Exception {

File tmpProject = this.tempFolder.newFolder("playground", "project", "templates");
File tmpProject = this.tempFolder.newFolder("playground", "project");
withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
.execute(() -> testBasicOpenAPIGenerationWithAdaptMonolithicTemplates());
}
Expand All @@ -210,22 +225,19 @@ public void testAdaptTemplateSetsAndGenerate() throws Exception {
.execute(() -> testBasicOpenAPIGenerationWithAdaptTemplateSets());
}

/*
*
/**
* Test adaption of template-sets/downloaded/.jar files and importing the project into Eclipse
*
* @throws Exception
*/
@Test
public void testAdaptTemplateSetsAndImport() throws Exception {

Path devTemplatesPath = new File(
AdaptTemplatesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile()
.getParentFile().toPath().resolve("cobigen-templates").resolve("crud-openapi-java-server-app")
.resolve("target").resolve("crud-openapi-java-server-app-2021.12.007-SNAPSHOT.jar");

File tmpProject = this.tempFolder.newFolder("playground", "project");
File downloaded = this.tempFolder.newFolder("playground", "project", "template-sets", "downloaded");
FileUtils.copyFileToDirectory(devTemplatesPath.toFile(), downloaded);

FileUtils.copyDirectory(new File(testResource), downloaded);

withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
.execute(() -> testAdaptTemplatesAndImportIntoEclipse());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ public static void runAndCaptureAdaptTemplatesSets(SWTWorkbenchBot bot) throws E

SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES);
view.bot().tree().contextMenu("CobiGen").menu("Adapt Templates...").click();

SWTBotShell informationDialog = bot.shell("Information");
bot.waitUntil(new AnyShellIsActive("Information"), DEFAULT_TIMEOUT);
takeScreenshot(bot, "Adapt Templates Information");
informationDialog.bot().button("Ok").click();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,46 +106,17 @@ public static void refreshConfigurationProject() {
*/
public static IProject getGeneratorConfigurationProject() throws GeneratorProjectNotExistentException, CoreException {

Path templateSetDirectory = getTemplateSetDirectory();

if (!Files.exists(templateSetDirectory)) {
File templatesDirectory = getTemplatesDirectory();
generatorProj = ResourcesPlugin.getWorkspace().getRoot()
.getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);
if (generatorProj.exists()) {
return generatorProj;
} else {
generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME);

if (!generatorProj.exists()) {
if (!isUpdateDialogShown) {
if (templatesDirectory.exists()) {
Path jarFilePath = TemplatesJarUtil.getJarFile(false, templatesDirectory.toPath());
// If we don't find at least one jar, then we do need to download new templates
if (jarFilePath == null || !Files.exists(jarFilePath)) {
int result = createUpdateTemplatesDialog();
isUpdateDialogShown = true;
if (result == 1) {
// User does not want to download templates.
userWantsToDownloadTemplates = false;
} else {
userWantsToDownloadTemplates = true;
}
}

} else {
int result = createUpdateTemplatesDialog();
isUpdateDialogShown = true;
if (result == 1) {
// User does not want to download templates.
userWantsToDownloadTemplates = false;
} else {
userWantsToDownloadTemplates = true;
}
}
}
}
if (userWantsToDownloadTemplates) {
if (generatorProj.exists()) {
return generatorProj;
} else {
return null;
}
}

return null;
}

Expand Down Expand Up @@ -265,7 +236,7 @@ public static void processJar(String fileName) throws MalformedURLException, IOE
}

try {
TemplateAdapter templateAdapter = new TemplateAdapterImpl(templateDirectory);
TemplateAdapter templateAdapter = new TemplateAdapterImpl(null);
templateAdapter.adaptMonolithicTemplates(templateDirectory.resolve(ConfigurationConstants.COBIGEN_TEMPLATES),
false);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;

import org.eclipse.core.commands.AbstractHandler;
Expand All @@ -27,11 +27,15 @@
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import com.devonfw.cobigen.api.TemplateAdapter;
import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.exception.TemplateSelectionForAdaptionException;
import com.devonfw.cobigen.api.exception.UpgradeTemplatesNotificationException;
import com.devonfw.cobigen.eclipse.common.constants.InfrastructureConstants;
import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants;
import com.devonfw.cobigen.eclipse.common.tools.ExceptionHandler;
import com.devonfw.cobigen.eclipse.common.tools.PlatformUIUtil;
import com.devonfw.cobigen.eclipse.common.tools.ResourcesPluginUtil;
import com.devonfw.cobigen.impl.adapter.TemplateAdapterImpl;

/**
* Handler for the Package-Explorer EventfimportProjectIntoWorkspace
Expand All @@ -53,31 +57,23 @@ public Object execute(ExecutionEvent event) throws ExecutionException {

MDC.put(InfrastructureConstants.CORRELATION_ID, UUID.randomUUID().toString());
IProject generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME);
IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot()
.getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);
Path templateSet = ResourcesPluginUtil.getTemplateSetDirectory();

if (generatorProjOfTempltesSets.exists()) {
// 1. the project is imported so, do not adapt(no need to extract the jar files) For now nothing to do.

} else if (Files.exists(templateSet)) {

// 2. downloaded exists? first adapt the jar file (files), then import project to eclipse

Path downloadedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_DOWNLOADED);
Path adaptedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED);
Path templateSet = ResourcesPluginUtil.getTemplateSetDirectory();
File template = ResourcesPluginUtil.getTemplatesDirectory();

// A. adapt the jar files if not already adapted
if (Files.exists(downloadedPath) && !Files.exists(adaptedPath))
ResourcesPluginUtil.adaptTemplateSet(templateSet);
TemplateAdapter templateAdapter;

// B. Import the project
importProjectIntoWorkspace(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME, templateSet);
/*
* 3. TODO downloaded does not exists? update command must be executed. then go to 2. // (step 3 can be ignored
* for now until the new template-sets are deployed online.)
*/
if (Files.exists(templateSet)) {
templateAdapter = new TemplateAdapterImpl(templateSet);
} else if (template.exists()) {
templateAdapter = new TemplateAdapterImpl(template.toPath());
} else {
LOG.debug("No configuration project was found");
return null;
}
try {
templateAdapter.adaptTemplates();

if (generatorProj.exists()) {
MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Info!", null,
"CobiGen_Templates folder is already imported, click on Update templates button to update with latest. ",
Expand All @@ -94,40 +90,48 @@ public Object execute(ExecutionEvent event) throws ExecutionException {

if (result == 0) {
try {
String fileName = ResourcesPluginUtil.getJarPath(true);
if (fileName.equals("")) {
result = createUpdateTemplatesDialog();
if (result == 1) {
MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning",
"Templates have not been found, please download them!");
throw new NullPointerException("Templates have not been found!");
} else {
fileName = ResourcesPluginUtil.downloadJar(true);
}

}
ResourcesPluginUtil.processJar(fileName);

importProjectIntoWorkspace(ResourceConstants.CONFIG_PROJECT_NAME,
ResourcesPluginUtil.getTemplatesDirectory().toPath());
dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null,
"CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" },
1);
dialog.setBlockOnOpen(true);
dialog.open();
} catch (MalformedURLException e) {
LOG.error("An exception with download url of maven central", e);
PlatformUIUtil.openErrorDialog("An exception with download url of maven central", e);
} catch (IOException e) {
LOG.error("An exception occurred while writing Jar files to .metadata folder", e);
PlatformUIUtil.openErrorDialog("An exception occurred while writing Jar files to .metadata folder", e);
} catch (Throwable e) {
ExceptionHandler.handle(e, HandlerUtil.getActiveShell(event));
}
}
MDC.remove(InfrastructureConstants.CORRELATION_ID);
}

} catch (TemplateSelectionForAdaptionException e) {
List<Path> templateJars = e.getTemplateSets();
if (!templateJars.isEmpty()) {

try {
// Adapt process
templateAdapter.adaptTemplateSets(templateJars, false);

// B. Import into Eclipse
importProjectIntoWorkspace(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME, templateSet);
MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null,
"CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" }, 1);
dialog.setBlockOnOpen(true);
dialog.open();

} catch (IOException e1) {
LOG.debug("Adapt was not possible", e1);
}

} else {
LOG.info("No template set jars found to extract.");
}
} catch (IOException e) {
LOG.debug("Error occur", e);
} catch (UpgradeTemplatesNotificationException e) {
LOG.debug("Upgrade Process", e);
}

return null;

}
Expand All @@ -150,7 +154,7 @@ private void importProjectIntoWorkspace(String projectName, Path projectPath) {
if (projectName == ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME) {
description.setLocationURI(projectPath.toUri());
} else {
description.setLocation(new org.eclipse.core.runtime.Path(this.ws.toPortableString() + "/" + projectName));
description.setLocationURI(projectPath.resolve(ConfigurationConstants.COBIGEN_TEMPLATES).toUri());
}
project.create(description, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,9 @@ public static Path getTemplatesFolderPath() {
*/
public static Path getTemplatesFolderPath(Path home) {

File file = new File(home.toString());
int lastSlash = file.getName().lastIndexOf("/");
String lastWord = file.getName().substring(lastSlash + 1);
if (lastWord.equals(ConfigurationConstants.TEMPLATES_FOLDER)) {
return home;
} else {
home = home.resolve(ConfigurationConstants.TEMPLATES_FOLDER);

}
Path templatesPath = home.resolve(ConfigurationConstants.TEMPLATES_FOLDER);

return home;
return createFolder(templatesPath);
}

/**
Expand Down
Loading

0 comments on commit 4c5cfab

Please sign in to comment.