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