SolutionImportHandler implements IPlatformImportHandler { protected Map<String, List<String>> importUsers( List<UserExport> users ) { Map<String, List<String>> roleToUserMap = new HashMap<>(); IUserRoleDao roleDao = PentahoSystem.get( IUserRoleDao.class ); ITenant tenant = new Tenant( "/pentaho/" + TenantUtils.getDefaultTenant(), true ); if ( users != null && roleDao != null ) { for ( UserExport user : users ) { String password = user.getPassword(); getLogger().debug( Messages.getInstance().getString( "USER.importing", user.getUsername() ) ); for ( String role : user.getRoles() ) { List<String> userList; if ( !roleToUserMap.containsKey( role ) ) { userList = new ArrayList<>(); roleToUserMap.put( role, userList ); } else { userList = roleToUserMap.get( role ); } userList.add( user.getUsername() ); } String[] userRoles = user.getRoles().toArray( new String[] {} ); try { roleDao.createUser( tenant, user.getUsername(), password, null, userRoles ); } catch ( AlreadyExistsException e ) { getLogger().info( Messages.getInstance().getString( "USER.Already.Exists", user.getUsername() ) ); try { if ( isOverwriteFile() ) { roleDao.setUserRoles( tenant, user.getUsername(), userRoles ); roleDao.setPassword( tenant, user.getUsername(), password ); } } catch ( Exception ex ) { getLogger().debug( Messages.getInstance() .getString( "ERROR.OverridingExistingUser", user.getUsername() ), ex ); } } catch ( Exception e ) { getLogger().error( Messages.getInstance() .getString( "ERROR.OverridingExistingUser", user.getUsername() ), e ); } importUserSettings( user ); } } return roleToUserMap; } SolutionImportHandler( List<IMimeType> mimeTypes ); ImportSession getImportSession(); Log getLogger(); @Override void importFile( IPlatformImportBundle bundle ); @Override List<IMimeType> getMimeTypes(); IPlatformImportBundle build( RepositoryFileImportBundle.Builder builder ); Response createSchedulerJob( SchedulerResource scheduler, JobScheduleRequest jobScheduleRequest ); boolean isOverwriteFile(); void setOverwriteFile( boolean overwriteFile );  }
@Test public void testImportUsers_oneUserManyRoles() throws Exception { List<UserExport> users = new ArrayList<>(); UserExport user = new UserExport(); user.setUsername( "scrum master" ); user.setRole( "coder" ); user.setRole( "product owner" ); user.setRole( "cat herder" ); user.setPassword( "password" ); users.add( user ); Map<String, List<String>> rolesToUsers = importHandler.importUsers( users ); Assert.assertEquals( 3, rolesToUsers.size() ); Assert.assertEquals( "scrum master", rolesToUsers.get( "coder" ).get( 0 ) ); Assert.assertEquals( "scrum master", rolesToUsers.get( "product owner" ).get( 0 ) ); Assert.assertEquals( "scrum master", rolesToUsers.get( "cat herder" ).get( 0 ) ); String[] strings = {}; Mockito.verify( userRoleDao ).createUser( Mockito.any( ITenant.class ), Mockito.eq( "scrum master" ), Mockito.anyString(), Mockito.anyString(), Mockito.any( strings.getClass() ) ); Mockito.verify( userRoleDao, Mockito.never() ) .setUserRoles( Mockito.any( ITenant.class ), Mockito.anyString(), Mockito.any( strings.getClass() ) ); Mockito.verify( userRoleDao, Mockito.never() ) .setPassword( Mockito.any( ITenant.class ), Mockito.anyString(), Mockito.anyString() ); }
@Test public void testImportUsers_manyUserManyRoles() throws Exception { List<UserExport> users = new ArrayList<>(); UserExport user = new UserExport(); user.setUsername( "scrum master" ); user.setRole( "coder" ); user.setRole( "product owner" ); user.setRole( "cat herder" ); user.setPassword( "password" ); users.add( user ); UserExport user2 = new UserExport(); user2.setUsername( "the dude" ); user2.setRole( "coder" ); user2.setRole( "awesome" ); user2.setPassword( "password" ); users.add( user2 ); Map<String, List<String>> rolesToUsers = importHandler.importUsers( users ); Assert.assertEquals( 4, rolesToUsers.size() ); Assert.assertEquals( 2, rolesToUsers.get( "coder" ).size() ); Assert.assertEquals( 1, rolesToUsers.get( "product owner" ).size() ); Assert.assertEquals( "scrum master", rolesToUsers.get( "product owner" ).get( 0 ) ); Assert.assertEquals( 1, rolesToUsers.get( "cat herder" ).size() ); Assert.assertEquals( "scrum master", rolesToUsers.get( "cat herder" ).get( 0 ) ); Assert.assertEquals( 1, rolesToUsers.get( "awesome" ).size() ); Assert.assertEquals( "the dude", rolesToUsers.get( "awesome" ).get( 0 ) ); String[] strings = {}; Mockito.verify( userRoleDao ).createUser( Mockito.any( ITenant.class ), Mockito.eq( "scrum master" ), Mockito.anyString(), Mockito.anyString(), Mockito.any( strings.getClass() ) ); Mockito.verify( userRoleDao ).createUser( Mockito.any( ITenant.class ), Mockito.eq( "the dude" ), Mockito.anyString(), Mockito.anyString(), Mockito.any( strings.getClass() ) ); Mockito.verify( userRoleDao, Mockito.never() ) .setUserRoles( Mockito.any( ITenant.class ), Mockito.anyString(), Mockito.any( strings.getClass() ) ); Mockito.verify( userRoleDao, Mockito.never() ) .setPassword( Mockito.any( ITenant.class ), Mockito.anyString(), Mockito.anyString() ); }