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