diff --git a/pom.xml b/pom.xml index fc13b6e7..6eed5f0d 100644 --- a/pom.xml +++ b/pom.xml @@ -113,6 +113,24 @@ + + org.springframework.boot + spring-boot-autoconfigure + 2.7.18 + true + + + org.springframework.boot + spring-boot-autoconfigure-processor + 2.7.18 + true + + + org.springframework.boot + spring-boot-starter-test + 2.7.18 + test + com.google.guava guava @@ -173,7 +191,6 @@ opencensus-api ${opencensus.version} - io.opentelemetry opentelemetry-api @@ -276,12 +293,6 @@ ${okhttp.version} test - - commons-io - commons-io - 2.14.0 - compile - diff --git a/src/main/java/com/spotify/github/v3/spring/GithubClientAutoConfiguration.java b/src/main/java/com/spotify/github/v3/spring/GithubClientAutoConfiguration.java new file mode 100644 index 00000000..4fdaad79 --- /dev/null +++ b/src/main/java/com/spotify/github/v3/spring/GithubClientAutoConfiguration.java @@ -0,0 +1,39 @@ +/*- + * -\-\- + * github-api + * -- + * Copyright (C) 2016 - 2025 Spotify AB + * -- + * 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 com.spotify.github.v3.spring; + +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.annotation.Bean; + +import com.spotify.github.v3.clients.GitHubClient; + +@AutoConfiguration +@ConditionalOnClass(GitHubClient.class) +public class GithubClientAutoConfiguration { + + @Bean + @ConditionalOnBean + public GitHubClient gitHubClient() { + return GitHubClient.create(null, null); + } +} diff --git a/src/test/java/com/spotify/github/v3/spring/GithubClientAutoConfigurationTest.java b/src/test/java/com/spotify/github/v3/spring/GithubClientAutoConfigurationTest.java new file mode 100644 index 00000000..7d5eac41 --- /dev/null +++ b/src/test/java/com/spotify/github/v3/spring/GithubClientAutoConfigurationTest.java @@ -0,0 +1,65 @@ +/*- + * -\-\- + * github-api + * -- + * Copyright (C) 2016 - 2025 Spotify AB + * -- + * 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 com.spotify.github.v3.spring; + +import org.junit.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import static org.assertj.core.api.Assertions.assertThat; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.spotify.github.jackson.GithubApiModule; + +public class GithubClientAutoConfigurationTest { + + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(GithubClientAutoConfiguration.class)); + + @Test + public void testModuleIsRegistered(){ + + this.contextRunner + .withUserConfiguration(JacksonConfig.class) + .run((context) -> { + assertThat(context).hasSingleBean(GithubApiModule.class); + }); + } + + @Test + public void testModuleIsNotRegisteredIfObjectMapperMissing(){ + + this.contextRunner + .run((context) -> { + assertThat(context).doesNotHaveBean(GithubApiModule.class); + }); + } + + @Configuration + static class JacksonConfig { + + @Bean + public ObjectMapper objectMapper() { + return new ObjectMapper(); + } + } +} diff --git a/src/test/resources/META-INF/Spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/src/test/resources/META-INF/Spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..182f3dbb --- /dev/null +++ b/src/test/resources/META-INF/Spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.spotify.github.v3.spring.GithubClientAutoConfiguration \ No newline at end of file