@Test public void testTimestamps() throws Exception { // 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); DateTime created = new DateTime(saved.getMetaData().get(EntityMetadataKey.CREATED.getKey())); DateTime updated = new DateTime(saved.getMetaData().get(EntityMetadataKey.UPDATED.getKey())); assertEquals(created, updated); saved.getBody().put("cityOfBirth", "Evanston"); // Needs to be here to prevent cases where code execution is so fast, // there // is no difference between create/update times Thread.sleep(2); repository.update("student", saved, false); updated = new DateTime(saved.getMetaData().get(EntityMetadataKey.UPDATED.getKey())); assertTrue(updated.isAfter(created)); }
@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()); }
@Test public void testNeedsId() { Entity e = new MongoEntity("student", buildTestStudentEntity()); assertFalse(repository.update("student", e, false)); }
@Test public void testSchoolLineage() { NeutralQuery query = null; repository.deleteAll(EntityNames.EDUCATION_ORGANIZATION, null); DBObject indexKeys = new BasicDBObject("body." + ParameterConstants.STATE_ORGANIZATION_ID, 1); mongoTemplate.getCollection(EntityNames.EDUCATION_ORGANIZATION).ensureIndex(indexKeys); mongoTemplate .getCollection(EntityNames.EDUCATION_ORGANIZATION) .ensureIndex(new BasicDBObject("metaData.edOrgs", 1), new BasicDBObject("sparse", true)); // Add a school Entity school = createEducationOrganizationEntity("school1", "school", "School", null); Set<String> expectedEdOrgs = new HashSet(); expectedEdOrgs.add(school.getEntityId()); assertTrue( "School not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs)); // Add an SEA Entity sea = createEducationOrganizationEntity( "sea1", "stateEducationAgency", "State Education Agency", null); assertTrue( "After adding SEA expected edOrgs not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs)); // Add an LEA List<String> parentRefs = new ArrayList<String>(Arrays.asList(sea.getEntityId())); Entity lea = createEducationOrganizationEntity( "lea1", "localEducationAgency", "Local Education Agency", parentRefs); assertTrue( "After adding LEA expected edOrgs not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs)); // doUpdate School parent ref to LEA List<String> parentRefsUpdate = new ArrayList<String>(Arrays.asList(lea.getEntityId())); query = new NeutralQuery( new NeutralCriteria("_id", NeutralCriteria.OPERATOR_EQUAL, school.getEntityId())); Update update = new Update() .set("body." + ParameterConstants.PARENT_EDUCATION_AGENCY_REFERENCE, parentRefsUpdate); repository.doUpdate(EntityNames.EDUCATION_ORGANIZATION, query, update); expectedEdOrgs.add(lea.getEntityId()); expectedEdOrgs.add(sea.getEntityId()); assertTrue( "After updating school parent ref expected edOrgs not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs)); // Patch LEA parent ref to an undefined id List<String> parentRefsPatch = new ArrayList<String>(Arrays.asList("undefinedId")); Map<String, Object> newValues = new HashMap<String, Object>(); newValues.put(ParameterConstants.PARENT_EDUCATION_AGENCY_REFERENCE, parentRefsPatch); repository.patch( "localEducationEntity", EntityNames.EDUCATION_ORGANIZATION, lea.getEntityId(), newValues); expectedEdOrgs.remove(sea.getEntityId()); assertTrue( "After updating school parent ref expected edOrgs not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs)); // Update LEA to set parent ref back to SEA repository.update(EntityNames.EDUCATION_ORGANIZATION, lea, false); expectedEdOrgs.add(sea.getEntityId()); assertTrue( "After updating school parent ref expected edOrgs not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs)); // Delete LEA - lineage should be recalculated repository.delete(EntityNames.EDUCATION_ORGANIZATION, lea.getEntityId()); expectedEdOrgs.remove(lea.getEntityId()); expectedEdOrgs.remove(sea.getEntityId()); assertTrue( "After deleting lea expected edOrgs not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs)); // Insert LEA with no parent ref to SEA lea.getBody().remove(ParameterConstants.PARENT_EDUCATION_AGENCY_REFERENCE); Entity insertedLea = ((MongoEntityRepository) repository).insert(lea, EntityNames.EDUCATION_ORGANIZATION); expectedEdOrgs.add(lea.getEntityId()); assertTrue( "After re-adding LEA with no parent ref expected edOrgs not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs)); // findAndUpdate School parent ref to SEA List<String> parentRefsSEA = new ArrayList<String>(Arrays.asList(sea.getEntityId())); query = new NeutralQuery( new NeutralCriteria("_id", NeutralCriteria.OPERATOR_EQUAL, school.getEntityId())); update = new Update() .set("body." + ParameterConstants.PARENT_EDUCATION_AGENCY_REFERENCE, parentRefsSEA); repository.findAndUpdate(EntityNames.EDUCATION_ORGANIZATION, query, update); expectedEdOrgs.remove(lea.getEntityId()); expectedEdOrgs.add(sea.getEntityId()); assertTrue( "After updating school parent ref to SEA, expected edOrgs not found in lineage.", schoolLineageIs(school.getEntityId(), expectedEdOrgs)); // Clear lineage for negative tests clearSchoolLineage(school); // Create an unrelated entity type and make sure school lineage isn't recalculated repository.create("student", buildTestStudentEntity()); assertTrue( "After adding a student lineage school lineage should not change.", schoolLineageIs(school.getEntityId(), new HashSet<String>())); // Patch an edOrg non-parent-ref and make sure school lineage isn't recalculated newValues = new HashMap<String, Object>(); newValues.put("body.nameOfInstitution", "updatedName"); repository.patch("school", EntityNames.EDUCATION_ORGANIZATION, school.getEntityId(), newValues); assertTrue( "Updating a school non-parent ref should not change school lineage.", schoolLineageIs(school.getEntityId(), new HashSet<String>())); mongoTemplate.getCollection(EntityNames.EDUCATION_ORGANIZATION).drop(); }
private void clearSchoolLineage(Entity school) { school.getMetaData().remove("edOrgs"); repository.update(EntityNames.EDUCATION_ORGANIZATION, school, false); }