Search in sources :

Example 6 with DhisHttpResponse

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

the class MetadataVersionDelegateTest method testShouldReturnEmptyMetadataDifference.

@Test
public void testShouldReturnEmptyMetadataDifference() throws Exception {
    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);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.BAD_REQUEST.value());
    PowerMockito.when(HttpUtils.httpGET(baselineUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    when(renderService.fromMetadataVersion(any(InputStream.class), eq(RenderFormat.JSON))).thenThrow(new IOException(""));
    expectedException.expect(MetadataVersionServiceException.class);
    expectedException.expectMessage("Client Error. Http call failed with status code: 400 Caused by: " + dhisHttpResponse.getResponse());
    metadataVersionDelegate.getMetaDataDifference(metadataVersion);
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) InputStream(org.omg.CORBA.portable.InputStream) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 7 with DhisHttpResponse

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

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenRenderServiceThrowsExceptionWhileGettingMetadataDifference.

@Test
public void testShouldThrowExceptionWhenRenderServiceThrowsExceptionWhileGettingMetadataDifference() throws Exception {
    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);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.OK.value());
    PowerMockito.when(HttpUtils.httpGET(baselineUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    when(renderService.fromMetadataVersion(any(InputStream.class), eq(RenderFormat.JSON))).thenThrow(new IOException(""));
    expectedException.expect(MetadataVersionServiceException.class);
    expectedException.expectMessage("Exception occurred while trying to do JSON conversion. Caused by: ");
    metadataVersionDelegate.getMetaDataDifference(metadataVersion);
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) InputStream(org.omg.CORBA.portable.InputStream) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 8 with DhisHttpResponse

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

the class MetadataVersionDelegateTest method testShouldGetMetaDataDifferenceWithStatusOk.

@Test
public void testShouldGetMetaDataDifferenceWithStatusOk() throws Exception {
    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());
    PowerMockito.mockStatic(HttpUtils.class);
    PowerMockito.when(HttpUtils.httpGET(url, 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);
    PowerMockito.when(HttpUtils.httpGET(baselineUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    when(renderService.fromMetadataVersion(any(InputStream.class), eq(RenderFormat.JSON))).thenReturn(metadataVersionList);
    List<MetadataVersion> metaDataDifference = metadataVersionDelegate.getMetaDataDifference(metadataVersion);
    assertTrue(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());
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) InputStream(org.omg.CORBA.portable.InputStream) ArrayList(java.util.ArrayList) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) HttpResponse(org.apache.http.HttpResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 9 with DhisHttpResponse

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

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithClientError.

@Test
public void testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithClientError() throws Exception {
    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?versionName=Version_Name";
    when(metadataSystemSettingService.getVersionDetailsUrl("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.CONFLICT.value());
    when(metadataSystemSettingService.getVersionDetailsUrl("testVersion")).thenReturn(versionUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    PowerMockito.when(HttpUtils.httpGET(versionUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    when(renderService.fromJson(response, MetadataVersion.class)).thenReturn(metadataVersion);
    expectedException.expect(MetadataVersionServiceException.class);
    expectedException.expectMessage("Client Error. Http call failed with status code: " + HttpStatus.CONFLICT.value() + " Caused by: " + response);
    metadataVersionDelegate.getRemoteMetadataVersion("testVersion");
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) HttpResponse(org.apache.http.HttpResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 10 with DhisHttpResponse

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

the class MetadataVersionDelegateTest method testShouldDownloadMetadataVersion.

@Test
public void testShouldDownloadMetadataVersion() throws Exception {
    MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
    metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
    String url = "http://localhost:9080/api/metadata/version/testVersion/data.gz";
    String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
    when(metadataSystemSettingService.getDownloadVersionSnapshotURL("testVersion")).thenReturn(url);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.OK.value());
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    PowerMockito.when(HttpUtils.httpGET(downloadUrl, true, username, password, null, DOWNLOAD_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    String actualVersionSnapShot = metadataVersionDelegate.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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)12 AvailabilityStatus (org.hisp.dhis.dxf2.synch.AvailabilityStatus)9 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)9 DhisSpringTest (org.hisp.dhis.DhisSpringTest)8 IntegrationTest (org.hisp.dhis.IntegrationTest)8 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 IOException (java.io.IOException)5 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)4 HttpResponse (org.apache.http.HttpResponse)3 InputStream (org.omg.CORBA.portable.InputStream)3 ArrayList (java.util.ArrayList)2 RemoteServerUnavailableException (org.hisp.dhis.exception.RemoteServerUnavailableException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1