Java Code Examples for io.grpc.internal.testing.StreamRecorder#create()

The following examples show how to use io.grpc.internal.testing.StreamRecorder#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MockUserManagementServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest(name = "{0}")
@MethodSource("conditionalEntitlementDataProvider")
void getAccountTestIncludesConditionalEntitlement(String testCaseName, String conditionFieldName, boolean condition, String entitlementName,
        boolean entitlementPresentExpected) {
    ReflectionTestUtils.setField(underTest, "cbLicense", VALID_LICENSE);
    underTest.initializeWorkloadPasswordPolicy();
    ReflectionTestUtils.setField(underTest, conditionFieldName, condition);

    GetAccountRequest req = GetAccountRequest.getDefaultInstance();
    StreamRecorder<GetAccountResponse> observer = StreamRecorder.create();

    underTest.getAccount(req, observer);

    assertThat(observer.getValues().size()).isEqualTo(1);
    GetAccountResponse res = observer.getValues().get(0);
    assertThat(res.hasAccount()).isTrue();
    Account account = res.getAccount();
    List<String> entitlements = account.getEntitlementsList().stream().map(Entitlement::getEntitlementName).collect(Collectors.toList());
    if (entitlementPresentExpected) {
        assertThat(entitlements).contains(entitlementName);
    } else {
        assertThat(entitlements).doesNotContain(entitlementName);
    }
}
 
Example 2
Source File: AbstractBrokenServerClientTest.java    From grpc-spring-boot-starter with MIT License 6 votes vote down vote up
/**
 * Test failing call with broken setup.
 */
@Test
@DirtiesContext
public void testFailingCallWithBrokenSetup() {
    log.info("--- Starting tests with failing call with broken setup ---");
    assertThrowsStatus(UNAVAILABLE,
            () -> TestServiceGrpc.newBlockingStub(this.channel).unimplemented(Empty.getDefaultInstance()));

    final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
    this.testServiceStub.unimplemented(Empty.getDefaultInstance(), streamRecorder);
    assertFutureThrowsStatus(UNAVAILABLE, streamRecorder.firstValue(), 5, TimeUnit.SECONDS);
    assertThrowsStatus(UNAVAILABLE, () -> this.testServiceBlockingStub.unimplemented(Empty.getDefaultInstance()));
    assertFutureThrowsStatus(UNAVAILABLE, this.testServiceFutureStub.unimplemented(Empty.getDefaultInstance()),
            5, TimeUnit.SECONDS);
    log.info("--- Test completed ---");
}
 
Example 3
Source File: ProtoReflectionServiceTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void allExtensionNumbersOfType() throws Exception {
  ServerReflectionRequest request =
      ServerReflectionRequest.newBuilder()
          .setHost(TEST_HOST)
          .setAllExtensionNumbersOfType("grpc.reflection.testing.ThirdLevelType")
          .build();

  Set<Integer> goldenResponse = new HashSet<>(Arrays.asList(100, 101));

  StreamRecorder<ServerReflectionResponse> responseObserver = StreamRecorder.create();
  StreamObserver<ServerReflectionRequest> requestObserver =
      stub.serverReflectionInfo(responseObserver);
  requestObserver.onNext(request);
  requestObserver.onCompleted();
  Set<Integer> extensionNumberResponseSet =
      new HashSet<>(
          responseObserver
              .firstValue()
              .get()
              .getAllExtensionNumbersResponse()
              .getExtensionNumberList());
  assertEquals(goldenResponse, extensionNumberResponseSet);
}
 
Example 4
Source File: AbstractBrokenServerClientTest.java    From grpc-spring-boot-starter with MIT License 6 votes vote down vote up
/**
 * Test successful call with broken setup.
 */
@Test
@DirtiesContext
public void testSuccessfulCallWithBrokenSetup() {
    log.info("--- Starting tests with successful call with broken setup ---");
    assertThrowsStatus(UNAVAILABLE,
            () -> TestServiceGrpc.newBlockingStub(this.channel).normal(Empty.getDefaultInstance()));

    final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
    this.testServiceStub.normal(Empty.getDefaultInstance(), streamRecorder);
    assertFutureThrowsStatus(UNAVAILABLE, streamRecorder.firstValue(), 5, TimeUnit.SECONDS);
    assertThrowsStatus(UNAVAILABLE, () -> this.testServiceBlockingStub.normal(Empty.getDefaultInstance()));
    assertFutureThrowsStatus(UNAVAILABLE, this.testServiceFutureStub.normal(Empty.getDefaultInstance()),
            5, TimeUnit.SECONDS);
    log.info("--- Test completed ---");
}
 
