@SuppressWarnings("unchecked") @Test public void testGetResponseEntities() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { securityContextInjector.setDualContext(); SecurityUtil.setUserContext(SecurityUtil.UserContext.DUAL_CONTEXT); List<Entity> entities = new ArrayList<Entity>(); Map<String, SecurityUtil.UserContext> studentContext = new HashMap<String, SecurityUtil.UserContext>(); for (int i=0; i<30; i++) { String id = "student" + i; entities.add(new MongoEntity("student", id, new HashMap<String,Object>(), new HashMap<String,Object>())); studentContext.put(id, SecurityUtil.UserContext.DUAL_CONTEXT); } Mockito.when(mockRepo.findAll(Mockito.eq("student"), Mockito.any(NeutralQuery.class))).thenReturn(entities); Mockito.when(mockRepo.count(Mockito.eq("student"), Mockito.any(NeutralQuery.class))).thenReturn(50L); ContextValidator mockContextValidator = Mockito.mock(ContextValidator.class); Field contextValidator = BasicService.class.getDeclaredField("contextValidator"); contextValidator.setAccessible(true); contextValidator.set(service, mockContextValidator); Mockito.when(mockContextValidator.getValidatedEntityContexts(Matchers.any(EntityDefinition.class), Matchers.any(Collection.class), Matchers.anyBoolean(), Matchers.anyBoolean())).thenReturn(studentContext); RightAccessValidator mockAccessValidator = Mockito.mock(RightAccessValidator.class); Field rightAccessValidator = BasicService.class.getDeclaredField("rightAccessValidator"); rightAccessValidator.setAccessible(true); rightAccessValidator.set(service, mockAccessValidator); Mockito.when(mockAccessValidator.getContextualAuthorities(Matchers.anyBoolean(), Matchers.any(Entity.class), Matchers.eq(SecurityUtil.UserContext.DUAL_CONTEXT), Matchers.anyBoolean())).thenReturn(new HashSet<GrantedAuthority>()); Mockito.doThrow(new AccessDeniedException("")).when(mockAccessValidator).checkAccess(Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.argThat(new MatchesNotAccessible()), Mockito.anyString(), Mockito.anyCollection()); Mockito.doThrow(new AccessDeniedException("")).when(mockAccessValidator).checkFieldAccess(Mockito.any(NeutralQuery.class), Mockito.argThat(new MatchesNotFieldAccessible()), Mockito.anyString(), Mockito.anyCollection()); NeutralQuery query = new NeutralQuery(); query.setLimit(ApiQuery.API_QUERY_DEFAULT_LIMIT); Method method = BasicService.class.getDeclaredMethod("getResponseEntities", NeutralQuery.class, boolean.class); method.setAccessible(true); RequestUtil.setCurrentRequestId(); Collection<Entity> accessibleEntities = (Collection<Entity>) method.invoke(service, query, false); Iterable<String> expectedResult1 = Arrays.asList("student2", "student4", "student5", "student6", "student13", "student16", "student17", "student18", "student22", "student23"); assertEntityIdsEqual(expectedResult1.iterator(), accessibleEntities.iterator()); long count = service.getAccessibleEntitiesCount("student"); Assert.assertEquals(10, count); RequestUtil.setCurrentRequestId(); accessibleEntities = (Collection<Entity>) method.invoke(service, query, false); Iterable<String> expectedResult2 = Arrays.asList("student2", "student4", "student5", "student6", "student13", "student16", "student17", "student18", "student22", "student23"); assertEntityIdsEqual(expectedResult2.iterator(), accessibleEntities.iterator()); count = service.getAccessibleEntitiesCount("student"); Assert.assertEquals(10, count); query.setOffset(7); RequestUtil.setCurrentRequestId(); accessibleEntities = (Collection<Entity>) method.invoke(service, query, false); Iterable<String> expectedResult3 = Arrays.asList("student18", "student22", "student23"); assertEntityIdsEqual(expectedResult3.iterator(), accessibleEntities.iterator()); count = service.getAccessibleEntitiesCount("student"); Assert.assertEquals(10, count); query.setOffset(7); RequestUtil.setCurrentRequestId(); accessibleEntities = (Collection<Entity>) method.invoke(service, query, false); Iterable<String> expectedResult4 = Arrays.asList("student18", "student22", "student23"); assertEntityIdsEqual(expectedResult4.iterator(), accessibleEntities.iterator()); count = service.getAccessibleEntitiesCount("student"); Assert.assertEquals(10, count); }
protected Collection<Entity> getResponseEntities(NeutralQuery neutralQuery, boolean isSelf) throws AccessDeniedException { LOG.debug(">>>BasicService.getResponseEntities()"); Collection<Entity> responseEntities = new ArrayList<Entity>(); Map<String, UserContext> entityContexts = new HashMap<String, UserContext>(); final int limit = neutralQuery.getLimit(); final int offset = neutralQuery.getOffset(); final int responseLimit = ((0 < limit) && (limit < getCountLimit())) ? limit : (int) getCountLimit(); long accessibleCount = 0; int currentOffset = 0; int totalCount = 0; Collection<Entity> batchedEntities; do { batchedEntities = getBatchedEntities(neutralQuery, getBatchSize(), currentOffset); totalCount += batchedEntities.size(); Map<String, UserContext> batchedEntityContexts = new HashMap<String, UserContext>(); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { batchedEntityContexts = getEntityContextMap(batchedEntities, true); } Iterator<Entity> batchedEntitiesIt = batchedEntities.iterator(); while ((accessibleCount < getCountLimit()) && batchedEntitiesIt.hasNext()) { Entity entity = batchedEntitiesIt.next(); try { validateEntity(entity, isSelf, neutralQuery, batchedEntityContexts); accessibleCount++; if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { responseEntities.add(entity); entityContexts.put(entity.getEntityId(), getEntityContext(entity.getEntityId(), batchedEntityContexts)); } } catch (AccessDeniedException aex) { if (totalCount == 1) { throw aex; } else if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { LOG.error(aex.getMessage()); } } } currentOffset += getBatchSize(); } while ((accessibleCount < getCountLimit()) && (batchedEntities.size() == getBatchSize())); neutralQuery.setLimit(limit); neutralQuery.setOffset(offset); if ((totalCount > 0) && (accessibleCount == 0)) { validateQuery(neutralQuery, isSelf); throw new APIAccessDeniedException("Access to resource denied."); } setEntityContexts(entityContexts); setAccessibleEntitiesCount(collectionName, accessibleCount); return responseEntities; }
BasicService implements EntityService, AccessibilityCheck { protected Collection<Entity> getResponseEntities(NeutralQuery neutralQuery, boolean isSelf) throws AccessDeniedException { LOG.debug(">>>BasicService.getResponseEntities()"); Collection<Entity> responseEntities = new ArrayList<Entity>(); Map<String, UserContext> entityContexts = new HashMap<String, UserContext>(); final int limit = neutralQuery.getLimit(); final int offset = neutralQuery.getOffset(); final int responseLimit = ((0 < limit) && (limit < getCountLimit())) ? limit : (int) getCountLimit(); long accessibleCount = 0; int currentOffset = 0; int totalCount = 0; Collection<Entity> batchedEntities; do { batchedEntities = getBatchedEntities(neutralQuery, getBatchSize(), currentOffset); totalCount += batchedEntities.size(); Map<String, UserContext> batchedEntityContexts = new HashMap<String, UserContext>(); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { batchedEntityContexts = getEntityContextMap(batchedEntities, true); } Iterator<Entity> batchedEntitiesIt = batchedEntities.iterator(); while ((accessibleCount < getCountLimit()) && batchedEntitiesIt.hasNext()) { Entity entity = batchedEntitiesIt.next(); try { validateEntity(entity, isSelf, neutralQuery, batchedEntityContexts); accessibleCount++; if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { responseEntities.add(entity); entityContexts.put(entity.getEntityId(), getEntityContext(entity.getEntityId(), batchedEntityContexts)); } } catch (AccessDeniedException aex) { if (totalCount == 1) { throw aex; } else if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { LOG.error(aex.getMessage()); } } } currentOffset += getBatchSize(); } while ((accessibleCount < getCountLimit()) && (batchedEntities.size() == getBatchSize())); neutralQuery.setLimit(limit); neutralQuery.setOffset(offset); if ((totalCount > 0) && (accessibleCount == 0)) { validateQuery(neutralQuery, isSelf); throw new APIAccessDeniedException("Access to resource denied."); } setEntityContexts(entityContexts); setAccessibleEntitiesCount(collectionName, accessibleCount); return responseEntities; } }
BasicService implements EntityService, AccessibilityCheck { protected Collection<Entity> getResponseEntities(NeutralQuery neutralQuery, boolean isSelf) throws AccessDeniedException { LOG.debug(">>>BasicService.getResponseEntities()"); Collection<Entity> responseEntities = new ArrayList<Entity>(); Map<String, UserContext> entityContexts = new HashMap<String, UserContext>(); final int limit = neutralQuery.getLimit(); final int offset = neutralQuery.getOffset(); final int responseLimit = ((0 < limit) && (limit < getCountLimit())) ? limit : (int) getCountLimit(); long accessibleCount = 0; int currentOffset = 0; int totalCount = 0; Collection<Entity> batchedEntities; do { batchedEntities = getBatchedEntities(neutralQuery, getBatchSize(), currentOffset); totalCount += batchedEntities.size(); Map<String, UserContext> batchedEntityContexts = new HashMap<String, UserContext>(); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { batchedEntityContexts = getEntityContextMap(batchedEntities, true); } Iterator<Entity> batchedEntitiesIt = batchedEntities.iterator(); while ((accessibleCount < getCountLimit()) && batchedEntitiesIt.hasNext()) { Entity entity = batchedEntitiesIt.next(); try { validateEntity(entity, isSelf, neutralQuery, batchedEntityContexts); accessibleCount++; if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { responseEntities.add(entity); entityContexts.put(entity.getEntityId(), getEntityContext(entity.getEntityId(), batchedEntityContexts)); } } catch (AccessDeniedException aex) { if (totalCount == 1) { throw aex; } else if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { LOG.error(aex.getMessage()); } } } currentOffset += getBatchSize(); } while ((accessibleCount < getCountLimit()) && (batchedEntities.size() == getBatchSize())); neutralQuery.setLimit(limit); neutralQuery.setOffset(offset); if ((totalCount > 0) && (accessibleCount == 0)) { validateQuery(neutralQuery, isSelf); throw new APIAccessDeniedException("Access to resource denied."); } setEntityContexts(entityContexts); setAccessibleEntitiesCount(collectionName, accessibleCount); return responseEntities; } BasicService(String collectionName, List<Treatment> treatments, Repository<Entity> repo); }
BasicService implements EntityService, AccessibilityCheck { protected Collection<Entity> getResponseEntities(NeutralQuery neutralQuery, boolean isSelf) throws AccessDeniedException { LOG.debug(">>>BasicService.getResponseEntities()"); Collection<Entity> responseEntities = new ArrayList<Entity>(); Map<String, UserContext> entityContexts = new HashMap<String, UserContext>(); final int limit = neutralQuery.getLimit(); final int offset = neutralQuery.getOffset(); final int responseLimit = ((0 < limit) && (limit < getCountLimit())) ? limit : (int) getCountLimit(); long accessibleCount = 0; int currentOffset = 0; int totalCount = 0; Collection<Entity> batchedEntities; do { batchedEntities = getBatchedEntities(neutralQuery, getBatchSize(), currentOffset); totalCount += batchedEntities.size(); Map<String, UserContext> batchedEntityContexts = new HashMap<String, UserContext>(); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { batchedEntityContexts = getEntityContextMap(batchedEntities, true); } Iterator<Entity> batchedEntitiesIt = batchedEntities.iterator(); while ((accessibleCount < getCountLimit()) && batchedEntitiesIt.hasNext()) { Entity entity = batchedEntitiesIt.next(); try { validateEntity(entity, isSelf, neutralQuery, batchedEntityContexts); accessibleCount++; if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { responseEntities.add(entity); entityContexts.put(entity.getEntityId(), getEntityContext(entity.getEntityId(), batchedEntityContexts)); } } catch (AccessDeniedException aex) { if (totalCount == 1) { throw aex; } else if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { LOG.error(aex.getMessage()); } } } currentOffset += getBatchSize(); } while ((accessibleCount < getCountLimit()) && (batchedEntities.size() == getBatchSize())); neutralQuery.setLimit(limit); neutralQuery.setOffset(offset); if ((totalCount > 0) && (accessibleCount == 0)) { validateQuery(neutralQuery, isSelf); throw new APIAccessDeniedException("Access to resource denied."); } setEntityContexts(entityContexts); setAccessibleEntitiesCount(collectionName, accessibleCount); return responseEntities; } BasicService(String collectionName, List<Treatment> treatments, Repository<Entity> repo); @Override long count(NeutralQuery neutralQuery); @Override long countBasedOnContextualRoles(NeutralQuery neutralQuery); @Override List<String> create(List<EntityBody> content); @Override List<String> createBasedOnContextualRoles(List<EntityBody> content); @Override Iterable<String> listIds(final NeutralQuery neutralQuery); @Override String create(EntityBody content); @Override String createBasedOnContextualRoles(EntityBody content); @Override boolean accessibilityCheck(String id); @Override void delete(String id); @Override void deleteBasedOnContextualRoles(String id); @Override boolean update(String id, EntityBody content, boolean applySecurityContext); @Override boolean patch(String id, EntityBody content, boolean applySecurityContext); @Override EntityBody get(String id); @Override EntityBody get(String id, NeutralQuery neutralQuery); @Override Iterable<EntityBody> get(Iterable<String> ids); @Override Iterable<EntityBody> get(Iterable<String> ids, final NeutralQuery neutralQuery); @Override Iterable<EntityBody> list(NeutralQuery neutralQuery); @Override Iterable<EntityBody> listBasedOnContextualRoles(NeutralQuery neutralQuery); @Override boolean exists(String id); @Override EntityBody getCustom(String id); @Override void deleteCustom(String id); @Override void createOrUpdateCustom(String id, EntityBody customEntity); void setDefn(EntityDefinition defn); @Override EntityDefinition getEntityDefinition(); long getCountLimit(); int getBatchSize(); @Override CalculatedData<String> getCalculatedValues(String id); @Override CalculatedData<Map<String, Integer>> getAggregates(String id); @Override boolean collectionExists(String collection); }
BasicService implements EntityService, AccessibilityCheck { protected Collection<Entity> getResponseEntities(NeutralQuery neutralQuery, boolean isSelf) throws AccessDeniedException { LOG.debug(">>>BasicService.getResponseEntities()"); Collection<Entity> responseEntities = new ArrayList<Entity>(); Map<String, UserContext> entityContexts = new HashMap<String, UserContext>(); final int limit = neutralQuery.getLimit(); final int offset = neutralQuery.getOffset(); final int responseLimit = ((0 < limit) && (limit < getCountLimit())) ? limit : (int) getCountLimit(); long accessibleCount = 0; int currentOffset = 0; int totalCount = 0; Collection<Entity> batchedEntities; do { batchedEntities = getBatchedEntities(neutralQuery, getBatchSize(), currentOffset); totalCount += batchedEntities.size(); Map<String, UserContext> batchedEntityContexts = new HashMap<String, UserContext>(); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { batchedEntityContexts = getEntityContextMap(batchedEntities, true); } Iterator<Entity> batchedEntitiesIt = batchedEntities.iterator(); while ((accessibleCount < getCountLimit()) && batchedEntitiesIt.hasNext()) { Entity entity = batchedEntitiesIt.next(); try { validateEntity(entity, isSelf, neutralQuery, batchedEntityContexts); accessibleCount++; if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { responseEntities.add(entity); entityContexts.put(entity.getEntityId(), getEntityContext(entity.getEntityId(), batchedEntityContexts)); } } catch (AccessDeniedException aex) { if (totalCount == 1) { throw aex; } else if ((accessibleCount > offset) && (responseEntities.size() < responseLimit)) { LOG.error(aex.getMessage()); } } } currentOffset += getBatchSize(); } while ((accessibleCount < getCountLimit()) && (batchedEntities.size() == getBatchSize())); neutralQuery.setLimit(limit); neutralQuery.setOffset(offset); if ((totalCount > 0) && (accessibleCount == 0)) { validateQuery(neutralQuery, isSelf); throw new APIAccessDeniedException("Access to resource denied."); } setEntityContexts(entityContexts); setAccessibleEntitiesCount(collectionName, accessibleCount); return responseEntities; } BasicService(String collectionName, List<Treatment> treatments, Repository<Entity> repo); @Override long count(NeutralQuery neutralQuery); @Override long countBasedOnContextualRoles(NeutralQuery neutralQuery); @Override List<String> create(List<EntityBody> content); @Override List<String> createBasedOnContextualRoles(List<EntityBody> content); @Override Iterable<String> listIds(final NeutralQuery neutralQuery); @Override String create(EntityBody content); @Override String createBasedOnContextualRoles(EntityBody content); @Override boolean accessibilityCheck(String id); @Override void delete(String id); @Override void deleteBasedOnContextualRoles(String id); @Override boolean update(String id, EntityBody content, boolean applySecurityContext); @Override boolean patch(String id, EntityBody content, boolean applySecurityContext); @Override EntityBody get(String id); @Override EntityBody get(String id, NeutralQuery neutralQuery); @Override Iterable<EntityBody> get(Iterable<String> ids); @Override Iterable<EntityBody> get(Iterable<String> ids, final NeutralQuery neutralQuery); @Override Iterable<EntityBody> list(NeutralQuery neutralQuery); @Override Iterable<EntityBody> listBasedOnContextualRoles(NeutralQuery neutralQuery); @Override boolean exists(String id); @Override EntityBody getCustom(String id); @Override void deleteCustom(String id); @Override void createOrUpdateCustom(String id, EntityBody customEntity); void setDefn(EntityDefinition defn); @Override EntityDefinition getEntityDefinition(); long getCountLimit(); int getBatchSize(); @Override CalculatedData<String> getCalculatedValues(String id); @Override CalculatedData<Map<String, Integer>> getAggregates(String id); @Override boolean collectionExists(String collection);  }