private static TimelineEntity GenerateEntity() { TimelineEntity entity = new TimelineEntity(); entity.SetEntityId("entity id"); entity.SetEntityType("entity type"); entity.SetStartTime(Runtime.CurrentTimeMillis()); for (int i = 0; i < 2; ++i) { TimelineEvent @event = new TimelineEvent(); @event.SetTimestamp(Runtime.CurrentTimeMillis()); @event.SetEventType("test event type " + i); @event.AddEventInfo("key1", "val1"); @event.AddEventInfo("key2", "val2"); entity.AddEvent(@event); } entity.AddRelatedEntity("test ref type 1", "test ref id 1"); entity.AddRelatedEntity("test ref type 2", "test ref id 2"); entity.AddPrimaryFilter("pkey1", "pval1"); entity.AddPrimaryFilter("pkey2", "pval2"); entity.AddOtherInfo("okey1", "oval1"); entity.AddOtherInfo("okey2", "oval2"); entity.SetDomainId("domain id 1"); return(entity); }
public virtual void TestRelatingToOldEntityWithoutDomainId() { // New entity is put in the default domain TimelineEntity entityToStore = new TimelineEntity(); entityToStore.SetEntityType("NEW_ENTITY_TYPE_1"); entityToStore.SetEntityId("NEW_ENTITY_ID_1"); entityToStore.SetDomainId(TimelineDataManager.DefaultDomainId); entityToStore.AddRelatedEntity("OLD_ENTITY_TYPE_1", "OLD_ENTITY_ID_1"); TimelineEntities entities = new TimelineEntities(); entities.AddEntity(entityToStore); store.Put(entities); TimelineEntity entityToGet = store.GetEntity("OLD_ENTITY_ID_1", "OLD_ENTITY_TYPE_1" , null); NUnit.Framework.Assert.IsNotNull(entityToGet); NUnit.Framework.Assert.IsNull(entityToGet.GetDomainId()); NUnit.Framework.Assert.AreEqual("NEW_ENTITY_TYPE_1", entityToGet.GetRelatedEntities ().Keys.GetEnumerator().Next()); NUnit.Framework.Assert.AreEqual("NEW_ENTITY_ID_1", entityToGet.GetRelatedEntities ().Values.GetEnumerator().Next().GetEnumerator().Next()); // New entity is not put in the default domain entityToStore = new TimelineEntity(); entityToStore.SetEntityType("NEW_ENTITY_TYPE_2"); entityToStore.SetEntityId("NEW_ENTITY_ID_2"); entityToStore.SetDomainId("NON_DEFAULT"); entityToStore.AddRelatedEntity("OLD_ENTITY_TYPE_1", "OLD_ENTITY_ID_1"); entities = new TimelineEntities(); entities.AddEntity(entityToStore); TimelinePutResponse response = store.Put(entities); NUnit.Framework.Assert.AreEqual(1, response.GetErrors().Count); NUnit.Framework.Assert.AreEqual(TimelinePutResponse.TimelinePutError.ForbiddenRelation , response.GetErrors()[0].GetErrorCode()); entityToGet = store.GetEntity("OLD_ENTITY_ID_1", "OLD_ENTITY_TYPE_1", null); NUnit.Framework.Assert.IsNotNull(entityToGet); NUnit.Framework.Assert.IsNull(entityToGet.GetDomainId()); // Still have one related entity NUnit.Framework.Assert.AreEqual(1, entityToGet.GetRelatedEntities().Keys.Count); NUnit.Framework.Assert.AreEqual(1, entityToGet.GetRelatedEntities().Values.GetEnumerator ().Next().Count); }
public virtual void TestRelatingToNonExistingEntity() { TimelineEntity entityToStore = new TimelineEntity(); entityToStore.SetEntityType("TEST_ENTITY_TYPE_1"); entityToStore.SetEntityId("TEST_ENTITY_ID_1"); entityToStore.SetDomainId(TimelineDataManager.DefaultDomainId); entityToStore.AddRelatedEntity("TEST_ENTITY_TYPE_2", "TEST_ENTITY_ID_2"); TimelineEntities entities = new TimelineEntities(); entities.AddEntity(entityToStore); store.Put(entities); TimelineEntity entityToGet = store.GetEntity("TEST_ENTITY_ID_2", "TEST_ENTITY_TYPE_2" , null); NUnit.Framework.Assert.IsNotNull(entityToGet); NUnit.Framework.Assert.AreEqual("DEFAULT", entityToGet.GetDomainId()); NUnit.Framework.Assert.AreEqual("TEST_ENTITY_TYPE_1", entityToGet.GetRelatedEntities ().Keys.GetEnumerator().Next()); NUnit.Framework.Assert.AreEqual("TEST_ENTITY_ID_1", entityToGet.GetRelatedEntities ().Values.GetEnumerator().Next().GetEnumerator().Next()); }
public virtual TimelinePutResponse Put(TimelineEntities data) { lock (this) { TimelinePutResponse response = new TimelinePutResponse(); foreach (TimelineEntity entity in data.GetEntities()) { EntityIdentifier entityId = new EntityIdentifier(entity.GetEntityId(), entity.GetEntityType ()); // store entity info in memory TimelineEntity existingEntity = entities[entityId]; if (existingEntity == null) { existingEntity = new TimelineEntity(); existingEntity.SetEntityId(entity.GetEntityId()); existingEntity.SetEntityType(entity.GetEntityType()); existingEntity.SetStartTime(entity.GetStartTime()); if (entity.GetDomainId() == null || entity.GetDomainId().Length == 0) { TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError (); error.SetEntityId(entityId.GetId()); error.SetEntityType(entityId.GetType()); error.SetErrorCode(TimelinePutResponse.TimelinePutError.NoDomain); response.AddError(error); continue; } existingEntity.SetDomainId(entity.GetDomainId()); entities[entityId] = existingEntity; entityInsertTimes[entityId] = Runtime.CurrentTimeMillis(); } if (entity.GetEvents() != null) { if (existingEntity.GetEvents() == null) { existingEntity.SetEvents(entity.GetEvents()); } else { existingEntity.AddEvents(entity.GetEvents()); } existingEntity.GetEvents().Sort(); } // check startTime if (existingEntity.GetStartTime() == null) { if (existingEntity.GetEvents() == null || existingEntity.GetEvents().IsEmpty()) { TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError (); error.SetEntityId(entityId.GetId()); error.SetEntityType(entityId.GetType()); error.SetErrorCode(TimelinePutResponse.TimelinePutError.NoStartTime); response.AddError(error); Sharpen.Collections.Remove(entities, entityId); Sharpen.Collections.Remove(entityInsertTimes, entityId); continue; } else { long min = long.MaxValue; foreach (TimelineEvent e in entity.GetEvents()) { if (min > e.GetTimestamp()) { min = e.GetTimestamp(); } } existingEntity.SetStartTime(min); } } if (entity.GetPrimaryFilters() != null) { if (existingEntity.GetPrimaryFilters() == null) { existingEntity.SetPrimaryFilters(new Dictionary <string, ICollection <object> >()); } foreach (KeyValuePair <string, ICollection <object> > pf in entity.GetPrimaryFilters ()) { foreach (object pfo in pf.Value) { existingEntity.AddPrimaryFilter(pf.Key, MaybeConvert(pfo)); } } } if (entity.GetOtherInfo() != null) { if (existingEntity.GetOtherInfo() == null) { existingEntity.SetOtherInfo(new Dictionary <string, object>()); } foreach (KeyValuePair <string, object> info in entity.GetOtherInfo()) { existingEntity.AddOtherInfo(info.Key, MaybeConvert(info.Value)); } } // relate it to other entities if (entity.GetRelatedEntities() == null) { continue; } foreach (KeyValuePair <string, ICollection <string> > partRelatedEntities in entity. GetRelatedEntities()) { if (partRelatedEntities == null) { continue; } foreach (string idStr in partRelatedEntities.Value) { EntityIdentifier relatedEntityId = new EntityIdentifier(idStr, partRelatedEntities .Key); TimelineEntity relatedEntity = entities[relatedEntityId]; if (relatedEntity != null) { if (relatedEntity.GetDomainId().Equals(existingEntity.GetDomainId())) { relatedEntity.AddRelatedEntity(existingEntity.GetEntityType(), existingEntity.GetEntityId ()); } else { // in this case the entity will be put, but the relation will be // ignored TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError (); error.SetEntityType(existingEntity.GetEntityType()); error.SetEntityId(existingEntity.GetEntityId()); error.SetErrorCode(TimelinePutResponse.TimelinePutError.ForbiddenRelation); response.AddError(error); } } else { relatedEntity = new TimelineEntity(); relatedEntity.SetEntityId(relatedEntityId.GetId()); relatedEntity.SetEntityType(relatedEntityId.GetType()); relatedEntity.SetStartTime(existingEntity.GetStartTime()); relatedEntity.AddRelatedEntity(existingEntity.GetEntityType(), existingEntity.GetEntityId ()); relatedEntity.SetDomainId(existingEntity.GetDomainId()); entities[relatedEntityId] = relatedEntity; entityInsertTimes[relatedEntityId] = Runtime.CurrentTimeMillis(); } } } } return(response); } }