Search in sources :

Example 1 with Request

use of com.blackducksoftware.integration.hub.request.Request in project hub-alert by blackducksoftware.

the class SlackChannelTestIT method testCreateRequestExceptions.

@Test
public void testCreateRequestExceptions() {
    final SlackChannel slackChannel = new SlackChannel(gson, null, null, null, null);
    final MockSlackEntity mockSlackEntity = new MockSlackEntity();
    Request request = null;
    try {
        request = slackChannel.createRequest(null, mockSlackEntity.createEmptyEntity(), null);
        fail();
    } catch (final IntegrationException e) {
        assertNull(request);
    }
    mockSlackEntity.setChannelName("");
    try {
        request = slackChannel.createRequest(null, mockSlackEntity.createEntity(), null);
        fail();
    } catch (final IntegrationException e) {
        assertNull(request);
    }
}
Also used : IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) Request(com.blackducksoftware.integration.hub.request.Request) MockSlackEntity(com.blackducksoftware.integration.hub.alert.channel.slack.mock.MockSlackEntity) ExternalConnectionTest(com.blackducksoftware.integration.test.annotation.ExternalConnectionTest) Test(org.junit.Test) ChannelTest(com.blackducksoftware.integration.hub.alert.channel.ChannelTest)

Example 2 with Request

use of com.blackducksoftware.integration.hub.request.Request in project hub-alert by blackducksoftware.

the class ChannelRequestHelperTest method testSendGenericRequestThrowInegrationException.

@Test
public void testSendGenericRequestThrowInegrationException() throws Exception {
    final Request request = createRequest();
    final RestConnection restConnection = Mockito.mock(RestConnection.class);
    Mockito.when(restConnection.executeRequest(request)).thenThrow(new IntegrationException());
    final ChannelRequestHelper channelRequestHelper = new ChannelRequestHelper(restConnection);
    IntegrationException thrown = null;
    try {
        channelRequestHelper.sendGenericRequest(request);
    } catch (final IntegrationException ex) {
        thrown = ex;
    }
    assertNotNull(thrown);
}
Also used : RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) Request(com.blackducksoftware.integration.hub.request.Request) Test(org.junit.Test)

Example 3 with Request

use of com.blackducksoftware.integration.hub.request.Request in project hub-alert by blackducksoftware.

the class ChannelRequestHelperTest method testCreateQueryParametersMessageRequest.

@Test
public void testCreateQueryParametersMessageRequest() throws Exception {
    final Request request = createRequest();
    final RestConnection restConnection = Mockito.mock(RestConnection.class);
    final ChannelRequestHelper channelRequestHelper = new ChannelRequestHelper(restConnection);
    final Request returnedRequest = channelRequestHelper.createPostMessageRequest("https://google.com", null, null, "{}");
    assertEquals(request.getUri(), returnedRequest.getUri());
    assertEquals(request.getMethod(), returnedRequest.getMethod());
    assertEquals(request.getMimeType(), returnedRequest.getMimeType());
    assertEquals(request.getQueryParameters(), returnedRequest.getQueryParameters());
    assertEquals(request.getAdditionalHeaders(), returnedRequest.getAdditionalHeaders());
    assertEquals(request.getBodyEncoding(), returnedRequest.getBodyEncoding());
    assertEquals(request.getBodyContent().getBodyContent(), returnedRequest.getBodyContent().getBodyContent());
}
Also used : RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) Request(com.blackducksoftware.integration.hub.request.Request) Test(org.junit.Test)

Example 4 with Request

use of com.blackducksoftware.integration.hub.request.Request in project hub-alert by blackducksoftware.

the class ChannelRequestHelper method createPostMessageRequest.

public Request createPostMessageRequest(final String url, final Map<String, String> headers, final Map<String, String> queryParameters, final String jsonString) {
    Request.Builder requestBuilder = new Request.Builder();
    final BodyContent bodyContent = new BodyContent(jsonString);
    requestBuilder = requestBuilder.method(HttpMethod.POST).uri(url).additionalHeaders(headers).queryParameters(queryParameters).bodyContent(bodyContent);
    final Request request = requestBuilder.build();
    return request;
}
Also used : BodyContent(com.blackducksoftware.integration.hub.request.BodyContent) Request(com.blackducksoftware.integration.hub.request.Request)

Example 5 with Request

use of com.blackducksoftware.integration.hub.request.Request in project hub-alert by blackducksoftware.

the class HipChatChannel method testGlobalConfig.

@Override
public String testGlobalConfig(final GlobalHipChatConfigEntity entity) throws IntegrationException {
    if (entity == null) {
        return "The provided entity was null.";
    }
    if (StringUtils.isBlank(entity.getApiKey())) {
        throw new IntegrationException("Invalid API key: API key not provided");
    }
    final RestConnection restConnection = channelRestConnectionFactory.createUnauthenticatedRestConnection(HIP_CHAT_API);
    if (restConnection != null) {
        try {
            final String url = HIP_CHAT_API + "/v2/room/*/notification";
            final Map<String, String> queryParameters = new HashMap<>();
            queryParameters.put("auth_test", "true");
            final Map<String, String> requestHeaders = new HashMap<>();
            requestHeaders.put("Authorization", "Bearer " + entity.getApiKey());
            requestHeaders.put("Content-Type", "application/json");
            // The {"message":"test"} is required to avoid a BAD_REQUEST (OkHttp issue: #854)
            final ChannelRequestHelper channelRequestHelper = new ChannelRequestHelper(restConnection);
            final Request request = channelRequestHelper.createPostMessageRequest(url, requestHeaders, queryParameters, "{\"message\":\"test\"}");
            final Response response = channelRequestHelper.sendGenericRequest(request);
            if (response.getStatusCode() >= 200 && response.getStatusCode() < 400) {
                return "API key is valid.";
            }
            return "Invalid API key: " + response.getStatusMessage();
        } catch (final IntegrationException e) {
            restConnection.logger.error("Unable to create a response", e);
            throw new IntegrationException("Invalid API key: " + e.getMessage());
        }
    }
    return "Connection error: see logs for more information.";
}
Also used : Response(com.blackducksoftware.integration.hub.request.Response) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) HashMap(java.util.HashMap) Request(com.blackducksoftware.integration.hub.request.Request) ChannelRequestHelper(com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper)

Aggregations

Request (com.blackducksoftware.integration.hub.request.Request)11 RestConnection (com.blackducksoftware.integration.hub.rest.RestConnection)6 Test (org.junit.Test)6 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)4 ChannelTest (com.blackducksoftware.integration.hub.alert.channel.ChannelTest)3 ChannelRequestHelper (com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper)3 MockSlackEntity (com.blackducksoftware.integration.hub.alert.channel.slack.mock.MockSlackEntity)3 Response (com.blackducksoftware.integration.hub.request.Response)3 ExternalConnectionTest (com.blackducksoftware.integration.test.annotation.ExternalConnectionTest)3 HashMap (java.util.HashMap)3 ProjectData (com.blackducksoftware.integration.hub.alert.digest.model.ProjectData)2 IOException (java.io.IOException)2 Map (java.util.Map)2 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 ExecutableRunnerException (com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunnerException)1 BodyContent (com.blackducksoftware.integration.hub.request.BodyContent)1 UnauthenticatedRestConnection (com.blackducksoftware.integration.hub.rest.UnauthenticatedRestConnection)1 UnauthenticatedRestConnectionBuilder (com.blackducksoftware.integration.hub.rest.UnauthenticatedRestConnectionBuilder)1 File (java.io.File)1