Search in sources :

Example 6 with Response

use of com.synopsys.integration.rest.response.Response in project hub-alert by blackducksoftware.

the class ExternalAlertRequestUtility method executeRequest.

private String executeRequest(String path, HttpMethod httpMethod, String requestBody, String error) throws IntegrationException {
    BodyContent requestBodyContent = new StringBodyContent(requestBody, BodyContentConverter.DEFAULT);
    Request.Builder requestBuilder = createRequestBuilder(path);
    requestBuilder.method(httpMethod);
    if (null != requestBody) {
        requestBuilder.bodyContent(requestBodyContent);
    }
    Request request = requestBuilder.build();
    Response response = client.execute(request);
    if (response.isStatusCodeError()) {
        intLogger.error(error);
        response.throwExceptionForError();
    }
    return response.getContentString();
}
Also used : BodyContent(com.synopsys.integration.rest.body.BodyContent) StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) Response(com.synopsys.integration.rest.response.Response) Request(com.synopsys.integration.rest.request.Request)

Example 7 with Response

use of com.synopsys.integration.rest.response.Response in project hub-alert by blackducksoftware.

the class ExternalAlertRequestUtility method loginToExternalAlert.

public void loginToExternalAlert() throws IntegrationException {
    String loginBody = "{\"alertUsername\":\"sysadmin\",\"alertPassword\":\"blackduck\"}";
    BodyContent requestBody = new StringBodyContent(loginBody, BodyContentConverter.DEFAULT);
    Request.Builder requestBuilder = createRequestBuilder("/api/login");
    requestBuilder.method(HttpMethod.POST);
    requestBuilder.bodyContent(requestBody);
    Request request = requestBuilder.build();
    Response response = client.execute(request);
    if (response.isStatusCodeError()) {
        intLogger.error("Could not log into Alert.");
        response.throwExceptionForError();
    }
    String csrfToken = response.getHeaderValue("X-CSRF-TOKEN");
    String cookie = response.getHeaderValue("Set-Cookie");
    client.addCommonRequestHeader("X-CSRF-TOKEN", csrfToken);
    client.addCommonRequestHeader("Cookie", cookie);
    intLogger.info("Logged into Alert.");
}
Also used : BodyContent(com.synopsys.integration.rest.body.BodyContent) StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) Response(com.synopsys.integration.rest.response.Response) Request(com.synopsys.integration.rest.request.Request)

Example 8 with Response

use of com.synopsys.integration.rest.response.Response in project hub-alert by blackducksoftware.

the class RestChannelUtilityTest method sendMessageSuccessTest.

@Test
public void sendMessageSuccessTest() throws IntegrationException {
    Response response = createMockResponse(HttpStatus.OK);
    IntHttpClient intHttpClient = createMockHttpClientWithResponse(response);
    RestChannelUtility restChannelUtility = new RestChannelUtility(intHttpClient);
    try {
        restChannelUtility.sendMessage(List.of(TEST_REQUEST, TEST_REQUEST), CLASS_NAME);
    } catch (AlertException e) {
        fail("Expected no exception to be thrown", e);
    }
}
Also used : Response(com.synopsys.integration.rest.response.Response) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Test(org.junit.jupiter.api.Test)

Example 9 with Response

use of com.synopsys.integration.rest.response.Response in project hub-alert by blackducksoftware.

the class RestChannelUtilityTest method sendMessageAlertExceptionTest.

@Test
public void sendMessageAlertExceptionTest() throws IntegrationException {
    Response response = createMockResponse(HttpStatus.BAD_REQUEST);
    IntHttpClient intHttpClient = createMockHttpClientWithResponse(response);
    RestChannelUtility restChannelUtility = new RestChannelUtility(intHttpClient);
    try {
        restChannelUtility.sendMessage(List.of(TEST_REQUEST, TEST_REQUEST), CLASS_NAME);
        fail("Expected an exception to be thrown");
    } catch (AlertException e) {
    // Pass
    }
}
Also used : Response(com.synopsys.integration.rest.response.Response) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Test(org.junit.jupiter.api.Test)

Example 10 with Response

use of com.synopsys.integration.rest.response.Response in project hub-alert by blackducksoftware.

the class RestChannelUtilityTest method createMockResponse.

private Response createMockResponse(HttpStatus httpStatus) {
    Response response = Mockito.mock(Response.class);
    Mockito.when(response.getStatusCode()).thenReturn(httpStatus.value());
    Mockito.when(response.getStatusMessage()).thenReturn(httpStatus.name());
    return response;
}
Also used : Response(com.synopsys.integration.rest.response.Response)

Aggregations

Response (com.synopsys.integration.rest.response.Response)11 Request (com.synopsys.integration.rest.request.Request)6 IntHttpClient (com.synopsys.integration.rest.client.IntHttpClient)5 Test (org.junit.jupiter.api.Test)4 DockerTagsResponseModel (com.synopsys.integration.alert.update.model.DockerTagsResponseModel)3 IntegrationException (com.synopsys.integration.exception.IntegrationException)3 HttpUrl (com.synopsys.integration.rest.HttpUrl)3 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)2 BodyContent (com.synopsys.integration.rest.body.BodyContent)2 StringBodyContent (com.synopsys.integration.rest.body.StringBodyContent)2 IOException (java.io.IOException)2 TestTags (com.synopsys.integration.alert.test.common.TestTags)1 DockerTagModel (com.synopsys.integration.alert.update.model.DockerTagModel)1 IntLogger (com.synopsys.integration.log.IntLogger)1 PrintStreamIntLogger (com.synopsys.integration.log.PrintStreamIntLogger)1 ProxyInfo (com.synopsys.integration.rest.proxy.ProxyInfo)1 Tags (org.junit.jupiter.api.Tags)1