UserSettingService implements IAnyUserSettingService, IUserSettingService { public void deleteUserSettings() { String homePath = ClientRepositoryPaths.getUserHomeFolderPath( PentahoSessionHolder.getSession().getName() ); Serializable id = repository.getFile( homePath ).getId(); Map<String, Serializable> fileMetadata = repository.getFileMetadata( id ); Map<String, Serializable> finalMetadata = new HashMap<String, Serializable>( fileMetadata.size() ); for ( Map.Entry<String, Serializable> entry : fileMetadata.entrySet() ) { String key = entry.getKey(); if ( !key.startsWith( SETTING_PREFIX ) ) { finalMetadata.put( key, entry.getValue() ); } } repository.setFileMetadata( id, finalMetadata ); } UserSettingService( IUnifiedRepository repository ); void init( IPentahoSession session ); void deleteUserSettings(); List<IUserSetting> getUserSettings(); IUserSetting getUserSetting( String settingName, String defaultValue ); void setUserSetting( String settingName, String settingValue ); @Override void deleteUserSettings( String username ); @Override List<IUserSetting> getUserSettings( String username ); @Override IUserSetting getUserSetting( String username, String settingName, String defaultValue ); @Override void setUserSetting( String username, String settingName, String settingValue ); IUserSetting getGlobalUserSetting( String settingName, String defaultValue ); List<IUserSetting> getGlobalUserSettings(); void setGlobalUserSetting( String settingName, String settingValue ); static final String SETTING_PREFIX; }
@Test public void testDeleteUserSettings() throws Exception { final RepositoryFile repositoryFile = mock( RepositoryFile.class ); when( repositoryFile.getId() ).thenReturn( USER_FOLDER_ID ); when( repository.getFile( anyString() ) ).thenReturn( repositoryFile ); IAuthorizationPolicy policy = mock( IAuthorizationPolicy.class ); when( policy.isAllowed( anyString() ) ).thenReturn( true ); PentahoSystem.registerObject( policy ); doAnswer( new Answer() { @Override public Object answer( InvocationOnMock invocation ) throws Throwable { final Map<String, Serializable> settings = (Map<String, Serializable>) invocation.getArguments()[1]; assertNotNull( settings ); assertEquals( 2, settings.size() ); final Iterator<String> iterator = settings.keySet().iterator(); assertFalse( iterator.next().startsWith( UserSettingService.SETTING_PREFIX ) ); assertFalse( iterator.next().startsWith( UserSettingService.SETTING_PREFIX ) ); return null; } } ).when( repository ).setFileMetadata( eq( USER_FOLDER_ID ), anyMap() ); userSettingService.deleteUserSettings(); }
@Test public void testDeleteUserSettingsByName() throws Exception { IAuthorizationPolicy policy = mock( IAuthorizationPolicy.class ); when( policy.isAllowed( anyString() ) ).thenReturn( true ); PentahoSystem.registerObject( policy ); final RepositoryFile repositoryFile = mock( RepositoryFile.class ); when( repositoryFile.getId() ).thenReturn( USER_FOLDER_ID ); when( repository.getFile( anyString() ) ).thenReturn( repositoryFile ); doAnswer( new Answer() { @Override public Object answer( InvocationOnMock invocation ) throws Throwable { final Map<String, Serializable> settings = (Map<String, Serializable>) invocation.getArguments()[1]; assertNotNull( settings ); assertEquals( 2, settings.size() ); final Iterator<String> iterator = settings.keySet().iterator(); assertFalse( iterator.next().startsWith( UserSettingService.SETTING_PREFIX ) ); assertFalse( iterator.next().startsWith( UserSettingService.SETTING_PREFIX ) ); return null; } } ).when( repository ).setFileMetadata( eq( USER_FOLDER_ID ), anyMap() ); userSettingService.deleteUserSettings( "test" ); }