use of org.hisp.dhis.system.util.HttpUtils in project dhis2-core by dhis2.
the class MetadataVersionDelegateTest method testShouldGetMetaDataDifferenceWithStatusOk.
@Test
void testShouldGetMetaDataDifferenceWithStatusOk() {
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);
HttpResponse httpResponse = mock(HttpResponse.class);
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);
List<MetadataVersion> metadataVersionList = new ArrayList<>();
metadataVersionList.add(metadataVersion);
when(metadataSystemSettingService.getMetaDataDifferenceURL("testVersion")).thenReturn(baselineUrl);
when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
when(renderService.fromMetadataVersion(any(ByteArrayInputStream.class), eq(RenderFormat.JSON))).thenReturn(metadataVersionList);
List<MetadataVersion> metaDataDifference = target.getMetaDataDifference(metadataVersion);
assertEquals(metaDataDifference.size(), metadataVersionList.size());
assertEquals(metadataVersionList.get(0).getType(), metaDataDifference.get(0).getType());
assertEquals(metadataVersionList.get(0).getName(), metaDataDifference.get(0).getName());
assertEquals(metadataVersionList.get(0).getHashCode(), metaDataDifference.get(0).getHashCode());
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.hisp.dhis.system.util.HttpUtils in project dhis2-core by dhis2.
the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithClientError.
@Test
void testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithClientError() {
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.CONFLICT.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"), "Client Error. Http call failed with status code: " + HttpStatus.CONFLICT.value() + " Caused by: " + response);
}
}
Aggregations