|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Broadcom, Inc. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Broadcom, Inc. - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.springframework.ide.vscode.boot.java.data.test; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | + |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.nio.file.Paths; |
| 19 | +import java.util.List; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import org.eclipse.lsp4j.Command; |
| 24 | +import org.eclipse.lsp4j.TextDocumentEdit; |
| 25 | +import org.eclipse.lsp4j.TextDocumentIdentifier; |
| 26 | +import org.eclipse.lsp4j.WorkspaceEdit; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.context.annotation.Import; |
| 32 | +import org.springframework.ide.vscode.boot.app.SpringSymbolIndex; |
| 33 | +import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest; |
| 34 | +import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf; |
| 35 | +import org.springframework.ide.vscode.boot.java.rewrite.RewriteRefactorings; |
| 36 | +import org.springframework.ide.vscode.commons.java.IJavaProject; |
| 37 | +import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; |
| 38 | +import org.springframework.ide.vscode.commons.util.text.LanguageId; |
| 39 | +import org.springframework.ide.vscode.languageserver.testharness.CodeAction; |
| 40 | +import org.springframework.ide.vscode.languageserver.testharness.Editor; |
| 41 | +import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness; |
| 42 | +import org.springframework.ide.vscode.project.harness.ProjectsHarness; |
| 43 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 44 | + |
| 45 | +import com.google.gson.JsonElement; |
| 46 | + |
| 47 | +@ExtendWith(SpringExtension.class) |
| 48 | +@BootLanguageServerTest |
| 49 | +@Import(SymbolProviderTestConf.class) |
| 50 | +public class QueryMethodCodeActionProviderMongoDbTest { |
| 51 | + |
| 52 | + @Autowired private BootLanguageServerHarness harness; |
| 53 | + @Autowired private JavaProjectFinder projectFinder; |
| 54 | + @Autowired private SpringSymbolIndex indexer; |
| 55 | + @Autowired private RewriteRefactorings refactorings; |
| 56 | + |
| 57 | + private IJavaProject testProject; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + public void setup() throws Exception { |
| 61 | + testProject = ProjectsHarness.INSTANCE.mavenProject("aot-data-repositories-mongodb"); |
| 62 | + harness.useProject(testProject); |
| 63 | + harness.intialize(null); |
| 64 | + |
| 65 | + // trigger project creation |
| 66 | + projectFinder.find(new TextDocumentIdentifier(testProject.getLocationUri().toASCIIString())).get(); |
| 67 | + |
| 68 | + CompletableFuture<Void> initProject = indexer.waitOperation(); |
| 69 | + initProject.get(5, TimeUnit.SECONDS); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void convertToQueryCodeAction() throws Exception { |
| 74 | + Path filePath = Paths.get(testProject.getLocationUri()) |
| 75 | + .resolve("src/main/java/example/springdata/aot/UserRepository.java"); |
| 76 | + Editor editor = harness.newEditor(LanguageId.JAVA, |
| 77 | + new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8), filePath.toUri().toASCIIString()); |
| 78 | + |
| 79 | + List<CodeAction> codeActions = editor.getCodeActions("findUserByLastnameStartingWith", 1); |
| 80 | + assertEquals(1, codeActions.size()); |
| 81 | + CodeAction ca = codeActions.get(0); |
| 82 | + assertEquals("Add `@Query`", ca.getLabel()); |
| 83 | + Command cmd = ca.getCommand(); |
| 84 | + assertEquals(RewriteRefactorings.REWRITE_RECIPE_QUICKFIX, cmd.getArguments().get(0)); |
| 85 | + WorkspaceEdit edit = refactorings.createEdit((JsonElement) cmd.getArguments().get(1)).get(5, TimeUnit.SECONDS); |
| 86 | + TextDocumentEdit docEdit = edit.getDocumentChanges().get(0).getLeft(); |
| 87 | + assertEquals( |
| 88 | + "@Query(\"{'lastname':{'$regex':/^\\\\Q?0\\\\E/}}\")", |
| 89 | + docEdit.getEdits().get(0).getNewText().trim()); |
| 90 | + assertEquals(filePath.toUri().toASCIIString(), docEdit.getTextDocument().getUri()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void noConvertToQueryCodeAction() throws Exception { |
| 95 | + Path filePath = Paths.get(testProject.getLocationUri()) |
| 96 | + .resolve("src/main/java/example/springdata/aot/UserRepository.java"); |
| 97 | + Editor editor = harness.newEditor(LanguageId.JAVA, |
| 98 | + new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8), filePath.toUri().toASCIIString()); |
| 99 | + |
| 100 | + List<CodeAction> codeActions = editor.getCodeActions("usersWithUsernamesStartingWith", 1); |
| 101 | + assertEquals(0, codeActions.size()); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments