Search in sources :

Example 1 with HttpClientOperations

use of org.finra.herd.dao.HttpClientOperations in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetBusinessObjectDataAssertNoAuthorizationHeaderWhenNoSsl.

@Test
public void testGetBusinessObjectDataAssertNoAuthorizationHeaderWhenNoSsl() throws Exception {
    HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
    HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
    ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
    try {
        CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
        when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse);
        when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
        when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new BusinessObjectData())));
        DownloaderInputManifestDto manifest = new DownloaderInputManifestDto();
        downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(false);
        downloaderWebClient.getBusinessObjectData(manifest);
        verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> httpUriRequest.getFirstHeader("Authorization") == null));
    } finally {
        ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
    }
}
Also used : HttpVersion(org.apache.http.HttpVersion) Arrays(java.util.Arrays) BasicStatusLine(org.apache.http.message.BasicStatusLine) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) Mockito.doThrow(org.mockito.Mockito.doThrow) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) UploaderInputManifestDto(org.finra.herd.model.dto.UploaderInputManifestDto) Command(org.finra.herd.core.Command) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) S3KeyPrefixInformation(org.finra.herd.model.api.xml.S3KeyPrefixInformation) Storage(org.finra.herd.model.api.xml.Storage) DataBridgeWebClient(org.finra.herd.tools.common.databridge.DataBridgeWebClient) Assert.fail(org.junit.Assert.fail) Objects(com.google.common.base.Objects) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) StringEntity(org.apache.http.entity.StringEntity) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) RegServerAccessParamsDto(org.finra.herd.model.dto.RegServerAccessParamsDto) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) Matchers.argThat(org.mockito.Matchers.argThat) Assert(org.junit.Assert) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) Assert.assertEquals(org.junit.Assert.assertEquals) StorageEntity(org.finra.herd.model.jpa.StorageEntity) Mockito.mock(org.mockito.Mockito.mock) BusinessObjectDataStatusEntity(org.finra.herd.model.jpa.BusinessObjectDataStatusEntity) StringEntity(org.apache.http.entity.StringEntity) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 2 with HttpClientOperations

use of org.finra.herd.dao.HttpClientOperations in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetBusinessObjectDataDownloadCredentialAssertHttpClientClosedWhenIOException.

@Test
public void testGetBusinessObjectDataDownloadCredentialAssertHttpClientClosedWhenIOException() throws Exception {
    HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
    HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
    ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
    try {
        IOException expectedException = new IOException();
        CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
        when(mockHttpClientOperations.createHttpClient()).thenReturn(closeableHttpClient);
        when(mockHttpClientOperations.execute(any(), any())).thenThrow(expectedException);
        DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
        String storageName = "storageName";
        try {
            downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName);
            verify(closeableHttpClient).close();
            fail();
        } catch (Exception e) {
            assertEquals(expectedException, e);
        }
    } finally {
        ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) IOException(java.io.IOException) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with HttpClientOperations

use of org.finra.herd.dao.HttpClientOperations in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetBusinessObjectDataAssertNamespaceOptional.

@Test
public void testGetBusinessObjectDataAssertNamespaceOptional() throws Exception {
    HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
    HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
    ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
    try {
        String expectedHttpMethod = "GET";
        String expectedUri = "https://testWebServiceHostname:1234/herd-app/rest/businessObjectData" + "/businessObjectDefinitionNames/businessObjectDefinitionName/businessObjectFormatUsages/businessObjectFormatUsage" + "/businessObjectFormatFileTypes/businessObjectFormatFileType?partitionKey=partitionKey&partitionValue=partitionValue&" + "businessObjectFormatVersion=0&businessObjectDataVersion=1";
        CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
        when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse);
        when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
        BusinessObjectData expectedBusinessObjectData = new BusinessObjectData();
        expectedBusinessObjectData.setId(1234);
        StringEntity httpEntity = new StringEntity(xmlHelper.objectToXml(expectedBusinessObjectData));
        when(closeableHttpResponse.getEntity()).thenReturn(httpEntity);
        DownloaderInputManifestDto manifest = new DownloaderInputManifestDto();
        manifest.setBusinessObjectDefinitionName("businessObjectDefinitionName");
        manifest.setBusinessObjectFormatUsage("businessObjectFormatUsage");
        manifest.setBusinessObjectFormatFileType("businessObjectFormatFileType");
        manifest.setBusinessObjectFormatVersion("0");
        manifest.setPartitionKey("partitionKey");
        manifest.setPartitionValue("partitionValue");
        manifest.setBusinessObjectDataVersion("1");
        assertEquals(expectedBusinessObjectData.getId(), downloaderWebClient.getBusinessObjectData(manifest).getId());
        verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> Objects.equal(expectedHttpMethod, httpUriRequest.getMethod()) && Objects.equal(expectedUri, httpUriRequest.getURI().toString())));
    } finally {
        ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
    }
}
Also used : HttpVersion(org.apache.http.HttpVersion) Arrays(java.util.Arrays) BasicStatusLine(org.apache.http.message.BasicStatusLine) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) Mockito.doThrow(org.mockito.Mockito.doThrow) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) UploaderInputManifestDto(org.finra.herd.model.dto.UploaderInputManifestDto) Command(org.finra.herd.core.Command) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) S3KeyPrefixInformation(org.finra.herd.model.api.xml.S3KeyPrefixInformation) Storage(org.finra.herd.model.api.xml.Storage) DataBridgeWebClient(org.finra.herd.tools.common.databridge.DataBridgeWebClient) Assert.fail(org.junit.Assert.fail) Objects(com.google.common.base.Objects) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) StringEntity(org.apache.http.entity.StringEntity) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) RegServerAccessParamsDto(org.finra.herd.model.dto.RegServerAccessParamsDto) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) Matchers.argThat(org.mockito.Matchers.argThat) Assert(org.junit.Assert) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) Assert.assertEquals(org.junit.Assert.assertEquals) StorageEntity(org.finra.herd.model.jpa.StorageEntity) Mockito.mock(org.mockito.Mockito.mock) BusinessObjectDataStatusEntity(org.finra.herd.model.jpa.BusinessObjectDataStatusEntity) StringEntity(org.apache.http.entity.StringEntity) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 4 with HttpClientOperations

