Search in sources :

Example 1 with ChannelRequestHelper

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

Example 2 with ChannelRequestHelper

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);
    }
}
Also used : AuditEntryRepositoryWrapper(com.blackducksoftware.integration.hub.alert.audit.repository.AuditEntryRepositoryWrapper) HipChatDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.channel.hipchat.repository.distribution.HipChatDistributionConfigEntity) ChannelRequestHelper(com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper) ProjectData(com.blackducksoftware.integration.hub.alert.digest.model.ProjectData) ExternalConnectionTest(com.blackducksoftware.integration.test.annotation.ExternalConnectionTest) Test(org.junit.Test) ChannelTest(com.blackducksoftware.integration.hub.alert.channel.ChannelTest)

Example 3 with ChannelRequestHelper

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());
}
Also used : Request(com.blackducksoftware.integration.hub.request.Request) ChannelRequestHelper(com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper) HashMap(java.util.HashMap) Map(java.util.Map) ProjectData(com.blackducksoftware.integration.hub.alert.digest.model.ProjectData) 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 4 with ChannelRequestHelper

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());
}
Also used : Request(com.blackducksoftware.integration.hub.request.Request) ChannelRequestHelper(com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper) HashMap(java.util.HashMap) Map(java.util.Map) ProjectData(com.blackducksoftware.integration.hub.alert.digest.model.ProjectData) 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)

Aggregations

ChannelRequestHelper (com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRequestHelper)4 ChannelTest (com.blackducksoftware.integration.hub.alert.channel.ChannelTest)3 ProjectData (com.blackducksoftware.integration.hub.alert.digest.model.ProjectData)3 Request (com.blackducksoftware.integration.hub.request.Request)3 ExternalConnectionTest (com.blackducksoftware.integration.test.annotation.ExternalConnectionTest)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 MockSlackEntity (com.blackducksoftware.integration.hub.alert.channel.slack.mock.MockSlackEntity)2 Map (java.util.Map)2 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)1 AuditEntryRepositoryWrapper (com.blackducksoftware.integration.hub.alert.audit.repository.AuditEntryRepositoryWrapper)1 HipChatDistributionConfigEntity (com.blackducksoftware.integration.hub.alert.channel.hipchat.repository.distribution.HipChatDistributionConfigEntity)1 Response (com.blackducksoftware.integration.hub.request.Response)1 RestConnection (com.blackducksoftware.integration.hub.rest.RestConnection)1