Skip to content

Commit

Permalink
devonfw#1509 New tests for Adaption in Eclipse
Browse files Browse the repository at this point in the history
  • Loading branch information
MansourD committed Feb 2, 2023
1 parent ee7c05f commit 79296a0
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.io.FileUtils;
import org.eclipse.core.resources.IFile;
Expand All @@ -17,10 +19,12 @@
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants;
import com.devonfw.cobigen.eclipse.test.common.SystemTest;
import com.devonfw.cobigen.eclipse.test.common.swtbot.AllJobsAreFinished;
import com.devonfw.cobigen.eclipse.test.common.utils.EclipseCobiGenUtils;
Expand Down Expand Up @@ -58,7 +62,7 @@ public static void setupClass() throws Exception {
*
* @throws Exception test fails
*/
public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception {
public void testBasicOpenAPIGenerationWithAdaptTemplateSets() throws Exception {

// copy sample project to external location and import it into the workspace
String testProjName = "ExtTestProj";
Expand Down Expand Up @@ -94,12 +98,123 @@ public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception {
}

/**
* Test for external projects (not in workspace) taken as input
*
* @throws Exception test fails
*/
public void testAdaptTemplatesAndImportIntoEclipse() throws Exception {

// copy sample project to external location and import it into the workspace
String testProjName = "ExtTestProj";
IJavaProject project = this.tmpMavenProjectRule.createProject(testProjName);
FileUtils.copyFile(new File(resourcesRootPath + "input/adapt-templates.yml"),
project.getUnderlyingResource().getLocation().append("adapt-templates.yml").toFile());
project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
this.tmpMavenProjectRule.updateProject();

EclipseCobiGenUtils.runAndCaptureAdaptTemplatesSets(bot);

EclipseUtils.openErrorsTreeInProblemsView(bot);

// 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();

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

Path adaptedFolder = Paths.get(generatorProjOfTempltesSets.getLocationURI())
.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED);

assertThat(generatorProjOfTempltesSets.exists()).isTrue();
assertThat(Files.exists(adaptedFolder)).isTrue();

}

/**
* Test for external projects (not in workspace) taken as input for generation
*
* @throws Exception test fails
*/
public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exception {

// copy sample project to external location and import it into the workspace
String testProjName = "ExtTestProj";
IJavaProject project = this.tmpMavenProjectRule.createProject(testProjName);
FileUtils.copyFile(new File(resourcesRootPath + "input/adapt-templates.yml"),
project.getUnderlyingResource().getLocation().append("adapt-templates.yml").toFile());
project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
this.tmpMavenProjectRule.updateProject();

EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot);
EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot);
EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME);

EclipseUtils.openErrorsTreeInProblemsView(bot);

// 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);
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(testProjName);
IFile generationResult = proj.getFile(
"src/main/java/com/devonfw/test/sampledatamanagement/service/impl/rest/SampledatamanagementRestServiceImpl.java");

assertThat(generationResult.exists()).isTrue();
}

/*
*
* Test of testBasicOpenAPIGenerationWithAdaptTemplates with custom COBIGEN_HOME environment variable
*
*/
@Test
public void testAdaptMonolithicTemplatesAndGenerate() throws Exception {

File tmpProject = this.tempFolder.newFolder("playground", "project");
withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
.execute(() -> testBasicOpenAPIGenerationWithAdaptMonolithicTemplates());
}

/**
* TODO Test of testBasicOpenAPIGenerationWithAdaptTemplates with custom template-sets after adapt the jar files
*
* @throws Exception test fails
*/
@Test
public void testAdaptTemplatesAndGenerate() throws Exception {
@Ignore
public void testAdaptTemplateSetsAndGenerate() 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);
withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
.execute(() -> testBasicOpenAPIGenerationWithAdaptTemplateSets());
}

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

Path devTemplatesPath = new File(
AdaptTemplatesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile()
Expand All @@ -110,6 +225,7 @@ public void testAdaptTemplatesAndGenerate() throws Exception {
File downloaded = this.tempFolder.newFolder("playground", "project", "template-sets", "downloaded");
FileUtils.copyFileToDirectory(devTemplatesPath.toFile(), downloaded);
withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
.execute(() -> testBasicOpenAPIGenerationWithAdaptTemplates());
.execute(() -> testAdaptTemplatesAndImportIntoEclipse());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ public class ResourceConstants {
*/
public static final String TEMPLATE_SETS_CONFIG_PROJECT_NAME = "template-sets";

/**
* Name of template-sets/downloaded folder
*/
public static final String TEMPLATE_SETS_DOWNLOADED = "downloaded";

/**
* Name of template-sets/adapted folder
*/
public static final String TEMPLATE_SETS_ADAPTED = "adapted";

}
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,10 @@ public static void adaptTemplateSet(Path templateSetsLocation) {

try {
TemplateAdapter templateAdapter = new TemplateAdapterImpl(templateSetsLocation);
List<Path> jarFilePath = TemplatesJarUtil.getJarFiles(templateSetsLocation.resolve("downloaded"));
templateAdapter.adaptTemplateSets(jarFilePath, templateSetsLocation.resolve("adapted"), false);
List<Path> jarFilePath = TemplatesJarUtil
.getJarFiles(templateSetsLocation.resolve(ResourceConstants.TEMPLATE_SETS_DOWNLOADED));
templateAdapter.adaptTemplateSets(jarFilePath,
templateSetsLocation.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED), false);

} catch (Exception e) {
LOG.error("An exception occurred while processing Jar files to create CobiGen_Templates folder", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ public Object execute(ExecutionEvent event) throws ExecutionException {

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

Path downloadedPath = templateSet.resolve("downloaded");
Path adaptedPath = templateSet.resolve("adapted");
Path downloadedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_DOWNLOADED);
Path adaptedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED);

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

// B. TODO Import the project
// 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.)
Expand Down Expand Up @@ -107,7 +108,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
}
ResourcesPluginUtil.processJar(fileName);

importProjectIntoWorkspace();
importProjectIntoWorkspace(ResourceConstants.CONFIG_PROJECT_NAME, null);
dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null,
"CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" },
1);
Expand All @@ -133,7 +134,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
/**
* CobiGen_Templates folder created at main folder using source jar will be imported into workspace
*/
private void importProjectIntoWorkspace() {
private void importProjectIntoWorkspace(String projectName, Path projectPath) {

ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(Display.getDefault().getActiveShell());

Expand All @@ -143,26 +144,30 @@ private void importProjectIntoWorkspace() {
progressMonitor.open();
progressMonitor.getProgressMonitor().beginTask("Importing templates...", 0);
try {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME);
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(project.getName());
description.setLocation(new org.eclipse.core.runtime.Path(this.ws.toPortableString() + "/CobiGen_Templates"));
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));
}
project.create(description, null);

// We set the current project to be converted to a Maven project
ISelection sel = new StructuredSelection(project);
mavenConverter.selectionChanged(null, sel);

project.open(null);

// Converts the current project to a Maven project
mavenConverter.run(null);
if (projectName == ResourceConstants.CONFIG_PROJECT_NAME)
mavenConverter.run(null);
progressMonitor.close();

} catch (CoreException e) {
progressMonitor.close();
e.printStackTrace();
MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning",
"Some Exception occurred while importing CobiGen_Templates into workspace");
"Some Exception occurred while importing " + projectName + " into workspace");
}
}

Expand Down

0 comments on commit 79296a0

Please sign in to comment.