|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Broadcom |
| 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 - 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.io.File; |
| 16 | +import java.util.List; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +import org.apache.commons.lang3.ArrayUtils; |
| 21 | +import org.eclipse.lsp4j.TextDocumentIdentifier; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.annotation.Import; |
| 27 | +import org.springframework.ide.vscode.boot.app.SpringSymbolIndex; |
| 28 | +import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest; |
| 29 | +import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf; |
| 30 | +import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex; |
| 31 | +import org.springframework.ide.vscode.boot.java.data.QueryMethodIndexElement; |
| 32 | +import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; |
| 33 | +import org.springframework.ide.vscode.commons.protocol.spring.Bean; |
| 34 | +import org.springframework.ide.vscode.commons.protocol.spring.DocumentElement; |
| 35 | +import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement; |
| 36 | +import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness; |
| 37 | +import org.springframework.ide.vscode.project.harness.ProjectsHarness; |
| 38 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Martin Lippert |
| 42 | + */ |
| 43 | +@ExtendWith(SpringExtension.class) |
| 44 | +@BootLanguageServerTest |
| 45 | +@Import(SymbolProviderTestConf.class) |
| 46 | +public class DataRepositoryIndexElementsTest { |
| 47 | + |
| 48 | + @Autowired private BootLanguageServerHarness harness; |
| 49 | + @Autowired private JavaProjectFinder projectFinder; |
| 50 | + @Autowired private SpringSymbolIndex indexer; |
| 51 | + @Autowired private SpringMetamodelIndex springIndex; |
| 52 | + |
| 53 | + private File directory; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + public void setup() throws Exception { |
| 57 | + harness.intialize(null); |
| 58 | + |
| 59 | + directory = new File(ProjectsHarness.class.getResource("/test-projects/test-spring-data-symbols/").toURI()); |
| 60 | + String projectDir = directory.toURI().toString(); |
| 61 | + |
| 62 | + // trigger project creation |
| 63 | + projectFinder.find(new TextDocumentIdentifier(projectDir)).get(); |
| 64 | + |
| 65 | + CompletableFuture<Void> initProject = indexer.waitOperation(); |
| 66 | + initProject.get(5, TimeUnit.SECONDS); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testSimpleRepositoryElements() throws Exception { |
| 71 | + String docUri = directory.toPath().resolve("src/main/java/org/test/CustomerRepository.java").toUri().toString(); |
| 72 | + |
| 73 | + DocumentElement document = springIndex.getDocument(docUri); |
| 74 | + List<SpringIndexElement> children = document.getChildren(); |
| 75 | + Bean repositoryElement = (Bean) children.get(0); |
| 76 | + assertEquals("customerRepository", repositoryElement.getName()); |
| 77 | + assertEquals(1, children.size()); |
| 78 | + |
| 79 | + Bean[] repoBean = this.springIndex.getBeansWithName("test-spring-data-symbols", "customerRepository"); |
| 80 | + assertEquals(1, repoBean.length); |
| 81 | + assertEquals("customerRepository", repoBean[0].getName()); |
| 82 | + assertEquals("org.test.CustomerRepository", repoBean[0].getType()); |
| 83 | + |
| 84 | + Bean[] matchingBeans = springIndex.getMatchingBeans("test-spring-data-symbols", "org.springframework.data.repository.CrudRepository"); |
| 85 | + assertEquals(3, matchingBeans.length); |
| 86 | + ArrayUtils.contains(matchingBeans, repoBean[0]); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testSimpleQueryMethodElements() throws Exception { |
| 91 | + String docUri = directory.toPath().resolve("src/main/java/org/test/CustomerRepository.java").toUri().toString(); |
| 92 | + |
| 93 | + DocumentElement document = springIndex.getDocument(docUri); |
| 94 | + List<SpringIndexElement> children = document.getChildren(); |
| 95 | + Bean repositoryElement = (Bean) children.get(0); |
| 96 | + |
| 97 | + List<SpringIndexElement> queryMethods = repositoryElement.getChildren(); |
| 98 | + assertEquals(1, queryMethods.size()); |
| 99 | + |
| 100 | + QueryMethodIndexElement queryMethod = (QueryMethodIndexElement) queryMethods.get(0); |
| 101 | + assertEquals("findByLastName", queryMethod.getMethodName()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testQueryMethodElementWithQueryString() throws Exception { |
| 106 | + String docUri = directory.toPath().resolve("src/main/java/org/test/CustomerRepositoryWithQuery.java").toUri().toString(); |
| 107 | + |
| 108 | + DocumentElement document = springIndex.getDocument(docUri); |
| 109 | + List<SpringIndexElement> children = document.getChildren(); |
| 110 | + Bean repositoryElement = (Bean) children.get(0); |
| 111 | + |
| 112 | + List<SpringIndexElement> queryMethods = repositoryElement.getChildren(); |
| 113 | + assertEquals(1, queryMethods.size()); |
| 114 | + |
| 115 | + QueryMethodIndexElement queryMethod = (QueryMethodIndexElement) queryMethods.get(0); |
| 116 | + assertEquals("findPetTypes", queryMethod.getMethodName()); |
| 117 | + assertEquals("SELECT ptype FROM PetType ptype ORDER BY ptype.name", queryMethod.getQueryString()); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments