MetadataVersionDelegate { public MetadataVersion getRemoteMetadataVersion( String versionName ) { String versionDetailsUrl = metadataSystemSettingService.getVersionDetailsUrl( versionName ); DhisHttpResponse dhisHttpResponse = getDhisHttpResponse( versionDetailsUrl, VERSION_TIMEOUT ); MetadataVersion dataVersion = null; if ( isValidDhisHttpResponse( dhisHttpResponse ) ) { try { dataVersion = renderService.fromJson( dhisHttpResponse.getResponse(), MetadataVersion.class ); } catch ( Exception e ) { String message = "Exception occurred while trying to do JSON conversion for metadata version"; log.error( message, e ); throw new MetadataVersionServiceException( message, e ); } } return dataVersion; } MetadataVersionDelegate( DefaultMetadataSystemSettingService metadataSystemSettingService,
        SynchronizationManager synchronizationManager, RenderService renderService,
        MetadataVersionService metadataVersionService ); MetadataVersion getRemoteMetadataVersion( String versionName ); List<MetadataVersion> getMetaDataDifference( MetadataVersion metadataVersion ); String downloadMetadataVersionSnapshot(MetadataVersion version ); synchronized void addNewMetadataVersion( MetadataVersion version );  }
@Test public void testShouldThrowExceptionWhenServerNotAvailable() { AvailabilityStatus availabilityStatus = new AvailabilityStatus( false, "test_message", null ); when( synchronizationManager.isRemoteServerAvailable() ).thenReturn( availabilityStatus ); expectedException.expect( RemoteServerUnavailableException.class ); expectedException.expectMessage( "test_message" ); target.getRemoteMetadataVersion( "testVersion" ); }
@Test public void testShouldGetRemoteVersionNullWhenDhisResponseReturnsNull() { AvailabilityStatus availabilityStatus = new AvailabilityStatus( true, "test_message", null ); when( synchronizationManager.isRemoteServerAvailable() ).thenReturn( availabilityStatus ); MetadataVersion version = target.getRemoteMetadataVersion( "testVersion" ); assertNull( version ); }
@Test public void testShouldThrowExceptionWhenHTTPRequestFails() throws Exception { when( metadataSystemSettingService.getRemoteInstanceUserName() ).thenReturn( username ); when( metadataSystemSettingService.getRemoteInstancePassword() ).thenReturn( password ); AvailabilityStatus availabilityStatus = new AvailabilityStatus( true, "testMessage", null ); when( metadataSystemSettingService.getVersionDetailsUrl( "testVersion" ) ).thenReturn( versionUrl ); when( synchronizationManager.isRemoteServerAvailable() ).thenReturn( availabilityStatus ); PowerMockito.when( HttpUtils.httpGET( versionUrl, true, username, password, null, VERSION_TIMEOUT, true ) ) .thenThrow( new Exception( "" ) ); expectedException.expect( MetadataVersionServiceException.class ); target.getRemoteMetadataVersion( "testVersion" ); }
@Test public void testShouldGetRemoteMetadataVersionWithStatusOk() throws Exception { 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 ); PowerMockito.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 ); }
@Test public void testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithClientError() 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.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 ); expectedException.expect( MetadataVersionServiceException.class ); expectedException.expectMessage( "Client Error. Http call failed with status code: " + HttpStatus.CONFLICT.value() + " Caused by: " + response ); target.getRemoteMetadataVersion( "testVersion" ); }
@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" ); }
@Test public void testShouldThrowExceptionWhenRenderServiceThrowsException() throws Exception { 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 ); PowerMockito.when( HttpUtils.httpGET( versionUrl, true, username, password, null, VERSION_TIMEOUT, true ) ) .thenReturn( dhisHttpResponse ); when( renderService.fromJson( response, MetadataVersion.class ) ) .thenThrow( new MetadataVersionServiceException( "" ) ); expectedException.expect( MetadataVersionServiceException.class ); expectedException.expectMessage( "Exception occurred while trying to do JSON conversion for metadata version" ); target.getRemoteMetadataVersion( "testVersion" ); }