Search in sources :

Example 6 with HttpUtils

use of org.hisp.dhis.system.util.HttpUtils in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenHTTPRequestFails.

@Test
void testShouldThrowExceptionWhenHTTPRequestFails() {
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "testMessage", null);
    when(metadataSystemSettingService.getVersionDetailsUrl("testVersion")).thenReturn(versionUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        mocked.when(() -> HttpUtils.httpGET(versionUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenThrow(new Exception(""));
        assertThrows(MetadataVersionServiceException.class, () -> target.getRemoteMetadataVersion("testVersion"));
    }
}
Also used : AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpUtils(org.hisp.dhis.system.util.HttpUtils) RemoteServerUnavailableException(org.hisp.dhis.dxf2.metadata.sync.exception.RemoteServerUnavailableException) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 7 with HttpUtils

use of org.hisp.dhis.system.util.HttpUtils in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldReturnEmptyMetadataDifference.

@Test
void testShouldReturnEmptyMetadataDifference() {
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
    MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
    metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
    String url = "http://localhost:9080/api/metadata/version/history?baseline=testVersion";
    when(metadataSystemSettingService.getMetaDataDifferenceURL("testVersion")).thenReturn(url);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.BAD_REQUEST.value());
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        mocked.when(() -> HttpUtils.httpGET(baselineUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
        assertThrows(MetadataVersionServiceException.class, () -> target.getMetaDataDifference(metadataVersion), "Client Error. Http call failed with status code: 400 Caused by: " + dhisHttpResponse.getResponse());
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpUtils(org.hisp.dhis.system.util.HttpUtils) Test(org.junit.jupiter.api.Test)

Example 8 with HttpUtils

use of org.hisp.dhis.system.util.HttpUtils in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenRenderServiceThrowsExceptionWhileGettingMetadataDifference.

@Test
void testShouldThrowExceptionWhenRenderServiceThrowsExceptionWhileGettingMetadataDifference() {
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
    MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
    metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
    String url = "http://localhost:9080/api/metadata/version/history?baseline=testVersion";
    when(metadataSystemSettingService.getMetaDataDifferenceURL("testVersion")).thenReturn(url);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.OK.value());
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        mocked.when(() -> HttpUtils.httpGET(baselineUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
        when(renderService.fromMetadataVersion(any(ByteArrayInputStream.class), eq(RenderFormat.JSON))).thenThrow(new IOException(""));
        assertThrows(MetadataVersionServiceException.class, () -> target.getMetaDataDifference(metadataVersion), "Exception occurred while trying to do JSON conversion. Caused by: ");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpUtils(org.hisp.dhis.system.util.HttpUtils) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 9 with HttpUtils

use of org.hisp.dhis.system.util.HttpUtils in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithServerError.

@Test
void testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithServerError() {
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
    MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
    metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    HttpResponse httpResponse = mock(HttpResponse.class);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.GATEWAY_TIMEOUT.value());
    when(metadataSystemSettingService.getVersionDetailsUrl("testVersion")).thenReturn(versionUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        mocked.when(() -> HttpUtils.httpGET(versionUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
        assertThrows(MetadataVersionServiceException.class, () -> target.getRemoteMetadataVersion("testVersion"), "Server Error. Http call failed with status code: " + HttpStatus.GATEWAY_TIMEOUT.value() + " Caused by: " + response);
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpUtils(org.hisp.dhis.system.util.HttpUtils) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 10 with HttpUtils

use of org.hisp.dhis.system.util.HttpUtils in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldGetRemoteVersionNullWhenDhisResponseReturnsNull.

@Test
void testShouldGetRemoteVersionNullWhenDhisResponseReturnsNull() {
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        mocked.when(() -> HttpUtils.httpGET(versionUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(null);
        MetadataVersion version = target.getRemoteMetadataVersion("testVersion");
        assertNull(version);
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpUtils(org.hisp.dhis.system.util.HttpUtils) Test(org.junit.jupiter.api.Test)

Aggregations

AvailabilityStatus (org.hisp.dhis.dxf2.synch.AvailabilityStatus)12 HttpUtils (org.hisp.dhis.system.util.HttpUtils)12 Test (org.junit.jupiter.api.Test)12 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)10 DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)8 IOException (java.io.IOException)5 HttpResponse (org.apache.http.HttpResponse)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)2 ArrayList (java.util.ArrayList)1 RemoteServerUnavailableException (org.hisp.dhis.dxf2.metadata.sync.exception.RemoteServerUnavailableException)1