|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.helidon.declarative.codegen.http.webserver; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import io.helidon.builder.api.Prototype; |
| 26 | +import io.helidon.codegen.apt.AptProcessor; |
| 27 | +import io.helidon.codegen.testing.TestCompiler; |
| 28 | +import io.helidon.common.Default; |
| 29 | +import io.helidon.common.Generated; |
| 30 | +import io.helidon.common.GenericType; |
| 31 | +import io.helidon.common.mapper.Mappers; |
| 32 | +import io.helidon.common.parameters.Parameters; |
| 33 | +import io.helidon.common.types.Annotation; |
| 34 | +import io.helidon.common.uri.UriQuery; |
| 35 | +import io.helidon.config.Config; |
| 36 | +import io.helidon.http.Http; |
| 37 | +import io.helidon.service.registry.Dependency; |
| 38 | +import io.helidon.service.registry.Service; |
| 39 | +import io.helidon.service.registry.ServiceDescriptor; |
| 40 | +import io.helidon.webserver.http.Handler; |
| 41 | +import io.helidon.webserver.http.HttpEntryPoint; |
| 42 | +import io.helidon.webserver.http.HttpFeature; |
| 43 | +import io.helidon.webserver.http.HttpRoute; |
| 44 | +import io.helidon.webserver.http.HttpRouting; |
| 45 | +import io.helidon.webserver.http.HttpRules; |
| 46 | +import io.helidon.webserver.http.RestServer; |
| 47 | +import io.helidon.webserver.http.ServerRequest; |
| 48 | +import io.helidon.webserver.http.ServerResponse; |
| 49 | + |
| 50 | +import org.junit.jupiter.api.Test; |
| 51 | + |
| 52 | +import static org.hamcrest.CoreMatchers.containsString; |
| 53 | +import static org.hamcrest.CoreMatchers.is; |
| 54 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 55 | + |
| 56 | +class QueryParamDefaultValueCodegenTest { |
| 57 | + private static final List<Class<?>> CLASSPATH = List.of( |
| 58 | + Annotation.class, |
| 59 | + Config.class, |
| 60 | + Default.class, |
| 61 | + Dependency.class, |
| 62 | + Generated.class, |
| 63 | + GenericType.class, |
| 64 | + Handler.class, |
| 65 | + Http.class, |
| 66 | + HttpEntryPoint.class, |
| 67 | + HttpFeature.class, |
| 68 | + HttpRoute.class, |
| 69 | + HttpRouting.class, |
| 70 | + HttpRules.class, |
| 71 | + Mappers.class, |
| 72 | + Parameters.class, |
| 73 | + Prototype.class, |
| 74 | + RestServer.class, |
| 75 | + ServerRequest.class, |
| 76 | + ServerResponse.class, |
| 77 | + Service.class, |
| 78 | + ServiceDescriptor.class, |
| 79 | + UriQuery.class |
| 80 | + ); |
| 81 | + |
| 82 | + @Test |
| 83 | + void generatedQueryDefaultUsesParameterType() throws IOException { |
| 84 | + var result = TestCompiler.builder() |
| 85 | + .currentRelease() |
| 86 | + .addClasspath(CLASSPATH) |
| 87 | + .addProcessor(AptProcessor::new) |
| 88 | + .workDir(Path.of("target/test-compiler/http-query-default-value")) |
| 89 | + .addSource("DefaultQueryEndpoint.java", """ |
| 90 | + package com.example; |
| 91 | +
|
| 92 | + import io.helidon.common.Default; |
| 93 | + import io.helidon.http.Http; |
| 94 | + import io.helidon.service.registry.Service; |
| 95 | + import io.helidon.webserver.http.RestServer; |
| 96 | +
|
| 97 | + @SuppressWarnings("deprecation") |
| 98 | + @RestServer.Listener("@default") |
| 99 | + @RestServer.Endpoint |
| 100 | + @Service.Singleton |
| 101 | + @Http.Path("/default-query") |
| 102 | + class DefaultQueryEndpoint { |
| 103 | + @Http.GET |
| 104 | + String limit(@Http.QueryParam("limit") @Default.Value("13") Integer limit) { |
| 105 | + return Integer.toString(limit); |
| 106 | + } |
| 107 | + } |
| 108 | + """) |
| 109 | + .addSource("Main.java", """ |
| 110 | + package com.example; |
| 111 | +
|
| 112 | + import io.helidon.service.registry.Service; |
| 113 | +
|
| 114 | + @Service.GenerateBinding |
| 115 | + class Main { |
| 116 | + } |
| 117 | + """) |
| 118 | + .build() |
| 119 | + .compile(); |
| 120 | + |
| 121 | + String diagnostics = String.join("\n", result.diagnostics()); |
| 122 | + assertThat(diagnostics, result.success(), is(true)); |
| 123 | + |
| 124 | + var generatedSources = Files.walk(result.sourceOutput()) |
| 125 | + .filter(it -> it.getFileName().toString().endsWith(".java")) |
| 126 | + .toList(); |
| 127 | + |
| 128 | + StringBuilder generatedContent = new StringBuilder(); |
| 129 | + for (Path generatedSource : generatedSources) { |
| 130 | + generatedContent.append(Files.readString(generatedSource, StandardCharsets.UTF_8)); |
| 131 | + generatedContent.append('\n'); |
| 132 | + } |
| 133 | + |
| 134 | + assertThat(generatedContent.toString(), containsString("GenericType.create(Integer.class)")); |
| 135 | + } |
| 136 | +} |
0 commit comments