Search in sources :

Example 11 with AvailabilityStatus

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();
    }
}
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 12 with AvailabilityStatus

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();
    }
}
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 13 with AvailabilityStatus

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();
    }
}
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 14 with AvailabilityStatus

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;
}
Also used : AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus)

Example 15 with AvailabilityStatus

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;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpStatus(org.springframework.http.HttpStatus) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

Aggregations

AvailabilityStatus (org.hisp.dhis.dxf2.synch.AvailabilityStatus)28 Test (org.junit.jupiter.api.Test)19 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)14 HttpUtils (org.hisp.dhis.system.util.HttpUtils)12 DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)9 IOException (java.io.IOException)6 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)6 MetadataRetryContext (org.hisp.dhis.dxf2.metadata.jobs.MetadataRetryContext)6 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)5 Date (java.util.Date)4 DhisSpringTest (org.hisp.dhis.DhisSpringTest)4 IntegrationTest (org.hisp.dhis.IntegrationTest)4 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 HttpResponse (org.apache.http.HttpResponse)3 MetadataSyncServiceException (org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException)3 MetadataRetryContext (org.hisp.dhis.dxf2.metadata.tasks.MetadataRetryContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 RemoteServerUnavailableException (org.hisp.dhis.dxf2.metadata.sync.exception.RemoteServerUnavailableException)2