MetadataSyncPreProcessor { public List<MetadataVersion> handleMetadataVersionsList( MetadataRetryContext context, MetadataVersion metadataVersion ) { log.debug( "Fetching the list of remote versions" ); List<MetadataVersion> metadataVersionList; try { metadataVersionList = metadataVersionDelegate.getMetaDataDifference( metadataVersion ); if ( metadataVersion == null ) { log.info( "There is no initial version in the system" ); } if ( isRemoteVersionEmpty( metadataVersion, metadataVersionList ) ) { log.info( "There are no metadata versions created in the remote instance." ); return metadataVersionList; } if ( isUsingLatestVersion( metadataVersion, metadataVersionList ) ) { log.info( "Your instance is already using the latest version:" + metadataVersion ); return metadataVersionList; } MetadataVersion latestVersion = getLatestVersion( metadataVersionList ); assert latestVersion != null; systemSettingManager.saveSystemSetting( SettingKey.REMOTE_METADATA_VERSION, latestVersion.getName() ); log.info( "Remote system is at version: " + latestVersion.getName() ); } catch ( MetadataVersionServiceException e ) { String message = setVersionListErrorInfoInContext( context, metadataVersion, e ); throw new MetadataSyncServiceException( message, e ); } catch ( Exception ex ) { if ( ex instanceof MetadataSyncServiceException ) { log.error( ex.getMessage(), ex ); throw ex; } String message = setVersionListErrorInfoInContext( context, metadataVersion, ex ); log.error( message, ex ); throw new MetadataSyncServiceException( message, ex ); } return metadataVersionList; } MetadataSyncPreProcessor(
        SystemSettingManager systemSettingManager,
        MetadataVersionService metadataVersionService,
        MetadataVersionDelegate metadataVersionDelegate,
        TrackerSynchronization trackerSync,
        EventSynchronization eventSync,
        DataValueSynchronization dataValueSync,
        CompleteDataSetRegistrationSynchronization completeDataSetRegistrationSync ); void setUp( MetadataRetryContext context ); void handleDataValuePush( MetadataRetryContext context, MetadataSyncJobParameters jobParameters ); void handleTrackerProgramsDataPush( MetadataRetryContext context, MetadataSyncJobParameters jobParameters ); void handleEventProgramsDataPush( MetadataRetryContext context, MetadataSyncJobParameters jobParameters ); List<MetadataVersion> handleMetadataVersionsList( MetadataRetryContext context, MetadataVersion metadataVersion ); MetadataVersion handleCurrentMetadataVersion( MetadataRetryContext context ); void handleCompleteDataSetRegistrationDataPush( MetadataRetryContext context );  }
