|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 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.maven; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertNotNull; |
| 14 | +import static org.junit.Assert.assertTrue; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.mockito.ArgumentMatchers.any; |
| 17 | +import static org.mockito.ArgumentMatchers.anyString; |
| 18 | +import static org.mockito.Mockito.mock; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Paths; |
| 23 | +import java.sql.Date; |
| 24 | +import java.time.Duration; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Optional; |
| 27 | + |
| 28 | +import org.eclipse.lsp4j.Command; |
| 29 | +import org.eclipse.lsp4j.InlayHint; |
| 30 | +import org.eclipse.lsp4j.InlayHintLabelPart; |
| 31 | +import org.eclipse.lsp4j.InlayHintParams; |
| 32 | +import org.eclipse.lsp4j.Position; |
| 33 | +import org.eclipse.lsp4j.TextDocumentIdentifier; |
| 34 | +import org.eclipse.lsp4j.jsonrpc.CancelChecker; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.springframework.ide.vscode.boot.validation.generations.SpringProjectsProvider; |
| 37 | +import org.springframework.ide.vscode.boot.validation.generations.json.Generation; |
| 38 | +import org.springframework.ide.vscode.boot.validation.generations.json.ResolvedSpringProject; |
| 39 | +import org.springframework.ide.vscode.commons.java.SpringProjectUtil; |
| 40 | +import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; |
| 41 | +import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver; |
| 42 | +import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer; |
| 43 | +import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService; |
| 44 | +import org.springframework.ide.vscode.commons.maven.java.MavenJavaProject; |
| 45 | +import org.springframework.ide.vscode.commons.util.text.LanguageId; |
| 46 | +import org.springframework.ide.vscode.commons.util.text.TextDocument; |
| 47 | +import org.springframework.ide.vscode.project.harness.ProjectsHarness; |
| 48 | + |
| 49 | +public class PomInlayHintHandlerTest { |
| 50 | + |
| 51 | + private ProjectsHarness projects = ProjectsHarness.INSTANCE; |
| 52 | + |
| 53 | + @Test |
| 54 | + void inlayProvided() throws Exception { |
| 55 | + MavenJavaProject jp = projects.mavenProject("empty-boot-15-web-app"); |
| 56 | + |
| 57 | + TextDocument doc = new TextDocument(jp.getProjectBuild().getBuildFile().toASCIIString(), LanguageId.XML, 0, Files.readString(Paths.get(jp.getProjectBuild().getBuildFile()))); |
| 58 | + |
| 59 | + JavaProjectFinder projectFinder = mock(JavaProjectFinder.class); |
| 60 | + when(projectFinder.find(any())).thenReturn(Optional.of(jp)); |
| 61 | + |
| 62 | + SimpleTextDocumentService documents = mock(SimpleTextDocumentService.class); |
| 63 | + when(documents.getLatestSnapshot(anyString())).thenReturn(doc); |
| 64 | + |
| 65 | + SimpleLanguageServer server = mock(SimpleLanguageServer.class); |
| 66 | + when(server.getTextDocumentService()).thenReturn(documents); |
| 67 | + |
| 68 | + Generation generation = mock(Generation.class); |
| 69 | + when(generation.getOssSupportEndDate()).thenReturn(new Date(System.currentTimeMillis() + Duration.ofDays(7).toMillis()).toString()); |
| 70 | + when(generation.getName()).thenReturn("1.5.8"); |
| 71 | + |
| 72 | + ResolvedSpringProject resolvedProject = mock(ResolvedSpringProject.class); |
| 73 | + when(resolvedProject.getGenerations()).thenReturn(List.of(generation)); |
| 74 | + when(resolvedProject.getSlug()).thenReturn(SpringProjectUtil.SPRING_BOOT); |
| 75 | + |
| 76 | + SpringProjectsProvider projectProvider = mock(SpringProjectsProvider.class); |
| 77 | + when(projectProvider.getProject(SpringProjectUtil.SPRING_BOOT)).thenReturn(resolvedProject); |
| 78 | + |
| 79 | + PomInlayHintHandler inlayHanlder = new PomInlayHintHandler(server, projectFinder, ProjectObserver.NULL, projectProvider); |
| 80 | + |
| 81 | + List<InlayHint> hints = inlayHanlder.handle(mock(CancelChecker.class), new InlayHintParams(new TextDocumentIdentifier(doc.getUri()), doc.toRange(0, doc.getLength()))); |
| 82 | + |
| 83 | + assertEquals(1, hints.size()); |
| 84 | + |
| 85 | + InlayHint hint = hints.get(0); |
| 86 | + |
| 87 | + assertEquals(new Position(27, 15), hint.getPosition()); |
| 88 | + |
| 89 | + assertTrue(hint.getLabel().isRight()); |
| 90 | + |
| 91 | + assertEquals(1, hint.getLabel().getRight().size()); |
| 92 | + |
| 93 | + InlayHintLabelPart labelPart = hint.getLabel().getRight().get(0); |
| 94 | + |
| 95 | + assertEquals("Add Spring Boot Starters...", labelPart.getValue()); |
| 96 | + |
| 97 | + Command cmd = labelPart.getCommand(); |
| 98 | + |
| 99 | + assertNotNull(cmd); |
| 100 | + |
| 101 | + assertEquals("spring.initializr.addStarters", cmd.getCommand()); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void inlayNotProvidedOutOfOssSupport() throws Exception { |
| 107 | + MavenJavaProject jp = projects.mavenProject("empty-boot-15-web-app"); |
| 108 | + |
| 109 | + TextDocument doc = new TextDocument(jp.getProjectBuild().getBuildFile().toASCIIString(), LanguageId.XML, 0, Files.readString(Paths.get(jp.getProjectBuild().getBuildFile()))); |
| 110 | + |
| 111 | + JavaProjectFinder projectFinder = mock(JavaProjectFinder.class); |
| 112 | + when(projectFinder.find(any())).thenReturn(Optional.of(jp)); |
| 113 | + |
| 114 | + SimpleTextDocumentService documents = mock(SimpleTextDocumentService.class); |
| 115 | + when(documents.getLatestSnapshot(anyString())).thenReturn(doc); |
| 116 | + |
| 117 | + SimpleLanguageServer server = mock(SimpleLanguageServer.class); |
| 118 | + when(server.getTextDocumentService()).thenReturn(documents); |
| 119 | + |
| 120 | + Generation generation = mock(Generation.class); |
| 121 | + when(generation.getOssSupportEndDate()).thenReturn(new Date(System.currentTimeMillis() - Duration.ofDays(7).toMillis()).toString()); |
| 122 | + when(generation.getName()).thenReturn("1.5.8"); |
| 123 | + |
| 124 | + ResolvedSpringProject resolvedProject = mock(ResolvedSpringProject.class); |
| 125 | + when(resolvedProject.getGenerations()).thenReturn(List.of(generation)); |
| 126 | + when(resolvedProject.getSlug()).thenReturn(SpringProjectUtil.SPRING_BOOT); |
| 127 | + |
| 128 | + SpringProjectsProvider projectProvider = mock(SpringProjectsProvider.class); |
| 129 | + when(projectProvider.getProject(SpringProjectUtil.SPRING_BOOT)).thenReturn(resolvedProject); |
| 130 | + |
| 131 | + PomInlayHintHandler inlayHanlder = new PomInlayHintHandler(server, projectFinder, ProjectObserver.NULL, projectProvider); |
| 132 | + |
| 133 | + List<InlayHint> hints = inlayHanlder.handle(mock(CancelChecker.class), new InlayHintParams(new TextDocumentIdentifier(doc.getUri()), doc.toRange(0, doc.getLength()))); |
| 134 | + |
| 135 | + assertEquals(0, hints.size()); |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments