From 108df5e1cc640070d1b03fb2dfd99306e661b8fe Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Fri, 18 Jun 2021 08:18:59 +0200 Subject: update some log messages --- .../eaaf/utils/springboot/utils/VersionHolder.java | 42 ++++++++++++++++++++++ .../test/SimpleSpringBootStarterTest.java | 21 ++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/utils/VersionHolder.java (limited to 'eaaf-springboot-utils') diff --git a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/utils/VersionHolder.java b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/utils/VersionHolder.java new file mode 100644 index 00000000..9d996853 --- /dev/null +++ b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/utils/VersionHolder.java @@ -0,0 +1,42 @@ +package at.gv.egiz.eaaf.utils.springboot.utils; + +import java.util.Optional; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +/** + * SpringBoot based implementation of an application-version holder. + * + * @author tlenz + * + */ +@Service +public class VersionHolder { + + private final String version; + + /** + * Holder that extracts the current version of the SpringBoot application. + * + * @param context Spring ApplicationContext + */ + public VersionHolder(ApplicationContext context) { + version = context.getBeansWithAnnotation(SpringBootApplication.class).entrySet().stream() + .findFirst() + .flatMap(es -> Optional.ofNullable(es.getValue().getClass().getPackage().getImplementationVersion())) + .orElse("unknown"); + + } + + /** + * Get version of this application. + * + * @return version + */ + public String getVersion() { + return version; + + } +} diff --git a/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/SimpleSpringBootStarterTest.java b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/SimpleSpringBootStarterTest.java index 611cc3aa..e0c478af 100644 --- a/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/SimpleSpringBootStarterTest.java +++ b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/SimpleSpringBootStarterTest.java @@ -1,5 +1,8 @@ package at.gv.egiz.eaaf.utils.springboot.test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + import java.io.IOException; import org.apache.http.client.ClientProtocolException; @@ -17,6 +20,7 @@ import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactor import org.springframework.context.ConfigurableApplicationContext; import at.gv.egiz.eaaf.utils.springboot.test.dummy.DummySpringBootApp; +import at.gv.egiz.eaaf.utils.springboot.utils.VersionHolder; public class SimpleSpringBootStarterTest { @@ -29,13 +33,15 @@ public class SimpleSpringBootStarterTest { ConfigurableApplicationContext ctx = DummySpringBootApp.getCtx(); Assert.assertNotNull("SpringBootContext", ctx); - //check if AJP Connector config was set + // check if AJP Connector config was set TomcatServletWebServerFactory ajp = ctx.getBean(TomcatServletWebServerFactory.class); Assert.assertNotNull("No AJP connector", ajp); - //check simple http calls + // check simple http calls testSimpleHttpCall(); + // check version holder + checkVersionHolder(ctx); SpringApplication.exit(ctx, new ExitCodeGenerator() { @@ -51,12 +57,17 @@ public class SimpleSpringBootStarterTest { // check if authentication works on actuator end-point final HttpClientBuilder builder = HttpClients.custom(); final CloseableHttpClient client = builder.build(); - Assert.assertNotNull("httpClient", client); + assertNotNull("httpClient", client); final HttpUriRequest httpGet1 = new HttpGet("http://localhost:8080/junit"); final CloseableHttpResponse httpResp1 = client.execute(httpGet1); - Assert.assertEquals("http statusCode", 200, httpResp1.getStatusLine().getStatusCode()); + assertEquals("http statusCode", 200, httpResp1.getStatusLine().getStatusCode()); + + } + + private void checkVersionHolder(ConfigurableApplicationContext ctx) { + VersionHolder versionHolder = ctx.getBean(VersionHolder.class); + assertEquals("can not extract version", "unknown", versionHolder.getVersion()); } - } -- cgit v1.2.3