### Question:
TopicHibernateDao extends GenericDao<Topic> implements TopicDao { @Override public List<Long> getAllowedBranchesIds(JCUser user) { List<Long> result = new ArrayList<>(); Query queryForbidden = session().getNamedQuery("getForbiddenBranchesIds"); Query queryAllowed = session().getNamedQuery("getAllowedBranchesIds"); if (user.isAnonymous()) { queryForbidden.setString("sid", user.getClass().getSimpleName()); queryAllowed.setString("sid", user.getClass().getSimpleName()); } else { queryForbidden.setParameterList("sid", getGroupIds(user)); queryAllowed.setParameterList("sid", getGroupIds(user)); } List<Long> forbidden = queryForbidden.list(); List<Long> allowed = queryAllowed.list(); for (Long aLong : allowed) { if (!forbidden.contains(aLong)) { result.add(aLong); } } return result; } TopicHibernateDao(SessionFactory sessionFactory); @Override Page<Topic> getTopicsUpdatedSince(DateTime timeStamp, PageRequest pageRequest, JCUser user); @Override Page<Topic> getUnansweredTopics(PageRequest pageRequest, JCUser user); @Override Topic getLastUpdatedTopicInBranch(Branch branch); @Override Page<Topic> getTopics(Branch branch, PageRequest pageRequest); @Override int countTopics(Branch branch); @Override Collection<JCUser> getAllowedSubscribers(SubscriptionAwareEntity entity); @Override List<Long> getForbiddenBranchesIds(JCUser user); @Override List<Long> getAllowedBranchesIds(JCUser user);  }### Answer:
@Test public void testGetAllowedBranchesIdsWithAnonymousUser() { AnonymousUser user = new AnonymousUser(); PersistedObjectsFactory.createAndSaveViewTopicsBranchesEntity( ObjectsFactory.getDefaultBranch().getId(), user.getClass().getSimpleName(), true ); List<Long> collection = this.dao.getAllowedBranchesIds(user); assertTrue(collection.size() > 0); }
@Test public void testGetAllowedBranchesIdsWithAnonymousUserAllowedAndRestricted() { AnonymousUser user = new AnonymousUser(); PersistedObjectsFactory.createAndSaveViewTopicsBranchesEntity( ObjectsFactory.getDefaultBranch().getId(), user.getClass().getSimpleName(), true ); PersistedObjectsFactory.createAndSaveViewTopicsBranchesEntity( ObjectsFactory.getDefaultBranch().getId(), user.getClass().getSimpleName(), false ); List<Long> collection = this.dao.getAllowedBranchesIds(user); assertTrue(collection.isEmpty()); }
@Test public void testGetAllowedBranchesIdsWithRealUser() { JCUser user = new JCUser("username", null, null); user.setGroups(ObjectsFactory.getDefaultGroupList()); PersistedObjectsFactory.createAndSaveViewTopicsBranchesEntity( ObjectsFactory.getDefaultBranch().getId(), String.valueOf(user.getGroups().get(0).getId()), true ); List<Long> collection = this.dao.getAllowedBranchesIds(user); assertTrue(collection.size() > 0); }
@Test public void testGetAllowedBranchesIdsWithRealUserAllowedAndRestricted() { JCUser user = new JCUser("username", null, null); user.setGroups(ObjectsFactory.getDefaultGroupList()); PersistedObjectsFactory.createAndSaveViewTopicsBranchesEntity( ObjectsFactory.getDefaultBranch().getId(), user.getClass().getSimpleName(), true ); PersistedObjectsFactory.createAndSaveViewTopicsBranchesEntity( ObjectsFactory.getDefaultBranch().getId(), user.getClass().getSimpleName(), false ); List<Long> collection = this.dao.getAllowedBranchesIds(user); assertTrue(collection.isEmpty()); }