-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add endpoint detection (#2938)
* feat: add endpoint detection * internal marker and test * fix assert messages * fix exising tests and add a new test for endpoint usage detector * feat(engine): skip running Node and cleanup generated endpoints on empty OpenAPI * chore: remove EndpointUsageDetector service file * refactor: replace @InternalBrowserCallable with a hard-coded list * test: update no-endponts IT module * test(no-endpoints): assert that internal endpoints are not generated * chore: formatting * chore(endpoint): remove extra line * fix(engine-core): do not fail if generated-file-list.txt does not exist --------- Co-authored-by: Mikhail Shabarov <[email protected]> Co-authored-by: Mikhail Shabarov <[email protected]> Co-authored-by: Anton Platonov <[email protected]>
- Loading branch information
1 parent
c2d0e1c
commit bf63b54
Showing
18 changed files
with
445 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
...ne-runtime/src/test/java/com/vaadin/hilla/internal/AbstractTaskEndpointGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/java/engine-runtime/src/test/java/com/vaadin/hilla/internal/EndpointsTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.vaadin.hilla.internal; | ||
|
||
import com.vaadin.flow.server.frontend.FrontendUtils; | ||
import com.vaadin.hilla.ApplicationContextProvider; | ||
import com.vaadin.hilla.internal.fixtures.CustomEndpoint; | ||
import com.vaadin.hilla.internal.fixtures.EndpointNoValue; | ||
import com.vaadin.hilla.internal.fixtures.MyEndpoint; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.util.FileSystemUtils; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
@SpringBootTest(classes = { CustomEndpoint.class, EndpointNoValue.class, | ||
MyEndpoint.class, ApplicationContextProvider.class }) | ||
public class EndpointsTaskTest extends TaskTest { | ||
|
||
static private Path npmDependenciesTempDirectory; | ||
|
||
@BeforeAll | ||
public static void setupNpmDependencies() | ||
throws IOException, FrontendUtils.CommandExecutionException, | ||
InterruptedException, URISyntaxException { | ||
npmDependenciesTempDirectory = Files | ||
.createTempDirectory(EndpointsTaskTest.class.getName()); | ||
|
||
Path packagesPath = Path | ||
.of(Objects.requireNonNull(EndpointsTaskTest.class | ||
.getClassLoader().getResource("")).toURI()) | ||
.getParent() // target | ||
.getParent() // engine-runtime | ||
.getParent() // java | ||
.getParent(); // packages | ||
|
||
Path projectRoot = packagesPath.getParent(); | ||
Files.copy(projectRoot.resolve(".npmrc"), | ||
npmDependenciesTempDirectory.resolve(".npmrc")); | ||
var tsPackagesDirectory = packagesPath.resolve("ts"); | ||
|
||
var shellCmd = FrontendUtils.isWindows() ? Stream.of("cmd.exe", "/c") | ||
: Stream.<String> empty(); | ||
|
||
var npmCmd = Stream.of("npm", "--no-update-notifier", "--no-audit", | ||
"install", "--no-save", "--install-links"); | ||
|
||
var generatorFiles = Files.list(tsPackagesDirectory) | ||
.map(Path::toString); | ||
|
||
var command = Stream.of(shellCmd, npmCmd, generatorFiles) | ||
.flatMap(Function.identity()).toList(); | ||
|
||
var processBuilder = FrontendUtils.createProcessBuilder(command) | ||
.directory(npmDependenciesTempDirectory.toFile()) | ||
.redirectOutput(ProcessBuilder.Redirect.INHERIT) | ||
.redirectError(ProcessBuilder.Redirect.INHERIT); | ||
var exitCode = processBuilder.start().waitFor(); | ||
if (exitCode != 0) { | ||
throw new FrontendUtils.CommandExecutionException(exitCode); | ||
} | ||
} | ||
|
||
@BeforeEach | ||
public void copyNpmDependencies() throws IOException { | ||
FileSystemUtils.copyRecursively(npmDependenciesTempDirectory, | ||
getTemporaryDirectory()); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...ages/java/engine-runtime/src/test/java/com/vaadin/hilla/internal/NoEndpointsTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.vaadin.hilla.internal; | ||
|
||
import com.vaadin.flow.server.ExecutionFailedException; | ||
import com.vaadin.flow.server.frontend.TaskGenerateEndpoint; | ||
import com.vaadin.flow.server.frontend.TaskGenerateOpenAPI; | ||
import com.vaadin.hilla.ApplicationContextProvider; | ||
import com.vaadin.hilla.engine.GeneratorProcessor; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.ApplicationContext; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@SpringBootTest(classes = { | ||
NoEndpointsTaskTest.NoopApplicationContextProvider.class }) | ||
public class NoEndpointsTaskTest extends TaskTest { | ||
private TaskGenerateOpenAPI taskGenerateOpenApi; | ||
private TaskGenerateEndpoint taskGenerateEndpoint; | ||
|
||
@Autowired | ||
ApplicationContext applicationContext; | ||
|
||
@Test | ||
public void should_GenerateEmptySchema_when_NoEndpointsFound() | ||
throws ExecutionFailedException, IOException, URISyntaxException { | ||
// Mock ApplicationContextProvider static API to prevent interference | ||
// with other tests. | ||
try (var mockApplicationContextProvider = Mockito | ||
.mockStatic(ApplicationContextProvider.class)) { | ||
mockApplicationContextProvider | ||
.when(ApplicationContextProvider::getApplicationContext) | ||
.thenReturn(applicationContext); | ||
mockApplicationContextProvider | ||
.when(() -> ApplicationContextProvider | ||
.runOnContext(Mockito.any())) | ||
.thenAnswer(invocationOnMock -> { | ||
invocationOnMock | ||
.<Consumer<ApplicationContext>> getArgument(0) | ||
.accept(applicationContext); | ||
return null; | ||
}); | ||
|
||
// Create files resembling output for previously existing endpoints | ||
var outputDirectory = Files.createDirectory( | ||
getTemporaryDirectory().resolve(getOutputDirectory())); | ||
var generatedFileListPath = outputDirectory | ||
.resolve(GeneratorProcessor.GENERATED_FILE_LIST_NAME); | ||
var referenceFileListPath = Path.of(Objects | ||
.requireNonNull(getClass().getResource( | ||
GeneratorProcessor.GENERATED_FILE_LIST_NAME)) | ||
.toURI()); | ||
Files.copy(referenceFileListPath, generatedFileListPath); | ||
var referenceFileList = Files.readAllLines(referenceFileListPath); | ||
for (String line : referenceFileList) { | ||
var path = outputDirectory.resolve(line); | ||
Files.createDirectories(path.getParent()); | ||
Files.createFile(path); | ||
} | ||
var arbitraryGeneratedFile = outputDirectory.resolve("vaadin.ts"); | ||
Files.createFile(arbitraryGeneratedFile); | ||
|
||
taskGenerateOpenApi = new TaskGenerateOpenAPIImpl( | ||
getEngineConfiguration()); | ||
taskGenerateEndpoint = new TaskGenerateEndpointImpl( | ||
getEngineConfiguration()); | ||
|
||
taskGenerateOpenApi.execute(); | ||
|
||
var generatedOpenAPI = getGeneratedOpenAPI(); | ||
|
||
assertNull(generatedOpenAPI.getTags(), | ||
"Expected OpenAPI tags to be null"); | ||
assertTrue(generatedOpenAPI.getPaths().isEmpty(), | ||
"Expected OpenAPI paths to be empty"); | ||
assertNull(generatedOpenAPI.getComponents(), | ||
"Expected OpenAPI schemas to be null"); | ||
|
||
assertDoesNotThrow(taskGenerateEndpoint::execute, | ||
"Expected to not fail without npm dependencies"); | ||
|
||
assertFalse(generatedFileListPath.toFile().exists(), | ||
"Expected file list to be deleted"); | ||
for (String line : referenceFileList) { | ||
var path = outputDirectory.resolve(line); | ||
assertFalse(path.toFile().exists(), | ||
String.format("Expected file %s to be deleted", path)); | ||
} | ||
assertTrue(arbitraryGeneratedFile.toFile().exists(), | ||
"Expected non-Hilla generated file to not be deleted"); | ||
} | ||
} | ||
|
||
static class NoopApplicationContextProvider | ||
extends ApplicationContextProvider { | ||
@Override | ||
public void setApplicationContext( | ||
@Nonnull ApplicationContext applicationContext) | ||
throws BeansException { | ||
// do nothing | ||
} | ||
} | ||
} |
Oops, something went wrong.