@Before public void setup() { service1 = mock(IUserRoleListService.class); service2 = mock(IUserRoleListService.class); mockTenant = mock(ITenant.class); String joe = "joe"; String ceo = "ceo"; when(service1.getAllRoles()).thenReturn(Arrays.asList("ceo", "admin")); when(service1.getAllRoles(mockTenant)).thenReturn(Arrays.asList("ceo", "admin")); when(service1.getAllUsers()).thenReturn(Arrays.asList("joe", "suzy")); when(service1.getAllUsers(mockTenant)).thenReturn(Arrays.asList("joe", "suzy")); when(service1.getRolesForUser(mockTenant, joe)).thenReturn(Arrays.asList("admin")); when(service1.getSystemRoles()).thenReturn(Arrays.asList("authenticated", "anonymous")); when(service1.getUsersInRole(mockTenant, ceo)).thenReturn(Arrays.asList("suzy")); when(service2.getAllRoles()).thenReturn(Arrays.asList("extraRole1", "admin")); when(service2.getAllRoles(mockTenant)).thenReturn(Arrays.asList("extraRole1", "admin")); when(service2.getAllUsers()).thenReturn(Arrays.asList("user1", "suzy")); when(service2.getAllUsers(mockTenant)).thenReturn(Arrays.asList("user1", "suzy")); when(service2.getRolesForUser(mockTenant, joe)).thenReturn(Arrays.asList("admin", "rockstar")); when(service2.getSystemRoles()).thenReturn(Arrays.asList("serveradmin", "anonymous")); when(service2.getUsersInRole(mockTenant, ceo)).thenReturn(Arrays.asList("ted", "suzy")); compositeService = new CompositeUserRoleListService(Arrays.asList(service1, service2)); }
@Override public void createContent() throws Exception { this.session = PentahoSessionHolder.getSession(); this.repository = PentahoSystem.get(IUnifiedRepository.class, session); final RepositoryFile BIRTfile = (RepositoryFile) parameterProviders.get("path").getParameter("file"); final String ExecBIRTFilePath = "../webapps/birt/" + BIRTfile.getId() + ".rptdocument"; /* * Get BIRT report design from repository */ final File ExecBIRTFile = new File(ExecBIRTFilePath); if (!ExecBIRTFile.exists()) { final FileOutputStream fos = new FileOutputStream(ExecBIRTFilePath); try { final SimpleRepositoryFileData data = repository.getDataForRead(BIRTfile.getId(), SimpleRepositoryFileData.class); final InputStream inputStream = data.getInputStream(); final byte[] buffer = new byte[0x1000]; int bytesRead = inputStream.read(buffer); while (bytesRead >= 0) { fos.write(buffer, 0, bytesRead); bytesRead = inputStream.read(buffer); } } catch (final Exception e) { Logger.error(getClass().getName(), e.getMessage()); } finally { fos.close(); } } /* * Redirect to BIRT Viewer */ try { // Get informations about user context final IUserRoleListService service = PentahoSystem.get(IUserRoleListService.class); String roles = ""; final ListIterator<String> li = service.getRolesForUser(null, session.getName()).listIterator(); while (li.hasNext()) { roles = roles + li.next().toString() + ","; } // Redirect final HttpServletResponse response = (HttpServletResponse) this.parameterProviders.get("path").getParameter("httpresponse"); response.sendRedirect( "/birt/frameset?__document=" + BIRTfile.getId() + ".rptdocument&__showtitle=false&username="******"&userroles=" + roles + "&reportname=" + BIRTfile.getTitle()); } catch (final Exception e) { Logger.error(getClass().getName(), e.getMessage()); } }
@Test public void testGetRolesForUser() throws Exception { IUserRoleListService mockService = mock(IUserRoleListService.class); when(mockService.getRolesForUser(tenant, "joe")).thenReturn(Arrays.asList("foo", "bar")); when(mockService.getRolesForUser(tenant, "admin")).thenReturn(Arrays.asList("foo", "bar")); CachingUserRoleListServiceDecorator decorator = new CachingUserRoleListServiceDecorator(mockService); List<String> allRoles = decorator.getRolesForUser(tenant, "joe"); assertArrayEquals("does not match", new String[] {"foo", "bar"}, allRoles.toArray()); // second call should be from cache allRoles = decorator.getRolesForUser(tenant, "joe"); assertArrayEquals("does not match", new String[] {"foo", "bar"}, allRoles.toArray()); allRoles = decorator.getRolesForUser(tenant, "admin"); assertArrayEquals("does not match", new String[] {"foo", "bar"}, allRoles.toArray()); verify(mockService, times(1)).getRolesForUser(tenant, "joe"); verify(mockService, times(1)).getRolesForUser(tenant, "admin"); }
@Override public List<String> getRolesForUser(ITenant tenant, String username) { return userRoleListService.getRolesForUser(tenant, username); }