Search in sources :

Example 1 with IntHttpClient

use of com.synopsys.integration.rest.client.IntHttpClient in project hub-alert by blackducksoftware.

the class UpdateChecker method getUpdateModel.

public UpdateModel getUpdateModel() {
    IntHttpClient intHttpClient = createHttpClient();
    DockerTagRetriever dockerTagRetriever = new DockerTagRetriever(gson, intHttpClient);
    Optional<AboutModel> aboutModel = aboutReader.getAboutModel();
    String currentVersion = aboutModel.map(AboutModel::getVersion).orElse(null);
    String alertCreated = aboutModel.map(AboutModel::getCreated).orElse(null);
    boolean isProduction = isProductionVersion(currentVersion);
    Optional<VersionDateModel> latestAvailableVersion = getLatestAvailableTag(dockerTagRetriever, isProduction);
    String repositoryUrl = dockerTagRetriever.getRepositoryUrl();
    // latestAvailableVersion will not be present if Alert can not reach Docker Hub and DockerTagRetriever will log a warning
    // if latestAvailableVersion is empty, use the Alert version and date so we report no update available
    String latestVersion = latestAvailableVersion.map(VersionDateModel::getVersionName).orElse(currentVersion);
    String latestDate = latestAvailableVersion.map(VersionDateModel::getDate).orElse(alertCreated);
    return getUpdateModel(currentVersion, alertCreated, latestVersion, latestDate, repositoryUrl);
}
Also used : IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) AboutModel(com.synopsys.integration.alert.web.api.about.AboutModel)

Example 2 with IntHttpClient

use of com.synopsys.integration.rest.client.IntHttpClient in project hub-alert by blackducksoftware.

the class MSTeamsChannelMessageSender method sendMessages.

@Override
public MessageResult sendMessages(MSTeamsJobDetailsModel msTeamsJobDetailsModel, List<MSTeamsChannelMessageModel> channelMessages) throws AlertException {
    String webhook = msTeamsJobDetailsModel.getWebhook();
    Map<String, String> requestHeaders = new HashMap<>();
    requestHeaders.put("Content-Type", "application/json");
    IntHttpClient intHttpClient = connectionFactory.createIntHttpClient(webhook);
    RestChannelUtility restChannelUtility = new RestChannelUtility(intHttpClient);
    List<Request> messageRequests = channelMessages.stream().map(this::createJsonString).map(jsonString -> restChannelUtility.createPostMessageRequest(webhook, requestHeaders, jsonString)).collect(Collectors.toList());
    restChannelUtility.sendMessage(messageRequests, msTeamsKey.getUniversalKey());
    return new MessageResult(String.format("Successfully sent %d MSTeams message(s)", channelMessages.size()));
}
Also used : JsonObject(com.google.gson.JsonObject) Request(com.synopsys.integration.rest.request.Request) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) RestChannelUtility(com.synopsys.integration.alert.api.channel.rest.RestChannelUtility) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ChannelRestConnectionFactory(com.synopsys.integration.alert.api.channel.rest.ChannelRestConnectionFactory) Collectors(java.util.stream.Collectors) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) MSTeamsJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.MSTeamsJobDetailsModel) ChannelMessageSender(com.synopsys.integration.alert.api.channel.ChannelMessageSender) List(java.util.List) Component(org.springframework.stereotype.Component) JsonArray(com.google.gson.JsonArray) Map(java.util.Map) MsTeamsKey(com.synopsys.integration.alert.descriptor.api.MsTeamsKey) HashMap(java.util.HashMap) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) RestChannelUtility(com.synopsys.integration.alert.api.channel.rest.RestChannelUtility) Request(com.synopsys.integration.rest.request.Request) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult)

Example 3 with IntHttpClient

use of com.synopsys.integration.rest.client.IntHttpClient in project hub-alert by blackducksoftware.

the class RestChannelUtilityTest method createMockHttpClientWithResponse.

private IntHttpClient createMockHttpClientWithResponse(Response response) throws IntegrationException {
    IntHttpClient intHttpClient = Mockito.mock(IntHttpClient.class);
    Mockito.when(intHttpClient.execute(Mockito.any(Request.class))).thenReturn(response);
    return intHttpClient;
}
Also used : IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) Request(com.synopsys.integration.rest.request.Request)

Example 4 with IntHttpClient

use of com.synopsys.integration.rest.client.IntHttpClient in project hub-alert by blackducksoftware.

the class RestChannelUtilityTest method sendMessageRuntimeExceptionTest.

@Test
public void sendMessageRuntimeExceptionTest() throws IntegrationException {
    IntHttpClient intHttpClient = Mockito.mock(IntHttpClient.class);
    Mockito.when(intHttpClient.execute(Mockito.any(Request.class))).thenThrow(new IllegalStateException("Something is wrong"));
    RestChannelUtility restChannelUtility = new RestChannelUtility(intHttpClient);
    try {
        restChannelUtility.sendMessage(List.of(TEST_REQUEST, TEST_REQUEST), CLASS_NAME);
        fail("Expected an exception to be thrown");
    } catch (AlertException e) {
    // Pass
    }
}
Also used : IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) Request(com.synopsys.integration.rest.request.Request) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Test(org.junit.jupiter.api.Test)

Example 5 with IntHttpClient

use of com.synopsys.integration.rest.client.IntHttpClient in project hub-alert by blackducksoftware.

the class ChannelRestConnectionFactoryTest method testConnectionFields.

@Test
void testConnectionFields() {
    String baseUrl = "https://example-base-url";
    final String host = "host";
    final int port = 1;
    CredentialsBuilder builder = Credentials.newBuilder();
    builder.setUsername("username");
    builder.setPassword("password");
    Credentials credentials = builder.build();
    ProxyInfoBuilder proxyBuilder = ProxyInfo.newBuilder();
    proxyBuilder.setHost(host);
    proxyBuilder.setPort(port);
    proxyBuilder.setCredentials(credentials);
    proxyBuilder.setNtlmDomain(null);
    proxyBuilder.setNtlmWorkstation(null);
    ProxyInfo expectedProxyInfo = proxyBuilder.build();
    MockAlertProperties testAlertProperties = new MockAlertProperties();
    testAlertProperties.setAlertTrustCertificate(true);
    ProxyManager proxyManager = Mockito.mock(ProxyManager.class);
    Mockito.when(proxyManager.createProxyInfoForHost(baseUrl)).thenReturn(expectedProxyInfo);
    ChannelRestConnectionFactory channelRestConnectionFactory = new ChannelRestConnectionFactory(testAlertProperties, proxyManager, gson);
    IntHttpClient intHttpClient = channelRestConnectionFactory.createIntHttpClient(baseUrl);
    assertNotNull(intHttpClient);
    assertEquals(expectedProxyInfo, intHttpClient.getProxyInfo());
}
Also used : ProxyInfo(com.synopsys.integration.rest.proxy.ProxyInfo) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) MockAlertProperties(com.synopsys.integration.alert.test.common.MockAlertProperties) ProxyManager(com.synopsys.integration.alert.common.rest.proxy.ProxyManager) CredentialsBuilder(com.synopsys.integration.rest.credentials.CredentialsBuilder) Credentials(com.synopsys.integration.rest.credentials.Credentials) ProxyInfoBuilder(com.synopsys.integration.rest.proxy.ProxyInfoBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

IntHttpClient (com.synopsys.integration.rest.client.IntHttpClient)14 Request (com.synopsys.integration.rest.request.Request)7 Test (org.junit.jupiter.api.Test)6 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)5 ProxyInfo (com.synopsys.integration.rest.proxy.ProxyInfo)5 Response (com.synopsys.integration.rest.response.Response)5 JsonObject (com.google.gson.JsonObject)2 ChannelMessageSender (com.synopsys.integration.alert.api.channel.ChannelMessageSender)2 ChannelRestConnectionFactory (com.synopsys.integration.alert.api.channel.rest.ChannelRestConnectionFactory)2 RestChannelUtility (com.synopsys.integration.alert.api.channel.rest.RestChannelUtility)2 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)2 DockerTagsResponseModel (com.synopsys.integration.alert.update.model.DockerTagsResponseModel)2 IntegrationException (com.synopsys.integration.exception.IntegrationException)2 IntLogger (com.synopsys.integration.log.IntLogger)2 HttpUrl (com.synopsys.integration.rest.HttpUrl)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2