Search in sources :

Example 6 with HttpResponse

use of com.azure.core.http.HttpResponse in project lowkey-vault by nagyesta.

the class ResponseEntityTest method testGetResponseObjectShouldMapResponseBodyUsingObjectReaderWhenCalledWithList.

@Test
void testGetResponseObjectShouldMapResponseBodyUsingObjectReaderWhenCalledWithList() {
    // given
    final VaultModel vaultModel = new VaultModel(URI.create(HTTPS_DEFAULT_LOCALHOST_8443), null, null, null, null);
    final List<VaultModel> expected = Collections.singletonList(vaultModel);
    final HttpResponse response = mock(HttpResponse.class);
    when(response.getStatusCode()).thenReturn(HttpStatus.SC_OK);
    when(response.getBodyAsString(eq(StandardCharsets.UTF_8))).thenReturn(Mono.just(VAULT_MODEL_LIST));
    final ObjectReader reader = new ObjectMapper().reader();
    final ResponseEntity underTest = new ResponseEntity(response, reader);
    // when
    final List<VaultModel> actual = underTest.getResponseObject(VAULT_MODEL_LIST_TYPE_REF);
    // then
    Assertions.assertEquals(expected, actual);
    verify(response).getStatusCode();
    verify(response).getBodyAsString(eq(StandardCharsets.UTF_8));
}
Also used : VaultModel(com.github.nagyesta.lowkeyvault.http.management.VaultModel) HttpResponse(com.azure.core.http.HttpResponse) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with HttpResponse

use of com.azure.core.http.HttpResponse in project bulk-scan-processor by hmcts.

the class BlobManagerTest method tryMoveFileToRejectedContainer_retry_delete_when_lease_lost.

@Test
void tryMoveFileToRejectedContainer_retry_delete_when_lease_lost() {
    // given
    given(inputContainerClient.getBlobClient(INPUT_FILE_NAME)).willReturn(inputBlobClient);
    given(rejectedContainerClient.getBlobClient(INPUT_FILE_NAME)).willReturn(rejectedBlobClient);
    given(blobServiceClient.getBlobContainerClient(INPUT_CONTAINER_NAME)).willReturn(inputContainerClient);
    given(blobServiceClient.getBlobContainerClient(REJECTED_CONTAINER_NAME)).willReturn(rejectedContainerClient);
    given(rejectedBlobClient.exists()).willReturn(Boolean.FALSE);
    mockBeginCopy("http://retry", UUID.randomUUID().toString());
    HttpResponse response = mock(HttpResponse.class);
    given(response.getStatusCode()).willReturn(412);
    HttpHeaders httpHeaders = mock(HttpHeaders.class);
    given(response.getHeaders()).willReturn(httpHeaders);
    given(httpHeaders.getValue(ERROR_CODE)).willReturn(BlobErrorCode.LEASE_LOST.toString());
    willThrow(new BlobStorageException(BlobErrorCode.LEASE_LOST.toString(), response, null)).willReturn(mock(Response.class)).given(inputBlobClient).deleteWithResponse(any(), any(), any(), any());
    // when
    blobManager.tryMoveFileToRejectedContainer(INPUT_FILE_NAME, INPUT_CONTAINER_NAME);
    // then
    verify(rejectedBlobClient).beginCopy(any(), any(), any(), any(), any(), any(), any());
    verify(inputBlobClient, times(2)).deleteWithResponse(any(), any(), any(), any());
}
Also used : PollResponse(com.azure.core.util.polling.PollResponse) HttpResponse(com.azure.core.http.HttpResponse) Response(com.azure.core.http.rest.Response) HttpHeaders(com.azure.core.http.HttpHeaders) HttpResponse(com.azure.core.http.HttpResponse) BlobStorageException(com.azure.storage.blob.models.BlobStorageException) Test(org.junit.jupiter.api.Test)

Example 8 with HttpResponse

use of com.azure.core.http.HttpResponse in project camel-quarkus by apache.

the class VertxHttpClientTests method testFlowableBackpressure.

@Test
public void testFlowableBackpressure() {
    HttpResponse response = getResponse("/long");
    StepVerifierOptions stepVerifierOptions = StepVerifierOptions.create();
    stepVerifierOptions.initialRequest(0);
    StepVerifier.create(response.getBody(), stepVerifierOptions).expectNextCount(0).thenRequest(1).expectNextCount(1).thenRequest(3).expectNextCount(3).thenRequest(Long.MAX_VALUE).thenConsumeWhile(ByteBuffer::hasRemaining).verifyComplete();
}
Also used : StepVerifierOptions(reactor.test.StepVerifierOptions) HttpResponse(com.azure.core.http.HttpResponse) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 9 with HttpResponse

use of com.azure.core.http.HttpResponse in project terra-cloud-resource-lib by DataBiosphere.

