@Test
public void testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithServerError()
    throws Exception
{

    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.GATEWAY_TIMEOUT.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 );
    expectedException.expect( MetadataVersionServiceException.class );
    expectedException.expectMessage( "Server Error. Http call failed with status code: "
        + HttpStatus.GATEWAY_TIMEOUT.value() + " Caused by: " + response );
    target.getRemoteMetadataVersion( "testVersion" );
}