-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Furer
committed
Jun 16, 2021
1 parent
4298b5c
commit c4b89e4
Showing
7 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
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
83 changes: 83 additions & 0 deletions
83
...pring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/kafka/GrpcKafkaTest.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,83 @@ | ||
package org.lognet.springboot.grpc.kafka; | ||
|
||
import io.grpc.examples.custom.Custom; | ||
import io.grpc.examples.custom.CustomServiceGrpc; | ||
import io.grpc.stub.StreamObserver; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.lognet.springboot.grpc.GRpcService; | ||
import org.lognet.springboot.grpc.GrpcServerTestBase; | ||
import org.lognet.springboot.grpc.demo.DemoApp; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.time.Duration; | ||
import java.util.function.Consumer; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; | ||
|
||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE) | ||
@ActiveProfiles({"disable-security", "kafka-test"}) | ||
@Import({GrpcKafkaTest.TestConfig.class}) | ||
public class GrpcKafkaTest extends GrpcServerTestBase { | ||
|
||
@Configuration | ||
@Slf4j | ||
public static class TestConfig { | ||
@Autowired | ||
private KafkaTemplate<String, byte[]> kafkaTemplate; | ||
|
||
@GRpcService | ||
class MyCustomService extends io.grpc.examples.custom.CustomServiceGrpc.CustomServiceImplBase { | ||
@Override | ||
public void custom(Custom.CustomRequest request, StreamObserver<Custom.CustomReply> responseObserver) { | ||
kafkaTemplate.send(kafkaTemplate.getDefaultTopic(), request.getName().getBytes()) | ||
.addCallback(e -> { | ||
responseObserver.onNext(Custom.CustomReply.newBuilder().setMessage(request.getName()).build()); | ||
responseObserver.onCompleted(); | ||
}, f -> { | ||
responseObserver.onError(f); | ||
}); | ||
} | ||
} | ||
|
||
|
||
@MockBean | ||
public Consumer<String> consumerMock; | ||
|
||
@Bean | ||
public Consumer<String> consumer() { | ||
return consumerMock; | ||
} | ||
} | ||
|
||
@Autowired | ||
public Consumer<String> consumerMock; | ||
|
||
@Test | ||
public void sendCustomMessage() { | ||
String name = "Johnny"; | ||
final Custom.CustomReply customReply = CustomServiceGrpc.newBlockingStub(getChannel()) | ||
.custom(Custom.CustomRequest.newBuilder() | ||
.setName(name) | ||
.build() | ||
); | ||
assertThat(customReply.getMessage(), Matchers.is(name)); | ||
Mockito.verify(consumerMock, | ||
Mockito.timeout(Duration.ofSeconds(3).toMillis()).times(1) | ||
).accept(name); | ||
} | ||
} |
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,21 @@ | ||
syntax = "proto3"; | ||
import "google/protobuf/empty.proto"; | ||
option java_package = "io.grpc.examples.custom"; | ||
|
||
|
||
|
||
// Custom service definition. | ||
service CustomService { | ||
rpc Custom ( CustomRequest) returns ( CustomReply) {} | ||
} | ||
|
||
|
||
message CustomRequest { | ||
string name = 1; | ||
} | ||
|
||
message CustomReply { | ||
string message = 1; | ||
} | ||
|
||
|
22 changes: 22 additions & 0 deletions
22
grpc-spring-boot-starter-demo/src/test/resources/bootstrap-kafka-test.yml
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,22 @@ | ||
embedded: | ||
kafka: | ||
enabled: true | ||
containers: | ||
enabled: true | ||
|
||
spring: | ||
kafka: | ||
bootstrap-servers: ${embedded.kafka.brokerList} | ||
template: | ||
default-topic: testTopic | ||
cloud: | ||
stream: | ||
default-binder: kafka | ||
kafka: | ||
binder: | ||
brokers: ${embedded.kafka.brokerList} | ||
bindings: | ||
consumer-in-0: | ||
destination: ${spring.kafka.template.default-topic} | ||
function: | ||
definition: consumer |
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 |
---|---|---|
|
@@ -20,4 +20,6 @@ embedded: | |
env: | ||
JAVA_OPTS_APPEND: "-Xms1024m -Xmx1024m" | ||
containers: | ||
enabled: false | ||
kafka: | ||
enabled: false |
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