|
|
|
|
@@ -0,0 +1,104 @@
|
|
|
|
|
/*******************************************************************************
|
|
|
|
|
* Copyright (c) 2025 Broadcom, Inc.
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
|
* https://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
*
|
|
|
|
|
* Contributors:
|
|
|
|
|
* Broadcom, Inc. - initial API and implementation
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
package org.springframework.ide.vscode.boot.java.data.test;
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import org.eclipse.lsp4j.Command;
|
|
|
|
|
import org.eclipse.lsp4j.TextDocumentEdit;
|
|
|
|
|
import org.eclipse.lsp4j.TextDocumentIdentifier;
|
|
|
|
|
import org.eclipse.lsp4j.WorkspaceEdit;
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
|
import org.springframework.ide.vscode.boot.app.SpringSymbolIndex;
|
|
|
|
|
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
|
|
|
|
|
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf;
|
|
|
|
|
import org.springframework.ide.vscode.boot.java.rewrite.RewriteRefactorings;
|
|
|
|
|
import org.springframework.ide.vscode.commons.java.IJavaProject;
|
|
|
|
|
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
|
|
|
|
|
import org.springframework.ide.vscode.commons.util.text.LanguageId;
|
|
|
|
|
import org.springframework.ide.vscode.languageserver.testharness.CodeAction;
|
|
|
|
|
import org.springframework.ide.vscode.languageserver.testharness.Editor;
|
|
|
|
|
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
|
|
|
|
|
import org.springframework.ide.vscode.project.harness.ProjectsHarness;
|
|
|
|
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
|
|
|
|
|
|
@ExtendWith(SpringExtension.class)
|
|
|
|
|
@BootLanguageServerTest
|
|
|
|
|
@Import(SymbolProviderTestConf.class)
|
|
|
|
|
public class QueryMethodCodeActionProviderMongoDbTest {
|
|
|
|
|
|
|
|
|
|
@Autowired private BootLanguageServerHarness harness;
|
|
|
|
|
@Autowired private JavaProjectFinder projectFinder;
|
|
|
|
|
@Autowired private SpringSymbolIndex indexer;
|
|
|
|
|
@Autowired private RewriteRefactorings refactorings;
|
|
|
|
|
|
|
|
|
|
private IJavaProject testProject;
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
public void setup() throws Exception {
|
|
|
|
|
testProject = ProjectsHarness.INSTANCE.mavenProject("aot-data-repositories-mongodb");
|
|
|
|
|
harness.useProject(testProject);
|
|
|
|
|
harness.intialize(null);
|
|
|
|
|
|
|
|
|
|
// trigger project creation
|
|
|
|
|
projectFinder.find(new TextDocumentIdentifier(testProject.getLocationUri().toASCIIString())).get();
|
|
|
|
|
|
|
|
|
|
CompletableFuture<Void> initProject = indexer.waitOperation();
|
|
|
|
|
initProject.get(5, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void convertToQueryCodeAction() throws Exception {
|
|
|
|
|
Path filePath = Paths.get(testProject.getLocationUri())
|
|
|
|
|
.resolve("src/main/java/example/springdata/aot/UserRepository.java");
|
|
|
|
|
Editor editor = harness.newEditor(LanguageId.JAVA,
|
|
|
|
|
new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8), filePath.toUri().toASCIIString());
|
|
|
|
|
|
|
|
|
|
List<CodeAction> codeActions = editor.getCodeActions("findUserByLastnameStartingWith", 1);
|
|
|
|
|
assertEquals(1, codeActions.size());
|
|
|
|
|
CodeAction ca = codeActions.get(0);
|
|
|
|
|
assertEquals("Add `@Query`", ca.getLabel());
|
|
|
|
|
Command cmd = ca.getCommand();
|
|
|
|
|
assertEquals(RewriteRefactorings.REWRITE_RECIPE_QUICKFIX, cmd.getArguments().get(0));
|
|
|
|
|
WorkspaceEdit edit = refactorings.createEdit((JsonElement) cmd.getArguments().get(1)).get(5, TimeUnit.SECONDS);
|
|
|
|
|
TextDocumentEdit docEdit = edit.getDocumentChanges().get(0).getLeft();
|
|
|
|
|
assertEquals(
|
|
|
|
|
"@Query(\"{'lastname':{'$regex':/^\\\\Q?0\\\\E/}}\")",
|
|
|
|
|
docEdit.getEdits().get(0).getNewText().trim());
|
|
|
|
|
assertEquals(filePath.toUri().toASCIIString(), docEdit.getTextDocument().getUri());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void noConvertToQueryCodeAction() throws Exception {
|
|
|
|
|
Path filePath = Paths.get(testProject.getLocationUri())
|
|
|
|
|
.resolve("src/main/java/example/springdata/aot/UserRepository.java");
|
|
|
|
|
Editor editor = harness.newEditor(LanguageId.JAVA,
|
|
|
|
|
new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8), filePath.toUri().toASCIIString());
|
|
|
|
|
|
|
|
|
|
List<CodeAction> codeActions = editor.getCodeActions("usersWithUsernamesStartingWith", 1);
|
|
|
|
|
assertEquals(0, codeActions.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|