@Test public void testHandleMetadataVersionsListShouldReturnDifferenceOfVersionsFromBaselineVersion() throws Exception { MetadataRetryContext mockRetryContext = mock( MetadataRetryContext.class ); MetadataVersion currentVersion = new MetadataVersion(); currentVersion.setType( VersionType.BEST_EFFORT ); currentVersion.setName( "test_version1" ); currentVersion.setCreated( new Date() ); currentVersion.setHashCode( "samplehashcode1" ); MetadataVersion newVersion = new MetadataVersion(); newVersion.setType( VersionType.ATOMIC ); newVersion.setName( "test_version2" ); newVersion.setCreated( new Date() ); newVersion.setHashCode( "samplehashcode2" ); List<MetadataVersion> listOfVersions = new ArrayList<>(); listOfVersions.add( newVersion ); when( metadataVersionDelegate.getMetaDataDifference( currentVersion ) ).thenReturn( listOfVersions ); List<MetadataVersion> expectedListOfVersions = metadataSyncPreProcessor.handleMetadataVersionsList( mockRetryContext, currentVersion ); assertEquals( listOfVersions.size(), expectedListOfVersions.size() ); assertEquals( expectedListOfVersions, listOfVersions ); }
@Test public void testHandleMetadataVersionsListShouldReturnNullIfInstanceDoesNotHaveAnyVersions() throws Exception { AvailabilityStatus availabilityStatus = new AvailabilityStatus( true, "test_message", null ); when( synchronizationManager.isRemoteServerAvailable() ).thenReturn( availabilityStatus ); MetadataRetryContext mockRetryContext = mock( MetadataRetryContext.class ); MetadataVersion currentVersion = new MetadataVersion(); currentVersion.setType( VersionType.BEST_EFFORT ); currentVersion.setName( "test_version" ); currentVersion.setCreated( new Date() ); currentVersion.setHashCode( "samplehashcode" ); when( metadataVersionDelegate.getMetaDataDifference( currentVersion ) ).thenReturn( new ArrayList<MetadataVersion>() ); List<MetadataVersion> expectedListOfVersions = metadataSyncPreProcessor.handleMetadataVersionsList( mockRetryContext, currentVersion ); assertTrue( expectedListOfVersions.size() == 0 ); }
@Test public void testHandleMetadataVersionsListShouldReturnEmptyListIfInstanceIsAlreadyOnLatestVersion() throws Exception { AvailabilityStatus availabilityStatus = new AvailabilityStatus( true, "test_message", null ); when( synchronizationManager.isRemoteServerAvailable() ).thenReturn( availabilityStatus ); MetadataRetryContext mockRetryContext = mock( MetadataRetryContext.class ); MetadataVersion currentVersion = new MetadataVersion(); currentVersion.setType( VersionType.BEST_EFFORT ); currentVersion.setName( "test_version" ); currentVersion.setCreated( new Date() ); currentVersion.setHashCode( "samplehashcode" ); List<MetadataVersion> listOfVersions = new ArrayList<>(); when( metadataVersionDelegate.getMetaDataDifference( currentVersion ) ).thenReturn( listOfVersions ); List<MetadataVersion> expectedListOfVersions = metadataSyncPreProcessor.handleMetadataVersionsList( mockRetryContext, currentVersion ); assertEquals( 0, expectedListOfVersions.size() ); }
@Test( expected = MetadataSyncServiceException.class ) public void testHandleMetadataVersionsListShouldThrowExceptionIfAnyIssueWithMetadataDifference() throws Exception { AvailabilityStatus availabilityStatus = new AvailabilityStatus( true, "test_message", null ); when( synchronizationManager.isRemoteServerAvailable() ).thenReturn( availabilityStatus ); MetadataRetryContext mockRetryContext = mock( MetadataRetryContext.class ); MetadataVersion currentVersion = new MetadataVersion(); currentVersion.setType( VersionType.BEST_EFFORT ); currentVersion.setName( "test_version" ); currentVersion.setCreated( new Date() ); currentVersion.setHashCode( "samplehashcode" ); List<MetadataVersion> listOfVersions = new ArrayList<>(); listOfVersions.add( currentVersion ); when( metadataVersionDelegate.getMetaDataDifference( currentVersion ) ).thenThrow( new MetadataSyncServiceException( "test_message" ) ); metadataSyncPreProcessor.handleMetadataVersionsList( mockRetryContext, currentVersion ); }
@Test public void testShouldGetLatestMetadataVersionForTheGivenVersionList() throws ParseException { MetadataRetryContext mockRetryContext = mock( MetadataRetryContext.class ); DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern( "yyyy-MM-dd HH:mm:ssZ" ); MetadataVersion currentVersion = new MetadataVersion(); currentVersion.setType( VersionType.BEST_EFFORT ); currentVersion.setName( "test_version1" ); currentVersion.setCreated( new Date() ); currentVersion.setHashCode( "samplehashcode1" ); MetadataVersion version2 = new MetadataVersion( "Version2", VersionType.ATOMIC ); org.joda.time.DateTime dateTime = dateTimeFormatter.parseDateTime( "2016-06-21 10:45:50Z" ); version2.setCreated( dateTime.toDate() ); MetadataVersion version3 = new MetadataVersion( "Version3", VersionType.ATOMIC ); org.joda.time.DateTime dateTime2 = dateTimeFormatter.parseDateTime( "2016-06-21 10:45:54Z" ); version3.setCreated( dateTime2.toDate() ); MetadataVersion version4 = new MetadataVersion( "Version4", VersionType.ATOMIC ); org.joda.time.DateTime dateTime3 = dateTimeFormatter.parseDateTime( "2016-06-21 10:45:58Z" ); version4.setCreated( dateTime3.toDate() ); List<MetadataVersion> metadataVersionList = new ArrayList<>(); metadataVersionList.add( version2 ); metadataVersionList.add( version3 ); metadataVersionList.add( version4 ); when( metadataVersionDelegate.getMetaDataDifference( currentVersion ) ).thenReturn( metadataVersionList ); List<MetadataVersion> expectedListOfVersions = metadataSyncPreProcessor.handleMetadataVersionsList( mockRetryContext, currentVersion ); verify( systemSettingManager ).saveSystemSetting( SettingKey.REMOTE_METADATA_VERSION, version4.getName() ); assertEquals( 3, expectedListOfVersions.size() ); }