MetadataVersionDelegate { public List<MetadataVersion> getMetaDataDifference( MetadataVersion metadataVersion ) { String url; List<MetadataVersion> metadataVersions = new ArrayList<>(); if ( metadataVersion == null ) { url = metadataSystemSettingService.getEntireVersionHistory(); } else { url = metadataSystemSettingService.getMetaDataDifferenceURL( metadataVersion.getName() ); } DhisHttpResponse dhisHttpResponse = getDhisHttpResponse( url, VERSION_TIMEOUT ); if ( isValidDhisHttpResponse( dhisHttpResponse ) ) { try { metadataVersions = renderService .fromMetadataVersion( new ByteArrayInputStream( dhisHttpResponse.getResponse().getBytes() ), RenderFormat.JSON ); return metadataVersions; } catch ( IOException io ) { String message = "Exception occurred while trying to do JSON conversion. Caused by: " + io.getMessage(); log.error( message, io ); throw new MetadataVersionServiceException( message, io ); } } log.warn( "Returning empty for the metadata versions difference" ); return metadataVersions; } 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 testShouldGetMetaDataDifferenceWithStatusOk() 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" ); String url = "http: 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( 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() ); }
@Test public void testShouldThrowExceptionWhenRenderServiceThrowsExceptionWhileGettingMetadataDifference() 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" ); String url = "http: 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( ByteArrayInputStream.class ), eq( RenderFormat.JSON ) ) ) .thenThrow( new IOException( "" ) ); expectedException.expect( MetadataVersionServiceException.class ); expectedException.expectMessage( "Exception occurred while trying to do JSON conversion. Caused by: " ); target.getMetaDataDifference( metadataVersion ); }
@Test public void testShouldReturnEmptyMetadataDifference() 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" ); String url = "http: 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 ); expectedException.expect( MetadataVersionServiceException.class ); expectedException.expectMessage( "Client Error. Http call failed with status code: 400 Caused by: " + dhisHttpResponse.getResponse() ); target.getMetaDataDifference( metadataVersion ); }