the class AzureResponseLogger method logResponse.

@Override
public Mono<HttpResponse> logResponse(ClientLogger clientLogger, HttpResponseLoggingContext loggingOptions) {
    final HttpResponse response = loggingOptions.getHttpResponse();
    final HttpRequest request = response.getRequest();
    // Always add request method and request URL
    JsonObject requestDataJson = new JsonObject();
    requestDataJson.addProperty("requestMethod", request.getHttpMethod().toString());
    requestDataJson.addProperty("requestUrl", request.getUrl().toString());
    // Optionally add rich request data if provided in the logging context
    Optional<ResourceManagerRequestData> requestData = Optional.ofNullable(loggingOptions.getContext()).flatMap(c -> c.getData(CLOUD_RESOURCE_REQUEST_DATA_KEY)).map(o -> (ResourceManagerRequestData) o);
    requestData.ifPresent(d -> requestDataJson.add("requestBody", d.serialize()));
    // verbose.
    if (logger.isDebugEnabled()) {
        logBody(request.getHeaders(), request.getBody(), s -> requestDataJson.addProperty("rawRequestBody", s));
        logBody(response.getHeaders(), response.buffer().getBody(), s -> requestDataJson.addProperty("rawResponseBody", s));
    }
    // Build OperationData object.
    OperationData operationData = OperationData.builder().setDuration(Optional.ofNullable(loggingOptions.getResponseDuration()).orElse(Duration.ZERO)).setTryCount(OptionalInt.of(loggingOptions.getTryCount())).setExecutionException(Optional.empty()).setHttpStatusCode(OptionalInt.of(response.getStatusCode())).setCloudOperation(requestData.map(ResourceManagerRequestData::cloudOperation).orElse(ResourceManagerOperation.AZURE_RESOURCE_MANAGER_UNKNOWN_OPERATION)).setRequestData(requestDataJson).build();
    // Invoke OperationAnnotator to record the operation.
    operationAnnotator.recordOperation(operationData);
    return Mono.justOrEmpty(response);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) JsonObject(com.google.gson.JsonObject) HttpResponse(com.azure.core.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) CoreUtils(com.azure.core.util.CoreUtils) OptionalInt(java.util.OptionalInt) OperationData(bio.terra.cloudres.common.OperationData) ByteBuffer(java.nio.ByteBuffer) Duration(java.time.Duration) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) HttpResponseLogger(com.azure.core.http.policy.HttpResponseLogger) ClientLogger(com.azure.core.util.logging.ClientLogger) Channels(java.nio.channels.Channels) ClientConfig(bio.terra.cloudres.common.ClientConfig) OperationAnnotator(bio.terra.cloudres.common.OperationAnnotator) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) HttpHeaders(com.azure.core.http.HttpHeaders) CLOUD_RESOURCE_REQUEST_DATA_KEY(bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) HttpRequest(com.azure.core.http.HttpRequest) HttpResponseLoggingContext(com.azure.core.http.policy.HttpResponseLoggingContext) ContentType(com.azure.core.http.ContentType) WritableByteChannel(java.nio.channels.WritableByteChannel) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) OperationData(bio.terra.cloudres.common.OperationData) HttpResponse(com.azure.core.http.HttpResponse) JsonObject(com.google.gson.JsonObject)

Example 10 with HttpResponse

use of com.azure.core.http.HttpResponse in project camel-quarkus by apache.

the class VertxHttpClientTests method testFlowableWhenServerReturnsBodyAndNoErrorsWhenHttp500Returned.

@Test
public void testFlowableWhenServerReturnsBodyAndNoErrorsWhenHttp500Returned() {
    HttpResponse response = getResponse("/error");
    assertEquals(500, response.getStatusCode());
    StepVerifier.create(response.getBodyAsString()).expectNext("error").expectComplete().verify(Duration.ofSeconds(20));
}
Also used : HttpResponse(com.azure.core.http.HttpResponse) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (com.azure.core.http.HttpResponse)18 Test (org.junit.jupiter.api.Test)10 HttpRequest (com.azure.core.http.HttpRequest)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)6 HttpHeaders (com.azure.core.http.HttpHeaders)4 VaultModel (com.github.nagyesta.lowkeyvault.http.management.VaultModel)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Mono (reactor.core.publisher.Mono)2 CLOUD_RESOURCE_REQUEST_DATA_KEY (bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY)1 ClientConfig (bio.terra.cloudres.common.ClientConfig)1 OperationAnnotator (bio.terra.cloudres.common.OperationAnnotator)1 OperationData (bio.terra.cloudres.common.OperationData)1 ContentType (com.azure.core.http.ContentType)1 HttpMethod (com.azure.core.http.HttpMethod)1 HttpPipelineCallContext (com.azure.core.http.HttpPipelineCallContext)1