Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit 64479a7

Browse files
authored
Merge pull request #72 from travis1111/openapi_wizard_tests
Add openapi wizard test
2 parents 6b5994b + 5e55080 commit 64479a7

3 files changed

Lines changed: 179 additions & 21 deletions

File tree

dev/org.eclipse.codewind.openapi.ui.test/src/org/eclipse/codewind/openapi/ui/test/AllUiTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
package org.eclipse.codewind.openapi.ui.test;
1414

1515
import org.eclipse.codewind.openapi.ui.test.menus.ContextMenuTest;
16+
import org.eclipse.codewind.openapi.ui.test.wizard.WizardTest;
1617
import org.junit.runner.RunWith;
1718
import org.junit.runners.Suite;
1819

1920
@RunWith(Suite.class)
2021
@Suite.SuiteClasses({
21-
ContextMenuTest.class
22+
ContextMenuTest.class,
23+
WizardTest.class
2224
})
2325
public class AllUiTests {
2426

dev/org.eclipse.codewind.openapi.ui.test/src/org/eclipse/codewind/openapi/ui/test/BaseTestCase.java

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,92 @@
2323
import org.eclipse.codewind.openapi.ui.test.menus.utils.UITestUtilities;
2424
import org.eclipse.core.resources.IFile;
2525
import org.eclipse.core.resources.IProject;
26+
import org.eclipse.core.resources.IWorkspaceRoot;
27+
import org.eclipse.core.resources.ResourcesPlugin;
2628
import org.eclipse.core.runtime.CoreException;
29+
import org.eclipse.core.runtime.jobs.IJobManager;
30+
import org.eclipse.core.runtime.jobs.Job;
31+
import org.eclipse.jface.dialogs.IDialogConstants;
2732
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
2833
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
2934
import org.eclipse.swtbot.swt.finder.SWTBotTestCase;
3035
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
36+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
37+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo;
3138
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
3239
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
3340

3441
public class BaseTestCase extends SWTBotTestCase {
3542

3643
protected SWTWorkbenchBot swtWorkbenchBot = new SWTWorkbenchBot();
37-
44+
3845
protected IProject testProject;
3946
protected IFile definitionFile;
40-
47+
4148
// Configurable options to test and should be overridden
4249
protected String newProjectName = "TestProject";
4350
protected String sourceDefinition = Constants.PETSTORE_30;
4451
protected String targetDefinitionInProject = "petstore.yaml";
45-
52+
4653
public BaseTestCase() {
4754
// Empty
4855
}
49-
56+
57+
protected void testOpenAPIWizardHappyPath(String type, String language, String fileToCheck) {
58+
SWTBotView view = UITestUtilities.getProjectExplorerView();
59+
view.show();
60+
StringTokenizer token = new StringTokenizer(targetDefinitionInProject, "/");
61+
Stack<String> pathSegments = new Stack<String>();
62+
while (token.hasMoreElements()) {
63+
pathSegments.add(0, token.nextToken());
64+
}
65+
SWTBotTreeItem targetTreeItem = UITestUtilities.findTreeItem(newProjectName, view, pathSegments);
66+
SWTBotMenu contextMenu = targetTreeItem.contextMenu(UIConstants.MENU_OPENAPI_GENERATE);
67+
SWTBotMenu menu = contextMenu.menu(type);
68+
menu.click();
69+
if (language != null) {
70+
SWTBotCombo langCombo = bot.comboBox(0);
71+
langCombo.setSelection(language);
72+
}
73+
SWTBotButton finishButton = bot.buttonWithId(null, Integer.valueOf(IDialogConstants.FINISH_ID));
74+
finishButton.click();
75+
// wait until it is done
76+
this.waitForProcess();
77+
78+
// verify
79+
IWorkspaceRoot wRoot = ResourcesPlugin.getWorkspace().getRoot();
80+
IProject project = wRoot.getProject(this.newProjectName);
81+
IFile file = project.getFile(fileToCheck);
82+
assertTrue("The project file " + fileToCheck + " does not exist.", file.exists());
83+
84+
}
85+
86+
protected void waitForProcess() {
87+
final IJobManager manager = Job.getJobManager();
88+
final Job[] jobs = manager.find(null); // return all jobs
89+
while (jobExists(jobs)) {
90+
try {
91+
Thread.sleep(100);
92+
} catch (Exception e) {
93+
throw new RuntimeException(e.getMessage(), e);
94+
}
95+
}
96+
}
97+
98+
private boolean jobExists(Job[] jobs) {
99+
int unfinished = 0;
100+
for (Job job : jobs) {
101+
if (job.getName().startsWith("Generating code and configuring project")
102+
|| job.getName().startsWith("Building workspace")) {
103+
if (job.getState() != Job.NONE) {
104+
unfinished++;
105+
}
106+
}
107+
108+
}
109+
return (unfinished != 0);
110+
}
111+
50112
protected void createGeneralProject(String projectName) {
51113
try {
52114
this.testProject = TestUtilities.createGeneralProject(projectName);
@@ -56,18 +118,19 @@ protected void createGeneralProject(String projectName) {
56118
assertNotNull("Project creation", this.testProject);
57119
assertEquals("Test project name", projectName, this.testProject.getName());
58120
}
59-
121+
60122
/**
61123
*
62-
* @param sourceFile - source OpenAPI definition in the test plugin resources folder
63-
* @param destinationFile - path of target OpenAPI definition in the test project but excludes
64-
* the project name
124+
* @param sourceFile - source OpenAPI definition in the test plugin
125+
* resources folder
126+
* @param destinationFile - path of target OpenAPI definition in the test
127+
* project but excludes the project name
65128
*/
66129
protected void copyDefinitionToProject(String sourceFile, String destinationFile) {
67130
try {
68-
this.definitionFile = TestUtilities.copyDefinitionToProject(sourceFile, destinationFile, this.testProject);
131+
this.definitionFile = TestUtilities.copyDefinitionToProject(sourceFile, destinationFile, this.testProject);
69132
assertTrue("Copied file exists in workspace", this.definitionFile.exists());
70-
assertEquals("Definition is in the test project", this.testProject, this.definitionFile.getProject());
133+
assertEquals("Definition is in the test project", this.testProject, this.definitionFile.getProject());
71134
} catch (CoreException e) {
72135
e.printStackTrace();
73136
fail(e.getMessage());
@@ -84,7 +147,7 @@ protected void setup() {
84147
createGeneralProject(this.newProjectName);
85148
copyDefinitionToProject(this.sourceDefinition, this.targetDefinitionInProject);
86149
}
87-
150+
88151
protected void doTestInProjectExplorer(boolean openApiMenuShouldAppear) {
89152
SWTBotView view = UITestUtilities.getProjectExplorerView();
90153
StringTokenizer token = new StringTokenizer(targetDefinitionInProject, "/");
@@ -94,37 +157,42 @@ protected void doTestInProjectExplorer(boolean openApiMenuShouldAppear) {
94157
}
95158
verifyContextMenu(openApiMenuShouldAppear, view, newProjectName, pathSegments);
96159
}
97-
160+
98161
// Accept first child node of root node
99-
protected void verifyContextMenu(boolean expectedOpenApiMenuExists, SWTBotView view, String rootNode, Stack<String> pathSegments) {
162+
protected void verifyContextMenu(boolean expectedOpenApiMenuExists, SWTBotView view, String rootNode,
163+
Stack<String> pathSegments) {
100164
SWTBotTreeItem targetTreeItem = UITestUtilities.findTreeItem(rootNode, view, pathSegments);
101165
verifyContextMenu(expectedOpenApiMenuExists, targetTreeItem);
102166
}
103167

104168
// Accept a path to the child node relative to the root node
105-
protected void verifyContextMenu(boolean expectedOpenApiMenuExists, SWTBotView view, String rootNode, String childNode) {
169+
protected void verifyContextMenu(boolean expectedOpenApiMenuExists, SWTBotView view, String rootNode,
170+
String childNode) {
106171
SWTBotTreeItem targetTreeItem = UITestUtilities.findTreeItem(rootNode, view, childNode);
107172
verifyContextMenu(expectedOpenApiMenuExists, targetTreeItem);
108173
}
109-
174+
110175
private void verifyContextMenu(boolean expectedOpenApiMenuExists, SWTBotTreeItem targetTreeItem) {
111176
assertNotNull("The test file should be in the project and the tree node exists", targetTreeItem);
112177
try {
113178
SWTBotMenu contextMenu = targetTreeItem.contextMenu(UIConstants.MENU_OPENAPI_GENERATE);
114179
if (expectedOpenApiMenuExists) {
115-
assertNotNull("OpenAPI Generate menu should exist", contextMenu);
180+
assertNotNull("OpenAPI Generate menu should exist", contextMenu);
116181
List<String> cascadeMenuItems = contextMenu.menuItems();
117-
for (String s: cascadeMenuItems) {
182+
for (String s : cascadeMenuItems) {
118183
System.out.println(s);
119184
}
120185
assertTrue("There should be three menu actions", cascadeMenuItems.size() == 3);
121-
assertTrue("Client API stub... menu exists", cascadeMenuItems.get(0).equals(UIConstants.MENU_GEN_CLIENT));
122-
assertTrue("Server API stub... menu exists", cascadeMenuItems.get(1).equals(UIConstants.MENU_GEN_SERVER));
186+
assertTrue("Client API stub... menu exists",
187+
cascadeMenuItems.get(0).equals(UIConstants.MENU_GEN_CLIENT));
188+
assertTrue("Server API stub... menu exists",
189+
cascadeMenuItems.get(1).equals(UIConstants.MENU_GEN_SERVER));
123190
assertTrue("HTML documentation...", cascadeMenuItems.get(2).equals(UIConstants.MENU_HTML));
124191
}
125192
} catch (Exception e) {
126193
if (!expectedOpenApiMenuExists) {
127-
assertTrue("OpenAPI Generate menu should not be available on non-OpenAPI 3.0 documents", e instanceof WidgetNotFoundException);
194+
assertTrue("OpenAPI Generate menu should not be available on non-OpenAPI 3.0 documents",
195+
e instanceof WidgetNotFoundException);
128196
return;
129197
}
130198
throw e; // Fail test if test otherwise
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBM Corporation - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.codewind.openapi.ui.test.wizard;
14+
15+
import org.eclipse.codewind.openapi.test.Constants;
16+
import org.eclipse.codewind.openapi.ui.test.BaseTestCase;
17+
import org.eclipse.codewind.openapi.ui.test.UIConstants;
18+
import org.eclipse.core.resources.ResourcesPlugin;
19+
import org.eclipse.core.runtime.CoreException;
20+
import org.eclipse.core.runtime.IProgressMonitor;
21+
import org.eclipse.core.runtime.NullProgressMonitor;
22+
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
23+
import org.junit.After;
24+
import org.junit.BeforeClass;
25+
import org.junit.FixMethodOrder;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
import org.junit.runners.MethodSorters;
30+
31+
@RunWith(JUnit4.class)
32+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
33+
public class WizardTest extends BaseTestCase {
34+
35+
public WizardTest() {
36+
}
37+
38+
@BeforeClass
39+
public static void beforeClass() {
40+
SWTWorkbenchBot swtWorkbenchBot = new SWTWorkbenchBot();
41+
try
42+
{
43+
swtWorkbenchBot.viewByTitle("Welcome").close();
44+
} catch (Exception e) {
45+
46+
}
47+
}
48+
49+
@Test
50+
public void testWorkingYAMLPHPCLient01() {
51+
sourceDefinition = Constants.PETSTORE_30;
52+
targetDefinitionInProject = "petstore.yaml";
53+
this.newProjectName = "ClientStubPHPProject";
54+
setup();
55+
testOpenAPIWizardHappyPath(UIConstants.MENU_GEN_CLIENT, "PHP","composer.json");
56+
}
57+
58+
@Test
59+
public void testWorkingYAMLNodeJSServer02() {
60+
sourceDefinition = Constants.PETSTORE_30;
61+
targetDefinitionInProject = "petstore.yaml";
62+
this.newProjectName = "ServerStubNodeProject";
63+
setup();
64+
testOpenAPIWizardHappyPath(UIConstants.MENU_GEN_SERVER, "Node.js","package.json");
65+
}
66+
67+
@Test
68+
public void testWorkingYAMLHTML03() {
69+
sourceDefinition = Constants.PETSTORE_30;
70+
targetDefinitionInProject = "petstore.yaml";
71+
this.newProjectName = "genHtmlProject";
72+
setup();
73+
testOpenAPIWizardHappyPath(UIConstants.MENU_HTML, null,"index.html");
74+
}
75+
76+
77+
78+
@After
79+
public void deleteTestProject() {
80+
IProgressMonitor monitor = new NullProgressMonitor();
81+
this.testProject = ResourcesPlugin.getWorkspace().getRoot().getProject(this.newProjectName);
82+
try {
83+
this.testProject.delete(true, monitor);
84+
} catch (CoreException e) {
85+
}
86+
assertFalse("Project deletion", testProject.exists());
87+
}
88+
}

0 commit comments

Comments
 (0)