DashboardService { @PreAuthorize("hasAuthority('ADD_DASHBOARD')") public long addDashboard(DBDashboard dashboard) { checkNotNull(dashboard); checkArgument( dashboard.getName() != null && !"".equals(dashboard.getName()), "dashboard name could not be empty."); checkArgument(!StringUtils.isEmpty(dashboard.getOwner()), "dashboard owner must be specified."); if (dashboard.getDisplayName() == null) dashboard.setDisplayName(dashboard.getName()); if (dashboard.getCreateTime() == null) dashboard.setCreateTime(new Date()); dashboard.setName(dashboard.getName()); DBDashboard dashboardCondition = new DBDashboard(); dashboardCondition.setName(dashboard.getName()); List<DBDashboard> list = dashboardService.get(dashboardCondition); checkState(list == null || list.size() == 0, "[%s] dashboard name already exists.", dashboard.getName()); long id = dashboardService.inser(dashboard); checkState(id > 0, "insert dashboard to db failed.dashboardName=%s,owner=", dashboard.getName(), dashboard.getOwner()); DBDashboard update=new DBDashboard(); update.setId(id); update.setName(String.format(PermissionConst.RESOURCE_NAME_TEMPLATGE,dashboard.getName(), id)); int row=dashboardService.updateById(update); if(row>0){ dashboard.setId(id); dashboard.setName(String.format(PermissionConst.RESOURCE_NAME_TEMPLATGE,dashboard.getName(), id)); } PermissionControlCache.getInstance().expireSessions(dashboard.getOwner()); return id; } DashboardService(); String getManagePermission(String dashboardName); String getViewPermission(String dashboardName); @PreAuthorize("hasAuthority('ADD_DASHBOARD')") long addDashboard(DBDashboard dashboard); @PreAuthorize("hasAnyAuthority(#dashboardName+'_MANAGE','SYS_MANAGE_DASHBOARD')") int deleteDashboard(String dashboardName); @PreFilter("hasAuthority(filterObject+'_MANAGE') or hasAuthority('SYS_MANAGE_DASHBOARD')") int deleteDashboards(List<String> dashboardNames); @PreAuthorize("hasAuthority(#dashboardName+'_MANAGE') or hasAuthority('SYS_MANAGE_DASHBOARD')") int updateDashboard(String dashboardName, String displayname, String newConfig); @PreAuthorize("hasAnyAuthority(#dashboardName+'_MANAGE',#dashboardName+'_VIEW','SYS_MANAGE_DASHBOARD','SYS_VIEW_DASHBOARD')") DBDashboard getDashboardByName(String dashboardName); List<String> getAllDashboardsForOwner(String userName); @PreFilter("hasAuthority(filterObject+'_MANAGE') or hasAuthority(filterObject+'_VIEW') or hasAuthority('SYS_MANAGE_DASHBOARD') or hasAuthority('SYS_VIEW_DASHBOARD')") List<DBDashboard> getDashboardByNames(List<String> names); @PostFilter("hasAuthority(filterObject.name+'_MANAGE') or hasAuthority(filterObject.name+'_VIEW') or hasAuthority('SYS_MANAGE_DASHBOARD') or hasAuthority('SYS_VIEW_DASHBOARD')") List<DBDashboard> getUserViewedDashboard(); @PostFilter("hasAuthority(filterObject.name+'_MANAGE') or hasAuthority('SYS_MANAGE_DASHBOARD')") List<DBDashboard> getAllUserManagedDashboard();  }
@SuppressWarnings("unchecked") @Test public void addDashboardTest() throws Exception { final DBDashboard dashboardCondition = new DBDashboard(); dashboardCondition.setOwner(uttestuser); dashboardCondition.setName(uttestdashbaord1); DBDashboard dashboardCondition2 = new DBDashboard(); dashboardCondition2.setOwner(uttestuser); dashboardCondition2.setName("UTDashboard2"); RDBMS db = Mockito.mock(RDBMS.class); when(db.query(Matchers.anyString(),Matchers.eq(ImmutableMap.of("name", uttestdashbaord1)), Matchers.eq(-1),Matchers.any(DBDashboardMapper.class))) .thenReturn(Lists.<DBDashboard>newArrayList()); when(db.insert(Mockito.anyString(), Matchers.anyMap(), Matchers.any(GeneratedKeyHolder.class))) .thenReturn(1); final GeneratedKeyHolder keyHolder = PowerMockito.mock(GeneratedKeyHolder.class); PowerMockito.whenNew(GeneratedKeyHolder.class).withNoArguments() .thenReturn(keyHolder); when(keyHolder.getKey()).thenReturn(1L); DashboardService dashboardService = new DashboardService(); BaseDBService<?> ds=(BaseDBService<?>)ReflectFieldUtil.getField(dashboardService, "dashboardService"); BaseDBService<?> rs=(BaseDBService<?>)ReflectFieldUtil.getField(dashboardService, "rightGroupService"); ReflectFieldUtil.setField(BaseDBService.class,ds, "db", db); ReflectFieldUtil.setField(BaseDBService.class,rs, "db", db); when(db.update(Mockito.anyString(), Matchers.anyMap())).thenReturn(1); long id = dashboardService.addDashboard(dashboardCondition); Assert.assertTrue(id > 0); Assert.assertEquals(String.format(PermissionConst.RESOURCE_NAME_TEMPLATGE, uttestdashbaord1,1L),dashboardCondition.getName()); when(db.query(Matchers.anyString(),Matchers.eq(ImmutableMap.of("name", uttestdashbaord1)), Matchers.eq(-1),Matchers.any(DBDashboardMapper.class))) .thenReturn(Lists.<DBDashboard>newArrayList(dashboardCondition)); when(db.update(Matchers.anyString(),Matchers.anyMap())) .thenReturn(1); Assert.assertTrue(uttestuser.equalsIgnoreCase(dashboardService .getDashboardByName(uttestdashbaord1).getOwner())); long id2 = dashboardService.addDashboard(dashboardCondition2); Assert.assertTrue(id2 > 0); when(db.query(Matchers.anyString(),Matchers.eq(ImmutableMap.of("owner", uttestuser)), Matchers.eq(-1),Matchers.any(DBDashboardMapper.class))) .thenReturn(Lists.<DBDashboard>newArrayList(dashboardCondition,dashboardCondition2)); List<String> list = dashboardService .getAllDashboardsForOwner(uttestuser); Assert.assertTrue(list.size() > 0); Assert.assertTrue(list.size()==2); }