2323import org .eclipse .codewind .openapi .ui .test .menus .utils .UITestUtilities ;
2424import org .eclipse .core .resources .IFile ;
2525import org .eclipse .core .resources .IProject ;
26+ import org .eclipse .core .resources .IWorkspaceRoot ;
27+ import org .eclipse .core .resources .ResourcesPlugin ;
2628import 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 ;
2732import org .eclipse .swtbot .eclipse .finder .SWTWorkbenchBot ;
2833import org .eclipse .swtbot .eclipse .finder .widgets .SWTBotView ;
2934import org .eclipse .swtbot .swt .finder .SWTBotTestCase ;
3035import 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 ;
3138import org .eclipse .swtbot .swt .finder .widgets .SWTBotMenu ;
3239import org .eclipse .swtbot .swt .finder .widgets .SWTBotTreeItem ;
3340
3441public 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
0 commit comments