Example 5
Source File: MockUserManagementServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAccountIncludesPasswordPolicy() throws IOException {
    Path licenseFilePath = Files.createTempFile("license", "txt");
    Files.writeString(licenseFilePath, VALID_LICENSE);
    ReflectionTestUtils.setField(underTest, "cmLicenseFilePath", licenseFilePath.toString());

    try {
        underTest.init();

        GetAccountRequest req = GetAccountRequest.getDefaultInstance();
        StreamRecorder<GetAccountResponse> observer = StreamRecorder.create();

        underTest.getAccount(req, observer);

        assertThat(observer.getValues().size()).isEqualTo(1);
        GetAccountResponse res = observer.getValues().get(0);
        assertThat(res.hasAccount()).isTrue();
        Account account = res.getAccount();
        assertThat(account.hasPasswordPolicy()).isTrue();
        WorkloadPasswordPolicy passwordPolicy = account.getPasswordPolicy();
        assertThat(passwordPolicy.getWorkloadPasswordMaxLifetime()).isEqualTo(MockUserManagementService.PASSWORD_LIFETIME);
    } finally {
        Files.delete(licenseFilePath);
    }
}
 
Example 6
Source File: OnlyInProcessServerTest.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Test successful call for inter-process server.
 */
@Test
@DirtiesContext
public void testSuccessfulInterProcessCall() {
    log.info("--- Starting tests with successful but unavailable inter-process call ---");
    assertThrowsStatus(UNAVAILABLE, () -> newBlockingStub(this.interProcessChannel).unimplemented(EMPTY));

    final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
    this.interProcessServiceStub.unimplemented(EMPTY, streamRecorder);
    assertFutureThrowsStatus(UNAVAILABLE, streamRecorder.firstValue(), 5, TimeUnit.SECONDS);
    assertThrowsStatus(UNAVAILABLE, () -> this.interProcessServiceBlockingStub.unimplemented(EMPTY));
    assertFutureThrowsStatus(UNAVAILABLE, this.interProcessServiceFutureStub.unimplemented(EMPTY),
            5, TimeUnit.SECONDS);
    log.info("--- Test completed ---");
}
 
Example 7
Source File: OnlyInProcessServerTest.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Test failing call for in-process server.
 */
@Test
@DirtiesContext
public void testFailingInProcessCall() {
    log.info("--- Starting tests with failing in-process call ---");
    assertThrowsStatus(UNIMPLEMENTED, () -> newBlockingStub(this.inProcessChannel).unimplemented(EMPTY));

    final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
    this.inProcessServiceStub.unimplemented(EMPTY, streamRecorder);
    assertFutureThrowsStatus(UNIMPLEMENTED, streamRecorder.firstValue(), 5, TimeUnit.SECONDS);
    assertThrowsStatus(UNIMPLEMENTED, () -> this.inProcessServiceBlockingStub.unimplemented(EMPTY));
    assertFutureThrowsStatus(UNIMPLEMENTED, this.inProcessServiceFutureStub.unimplemented(EMPTY),
            5, TimeUnit.SECONDS);
    log.info("--- Test completed ---");
}
 
Example 8
Source File: ProtoReflectionServiceTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private void assertServiceResponseEquals(Set<ServiceResponse> goldenResponse) throws Exception {
  ServerReflectionRequest request =
      ServerReflectionRequest.newBuilder().setHost(TEST_HOST).setListServices("services").build();
  StreamRecorder<ServerReflectionResponse> responseObserver = StreamRecorder.create();
  StreamObserver<ServerReflectionRequest> requestObserver =
      stub.serverReflectionInfo(responseObserver);
  requestObserver.onNext(request);
  requestObserver.onCompleted();
  List<ServiceResponse> response =
      responseObserver.firstValue().get().getListServicesResponse().getServiceList();
  assertEquals(goldenResponse.size(), response.size());
  assertEquals(goldenResponse, new HashSet<ServiceResponse>(response));
}
 
Example 9
Source File: InterAndInProcessSetupTest.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Test failing call for inter-process server.
 */
@Test
@DirtiesContext
public void testFailingInterProcessCall() {
    log.info("--- Starting tests with failing inter-process call ---");
    assertThrowsStatus(UNIMPLEMENTED, () -> newBlockingStub(this.interProcessChannel).unimplemented(EMPTY));

    final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
    this.interProcessServiceStub.unimplemented(EMPTY, streamRecorder);
    assertFutureThrowsStatus(UNIMPLEMENTED, streamRecorder.firstValue(), 5, TimeUnit.SECONDS);
    assertThrowsStatus(UNIMPLEMENTED, () -> this.interProcessServiceBlockingStub.unimplemented(EMPTY));
    assertFutureThrowsStatus(UNIMPLEMENTED, this.interProcessServiceFutureStub.unimplemented(EMPTY),
            5, TimeUnit.SECONDS);
    log.info("--- Test completed ---");
}
 
