forked from OpenLiberty/liberty-tools-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestSnippetSuite.ts
More file actions
186 lines (157 loc) · 8.48 KB
/
Copy pathrestSnippetSuite.ts
File metadata and controls
186 lines (157 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* IBM Confidential
* Copyright IBM Corp. 2026
*/
import { expect } from 'chai';
import { EditorView, TextEditor, VSBrowser, Workbench, WebDriver, BottomBarPanel, MarkerType, Key, By } from 'vscode-extension-tester';
import * as utils from '../utils/testUtils';
import { logger } from '../utils/testLogger';
import * as path from 'path';
import { ProblemsPage } from '../pages/ProblemsPage';
import { EditorPage } from '../pages/EditorPage';
import { CodeAssistPage } from '../pages/CodeAssistPage';
import { QuickFixPage } from '../pages/QuickFixPage';
import * as editorUtils from '../utils/editorUtils';
// Configuration interface for parameterizing the test suite
export interface RestSnippetConfig {
buildTool: 'maven' | 'gradle';
getProjectPath: () => string;
}
// Shared test suite that works for both Maven and Gradle
export function runRestSnippetSuite(config: RestSnippetConfig) {
describe(`Rest Class Snippet Test for ${config.buildTool === 'maven' ? 'Maven' : 'Gradle'} Project`, () => {
let editorPage: EditorPage;
let wait: any;
let driver: WebDriver;
// Compute the test file path using config
const testRestPath = path.resolve(
config.getProjectPath(),
'src', 'main', 'java', 'test', config.buildTool, 'liberty', 'web', 'app',
'TestRest.java'
);
before(async function() {
this.timeout(120000);
logger.info('Setting up rest_class snippet test');
driver = VSBrowser.instance.driver;
wait = utils.getWaitHelper();
// Open folder, wait for workbench
await VSBrowser.instance.openResources(config.getProjectPath());
await VSBrowser.instance.waitForWorkbench();
// Open file and bind page object to editor
editorPage = await new EditorPage().openFile(testRestPath, 'TestRest.java');
});
afterEach(async function() {
// Take screenshot on failure but don't close editor
if (this.currentTest?.state === 'failed') {
const driver = VSBrowser.instance.driver;
const screenshot = await driver.takeScreenshot();
logger.error(`Test failed: ${this.currentTest.title}`);
}
});
after(async function() {
this.timeout(60000);
try {
if (editorPage) {
// Select all text first, then clear
const currentText = await editorPage.getEditor().getText();
try {
await editorPage.getEditor().selectText(currentText);
await wait.sleep(300);
} catch (selectError) {
// selectText may fail but setText still works
}
await editorUtils.clearEditor(editorPage.getEditor())
logger.info('Reset TestRest.java to empty after test');
}
} catch (error) {
logger.error('Failed to reset TestRest.java', error);
}
try {
await new EditorView().closeAllEditors();
await wait.sleep(500);
logger.info('Closed all editors after test suite');
} catch (error) {
logger.error('Failed to close editors in after hook', error);
}
await utils.closeWorkspace();
utils.copyScreenshotsToProjectFolder(config.buildTool);
});
it('LSP4Jakarta Language Server should initialize', async function() {
this.timeout(300000);
logger.testStart('LSP4Jakarta Language Server should initialize');
try {
await utils.waitForLanguageServerInit(
'Language Support for Jakarta EE',
'Initializing Jakarta EE server',
240
);
logger.testComplete('LSP4Jakarta Language Server initialized successfully');
} catch (error) {
logger.testFailed('LSP4Jakarta Language Server should initialize', error);
throw error;
}
});
it('rest_class snippet populates correct REST class', async function () {
this.timeout(275000);
logger.testStart('rest_class snippet inserts correct REST class');
// at the top of the rest_class snippet test, before positioning the cursor
await editorUtils.clearEditor(editorPage.getEditor());
await wait.sleep(1500); // let any auto-stub settle, then confirm
const check = await editorPage.getEditor().getText();
logger.info('Buffer before snippet: ' + JSON.stringify(check));
try {
logger.step(1, 'Positioning cursor for snippet insertion');
// Position cursor at end of file
await editorUtils.moveCursorToEnd(editorPage.getEditor());
// Type rest and trigger snippet insertion
logger.step(2, 'Opening content assist');
const codeAssist = new CodeAssistPage();
await codeAssist.insertSnippet(editorPage, 'rest', 'rest_class');
logger.stepSuccess(2, 'Snippet inserted');
await wait.sleep(1000); // Give time for snippet to fully expand
logger.step(3, 'Verifying snippet insertion');
const codeInsertion = await editorPage.getEditor().getText();
expect(codeInsertion).to.include('@GET')
expect(codeInsertion).to.include('methodname');
logger.stepSuccess(3, 'Snippet rest_class was inserted correctly');
// Save the file so the content persists for the next test
await editorPage.getEditor().save();
await wait.sleep(500);
logger.testComplete('rest_class snippet inserts correct REST class');
} catch (error) {
logger.testFailed('rest_class snippet inserts correct REST class', error);
throw error;
}
});
it('Show that private @GET method displays diagnostic and quick fix removes it ', async function () {
this.timeout(90000);
logger.testStart('Diagnostic for a private @GET method and quick fix clears it');
try {
// Find the method with @GET and change it to private
await editorUtils.replaceTextWithinLineContaining(editorPage.getEditor(), 'methodname', 'public', 'private');
// Save file and wait for reanalysis
await editorPage.getEditor().save();
await wait.sleep(500);
// Check that the diagnostic is there
let problems = new ProblemsPage();
const found = await problems.hasDiagnostic('Only public methods can be exposed as resource methods.');
expect(found).to.be.true;
logger.testComplete('Diagnostic shows for private @GET method');
// Quick fix implementation to get rid of diagnostic
// Re-find line and place cursor on "methodname()"
const buffer = await editorPage.getEditor().getText();
logger.info('Buffer before quick fix: ' + JSON.stringify(buffer));
await new QuickFixPage().applyFix(editorPage, 'methodname', 'make method public');
await wait.sleep(3000);
await editorPage.getEditor().save();
await wait.sleep(5000);
const after = await editorPage.getEditor().getText();
expect(after).to.include('public String methodname');
expect(await problems.hasDiagnostic('Only public methods can be exposed as resource methods.')).to.be.false;
} catch (error) {
logger.testFailed('Diagnostic/quick-fix test flow failed', error);
throw error;
}
});
});
}