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);
}
}
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);
}
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());
}
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;
}
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.";
}
Aggregations