Example 10
Source File: InterAndInProcessSetupTest.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Test successful call for inter-process server.
 *
 * @throws ExecutionException Should never happen.
 * @throws InterruptedException Should never happen.
 */
@Test
@DirtiesContext
public void testSuccessfulInterProcessCall() throws InterruptedException, ExecutionException {
    log.info("--- Starting tests with successful inter-process call ---");
    assertEquals("1.2.3", newBlockingStub(this.interProcessChannel).normal(EMPTY).getVersion());

    final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
    this.interProcessServiceStub.normal(EMPTY, streamRecorder);
    assertEquals("1.2.3", streamRecorder.firstValue().get().getVersion());
    assertEquals("1.2.3", this.interProcessServiceBlockingStub.normal(EMPTY).getVersion());
    assertEquals("1.2.3", this.interProcessServiceFutureStub.normal(EMPTY).get().getVersion());
    log.info("--- Test completed ---");
}
 
Example 11
Source File: ProtoReflectionServiceTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void assertServiceResponseEquals(Set<ServiceResponse> goldenResponse) throws Exception {
  ServerReflectionRequest request =
      ServerReflectionRequest.newBuilder().setHost(TEST_HOST).setListServices("services").build();
  StreamRecorder<ServerReflectionResponse> responseObserver = StreamRecorder.create();
  StreamObserver<ServerReflectionRequest> requestObserver =
      stub.serverReflectionInfo(responseObserver);
  requestObserver.onNext(request);
  requestObserver.onCompleted();
  List<ServiceResponse> response =
      responseObserver.firstValue().get().getListServicesResponse().getServiceList();
  assertEquals(goldenResponse.size(), response.size());
  assertEquals(goldenResponse, new HashSet<>(response));
}
 
Example 12
Source File: InterAndInProcessSetupTest.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Test failing call for in-process server.
 */
@Test
@DirtiesContext
public void testFailingInProcessCall() {
    log.info("--- Starting tests with failing in-process call ---");
    assertThrowsStatus(UNIMPLEMENTED, () -> newBlockingStub(this.inProcessChannel).unimplemented(EMPTY));

    final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
    this.inProcessServiceStub.unimplemented(EMPTY, streamRecorder);
    assertFutureThrowsStatus(UNIMPLEMENTED, streamRecorder.firstValue(), 5, TimeUnit.SECONDS);
    assertThrowsStatus(UNIMPLEMENTED, () -> this.inProcessServiceBlockingStub.unimplemented(EMPTY));
    assertFutureThrowsStatus(UNIMPLEMENTED, this.inProcessServiceFutureStub.unimplemented(EMPTY),
            5, TimeUnit.SECONDS);
    log.info("--- Test completed ---");
}
 
Example 13
Source File: AbstractSecurityTest.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
protected void assertClientStreamingCallFailure(final TestServiceStub serviceStub, final Code expectedCode) {
    final StreamRecorder<Empty> responseRecorder = StreamRecorder.create();
    final StreamObserver<SomeType> requestObserver = serviceStub.secureDrain(responseRecorder);
    // Let the server throw an exception if he receives that (assert security):
    sendAndComplete(requestObserver, "explode");
    assertFutureThrowsStatus(expectedCode, responseRecorder, 15, SECONDS);
}
 
Example 14
Source File: AbstractInteropTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void emptyStream() throws Exception {
  StreamRecorder<StreamingOutputCallResponse> responseObserver = StreamRecorder.create();
  StreamObserver<StreamingOutputCallRequest> requestObserver
      = asyncStub.fullDuplexCall(responseObserver);
  requestObserver.onCompleted();
  responseObserver.awaitCompletion(operationTimeoutMillis(), TimeUnit.MILLISECONDS);
}
 
Example 15
Source File: InterAndInProcessSetupTest.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Test successful call for in-process server.
 *
 * @throws ExecutionException Should never happen.
 * @throws InterruptedException Should never happen.
 */
