Skip to content

Commit bf994f1

Browse files
shivam71Achal1607
authored andcommitted
Unit tests for notebook module
1. Added mock lsp client helper class 2. Added test for NotebookConfigs class 3. Updated test dependency in project.xml
1 parent d39fe55 commit bf994f1

File tree

3 files changed

+389
-0
lines changed

3 files changed

+389
-0
lines changed

nbcode/notebooks/nbproject/project.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@
153153
<recursive/>
154154
<compile-dependency/>
155155
</test-dependency>
156+
<test-dependency>
157+
<code-name-base>org.netbeans.modules.java.lsp.server</code-name-base>
158+
<compile-dependency/>
159+
</test-dependency>
156160
</test-type>
157161
</test-dependencies>
158162
<public-packages/>
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may 'ou may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.netbeans.modules.nbcode.java.notebook;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.concurrent.CompletableFuture;
22+
import org.eclipse.lsp4j.ConfigurationParams;
23+
import org.eclipse.lsp4j.MessageActionItem;
24+
import org.eclipse.lsp4j.MessageParams;
25+
import org.eclipse.lsp4j.PublishDiagnosticsParams;
26+
import org.eclipse.lsp4j.ShowMessageRequestParams;
27+
import org.eclipse.lsp4j.jsonrpc.messages.Either;
28+
import org.netbeans.modules.java.lsp.server.explorer.api.NodeChangedParams;
29+
import org.netbeans.modules.java.lsp.server.input.QuickPickItem;
30+
import org.netbeans.modules.java.lsp.server.input.ShowInputBoxParams;
31+
import org.netbeans.modules.java.lsp.server.input.ShowMutliStepInputParams;
32+
import org.netbeans.modules.java.lsp.server.input.ShowQuickPickParams;
33+
import org.netbeans.modules.java.lsp.server.notebook.CellStateResponse;
34+
import org.netbeans.modules.java.lsp.server.notebook.NotebookCellExecutionProgressResultParams;
35+
import org.netbeans.modules.java.lsp.server.notebook.NotebookCellStateParams;
36+
import org.netbeans.modules.java.lsp.server.protocol.DecorationRenderOptions;
37+
import org.netbeans.modules.java.lsp.server.protocol.HtmlPageParams;
38+
import org.netbeans.modules.java.lsp.server.protocol.NbCodeClientCapabilities;
39+
import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient;
40+
import org.netbeans.modules.java.lsp.server.protocol.OutputMessage;
41+
import org.netbeans.modules.java.lsp.server.protocol.SaveDocumentRequestParams;
42+
import org.netbeans.modules.java.lsp.server.protocol.SetTextEditorDecorationParams;
43+
import org.netbeans.modules.java.lsp.server.protocol.ShowStatusMessageParams;
44+
import org.netbeans.modules.java.lsp.server.protocol.TestProgressParams;
45+
import org.netbeans.modules.java.lsp.server.protocol.UpdateConfigParams;
46+
47+
/**
48+
* Overrides all the methods with UnsupportedOperation extend this class and
49+
* override the method you want to mock
50+
*
51+
* @author shimadan
52+
*/
53+
public class MockNbClient implements NbCodeLanguageClient {
54+
55+
@Override
56+
public NbCodeClientCapabilities getNbCodeCapabilities() {
57+
NbCodeClientCapabilities caps = new NbCodeClientCapabilities();
58+
caps.setConfigurationPrefix("jdk.");
59+
return caps;
60+
}
61+
62+
@Override
63+
public CompletableFuture<Void> configurationUpdate(UpdateConfigParams ucp) {
64+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
65+
}
66+
67+
@Override
68+
public CompletableFuture<List<Object>> configuration(ConfigurationParams configurationParams) {
69+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
70+
}
71+
72+
@Override
73+
public void showStatusBarMessage(ShowStatusMessageParams ssmp) {
74+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
75+
}
76+
77+
@Override
78+
public CompletableFuture<String> showHtmlPage(HtmlPageParams hpp) {
79+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
80+
}
81+
82+
@Override
83+
public CompletableFuture<String> execInHtmlPage(HtmlPageParams hpp) {
84+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
85+
}
86+
87+
@Override
88+
public CompletableFuture<List<QuickPickItem>> showQuickPick(ShowQuickPickParams sqpp) {
89+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
90+
}
91+
92+
@Override
93+
public CompletableFuture<String> showInputBox(ShowInputBoxParams sibp) {
94+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
95+
}
96+
97+
@Override
98+
public CompletableFuture<Map<String, Either<List<QuickPickItem>, String>>> showMultiStepInput(ShowMutliStepInputParams smsip) {
99+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
100+
}
101+
102+
@Override
103+
public void notifyTestProgress(TestProgressParams tpp) {
104+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
105+
}
106+
107+
@Override
108+
public CompletableFuture<String> createTextEditorDecoration(DecorationRenderOptions dro) {
109+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
110+
}
111+
112+
@Override
113+
public void setTextEditorDecoration(SetTextEditorDecorationParams stedp) {
114+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
115+
}
116+
117+
@Override
118+
public void disposeTextEditorDecoration(String params) {
119+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
120+
}
121+
122+
@Override
123+
public void notifyNodeChange(NodeChangedParams ncp) {
124+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
125+
}
126+
127+
@Override
128+
public CompletableFuture<Boolean> requestDocumentSave(SaveDocumentRequestParams sdrp) {
129+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
130+
}
131+
132+
@Override
133+
public CompletableFuture<Void> writeOutput(OutputMessage om) {
134+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
135+
}
136+
137+
@Override
138+
public CompletableFuture<Void> showOutput(String outputName) {
139+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
140+
}
141+
142+
@Override
143+
public CompletableFuture<Void> closeOutput(String outputName) {
144+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
145+
}
146+
147+
@Override
148+
public CompletableFuture<Void> resetOutput(String outputName) {
149+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
150+
}
151+
152+
@Override
153+
public void telemetryEvent(Object object) {
154+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
155+
}
156+
157+
@Override
158+
public void publishDiagnostics(PublishDiagnosticsParams diagnostics) {
159+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
160+
}
161+
162+
@Override
163+
public void showMessage(MessageParams messageParams) {
164+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
165+
}
166+
167+
@Override
168+
public CompletableFuture<MessageActionItem> showMessageRequest(ShowMessageRequestParams requestParams) {
169+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
170+
}
171+
172+
@Override
173+
public void logMessage(MessageParams message) {
174+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
175+
}
176+
177+
@Override
178+
public void notifyNotebookCellExecutionProgress(NotebookCellExecutionProgressResultParams params) {
179+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
180+
}
181+
182+
@Override
183+
public CompletableFuture<CellStateResponse> getNotebookCellState(NotebookCellStateParams params) {
184+
throw new UnsupportedOperationException("Not supported yet.");
185+
}
186+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may 'ou may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.netbeans.modules.nbcode.java.notebook;
18+
19+
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonArray;
21+
import com.google.gson.JsonPrimitive;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.concurrent.CompletableFuture;
25+
import java.util.concurrent.TimeUnit;
26+
import org.eclipse.lsp4j.ConfigurationItem;
27+
import org.eclipse.lsp4j.ConfigurationParams;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
import static org.junit.Assert.*;
32+
33+
/* TODO
34+
test multiple configuration scenarios
35+
1.Client sends nulls/empty
36+
2.Missing keys
37+
*/
38+
39+
/*
40+
Version 1 21/08/25
41+
*/
42+
43+
/**
44+
* Mock LSP Client sending sample configurations
45+
* Verifies that the NotebookConfigs class
46+
* parses and handles configurations appropriately
47+
*
48+
* @author shimadan
49+
*/
50+
public class NotebookConfigsTest {
51+
52+
private NotebookConfigs instance;
53+
private CompletableFuture<Void> initialized;
54+
private JsonObject configsObj = new JsonObject();
55+
private static final String CLASSPATH_KEY = "jdk.notebook.classpath";
56+
private static final String IMPLICIT_IMPORTS_KEY = "jdk.notebook.implicitImports";
57+
private static final String ADD_MODULES_KEY = "jdk.notebook.addmodules";
58+
private static final String ENABLE_PREVIEW_KEY = "jdk.notebook.enablePreview";
59+
private static final String MODULEPATH_KEY = "jdk.notebook.modulepath";
60+
61+
public NotebookConfigsTest() {
62+
}
63+
64+
@Before
65+
public void setUp() {
66+
setConfigObject();
67+
LanguageClientInstance.getInstance().
68+
setClient(new MockNbClientConfigs());
69+
instance = NotebookConfigs.getInstance();
70+
instance.initConfigs();
71+
initialized = instance.getInitialized();
72+
}
73+
74+
@After
75+
public void tearDown() {
76+
}
77+
78+
/**
79+
* Test of getInitialized method, of class NotebookConfigs.
80+
*/
81+
@Test
82+
public void testGetInitialized() {
83+
System.out.println("getInitialized");
84+
CompletableFuture<Void> result = instance.getInitialized();
85+
try {
86+
result.get(5, TimeUnit.SECONDS);
87+
} catch (Exception ex) {
88+
fail("Configuration initialization failed");
89+
}
90+
}
91+
92+
/**
93+
* Test of getClassPath method, of class NotebookConfigs.
94+
*/
95+
@Test
96+
public void testGetClassPath() {
97+
System.out.println("getClassPath");
98+
try {
99+
initialized.get(5, TimeUnit.SECONDS);
100+
String expResult = configsObj.get(CLASSPATH_KEY).getAsString();
101+
String result = instance.getClassPath();
102+
assertEquals(expResult, result);
103+
} catch (Exception ex) {
104+
fail("Configuration initialization failed");
105+
}
106+
}
107+
108+
/**
109+
* Test of getModulePath method, of class NotebookConfigs.
110+
*/
111+
@Test
112+
public void testGetModulePath() {
113+
System.out.println("getModulePath");
114+
115+
try {
116+
initialized.get(5, TimeUnit.SECONDS);
117+
String expResult = configsObj.get(MODULEPATH_KEY).getAsString();
118+
String result = instance.getModulePath();
119+
assertEquals(expResult, result);
120+
} catch (Exception ex) {
121+
fail("Configuration initialization failed");
122+
}
123+
}
124+
125+
/**
126+
* Test of getAddModules method, of class NotebookConfigs.
127+
*/
128+
@Test
129+
public void testGetAddModules() {
130+
System.out.println("getAddModules");
131+
try {
132+
initialized.get(5, TimeUnit.SECONDS);
133+
String expResult = configsObj.get(ADD_MODULES_KEY).getAsString();
134+
String result = instance.getAddModules();
135+
assertEquals(expResult, result);
136+
} catch (Exception ex) {
137+
fail("Configuration initialization failed");
138+
}
139+
}
140+
141+
/**
142+
* Test of isEnablePreview method, of class NotebookConfigs.
143+
*/
144+
@Test
145+
public void testIsEnablePreview() {
146+
System.out.println("getIsEnablePreview");
147+
try {
148+
initialized.get(5, TimeUnit.SECONDS);
149+
boolean expResult = configsObj.get(ENABLE_PREVIEW_KEY).getAsBoolean();
150+
boolean result = instance.isEnablePreview();
151+
assertEquals(expResult, result);
152+
} catch (Exception ex) {
153+
fail("Configuration initialization failed");
154+
}
155+
}
156+
157+
/**
158+
* Test of getImplicitImports method, of class NotebookConfigs.
159+
*/
160+
@Test
161+
public void testGetImplicitImports() {
162+
System.out.println("getImplicitImports");
163+
try {
164+
initialized.get(5, TimeUnit.SECONDS);
165+
List<String> expResult = configsObj.get(IMPLICIT_IMPORTS_KEY).
166+
getAsJsonArray().asList().stream().
167+
map((elem) -> elem.getAsString()).toList();
168+
List<String> result = instance.getImplicitImports();
169+
assertEquals(expResult, result);
170+
} catch (Exception ex) {
171+
fail("Configuration initialization failed");
172+
}
173+
}
174+
175+
private void setConfigObject() {
176+
JsonArray imports = new JsonArray();
177+
imports.add(new JsonPrimitive("java.math.*"));
178+
imports.add(new JsonPrimitive("javafx.scene.control.*"));
179+
configsObj.add(IMPLICIT_IMPORTS_KEY, imports);
180+
configsObj.add(CLASSPATH_KEY, new JsonPrimitive("path/to/javafx-sdk-24.0.1/lib/javafx.base.jar"));
181+
configsObj.add(MODULEPATH_KEY, new JsonPrimitive("/path/to/javafx-sdk/lib"));
182+
configsObj.add(ENABLE_PREVIEW_KEY, new JsonPrimitive(false));
183+
configsObj.add(ADD_MODULES_KEY, new JsonPrimitive("javafx.controls,javafx.graphics"));
184+
185+
}
186+
187+
private class MockNbClientConfigs extends MockNbClient {
188+
189+
@Override
190+
public CompletableFuture<List<Object>> configuration(ConfigurationParams configurationParams) {
191+
List<ConfigurationItem> items = configurationParams.getItems();
192+
List<Object> configs = new ArrayList<>();
193+
for (ConfigurationItem item : items) {
194+
configs.add(configsObj.get(item.getSection()));
195+
}
196+
return CompletableFuture.completedFuture(configs);
197+
}
198+
}
199+
}

0 commit comments

Comments
 (0)