@Test public void testCRUDEntityRepository() { // clean up the existing student data repository.deleteAll("student", null); // create new student entity Map<String, Object> student = buildTestStudentEntity(); // test save Entity saved = repository.create("student", student); String id = saved.getEntityId(); assertTrue(!id.equals("")); // test findAll NeutralQuery neutralQuery = new NeutralQuery(); neutralQuery.setOffset(0); neutralQuery.setLimit(20); Iterable<Entity> entities = repository.findAll("student", neutralQuery); assertNotNull(entities); Entity found = entities.iterator().next(); assertEquals(found.getBody().get("birthDate"), student.get("birthDate")); assertEquals((found.getBody()).get("firstName"), "Jane"); assertEquals((found.getBody()).get("lastName"), "Doe"); // test find by id Entity foundOne = repository.findById("student", saved.getEntityId()); assertNotNull(foundOne); assertEquals(foundOne.getBody().get("birthDate"), student.get("birthDate")); assertEquals((found.getBody()).get("firstName"), "Jane"); // test update found.getBody().put("firstName", "Mandy"); assertTrue(repository.update("student", found, false)); entities = repository.findAll("student", neutralQuery); assertNotNull(entities); Entity updated = entities.iterator().next(); assertEquals(updated.getBody().get("firstName"), "Mandy"); // test delete by id Map<String, Object> student2Body = buildTestStudentEntity(); Entity student2 = repository.create("student", student2Body); entities = repository.findAll("student", neutralQuery); assertNotNull(entities.iterator().next()); repository.delete("student", student2.getEntityId()); Entity zombieStudent = repository.findById("student", student2.getEntityId()); assertNull(zombieStudent); assertFalse(repository.delete("student", student2.getEntityId())); // test deleteAll by entity type repository.deleteAll("student", null); entities = repository.findAll("student", neutralQuery); assertFalse(entities.iterator().hasNext()); }
@SuppressWarnings("unchecked") @Test public void testSort() { // clean up the existing student data repository.deleteAll("student", null); // create new student entity Map<String, Object> body1 = buildTestStudentEntity(); Map<String, Object> body2 = buildTestStudentEntity(); Map<String, Object> body3 = buildTestStudentEntity(); Map<String, Object> body4 = buildTestStudentEntity(); body1.put("firstName", "Austin"); body2.put("firstName", "Jane"); body3.put("firstName", "Mary"); body4.put("firstName", "Suzy"); body1.put("performanceLevels", new String[] {"1"}); body2.put("performanceLevels", new String[] {"2"}); body3.put("performanceLevels", new String[] {"3"}); body4.put("performanceLevels", new String[] {"4"}); // save entities repository.create("student", body1); repository.create("student", body2); repository.create("student", body3); repository.create("student", body4); // sort entities by firstName with ascending order NeutralQuery sortQuery1 = new NeutralQuery(); sortQuery1.setSortBy("firstName"); sortQuery1.setSortOrder(NeutralQuery.SortOrder.ascending); sortQuery1.setOffset(0); sortQuery1.setLimit(100); Iterable<Entity> entities = repository.findAll("student", sortQuery1); assertNotNull(entities); Iterator<Entity> it = entities.iterator(); assertEquals("Austin", it.next().getBody().get("firstName")); assertEquals("Jane", it.next().getBody().get("firstName")); assertEquals("Mary", it.next().getBody().get("firstName")); assertEquals("Suzy", it.next().getBody().get("firstName")); // sort entities by firstName with descending order NeutralQuery sortQuery2 = new NeutralQuery(); sortQuery2.setSortBy("firstName"); sortQuery2.setSortOrder(NeutralQuery.SortOrder.descending); sortQuery2.setOffset(0); sortQuery2.setLimit(100); entities = repository.findAll("student", sortQuery2); assertNotNull(entities); it = entities.iterator(); assertEquals("Suzy", it.next().getBody().get("firstName")); assertEquals("Mary", it.next().getBody().get("firstName")); assertEquals("Jane", it.next().getBody().get("firstName")); assertEquals("Austin", it.next().getBody().get("firstName")); // sort entities by performanceLevels which is an array with ascending // order NeutralQuery sortQuery3 = new NeutralQuery(); sortQuery3.setSortBy("performanceLevels"); sortQuery3.setSortOrder(NeutralQuery.SortOrder.ascending); sortQuery3.setOffset(0); sortQuery3.setLimit(100); entities = repository.findAll("student", sortQuery3); assertNotNull(entities); it = entities.iterator(); assertEquals("1", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0)); assertEquals("2", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0)); assertEquals("3", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0)); assertEquals("4", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0)); // sort entities by performanceLevels which is an array with descending // order NeutralQuery sortQuery4 = new NeutralQuery(); sortQuery4.setSortBy("performanceLevels"); sortQuery4.setSortOrder(NeutralQuery.SortOrder.descending); sortQuery4.setOffset(0); sortQuery4.setLimit(100); entities = repository.findAll("student", sortQuery4); assertNotNull(entities); it = entities.iterator(); assertEquals("4", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0)); assertEquals("3", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0)); assertEquals("2", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0)); assertEquals("1", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0)); }
@Override public SLIPrincipal locate( String tenantId, String externalUserId, String userType, String clientId) { LOG.info("Locating user {}@{} of type: {}", new Object[] {externalUserId, tenantId, userType}); SLIPrincipal user = new SLIPrincipal(externalUserId + "@" + tenantId); user.setExternalId(externalUserId); user.setTenantId(tenantId); user.setUserType(userType); TenantContext.setTenantId(tenantId); if (EntityNames.STUDENT.equals(userType)) { NeutralQuery neutralQuery = new NeutralQuery( new NeutralCriteria( ParameterConstants.STUDENT_UNIQUE_STATE_ID, NeutralCriteria.OPERATOR_EQUAL, externalUserId)); neutralQuery.setOffset(0); neutralQuery.setLimit(1); user.setEntity(repo.findOne(EntityNames.STUDENT, neutralQuery, true)); } else if (EntityNames.PARENT.equals(userType)) { NeutralQuery neutralQuery = new NeutralQuery( new NeutralCriteria( ParameterConstants.PARENT_UNIQUE_STATE_ID, NeutralCriteria.OPERATOR_EQUAL, externalUserId)); neutralQuery.setOffset(0); neutralQuery.setLimit(1); user.setEntity(repo.findOne(EntityNames.PARENT, neutralQuery, true)); } else if (isStaff(userType)) { NeutralQuery neutralQuery = new NeutralQuery(); neutralQuery.setOffset(0); neutralQuery.setLimit(1); neutralQuery.addCriteria( new NeutralCriteria( ParameterConstants.STAFF_UNIQUE_STATE_ID, NeutralCriteria.OPERATOR_EQUAL, externalUserId)); Iterable<Entity> staff = repo.findAll(EntityNames.STAFF, neutralQuery); if (staff != null && staff.iterator().hasNext()) { Entity entity = staff.iterator().next(); Set<String> edorgs = edorgHelper.locateDirectEdorgs(entity); if (edorgs.size() == 0) { LOG.warn("User {} is not currently associated to a school/edorg", user.getId()); throw new APIAccessDeniedException( "User is not currently associated to a school/edorg", user, clientId); } user.setEntity(entity); } } if (user.getEntity() == null) { LOG.warn("Failed to locate user {} in the datastore", user.getId()); Entity entity = new MongoEntity( "user", SLIPrincipal.NULL_ENTITY_ID, new HashMap<String, Object>(), new HashMap<String, Object>()); user.setEntity(entity); } else { LOG.info( "Matched user: {}@{} -> {}", new Object[] {externalUserId, tenantId, user.getEntity().getEntityId()}); } return user; }