@Test
@DirtiesContext
public void testSuccessfulInProcessCall() throws InterruptedException, ExecutionException {
    log.info("--- Starting tests with successful in-process call ---");
    assertEquals("1.2.3", newBlockingStub(this.inProcessChannel).normal(EMPTY).getVersion());

    final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
    this.inProcessServiceStub.normal(EMPTY, streamRecorder);
    assertEquals("1.2.3", streamRecorder.firstValue().get().getVersion());
    assertEquals("1.2.3", this.inProcessServiceBlockingStub.normal(EMPTY).getVersion());
    assertEquals("1.2.3", this.inProcessServiceFutureStub.normal(EMPTY).get().getVersion());
    log.info("--- Test completed ---");
}
 
Example 16
Source File: AbstractSecurityTest.java    From grpc-spring-boot-starter with MIT License 4 votes vote down vote up
protected void assertServerStreamingCallFailure(final TestServiceStub serviceStub, final Code expectedCode) {
    final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
    serviceStub.secureSupply(EMPTY, streamRecorder);
    assertFutureThrowsStatus(expectedCode, streamRecorder, 15, SECONDS);
}
 
Example 17
Source File: AbstractSecurityTest.java    From grpc-spring-boot-starter with MIT License 4 votes vote down vote up
protected void assertBidiCallFailure(final TestServiceStub serviceStub, final Code expectedCode) {
    final StreamRecorder<SomeType> responseRecorder = StreamRecorder.create();
    final StreamObserver<SomeType> requestObserver = serviceStub.secureBidi(responseRecorder);
    sendAndComplete(requestObserver, "explode");
    assertFutureThrowsStatus(expectedCode, responseRecorder, 15, SECONDS);
}
 
Example 18
Source File: AbstractInteropTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void exchangeMetadataStreamingCall() throws Exception {
  TestServiceGrpc.TestServiceStub stub = asyncStub;

  // Capture the metadata exchange
  Metadata fixedHeaders = new Metadata();
  // Send a context proto (as it's in the default extension registry)
  Messages.SimpleContext contextValue =
      Messages.SimpleContext.newBuilder().setValue("dog").build();
  fixedHeaders.put(Util.METADATA_KEY, contextValue);
  stub = MetadataUtils.attachHeaders(stub, fixedHeaders);
  // .. and expect it to be echoed back in trailers
  AtomicReference<Metadata> trailersCapture = new AtomicReference<>();
  AtomicReference<Metadata> headersCapture = new AtomicReference<>();
  stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture);

  List<Integer> responseSizes = Arrays.asList(50, 100, 150, 200);
  Messages.StreamingOutputCallRequest.Builder streamingOutputBuilder =
      Messages.StreamingOutputCallRequest.newBuilder();
  for (Integer size : responseSizes) {
    streamingOutputBuilder.addResponseParameters(
        ResponseParameters.newBuilder().setSize(size).setIntervalUs(0));
  }
  final Messages.StreamingOutputCallRequest request = streamingOutputBuilder.build();

  StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create();
  StreamObserver<Messages.StreamingOutputCallRequest> requestStream =
      stub.fullDuplexCall(recorder);

  final int numRequests = 10;
  for (int ix = numRequests; ix > 0; --ix) {
    requestStream.onNext(request);
  }
  requestStream.onCompleted();
  recorder.awaitCompletion();
  assertSuccess(recorder);
  org.junit.Assert.assertEquals(responseSizes.size() * numRequests, recorder.getValues().size());

  // Assert that our side channel object is echoed back in both headers and trailers
  Assert.assertEquals(contextValue, headersCapture.get().get(Util.METADATA_KEY));
  Assert.assertEquals(contextValue, trailersCapture.get().get(Util.METADATA_KEY));
}
 
Example 19
Source File: AbstractSecurityTest.java    From grpc-spring-boot-starter with MIT License 4 votes vote down vote up
protected void assertBidiCallSuccess(final TestServiceStub testStub) {
    final StreamRecorder<SomeType> responseRecorder = StreamRecorder.create();
    final StreamObserver<SomeType> requestObserver = testStub.secureBidi(responseRecorder);
    sendAndComplete(requestObserver, "1.2.3");
    assertFutureFirstEquals("1.2.3", responseRecorder, SomeType::getVersion, 5, SECONDS);
}
 
Example 20
Source File: AbstractSecurityTest.java    From grpc-spring-boot-starter with MIT License 4 votes vote down vote up
protected void assertClientStreamingCallSuccess(final TestServiceStub serviceStub) {
    final StreamRecorder<Empty> responseRecorder = StreamRecorder.create();
    final StreamObserver<SomeType> requestObserver = serviceStub.secureDrain(responseRecorder);
    sendAndComplete(requestObserver, "1.2.3");
    assertFutureFirstEquals(EMPTY, responseRecorder, 15, TimeUnit.SECONDS);
}