use of org.hisp.dhis.dxf2.synch.AvailabilityStatus 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.dxf2.synch.AvailabilityStatus 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.dxf2.synch.AvailabilityStatus 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.dxf2.synch.AvailabilityStatus in project dhis2-core by dhis2.
the class SyncUtils method testServerAvailabilityWithRetries.
/**
* Checks the availability of remote server. In case of error it tries
* {@code maxAttempts} of time with a {@code delaybetweenAttempts} delay
* between retries before giving up.
*
* @param systemSettingManager Reference to SystemSettingManager
* @param restTemplate Reference to RestTemplate
* @param maxAttempts Specifies how many retries are done in case of error
* @param delayBetweenAttempts Specifies delay between retries
* @return AvailabilityStatus that says whether the server is available or
* not
*/
private static AvailabilityStatus testServerAvailabilityWithRetries(SystemSettingManager systemSettingManager, RestTemplate restTemplate, int maxAttempts, long delayBetweenAttempts) {
AvailabilityStatus serverStatus = isRemoteServerAvailable(systemSettingManager, restTemplate);
for (int i = 1; i < maxAttempts; i++) {
if (serverStatus.isAvailable()) {
return serverStatus;
}
try {
log.info("Remote server is not available. Retry #" + i + " in " + delayBetweenAttempts + " ms.");
Thread.sleep(delayBetweenAttempts);
} catch (InterruptedException e) {
log.error("Sleep between sync retries failed.", e);
Thread.currentThread().interrupt();
}
serverStatus = isRemoteServerAvailable(systemSettingManager, restTemplate);
}
log.error("Remote server is not available. Details: " + serverStatus);
return serverStatus;
}
use of org.hisp.dhis.dxf2.synch.AvailabilityStatus in project dhis2-core by dhis2.
the class SyncUtils method isRemoteServerAvailable.
/**
* Checks the availability of remote server
*
* @param systemSettingManager Reference to SystemSettingManager
* @param restTemplate Reference to RestTemplate
* @return AvailabilityStatus that says whether the server is available or
* not
*/
public static AvailabilityStatus isRemoteServerAvailable(SystemSettingManager systemSettingManager, RestTemplate restTemplate) {
if (!isRemoteServerConfigured(systemSettingManager)) {
return new AvailabilityStatus(false, "Remote server is not configured", HttpStatus.BAD_GATEWAY);
}
String url = systemSettingManager.getStringSetting(SettingKey.REMOTE_INSTANCE_URL) + PING_PATH;
String username = systemSettingManager.getStringSetting(SettingKey.REMOTE_INSTANCE_USERNAME);
String password = systemSettingManager.getStringSetting(SettingKey.REMOTE_INSTANCE_PASSWORD);
log.debug(String.format("Remote server ping URL: %s, username: %s", url, username));
HttpEntity<String> request = getBasicAuthRequestEntity(username, password);
ResponseEntity<String> response = null;
HttpStatus sc = null;
String st = null;
AvailabilityStatus status = null;
try {
response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
sc = response.getStatusCode();
} catch (HttpClientErrorException | HttpServerErrorException ex) {
sc = ex.getStatusCode();
st = ex.getStatusText();
} catch (ResourceAccessException ex) {
return new AvailabilityStatus(false, "Network is unreachable", HttpStatus.BAD_GATEWAY);
}
log.debug("Response status code: " + sc);
if (HttpStatus.OK.equals(sc)) {
status = new AvailabilityStatus(true, "Authentication was successful", sc);
} else if (HttpStatus.FOUND.equals(sc)) {
status = new AvailabilityStatus(false, "No authentication was provided", sc);
} else if (HttpStatus.UNAUTHORIZED.equals(sc)) {
status = new AvailabilityStatus(false, "Authentication failed", sc);
} else if (HttpStatus.INTERNAL_SERVER_ERROR.equals(sc)) {
status = new AvailabilityStatus(false, "Remote server experienced an internal error", sc);
} else {
status = new AvailabilityStatus(false, "Server is not available: " + st, sc);
}
log.info("Status: " + status);
return status;
}
Aggregations