public void Test_ProcessRequest_Returns_The_First_Successful_Result() { var m1 = new Mock <IGitlabProcessor>(); m1.Setup(p => p.Process(It.IsAny <JObject>())) .Returns(RequestProcessResult.CreateNoResult()); //no result var m2 = new Mock <IGitlabProcessor>(); m2.Setup(p => p.Process(It.IsAny <JObject>())) .Returns(RequestProcessResult.CreateSuccess()); //success var m3 = new Mock <IGitlabProcessor>(); m3.Setup(p => p.Process(It.IsAny <JObject>())) .Returns(RequestProcessResult.CreateNoResult()); //should not have been called using (AutoMock mock = AutoMock.GetLoose(builder => { builder.RegisterXUnit(this.output); builder.RegisterInstance(m1.Object); builder.RegisterInstance(m2.Object); builder.RegisterInstance(m3.Object); })) { var service = mock.Create <GitlabProcessServiceImpl>(); JObject request = JObject.Parse("{prop:'value'}"); RequestProcessResult result = service.ProcessRequest(request); Assert.True(result.Success); m1.Verify(p => p.Process(request), Times.Once); m2.Verify(p => p.Process(request), Times.Once); m3.Verify(p => p.Process(request), Times.Never); } }
public RequestProcessResult Process(JObject request) { RequestProcessResult result; string objectKind = request[GitlabKeys.ObjectKind]?.ToString(); this.logger.LogTrace("The request object kind was determined as: \"{0}\"", objectKind); if (string.Equals(objectKind, GitlabKeys.ObjectKindMergeRequest, StringComparison.InvariantCultureIgnoreCase)) { result = this.formatter.TryFormat(request, out string message); if (result.Success) { this.logger.LogDebug("Successfully formatted the message: \"{0}\"", message); this.messageClient.ScheduleDelivery(message); } else { this.logger.LogDebug("Could not format the message: {@0}", result); } } else { this.logger.LogTrace("Can not handle the request of the \"{0}\" object kind", objectKind); result = RequestProcessResult.CreateNoResult(); } return(result); }
public RequestProcessResult Process(JObject request) { RequestProcessResult result; string objectKind = request[GitlabKeys.ObjectKind]?.ToString(); this.logger.LogTrace("The request object kind was determined as: \"{0}\"", objectKind); if (string.Equals(objectKind, GitlabKeys.ObjectKindNote, StringComparison.InvariantCultureIgnoreCase)) { var errors = new JTokenErrors(); string noteableType = request.RequireChild(GitlabKeys.ObjectAttributes, errors)?.RequireString(GitlabKeys.NoteableType, errors); this.logger.LogDebug("The noteable type was determined as \"{0}\"", noteableType); if (errors.HasAny) { string error = errors.Compose(); result = RequestProcessResult.CreateFailure(error); this.logger.LogDebug("The request processing was rejected with message: \"{0}\"", error); } else { if (this.CanHandle(noteableType)) { result = this.TryFormat(request, out string message); if (result.Success) { this.logger.LogDebug("Successfully formatted the message: \"{0}\"", message); this.messageClient.ScheduleDelivery(message); } else { this.logger.LogDebug("Could not format the message: {@0}", result); } } else { this.logger.LogDebug("Can not handle the request with the \"{0}\" noteable type", noteableType); result = RequestProcessResult.CreateNoResult(); } } } else { this.logger.LogTrace("Can not handle the request of the \"{0}\" object kind", objectKind); result = RequestProcessResult.CreateNoResult(); } return(result); }
public void Test_ProcessRequest_Returns_Failure_If_No_Success_Result_Received() { using (AutoMock mock = AutoMock.GetLoose(this.output.Capture())) { mock.Mock <IGitlabProcessor>() .Setup(p => p.Process(It.IsAny <JObject>())) .Returns(RequestProcessResult.CreateNoResult()); var service = mock.Create <GitlabProcessServiceImpl>(); JObject request = JObject.Parse("{prop:'value'}"); RequestProcessResult result = service.ProcessRequest(request); Assert.False(result.Success); Assert.Equal("The system is not capable of processing such requests", result.Reason); } }