diff --git a/annotations/component-annotations/src/it/issue-254/src/test/java/io/dekorate/issue254/Issue254Test.java b/annotations/component-annotations/src/it/issue-254/src/test/java/io/dekorate/issue254/Issue254Test.java index 978dddf06..2cd43044b 100644 --- a/annotations/component-annotations/src/it/issue-254/src/test/java/io/dekorate/issue254/Issue254Test.java +++ b/annotations/component-annotations/src/it/issue-254/src/test/java/io/dekorate/issue254/Issue254Test.java @@ -38,6 +38,7 @@ public void shouldExposeService() { Optional component = findFirst(list, Component.class); assertTrue(component.isPresent()); assertTrue(component.get().getSpec().isExposeService()); + assertEquals("issue-254", component.get().getMetadata().getName()); assertEquals(8080, (long)component.get().getSpec().getPort()); } diff --git a/annotations/component-annotations/src/it/issue-276/pom.xml b/annotations/component-annotations/src/it/issue-276/pom.xml new file mode 100644 index 000000000..57963e30f --- /dev/null +++ b/annotations/component-annotations/src/it/issue-276/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.5.RELEASE + + + io.dekorate + issue-276 + 0.0.1-SNAPSHOT + Dekorate :: Annotation :: Component :: Integration Test for #276 + Regression test for issue #276. Ensure @Component name is propagated to metadata + + + + org.springframework.boot + spring-boot-starter-web + + + io.dekorate + kubernetes-spring-starter + @project.version@ + + + io.dekorate + component-annotations + @project.version@ + + + + + org.junit.jupiter + junit-jupiter + @version.junit-jupiter@ + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + @version.maven-surefire-plugin@ + true + + false + + + + org.apache.maven.plugins + maven-compiler-plugin + @version.maven-compiler-plugin@ + + @java.source@ + @java.target@ + + + + + diff --git a/annotations/component-annotations/src/it/issue-276/src/main/java/io/dekorate/issue276/Controller.java b/annotations/component-annotations/src/it/issue-276/src/main/java/io/dekorate/issue276/Controller.java new file mode 100644 index 000000000..f4ebabc03 --- /dev/null +++ b/annotations/component-annotations/src/it/issue-276/src/main/java/io/dekorate/issue276/Controller.java @@ -0,0 +1,28 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.dekorate.issue276; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class Controller { + + @RequestMapping("/") + public String hello() { + return "Hello world"; + } +} diff --git a/annotations/component-annotations/src/it/issue-276/src/main/java/io/dekorate/issue276/DemoApplication.java b/annotations/component-annotations/src/it/issue-276/src/main/java/io/dekorate/issue276/DemoApplication.java new file mode 100644 index 000000000..bf98ab79b --- /dev/null +++ b/annotations/component-annotations/src/it/issue-276/src/main/java/io/dekorate/issue276/DemoApplication.java @@ -0,0 +1,16 @@ +package io.dekorate.issue276; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import io.dekorate.component.annotation.ComponentApplication; + +@SpringBootApplication +@ComponentApplication(exposeService=true, name="customName") +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + +} diff --git a/annotations/component-annotations/src/it/issue-276/src/main/resources/application.properties b/annotations/component-annotations/src/it/issue-276/src/main/resources/application.properties new file mode 100644 index 000000000..51ad5ebf4 --- /dev/null +++ b/annotations/component-annotations/src/it/issue-276/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=9090 diff --git a/annotations/component-annotations/src/it/issue-276/src/test/java/io/dekorate/issue276/Issue276Test.java b/annotations/component-annotations/src/it/issue-276/src/test/java/io/dekorate/issue276/Issue276Test.java new file mode 100644 index 000000000..be60a05f2 --- /dev/null +++ b/annotations/component-annotations/src/it/issue-276/src/test/java/io/dekorate/issue276/Issue276Test.java @@ -0,0 +1,52 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +**/ + +package io.dekorate.issue276; + +import io.dekorate.deps.kubernetes.api.model.KubernetesList; +import io.dekorate.deps.kubernetes.api.model.HasMetadata; +import io.dekorate.component.model.Component; +import io.dekorate.utils.Serialization; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class Issue276Test { + + @Test + public void shouldExposeServiceAndHaveCorrectPort() { + KubernetesList list = Serialization.unmarshal(getClass().getClassLoader().getResourceAsStream("META-INF/dekorate/component.yml")); + assertNotNull(list); + Optional component = findFirst(list, Component.class); + assertTrue(component.isPresent()); + assertTrue(component.get().getSpec().isExposeService()); + assertEquals("customName", component.get().getMetadata().getName()); + assertEquals("customName", component.get().getMetadata().getLabels().get("app")); + assertEquals(9090, (long)component.get().getSpec().getPort()); + } + + Optional findFirst(KubernetesList list, Class t) { + return (Optional) list.getItems().stream() + .filter(i -> t.isInstance(i)) + .findFirst(); + } +} + diff --git a/annotations/component-annotations/src/main/java/io/dekorate/component/handler/ComponentHandler.java b/annotations/component-annotations/src/main/java/io/dekorate/component/handler/ComponentHandler.java index e3ba44e22..55ad4eee8 100644 --- a/annotations/component-annotations/src/main/java/io/dekorate/component/handler/ComponentHandler.java +++ b/annotations/component-annotations/src/main/java/io/dekorate/component/handler/ComponentHandler.java @@ -42,9 +42,11 @@ import io.dekorate.kubernetes.configurator.ApplyAutoBuild; import io.dekorate.project.ApplyProjectInfo; import io.dekorate.project.Project; +import io.dekorate.utils.Labels; import io.dekorate.utils.Strings; import java.nio.file.Path; +import java.util.Map; import java.util.Optional; public class ComponentHandler implements HandlerFactory, Handler, WithProject { @@ -150,10 +152,12 @@ private BaseConfig getKubernetesConfig() { * @return The component. */ private Component createComponent(ComponentConfig config) { - return new ComponentBuilder() + Map labels = resources.getLabels(); + labels.put(Labels.APP, config.getName()); + return new ComponentBuilder() .withNewMetadata() - .withName(resources.getName()) - .withLabels(resources.getLabels()) + .withName(config.getName()) + .withLabels(labels) .endMetadata() .withNewSpec() .withDeploymentMode(config.getDeploymentMode()) diff --git a/core/src/main/java/io/dekorate/utils/Labels.java b/core/src/main/java/io/dekorate/utils/Labels.java index c178ad50f..7e0d61de7 100644 --- a/core/src/main/java/io/dekorate/utils/Labels.java +++ b/core/src/main/java/io/dekorate/utils/Labels.java @@ -25,9 +25,9 @@ public class Labels { - private static final String APP = "app"; - private static final String VERSION = "version"; - private static final String GROUP = "group"; + public static final String APP = "app"; + public static final String VERSION = "version"; + public static final String GROUP = "group"; /** * Creates a {@link Map} with the labels for the {@link BaseConfig}.