use of org.finra.herd.dao.HttpClientOperations in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetBusinessObjectDataDownloadCredentialAssertNoAuthorizationHeaderWhenNoSsl.

@Test
public void testGetBusinessObjectDataDownloadCredentialAssertNoAuthorizationHeaderWhenNoSsl() throws Exception {
    HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
    HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
    ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
    try {
        CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
        when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse);
        when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "SUCCESS"));
        when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential())));
        DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
        String storageName = "storageName";
        boolean useSsl = false;
        downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(useSsl);
        downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName);
        verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> httpUriRequest.getFirstHeader("Authorization") == null));
    } finally {
        ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
    }
}
Also used : HttpVersion(org.apache.http.HttpVersion) Arrays(java.util.Arrays) BasicStatusLine(org.apache.http.message.BasicStatusLine) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) Mockito.doThrow(org.mockito.Mockito.doThrow) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) UploaderInputManifestDto(org.finra.herd.model.dto.UploaderInputManifestDto) Command(org.finra.herd.core.Command) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) S3KeyPrefixInformation(org.finra.herd.model.api.xml.S3KeyPrefixInformation) Storage(org.finra.herd.model.api.xml.Storage) DataBridgeWebClient(org.finra.herd.tools.common.databridge.DataBridgeWebClient) Assert.fail(org.junit.Assert.fail) Objects(com.google.common.base.Objects) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) StringEntity(org.apache.http.entity.StringEntity) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) RegServerAccessParamsDto(org.finra.herd.model.dto.RegServerAccessParamsDto) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) Matchers.argThat(org.mockito.Matchers.argThat) Assert(org.junit.Assert) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) Assert.assertEquals(org.junit.Assert.assertEquals) StorageEntity(org.finra.herd.model.jpa.StorageEntity) Mockito.mock(org.mockito.Mockito.mock) BusinessObjectDataStatusEntity(org.finra.herd.model.jpa.BusinessObjectDataStatusEntity) StringEntity(org.apache.http.entity.StringEntity) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 5 with HttpClientOperations

use of org.finra.herd.dao.HttpClientOperations in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetBusinessObjectDataDownloadCredentialAssertThrowIOExceptionWhenClosingHttpClientThrowsIOException.

/**
 * Asserts that the http client is closed and if an exception is thrown during closing of the http client, the same exception is bubbled up.
 */
@Test
public void testGetBusinessObjectDataDownloadCredentialAssertThrowIOExceptionWhenClosingHttpClientThrowsIOException() throws Exception {
    HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
    HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
    ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
    try {
        CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
        when(mockHttpClientOperations.createHttpClient()).thenReturn(closeableHttpClient);
        doThrow(IOException.class).when(closeableHttpClient).close();
        CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
        when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse);
        when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "SUCCESS"));
        when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential())));
        DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
        String storageName = "storageName";
        try {
            downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName);
            verify(closeableHttpClient).close();
            fail();
        } catch (Exception e) {
            assertEquals(IOException.class, e.getClass());
        }
    } finally {
        ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) IOException(java.io.IOException) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 HttpClientOperations (org.finra.herd.dao.HttpClientOperations)5 DownloaderInputManifestDto (org.finra.herd.model.dto.DownloaderInputManifestDto)5 Test (org.junit.Test)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 StringEntity (org.apache.http.entity.StringEntity)4 BasicStatusLine (org.apache.http.message.BasicStatusLine)4 StorageUnitDownloadCredential (org.finra.herd.model.api.xml.StorageUnitDownloadCredential)4 Objects (com.google.common.base.Objects)3 Arrays (java.util.Arrays)3 HttpVersion (org.apache.http.HttpVersion)3 Command (org.finra.herd.core.Command)3 AwsCredential (org.finra.herd.model.api.xml.AwsCredential)3 BusinessObjectData (org.finra.herd.model.api.xml.BusinessObjectData)3 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)3 S3KeyPrefixInformation (org.finra.herd.model.api.xml.S3KeyPrefixInformation)3 Storage (org.finra.herd.model.api.xml.Storage)3 StorageUnit (org.finra.herd.model.api.xml.StorageUnit)3 RegServerAccessParamsDto (org.finra.herd.model.dto.RegServerAccessParamsDto)3