public void ValidateServiceResponseAndPublishException_PublishException() { var eventPublished = false; string traceId = "exId", eventKey = "ek"; var wc = new WorkContext { CurrentEntityConfigRecord = new EntityConfigRecord { EventKeys = new EventKeyRecord("create", null, null, null) }, TraceId = traceId }; var eb = new Mock <IEventBus>(); eb.Setup(e => e.Publish(It.Is <string>(s => s == eventKey), It.Is <DomainEvent>(d => d.Data.GetPropertyValueByName <string>("TraceId") == traceId))) .Callback(() => eventPublished = true); ServiceProviderMock.Setup(s => s.GetService(typeof(IEventBus))).Returns(eb.Object); ServiceProviderMock.Setup(s => s.GetService(typeof(WorkContext))).Returns(wc); ServiceResponseWrapperExtensions.Init(ServiceProviderMock.Object); var serviceResponse = new ServiceResponse <object>(); var w = new ServiceResponseWrapper(serviceResponse); var pi = typeof(ServiceResponseWrapper).GetProperty("Exception"); pi.SetValue(w, new Exception()); ServiceResponseWrapperExtensions.ValidateServiceResponseAndPublishException <object>(w, eventKey, "ddd"); eventPublished.ShouldBeTrue(); serviceResponse.TraceId.ShouldBe(traceId); }
public async virtual Task <ServiceResponse <AuditPagination> > GetAll(AuditPagination pagination) { _logger.LogInformation(LoggingEvents.BusinessLogicFlow, "Start get all audit records flow"); pagination.QueryFunc = BuildAuditPaginationQuery(pagination); _logger.LogDebug(LoggingEvents.Repository, "Get all audit-records from repository using paginate = " + pagination); var wrapper = new ServiceResponseWrapper(new ServiceResponse <IEnumerable <AuditRecord> >()); var data = await _repository.Query(r => r.GetAll(pagination), wrapper); _logger.LogDebug(LoggingEvents.Repository, $"Repository response: {data?.ToJsonString()}"); var wSrvRes = wrapper.ServiceResponse; var isFault = FaultedServiceResult.Contains(wSrvRes.Result); pagination.Data = isFault ? data : (data ?? new AuditRecord[] { }); var serviceResponse = new ServiceResponse <AuditPagination> { Payload = pagination, Message = wSrvRes.Message, TraceId = wSrvRes.TraceId, Result = isFault ? wSrvRes.Result : ServiceResult.Ok }; _logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Service Response: {serviceResponse}"); return(serviceResponse); }
public void PublishExceptionIfExists_DoesNotPublish() { var serviceResponse = new ServiceResponse <object>(); var w = new ServiceResponseWrapper(serviceResponse); ServiceResponseWrapperExtensions.PublishExceptionIfExists(w, "ek", "ddd").ShouldBeFalse(); }
public void Ctor() { var sr = new ServiceResponse <object>(); var w = new ServiceResponseWrapper(sr); w.ServiceResponse.ShouldBe(sr); w.Exception.ShouldBeNull(); }
public static bool PublishExceptionIfExists(this ServiceResponseWrapper wrapper, string eventKey, object data) { if (wrapper.Exception == null) { return(false); } PublishException(wrapper.ServiceResponse, eventKey, data, wrapper.Exception); return(true); }
public static bool ValidateServiceResponseAndPublishException <T>(this ServiceResponseWrapper wrapper, string eventKey, object data) { if (!PublishExceptionIfExists(wrapper, eventKey, data)) { return(wrapper.ServiceResponse.ValidateServiceResponse <T>()); } return(false); }
public void ValidateServiceResponseAndPublishException_ReturnServiceResponse_True_Ok() { var srvRes = new ServiceResponse <string>() { Payload = "name", Result = ServiceResult.Ok }; var w = new ServiceResponseWrapper(srvRes); ServiceResponseWrapperExtensions.ValidateServiceResponseAndPublishException <string>(w, "ek", "ddd").ShouldBeTrue(); }
public void ValidateServiceResponseAndPublishException_ReturnServiceResponse_False_InvalidDataObject() { var srvRes = new ServiceResponse <string>() { Payload = "name", Result = ServiceResult.Ok }; var w = new ServiceResponseWrapper(srvRes); ServiceResponseWrapperExtensions.ValidateServiceResponseAndPublishException <int>(w, "ek", "ddd").ShouldBeFalse(); }
public async Task Query_NotFound_Paginate_ReturnsNull() { var repo = new Mock <IRepository <TestClass> >(); repo.Setup(r => r.GetAll(It.IsAny <Pagination <TestClass> >())).ReturnsAsync(null as IEnumerable <TestClass>); var sr = new ServiceResponse <IEnumerable <TestClass> >(); var w = new ServiceResponseWrapper(sr); var p = new Pagination <TestClass>(); var res = await ServiceRepositoryExtensions.Query(repo.Object, r => r.GetAll(p), w); res.ShouldBeNull(); }
public void ValidateServiceResponseAndPublishException_ReturnServiceResponse_False() { var allFaultedResults = ServiceResult.All.Where(r => r != ServiceResult.Ok && r != ServiceResult.Accepted); foreach (var fr in allFaultedResults) { var srvRes = new ServiceResponse <object>() { Result = fr }; var w = new ServiceResponseWrapper(srvRes); ServiceResponseWrapperExtensions.ValidateServiceResponseAndPublishException <object>(w, "ek", "ddd").ShouldBeFalse(); } }
public async Task Query_Empty_Collection(IEnumerable <TestClass> dbData) { var repo = new Mock <IRepository <TestClass> >(); repo.Setup(r => r.GetAll(null)).ReturnsAsync(dbData); var sr = new ServiceResponse <IEnumerable <TestClass> >(); var w = new ServiceResponseWrapper(sr); var res = await ServiceRepositoryExtensions.Query(repo.Object, r => r.GetAll(null), w); res.ShouldBe(dbData); sr.Payload.ShouldBe(dbData); sr.Result.ShouldBe(ServiceResult.NotSet); sr.Message.ShouldBeNullOrEmpty(); }
public async Task Query_NotFound_SingleItem() { var repo = new Mock <IRepository <TestClass> >(); repo.Setup(r => r.GetById(It.IsAny <string>())).ReturnsAsync(null as TestClass); var sr = new ServiceResponse <TestClass>(); var w = new ServiceResponseWrapper(sr); var res = await ServiceRepositoryExtensions.Query(repo.Object, r => r.GetById("some-id"), w); res.ShouldBeNull(); sr.Payload.ShouldBeNull(); sr.Result.ShouldBe(ServiceResult.NotFound); sr.Message.ShouldNotBeNullOrEmpty(); }
public async Task Query_RepositoryThrows() { var repo = new Mock <IRepository <TestClass> >(); var ex = new Exception(); repo.Setup(r => r.GetById(It.IsAny <string>())).Throws(ex); var sr = new ServiceResponse <TestClass>(); var w = new ServiceResponseWrapper(sr); var res = await ServiceRepositoryExtensions.Query(repo.Object, r => r.GetById("some-id"), w); res.ShouldBeNull(); sr.Payload.ShouldBeNull(); sr.Result.ShouldBe(ServiceResult.Error); sr.Message.ShouldNotBeNullOrEmpty(); w.Exception.ShouldBe(ex); }
public async Task Command_RepositoryReturnsSavedObject() { var tc = new TestClass(); var repo = new Mock <IRepository <TestClass> >(); repo.Setup(r => r.Insert(It.IsAny <TestClass>())).ReturnsAsync(tc); var sr = new ServiceResponse <TestClass>(); var w = new ServiceResponseWrapper(sr); var res = await ServiceRepositoryExtensions.Command(repo.Object, r => r.Insert(tc), w); res.ShouldBe(tc); sr.Payload.ShouldBe(tc); sr.Result.ShouldBe(ServiceResult.NotSet); sr.Message.ShouldBeNullOrEmpty(); }
public async Task Command_RepositoryThrows() { var repo = new Mock <IRepository <TestClass> >(); var ex = new Exception(); repo.Setup(r => r.Insert(It.IsAny <TestClass>())).Throws(ex); var sr = new ServiceResponse <TestClass>(); var tc = new TestClass(); var w = new ServiceResponseWrapper(sr); var res = await ServiceRepositoryExtensions.Command(repo.Object, r => r.Insert(tc), w); res.ShouldBeNull(); sr.Payload.ShouldBeNull(); sr.Result.ShouldBe(ServiceResult.Error); sr.Message.ShouldNotBeNullOrEmpty(); w.Exception.ShouldBe(ex); }