MetadataSyncImportHandler { public MetadataSyncSummary importMetadata( MetadataSyncParams syncParams, String versionSnapShot ) { MetadataVersion version = getMetadataVersion( syncParams ); MetadataImportParams importParams = syncParams.getImportParams(); MetadataSyncSummary metadataSyncSummary = new MetadataSyncSummary(); if ( importParams == null ) { throw new MetadataSyncServiceException( "MetadataImportParams for the Sync cant be null." ); } Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> classListMap = parseClassListMap( versionSnapShot ); if ( classListMap == null ) { throw new MetadataSyncServiceException( "ClassListMap can't be null" ); } classListMap.remove( JobConfiguration.class ); importParams.setObjects( classListMap ); ImportReport importReport = null; try { importReport = metadataImportService.importMetadata( importParams ); } catch ( Exception e ) { String message = "Exception occurred while trying to import the metadata. " + e.getMessage(); log.error( message,e ); throw new MetadataSyncImportException( message,e ); } boolean addNewVersion = handleImportReport( importReport, version ); if ( addNewVersion ) { try { metadataVersionDelegate.addNewMetadataVersion( version ); } catch ( MetadataVersionServiceException e ) { throw new MetadataSyncServiceException( e.getMessage(), e ); } } metadataSyncSummary.setImportReport( importReport ); metadataSyncSummary.setMetadataVersion( version ); return metadataSyncSummary; }  MetadataSyncSummary importMetadata( MetadataSyncParams syncParams, String versionSnapShot );  }
@Test public void testShouldThrowExceptionWhenNoVersionSet() { syncParams.setImportParams( null ); expectedException.expect( MetadataSyncServiceException.class ); expectedException.expectMessage( "MetadataVersion for the Sync cant be null." ); metadataSyncImportHandler.importMetadata( syncParams, expectedMetadataSnapshot ); }
@Test public void testShouldThrowExceptionWhenNoImportParams() { syncParams.setVersion( metadataVersion ); syncParams.setImportParams( null ); expectedException.expect( MetadataSyncServiceException.class ); expectedException.expectMessage( "MetadataImportParams for the Sync cant be null." ); metadataSyncImportHandler.importMetadata( syncParams, expectedMetadataSnapshot ); }
@Test public void testShouldThrowExceptionWhenImportServiceFails() { syncParams.setImportParams( new MetadataImportParams() ); syncParams.setVersion( metadataVersion ); when( metadataImportService.importMetadata( syncParams.getImportParams() ) ) .thenThrow( new MetadataSyncServiceException( "" ) ); expectedException.expect( MetadataSyncImportException.class ); metadataSyncImportHandler.importMetadata( syncParams, expectedMetadataSnapshot ); verify( metadataVersionDelegate, never() ).addNewMetadataVersion( metadataVersion ); }
@Test public void testShouldThrowExceptionWhenClassListMapIsNull() throws IOException { syncParams.setImportParams( new MetadataImportParams() ); syncParams.setVersion( metadataVersion ); importReport.setStatus( Status.OK ); when( renderService.fromMetadata( any( InputStream.class ), eq( RenderFormat.JSON ) ) ).thenReturn( null ); expectedException.expect( MetadataSyncServiceException.class ); expectedException.expectMessage( "ClassListMap can't be null" ); metadataSyncImportHandler.importMetadata( syncParams, expectedMetadataSnapshot ); verify( renderService ).fromMetadata( any( InputStream.class ), RenderFormat.JSON ); verify( metadataImportService, never() ).importMetadata( syncParams.getImportParams() ); verify( metadataVersionDelegate, never() ).addNewMetadataVersion( metadataVersion ); verify( metadataVersionDelegate, never() ).addNewMetadataVersion( metadataVersion ); }
@Test public void testShouldThrowExceptionWhenParsingClassListMap() throws IOException { syncParams.setImportParams( new MetadataImportParams() ); syncParams.setVersion( metadataVersion ); importReport.setStatus( Status.OK ); when( renderService.fromMetadata( any( InputStream.class ), eq( RenderFormat.JSON ) ) ) .thenThrow( new IOException() ); expectedException.expect( MetadataSyncServiceException.class ); expectedException .expectMessage( "Exception occurred while trying to do JSON conversion while parsing class list map" ); metadataSyncImportHandler.importMetadata( syncParams, expectedMetadataSnapshot ); verify( renderService ).fromMetadata( any( InputStream.class ), RenderFormat.JSON ); verify( metadataImportService, never() ).importMetadata( syncParams.getImportParams() ); verify( metadataVersionDelegate, never() ).addNewMetadataVersion( metadataVersion ); verify( metadataVersionDelegate, never() ).addNewMetadataVersion( metadataVersion ); }