Search in sources :

Example 1 with HttpUtils

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

the class MetadataVersionDelegateTest method testShouldReturnNullOnInValidDHISResponse.

@Test
void testShouldReturnNullOnInValidDHISResponse() {
    when(metadataSystemSettingService.getDownloadVersionSnapshotURL("testVersion")).thenReturn(downloadUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(new AvailabilityStatus(true, "test_message", null));
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        mocked.when(() -> HttpUtils.httpGET(downloadUrl, true, username, password, null, DOWNLOAD_TIMEOUT, true)).thenReturn(null);
        String actualVersionSnapShot = target.downloadMetadataVersionSnapshot(new MetadataVersion("testVersion", VersionType.BEST_EFFORT));
        assertNull(actualVersionSnapShot);
    }
}
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)

Example 2 with HttpUtils

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

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenRenderServiceThrowsException.

@Test
void testShouldThrowExceptionWhenRenderServiceThrowsException() {
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "testMessage", null);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.OK.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);
        when(renderService.fromJson(response, MetadataVersion.class)).thenThrow(new MetadataVersionServiceException(""));
        assertThrows(MetadataVersionServiceException.class, () -> target.getRemoteMetadataVersion("testVersion"), "Exception occurred while trying to do JSON conversion for metadata version");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpUtils(org.hisp.dhis.system.util.HttpUtils) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 3 with HttpUtils

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

the class MetadataVersionDelegateTest method testShouldGetRemoteMetadataVersionWithStatusOk.

@Test
void testShouldGetRemoteMetadataVersionWithStatusOk() {
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "testMessage", null);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.OK.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);
        when(renderService.fromJson(response, MetadataVersion.class)).thenReturn(metadataVersion);
        MetadataVersion remoteMetadataVersion = target.getRemoteMetadataVersion("testVersion");
        assertEquals(metadataVersion.getType(), remoteMetadataVersion.getType());
        assertEquals(metadataVersion.getHashCode(), remoteMetadataVersion.getHashCode());
        assertEquals(metadataVersion.getName(), remoteMetadataVersion.getName());
        assertEquals(metadataVersion, remoteMetadataVersion);
    } 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) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 4 with HttpUtils

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

the class MetadataVersionDelegateTest method testShouldNotGetMetadataVersionIfRemoteServerIsUnavailable.

@Test
void testShouldNotGetMetadataVersionIfRemoteServerIsUnavailable() {
    when(metadataSystemSettingService.getDownloadVersionSnapshotURL("testVersion")).thenReturn(downloadUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(new AvailabilityStatus(false, "test_message", null));
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        mocked.when(() -> HttpUtils.httpGET(downloadUrl, true, username, password, null, DOWNLOAD_TIMEOUT, true)).thenReturn(null);
        assertThrows(RemoteServerUnavailableException.class, () -> target.downloadMetadataVersionSnapshot(new MetadataVersion("testVersion", VersionType.BEST_EFFORT)));
        mocked.verifyNoInteractions();
    }
}
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)

Example 5 with HttpUtils

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

the class MetadataVersionDelegateTest method testShouldDownloadMetadataVersion.

@Test
void testShouldDownloadMetadataVersion() {
    when(metadataSystemSettingService.getDownloadVersionSnapshotURL("testVersion")).thenReturn(downloadUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(new AvailabilityStatus(true, "test_message", null));
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
        DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.OK.value());
        mocked.when(() -> HttpUtils.httpGET(downloadUrl, true, username, password, null, DOWNLOAD_TIMEOUT, true)).thenReturn(dhisHttpResponse);
        MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
        metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
        String actualVersionSnapShot = target.downloadMetadataVersionSnapshot(metadataVersion);
        assertEquals(response, actualVersionSnapShot);
    }
}
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)

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