-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chris Bono <[email protected]>
- Loading branch information
Showing
14 changed files
with
337 additions
and
13 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...in/java/org/springframework/grpc/autoconfigure/server/ConditionalOnGrpcServerEnabled.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,43 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or 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 | ||
* | ||
* https://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 org.springframework.grpc.autoconfigure.server; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Conditional; | ||
|
||
import io.grpc.BindableService; | ||
|
||
/** | ||
* {@link Conditional @Conditional} that only matches when the | ||
* {@code io.grpc.BindableService} class is on the classpath and the | ||
* {@code spring.grpc.server.enabled} property is not explicitly set to {@code false}. | ||
* | ||
* @author Freeman | ||
* @author Chris Bono | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@ConditionalOnClass(BindableService.class) | ||
@ConditionalOnProperty(prefix = "spring.grpc.server", name = "enabled", matchIfMissing = true) | ||
public @interface ConditionalOnGrpcServerEnabled { | ||
|
||
} |
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
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
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
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
121 changes: 121 additions & 0 deletions
121
...ework/grpc/autoconfigure/server/exception/GrpcExceptionHandlerAutoConfigurationTests.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,121 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or 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 | ||
* | ||
* https://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 org.springframework.grpc.autoconfigure.server.exception; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.assertj.core.api.InstanceOfAssertFactories; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.FilteredClassLoader; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.grpc.server.exception.GrpcExceptionHandler; | ||
import org.springframework.grpc.server.exception.GrpcExceptionHandlerInterceptor; | ||
import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; | ||
|
||
import io.grpc.Grpc; | ||
|
||
/** | ||
* Tests for {@link GrpcExceptionHandlerAutoConfiguration}. | ||
* | ||
* @author Chris Bono | ||
*/ | ||
class GrpcExceptionHandlerAutoConfigurationTests { | ||
|
||
private ApplicationContextRunner contextRunner() { | ||
// NOTE: we use noop server lifecycle to avoid startup | ||
return new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(GrpcExceptionHandlerAutoConfiguration.class)) | ||
.withBean("noopServerLifecycle", GrpcServerLifecycle.class, Mockito::mock) | ||
.withBean("mockGrpcExceptionHandler", GrpcExceptionHandler.class, Mockito::mock); | ||
} | ||
|
||
@Test | ||
void whenGrpcNotOnClasspathAutoConfigurationIsSkipped() { | ||
this.contextRunner() | ||
.withClassLoader(new FilteredClassLoader(Grpc.class)) | ||
.run((context) -> assertThat(context).doesNotHaveBean(GrpcExceptionHandlerAutoConfiguration.class)); | ||
} | ||
|
||
@Test | ||
void whenNoGrpcExceptionHandlerRegisteredAutoConfigurationIsSkipped() { | ||
new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(GrpcExceptionHandlerAutoConfiguration.class)) | ||
.run((context) -> assertThat(context).doesNotHaveBean(GrpcExceptionHandlerAutoConfiguration.class)); | ||
} | ||
|
||
@Test | ||
void whenExceptionHandlerPropertyNotSetExceptionHandlerIsAutoConfigured() { | ||
this.contextRunner() | ||
.run((context) -> assertThat(context).hasSingleBean(GrpcExceptionHandlerAutoConfiguration.class)); | ||
} | ||
|
||
@Test | ||
void whenExceptionHandlerPropertyIsTrueExceptionHandlerIsAutoConfigured() { | ||
this.contextRunner() | ||
.withPropertyValues("spring.grpc.server.exception-handler.enabled=true") | ||
.run((context) -> assertThat(context).hasSingleBean(GrpcExceptionHandlerAutoConfiguration.class)); | ||
} | ||
|
||
@Test | ||
void whenExceptionHandlerPropertyIsFalseAutoConfigurationIsSkipped() { | ||
this.contextRunner() | ||
.withPropertyValues("spring.grpc.server.exception-handler.enabled=false") | ||
.run((context) -> assertThat(context).doesNotHaveBean(GrpcExceptionHandlerAutoConfiguration.class)); | ||
} | ||
|
||
@Test | ||
void whenServerEnabledPropertySetFalseThenAutoConfigurationIsSkipped() { | ||
this.contextRunner() | ||
.withPropertyValues("spring.grpc.server.enabled=false") | ||
.run((context) -> assertThat(context).doesNotHaveBean(GrpcExceptionHandlerAutoConfiguration.class)); | ||
} | ||
|
||
@Test | ||
void whenServerEnabledPropertyNotSetThenAutoConfigurationIsNotSkipped() { | ||
this.contextRunner() | ||
.run((context) -> assertThat(context).hasSingleBean(GrpcExceptionHandlerAutoConfiguration.class)); | ||
} | ||
|
||
@Test | ||
void whenServerEnabledPropertySetTrueThenAutoConfigurationIsNotSkipped() { | ||
this.contextRunner() | ||
.withPropertyValues("spring.grpc.server.enabled=true") | ||
.run((context) -> assertThat(context).hasSingleBean(GrpcExceptionHandlerAutoConfiguration.class)); | ||
} | ||
|
||
@Test | ||
void whenHasUserDefinedGrpcExceptionHandlerInterceptorDoesNotAutoConfigureBean() { | ||
GrpcExceptionHandlerInterceptor customInterceptor = Mockito.mock(); | ||
this.contextRunner() | ||
.withBean("customInterceptor", GrpcExceptionHandlerInterceptor.class, () -> customInterceptor) | ||
.run((context) -> assertThat(context).getBean(GrpcExceptionHandlerInterceptor.class) | ||
.isSameAs(customInterceptor)); | ||
} | ||
|
||
@Test | ||
void exceptionHandlerInterceptorAutoConfiguredAsExpected() { | ||
this.contextRunner() | ||
.run((context) -> assertThat(context).getBean(GrpcExceptionHandlerInterceptor.class) | ||
.extracting("exceptionHandler.exceptionHandlers", | ||
InstanceOfAssertFactories.array(GrpcExceptionHandler[].class)) | ||
.containsExactly(context.getBean(GrpcExceptionHandler.class))); | ||
} | ||
|
||
} |
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
Oops, something went wrong.