DefaultMetadataSyncService implements MetadataSyncService { @Override public synchronized MetadataSyncSummary doMetadataSync( MetadataSyncParams syncParams ) throws MetadataSyncServiceException, DhisVersionMismatchException { MetadataVersion version = getMetadataVersion( syncParams ); setMetadataImportMode( syncParams, version ); String metadataVersionSnapshot = getMetadataVersionSnapshot( version ); if ( metadataSyncDelegate.shouldStopSync( metadataVersionSnapshot ) ) { throw new DhisVersionMismatchException( "Metadata sync failed because your version of DHIS does not match the master version" ); } saveMetadataVersionSnapshotLocally( version, metadataVersionSnapshot ); MetadataSyncSummary metadataSyncSummary = metadataSyncImportHandler.importMetadata( syncParams, metadataVersionSnapshot ); log.info( "Metadata Sync Summary: " + metadataSyncSummary ); return metadataSyncSummary; } DefaultMetadataSyncService( MetadataVersionDelegate metadataVersionDelegate,
        MetadataVersionService metadataVersionService, MetadataSyncDelegate metadataSyncDelegate,
        MetadataSyncImportHandler metadataSyncImportHandler ); @Override MetadataSyncParams getParamsFromMap( Map<String, List<String>> parameters ); @Override synchronized MetadataSyncSummary doMetadataSync( MetadataSyncParams syncParams ); @Override boolean isSyncRequired( MetadataSyncParams syncParams );  }
@Test public void testShouldThrowExceptionWhenSyncParamsIsNull() throws DhisVersionMismatchException { expectedException.expect( MetadataSyncServiceException.class ); expectedException.expectMessage( "MetadataSyncParams cant be null" ); metadataSyncService.doMetadataSync( null ); }
@Test public void testShouldThrowExceptionWhenVersionIsNulInSyncParams() throws DhisVersionMismatchException { MetadataSyncParams syncParams = new MetadataSyncParams(); syncParams.setVersion( null ); expectedException.expect( MetadataSyncServiceException.class ); expectedException .expectMessage( "MetadataVersion for the Sync cant be null. The ClassListMap could not be constructed." ); metadataSyncService.doMetadataSync( syncParams ); }
@Test public void testShouldThrowExceptionWhenSnapshotReturnsNullForGivenVersion() throws DhisVersionMismatchException { MetadataSyncParams syncParams = Mockito.mock( MetadataSyncParams.class ); MetadataVersion metadataVersion = new MetadataVersion( "testVersion", VersionType.ATOMIC ); when( syncParams.getVersion() ).thenReturn( metadataVersion ); when( metadataVersionDelegate.downloadMetadataVersionSnapshot( metadataVersion ) ).thenReturn( null ); expectedException.expect( MetadataSyncServiceException.class ); expectedException.expectMessage( "Metadata snapshot can't be null." ); metadataSyncService.doMetadataSync( syncParams ); }
@Test public void testShouldThrowExceptionWhenDHISVersionsMismatch() throws DhisVersionMismatchException { MetadataSyncParams syncParams = Mockito.mock( MetadataSyncParams.class ); MetadataVersion metadataVersion = new MetadataVersion( "testVersion", VersionType.ATOMIC ); String expectedMetadataSnapshot = "{\"date\":\"2016-05-24T05:27:25.128+0000\"}"; when( syncParams.getVersion() ).thenReturn( metadataVersion ); when( metadataVersionDelegate.downloadMetadataVersionSnapshot( metadataVersion ) ) .thenReturn( expectedMetadataSnapshot ); when( metadataSyncDelegate.shouldStopSync( expectedMetadataSnapshot ) ).thenReturn( true ); when( metadataVersionService.isMetadataPassingIntegrity( metadataVersion, expectedMetadataSnapshot ) ) .thenReturn( true ); expectedException.expect( DhisVersionMismatchException.class ); expectedException .expectMessage( "Metadata sync failed because your version of DHIS does not match the master version" ); metadataSyncService.doMetadataSync( syncParams ); }
@Test public void testShouldNotThrowExceptionWhenDHISVersionsMismatch() throws DhisVersionMismatchException { MetadataSyncParams syncParams = Mockito.mock( MetadataSyncParams.class ); MetadataVersion metadataVersion = new MetadataVersion( "testVersion", VersionType.ATOMIC ); String expectedMetadataSnapshot = "{\"date\":\"2016-05-24T05:27:25.128+0000\", \"version\": \"2.26\"}"; when( syncParams.getVersion() ).thenReturn( metadataVersion ); when( metadataVersionDelegate.downloadMetadataVersionSnapshot( metadataVersion ) ) .thenReturn( expectedMetadataSnapshot ); when( metadataSyncDelegate.shouldStopSync( expectedMetadataSnapshot ) ).thenReturn( false ); when( metadataVersionService.isMetadataPassingIntegrity( metadataVersion, expectedMetadataSnapshot ) ) .thenReturn( true ); metadataSyncService.doMetadataSync( syncParams ); }
@Test public void testShouldThrowExceptionWhenSnapshotNotPassingIntegrity() throws DhisVersionMismatchException { MetadataSyncParams syncParams = Mockito.mock( MetadataSyncParams.class ); MetadataVersion metadataVersion = new MetadataVersion( "testVersion", VersionType.ATOMIC ); String expectedMetadataSnapshot = "{\"date\":\"2016-05-24T05:27:25.128+0000\"}"; when( syncParams.getVersion() ).thenReturn( metadataVersion ); when( metadataVersionDelegate.downloadMetadataVersionSnapshot( metadataVersion ) ) .thenReturn( expectedMetadataSnapshot ); when( metadataVersionService.isMetadataPassingIntegrity( metadataVersion, expectedMetadataSnapshot ) ) .thenReturn( false ); expectedException.expect( MetadataSyncServiceException.class ); expectedException.expectMessage( "Metadata snapshot is corrupted." ); metadataSyncService.doMetadataSync( syncParams ); }
@Test public void testShouldVerifyImportParamsAtomicTypeForTheGivenBestEffortVersion() throws DhisVersionMismatchException { MetadataSyncParams syncParams = new MetadataSyncParams(); MetadataVersion metadataVersion = new MetadataVersion( "testVersion", VersionType.BEST_EFFORT ); MetadataImportParams metadataImportParams = new MetadataImportParams(); syncParams.setVersion( metadataVersion ); syncParams.setImportParams( metadataImportParams ); MetadataSyncSummary metadataSyncSummary = new MetadataSyncSummary(); metadataSyncSummary.setMetadataVersion( metadataVersion ); String expectedMetadataSnapshot = "{\"date\":\"2016-05-24T05:27:25.128+0000\"}"; when( metadataVersionService.getVersionData( "testVersion" ) ).thenReturn( expectedMetadataSnapshot ); metadataSyncService.doMetadataSync( syncParams ); verify( metadataSyncImportHandler, times( 1 ) ).importMetadata( (argThat( metadataSyncParams -> syncParams.getImportParams().getAtomicMode().equals( AtomicMode.NONE ) )), eq( expectedMetadataSnapshot ) ); verify( metadataVersionService, never() ).createMetadataVersionInDataStore( metadataVersion.getName(), expectedMetadataSnapshot ); verify( metadataVersionDelegate, never() ).downloadMetadataVersionSnapshot( metadataVersion ); }