use of com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper 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.";
}
use of com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper in project hub-alert by blackducksoftware.
the class HipChatChannelTest method createRequestThrowsExceptionTest.
@Test
public void createRequestThrowsExceptionTest() throws Exception {
final AuditEntryRepositoryWrapper auditEntryRepository = Mockito.mock(AuditEntryRepositoryWrapper.class);
final HipChatChannel hipChatChannel = new HipChatChannel(gson, auditEntryRepository, null, null, null, null);
final ChannelRequestHelper channelRequestHelper = new ChannelRequestHelper(null);
final HipChatDistributionConfigEntity config = new HipChatDistributionConfigEntity(12345, Boolean.FALSE, null);
final ProjectData projectData = createProjectData("HipChat IT test");
final String userDir = System.getProperties().getProperty("user.dir");
try {
System.getProperties().setProperty("user.dir", "garbage");
RuntimeException thrownException = null;
try {
hipChatChannel.createRequest(channelRequestHelper, config, projectData);
} catch (final RuntimeException e) {
thrownException = e;
}
assertNotNull(thrownException);
} finally {
System.getProperties().setProperty("user.dir", userDir);
}
}
use of com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper in project hub-alert by blackducksoftware.
the class SlackChannelTestIT method testCreateHtmlMessage.
@SuppressWarnings("unchecked")
@Test
public void testCreateHtmlMessage() throws IntegrationException {
final SlackChannel slackChannel = new SlackChannel(gson, null, null, null, null);
final MockSlackEntity mockSlackEntity = new MockSlackEntity();
final ProjectData projectData = createSlackProjectData();
final ChannelRequestHelper channelRequestHelper = new ChannelRequestHelper(null) {
@Override
public Request createPostMessageRequest(final String url, final Map<String, String> headers, final String body) {
assertTrue(body.contains("Vulnerability Count Added: "));
assertTrue(body.contains("Vulnerability Count Updated: "));
assertTrue(body.contains("Vulnerability Count Deleted: "));
return null;
}
};
final ChannelRequestHelper spyChannelRequestHelper = Mockito.spy(channelRequestHelper);
final Request request = slackChannel.createRequest(spyChannelRequestHelper, mockSlackEntity.createEntity(), projectData);
assertNull(request);
Mockito.verify(spyChannelRequestHelper).createPostMessageRequest(Mockito.anyString(), Mockito.anyMap(), Mockito.anyString());
}
use of com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper in project hub-alert by blackducksoftware.
the class SlackChannelTestIT method testCreateHtmlMessageEmpty.
@Test
@SuppressWarnings("unchecked")
public void testCreateHtmlMessageEmpty() throws IntegrationException {
final SlackChannel slackChannel = new SlackChannel(gson, null, null, null, null);
final MockSlackEntity mockSlackEntity = new MockSlackEntity();
final ProjectData projectData = new ProjectData(DigestTypeEnum.DAILY, "Slack", "1", null, null);
final ChannelRequestHelper channelRequestHelper = new ChannelRequestHelper(null) {
@Override
public Request createPostMessageRequest(final String url, final Map<String, String> headers, final String body) {
assertTrue(body.contains("A notification was received, but it was empty."));
return null;
}
};
final ChannelRequestHelper spyChannelRequestHelper = Mockito.spy(channelRequestHelper);
final Request request = slackChannel.createRequest(spyChannelRequestHelper, mockSlackEntity.createEntity(), projectData);
assertNull(request);
Mockito.verify(spyChannelRequestHelper).createPostMessageRequest(Mockito.anyString(), Mockito.anyMap(), Mockito.anyString());
}
Aggregations