Search in sources :

Example 6 with RestConnection

use of com.blackducksoftware.integration.hub.rest.RestConnection in project hub-alert by blackducksoftware.

the class GlobalHubConfigActionsTest method testTestConfig.

@Test
public void testTestConfig() throws Exception {
    final RestConnection mockedRestConnection = Mockito.mock(RestConnection.class);
    final GlobalHubRepositoryWrapper mockedGlobalRepository = Mockito.mock(GlobalHubRepositoryWrapper.class);
    final TestGlobalProperties globalProperties = new TestGlobalProperties(mockedGlobalRepository);
    GlobalHubConfigActions configActions = new GlobalHubConfigActions(mockedGlobalRepository, globalProperties, new ObjectTransformer());
    configActions = Mockito.spy(configActions);
    Mockito.doAnswer(new Answer<RestConnection>() {

        @Override
        public RestConnection answer(final InvocationOnMock invocation) throws Throwable {
            return mockedRestConnection;
        }
    }).when(configActions).createRestConnection(Mockito.any(HubServerConfigBuilder.class));
    Mockito.doNothing().when(configActions).validateHubConfiguration(Mockito.any(HubServerConfigBuilder.class));
    configActions.testConfig(getGlobalRestModelMockUtil().createGlobalRestModel());
    Mockito.verify(mockedRestConnection, Mockito.times(1)).connect();
    Mockito.reset(mockedRestConnection);
    final GlobalHubConfigRestModel fullRestModel = getGlobalRestModelMockUtil().createGlobalRestModel();
    configActions.testConfig(fullRestModel);
    Mockito.verify(mockedRestConnection, Mockito.times(1)).connect();
    Mockito.reset(mockedRestConnection);
    final GlobalHubConfigRestModel restModel = getGlobalRestModelMockUtil().createGlobalRestModel();
    final GlobalHubConfigRestModel partialRestModel = configActions.maskRestModel(restModel);
    Mockito.doAnswer(new Answer<GlobalHubConfigEntity>() {

        @Override
        public GlobalHubConfigEntity answer(final InvocationOnMock invocation) throws Throwable {
            return getGlobalEntityMockUtil().createGlobalEntity();
        }
    }).when(mockedGlobalRepository).findOne(Mockito.anyLong());
    final String result = configActions.testConfig(partialRestModel);
    assertEquals("Successfully connected to the Hub.", result);
    Mockito.verify(mockedRestConnection, Mockito.times(1)).connect();
}
Also used : GlobalHubConfigActions(com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigActions) GlobalHubConfigRestModel(com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigRestModel) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ObjectTransformer(com.blackducksoftware.integration.hub.alert.web.ObjectTransformer) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) GlobalHubRepositoryWrapper(com.blackducksoftware.integration.hub.alert.datasource.entity.repository.global.GlobalHubRepositoryWrapper) TestGlobalProperties(com.blackducksoftware.integration.hub.alert.TestGlobalProperties) GlobalHubConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.global.GlobalHubConfigEntity) Test(org.junit.Test) GlobalActionsTest(com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)

Example 7 with RestConnection

use of com.blackducksoftware.integration.hub.rest.RestConnection 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 8 with RestConnection

use of com.blackducksoftware.integration.hub.rest.RestConnection 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 9 with RestConnection

use of com.blackducksoftware.integration.hub.rest.RestConnection in project hub-alert by blackducksoftware.

the class GlobalHubConfigActionsTest method testCreateRestConnection.

@Test
public void testCreateRestConnection() throws Exception {
    final GlobalHubConfigActions configActions = new GlobalHubConfigActions(null, null, null);
    final String url = "https://www.google.com/";
    final String apiToken = "User";
    HubServerConfigBuilder serverConfigBuilder = new HubServerConfigBuilder();
    serverConfigBuilder.setHubUrl(url);
    serverConfigBuilder.setApiToken(apiToken);
    // we create this spy to skip the server validation that happens in the build method
    serverConfigBuilder = Mockito.spy(serverConfigBuilder);
    Mockito.doAnswer(new Answer<HubServerConfig>() {

        @Override
        public HubServerConfig answer(final InvocationOnMock invocation) throws Throwable {
            final HubServerConfig hubServerConfig = new HubServerConfig(new URL(url), 0, apiToken, new ProxyInfo(null, 0, null, null, null, null), false, new UriCombiner());
            return hubServerConfig;
        }
    }).when(serverConfigBuilder).build();
    final RestConnection restConnection = configActions.createRestConnection(serverConfigBuilder);
    assertNotNull(restConnection);
}
Also used : ProxyInfo(com.blackducksoftware.integration.hub.proxy.ProxyInfo) UriCombiner(com.blackducksoftware.integration.hub.rest.UriCombiner) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) HubServerConfig(com.blackducksoftware.integration.hub.configuration.HubServerConfig) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) GlobalHubConfigActions(com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigActions) URL(java.net.URL) Test(org.junit.Test) GlobalActionsTest(com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)

Example 10 with RestConnection

use of com.blackducksoftware.integration.hub.rest.RestConnection 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

RestConnection (com.blackducksoftware.integration.hub.rest.RestConnection)16 Test (org.junit.Test)10 HubServerConfigBuilder (com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder)6 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)5 TestGlobalProperties (com.blackducksoftware.integration.hub.alert.TestGlobalProperties)5 Request (com.blackducksoftware.integration.hub.request.Request)5 ChannelRestConnectionFactory (com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRestConnectionFactory)3 GlobalHubConfigActions (com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigActions)3 GlobalActionsTest (com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)3 HubServerConfig (com.blackducksoftware.integration.hub.configuration.HubServerConfig)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 GlobalProperties (com.blackducksoftware.integration.hub.alert.config.GlobalProperties)2 GlobalHubConfigEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.global.GlobalHubConfigEntity)2 GlobalHubRepositoryWrapper (com.blackducksoftware.integration.hub.alert.datasource.entity.repository.global.GlobalHubRepositoryWrapper)2 GlobalHubConfigRestModel (com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigRestModel)2 ObjectTransformer (com.blackducksoftware.integration.hub.alert.web.ObjectTransformer)2 LoginRestModel (com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel)2 ProxyInfo (com.blackducksoftware.integration.hub.proxy.ProxyInfo)2 UriCombiner (com.blackducksoftware.integration.hub.rest.UriCombiner)2 HubServicesFactory (com.blackducksoftware.integration.hub.service.HubServicesFactory)2