private static ClientResponse MockEntityClientResponse(TimelineClientImpl client, ClientResponse.Status status, bool hasError, bool hasRuntimeError) { ClientResponse response = Org.Mockito.Mockito.Mock <ClientResponse>(); if (hasRuntimeError) { Org.Mockito.Mockito.DoThrow(new ClientHandlerException(new ConnectException())).When (client).DoPostingObject(Matchers.Any <TimelineEntities>(), Matchers.Any <string>( )); return(response); } Org.Mockito.Mockito.DoReturn(response).When(client).DoPostingObject(Matchers.Any < TimelineEntities>(), Matchers.Any <string>()); Org.Mockito.Mockito.When(response.GetClientResponseStatus()).ThenReturn(status); TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError (); error.SetEntityId("test entity id"); error.SetEntityType("test entity type"); error.SetErrorCode(TimelinePutResponse.TimelinePutError.IoException); TimelinePutResponse putResponse = new TimelinePutResponse(); if (hasError) { putResponse.AddError(error); } Org.Mockito.Mockito.When(response.GetEntity <TimelinePutResponse>()).ThenReturn(putResponse ); return(response); }
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); } }