public void Validate_WithTwoInteractionsAndProviderStateSetupAndTearDownDefined_SetUpIsInvokedBeforeCompareAndTearDownIsInvokedAfterCompare() { const string providerState1 = "My Provider State"; const string providerState2 = "My Provider State 2"; const string setUpSuffix = "-SetUp"; const string tearDownSuffix = "-TearDown"; var actionInvocationLog = new List <string>(); var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = providerState1, Description = "My interaction" }, new ProviderServiceInteraction { ProviderState = providerState2, Description = "My interaction" }, } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState(providerState1, setUp: () => actionInvocationLog.Add(providerState1 + setUpSuffix), tearDown: () => actionInvocationLog.Add(providerState1 + tearDownSuffix))); providerStates.Add(new ProviderState(providerState2, setUp: () => actionInvocationLog.Add(providerState2 + setUpSuffix), tearDown: () => actionInvocationLog.Add(providerState2 + tearDownSuffix))); var mockProviderServiceResponseComparer = Substitute.For <IProviderServiceResponseComparer>(); var fakeHttpClient = new FakeHttpClient(); var mockHttpRequestMessageMapper = Substitute.For <IHttpRequestMessageMapper>(); var mockProviderServiceResponseMapper = Substitute.For <IProviderServiceResponseMapper>(); var providerServiceValidator = new ProviderServiceValidator( mockProviderServiceResponseComparer, fakeHttpClient, mockHttpRequestMessageMapper, mockProviderServiceResponseMapper, new Reporter(new NoOpReportOutputter())); providerServiceValidator.Validate(pact, providerStates); Assert.Equal(4, actionInvocationLog.Count()); Assert.Equal(providerState1 + setUpSuffix, actionInvocationLog.First()); Assert.Equal(providerState1 + tearDownSuffix, actionInvocationLog.Skip(1).First()); Assert.Equal(providerState2 + setUpSuffix, actionInvocationLog.Skip(2).First()); Assert.Equal(providerState2 + tearDownSuffix, actionInvocationLog.Last()); }
public void Add_WithAndAlreadyAddedProviderState_ThrowsArgumentException() { const string providerStateDescription = "my provider state"; var providerState1 = new ProviderState(providerStateDescription); var providerState2 = new ProviderState(providerStateDescription); var providerStates = new ProviderStates(); providerStates.Add(providerState1); Assert.Throws <ArgumentException>(() => providerStates.Add(providerState2)); }
public void Find_WithProviderStateThatDoesNotMatchProviderStates_ReturnsNull() { const string providerStateDescription = "my provider state 2"; var providerState1 = new ProviderState("my provider state"); var providerState2 = new ProviderState(providerStateDescription); var providerStates = new ProviderStates(); providerStates.Add(providerState1); providerStates.Add(providerState2); var actualProviderState = providerStates.Find("something else"); Assert.Null(actualProviderState); }
public void Find_WithProviderStateThatHasBeenAdded_ReturnsProviderState() { const string providerStateDescription = "my provider state 2"; var providerState1 = new ProviderState("my provider state"); var providerState2 = new ProviderState(providerStateDescription); var providerStates = new ProviderStates(); providerStates.Add(providerState1); providerStates.Add(providerState2); var actualProviderState = providerStates.Find(providerStateDescription); Assert.Equal(providerState2, actualProviderState); }
public void Validate_WhenInteractionDefinesAProviderStateAndProviderStateIsSuppliedWithNoSetUpOrTearDown_NoProviderStateSetUpOrTearDownActionsAreInvoked() { var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = "My Provider State", Description = "My interaction" } } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState("My Provider State")); var validator = GetSubject(); validator.Validate(pact, providerStates); }
public void Validate_WhenInteractionDefinesAProviderStateAndProviderStateIsNotFound_ThrowsInvalidOperationException() { var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = "My Provider State", Description = "My interaction" } } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState("Some other provider state")); var validator = GetSubject(); Assert.Throws <InvalidOperationException>(() => validator.Validate(pact, providerStates)); }
public void Validate_WhenInteractionDefinesAProviderStateAndProviderStateTearDownDefinedAndProviderServiceResponseComparerThrows_TearDownActionIsInvoked() { var actionInkoved = false; var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = "My Provider State", Description = "My interaction" } } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState("My Provider State", setUp: null, tearDown: () => { actionInkoved = true; })); var validator = GetSubject(); _mockResponseComparer .When(x => x.Compare(Arg.Any <ProviderServiceResponse>(), Arg.Any <ProviderServiceResponse>())) .Do(x => { throw new PactFailureException("Expected response cannot be null"); }); Assert.Throws <PactFailureException>(() => validator.Validate(pact, providerStates)); Assert.True(actionInkoved, "Provider state tearDown action is invoked"); }
public void Validate_WhenInteractionDefinesAProviderStateAndProviderStateTearDownDefined_TearDownActionIsInvoked() { var actionInkoved = false; var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = "My Provider State", Description = "My interaction" } } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState("My Provider State", setUp: null, tearDown: () => { actionInkoved = true; })); var validator = GetSubject(); validator.Validate(pact, providerStates); Assert.True(actionInkoved, "Provider state tearDown action is invoked"); }
public void Validate_WithTwoInteractionsAndProviderStateSetupAndTearDownDefined_SetUpIsInvokedBeforeCompareAndTearDownIsInvokedAfterCompare() { const string providerState1 = "My Provider State"; const string providerState2 = "My Provider State 2"; const string setUpSuffix = "-SetUp"; const string tearDownSuffix = "-TearDown"; var actionInvocationLog = new List <string>(); var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = providerState1, Description = "My interaction" }, new ProviderServiceInteraction { ProviderState = providerState2, Description = "My interaction" }, } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState(providerState1, setUp: () => actionInvocationLog.Add(providerState1 + setUpSuffix), tearDown: () => actionInvocationLog.Add(providerState1 + tearDownSuffix))); providerStates.Add(new ProviderState(providerState2, setUp: () => actionInvocationLog.Add(providerState2 + setUpSuffix), tearDown: () => actionInvocationLog.Add(providerState2 + tearDownSuffix))); var validator = GetSubject(); validator.Validate(pact, providerStates); Assert.Equal(4, actionInvocationLog.Count()); Assert.Equal(providerState1 + setUpSuffix, actionInvocationLog.First()); Assert.Equal(providerState1 + tearDownSuffix, actionInvocationLog.Skip(1).First()); Assert.Equal(providerState2 + setUpSuffix, actionInvocationLog.Skip(2).First()); Assert.Equal(providerState2 + tearDownSuffix, actionInvocationLog.Last()); }
public void Add_WithProviderState_AddsProviderState() { const string providerStateDescription = "my provider state"; var providerState = new ProviderState(providerStateDescription); var providerStates = new ProviderStates(); providerStates.Add(providerState); Assert.Equal(providerState, providerStates.Find(providerStateDescription)); }
/// <summary> /// Define a set up and/or tear down action for a specific state specified by the consumer. /// This is where you should set up test data, so that you can fulfil the contract outlined by a consumer. /// </summary> /// <param name="providerState">The name of the provider state as defined by the consumer interaction, which lives in the Pact file.</param> /// <param name="setUp">A set up action that will be run before the interaction verify, if the provider has specified it in the interaction. If no action is required please use an empty lambda () => {}.</param> /// <param name="tearDown">A tear down action that will be run after the interaction verify, if the provider has specified it in the interaction. If no action is required please use an empty lambda () => {}.</param> public IPactVerifier ProviderState(string providerState, Action setUp = null, Action tearDown = null) { if (String.IsNullOrEmpty(providerState)) { throw new ArgumentException("Please supply a non null or empty providerState"); } var providerStateItem = new ProviderState(providerState, setUp, tearDown); ProviderStates.Add(providerStateItem); return(this); }
public IProviderStates ProviderState(string providerState, Action setUp = null, Action tearDown = null) { if (ProviderStates == null) { throw new InvalidOperationException("Please intitialise the provider states by first calling the ProviderStatesFor method"); } if (String.IsNullOrEmpty(providerState)) { throw new ArgumentException("Please supply a non null or empty providerState"); } var providerStateItem = new ProviderState(providerState, setUp, tearDown); ProviderStates.Add(providerStateItem); return(this); }
public void Validate_WhenInteractionDefinesAProviderStateAndProviderStateTearDownDefinedAndProviderServiceResponseComparerThrows_TearDownActionIsInvoked() { var actionInkoved = false; var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = "My Provider State", Description = "My interaction" } } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState("My Provider State", setUp: null, tearDown: () => { actionInkoved = true; })); var mockProviderServiceResponseComparer = Substitute.For <IProviderServiceResponseComparer>(); var fakeHttpClient = new FakeHttpClient(); var mockHttpRequestMessageMapper = Substitute.For <IHttpRequestMessageMapper>(); var mockProviderServiceResponseMapper = Substitute.For <IProviderServiceResponseMapper>(); mockProviderServiceResponseComparer .When(x => x.Compare(Arg.Any <ProviderServiceResponse>(), Arg.Any <ProviderServiceResponse>())) .Do(x => { throw new CompareFailedException("Expected response cannot be null"); }); var providerServiceValidator = new ProviderServiceValidator( mockProviderServiceResponseComparer, fakeHttpClient, mockHttpRequestMessageMapper, mockProviderServiceResponseMapper, new Reporter(new NoOpReportOutputter())); Assert.Throws <CompareFailedException>(() => providerServiceValidator.Validate(pact, providerStates)); Assert.True(actionInkoved, "Provider state tearDown action is invoked"); }
public void Validate_WhenInteractionDefinesAProviderStateAndProviderStateSetUpDefined_SetUpActionIsInvoked() { var actionInkoved = false; var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = "My Provider State", Description = "My interaction" } } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState("My Provider State", setUp: () => { actionInkoved = true; }, tearDown: null)); var mockProviderServiceResponseComparer = Substitute.For <IProviderServiceResponseComparer>(); var fakeHttpClient = new FakeHttpClient(); var mockHttpRequestMessageMapper = Substitute.For <IHttpRequestMessageMapper>(); var mockProviderServiceResponseMapper = Substitute.For <IProviderServiceResponseMapper>(); var providerServiceValidator = new ProviderServiceValidator( mockProviderServiceResponseComparer, fakeHttpClient, mockHttpRequestMessageMapper, mockProviderServiceResponseMapper, new Reporter(new NoOpReportOutputter())); providerServiceValidator.Validate(pact, providerStates); Assert.True(actionInkoved, "Provider state setUp action is invoked"); }
public void Validate_WhenInteractionDefinesAProviderStateAndProviderStateIsNoFound_ThrowsInvalidOperationException() { var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = "My Provider State", Description = "My interaction" } } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState("Some other provider state")); var mockProviderServiceResponseComparer = Substitute.For <IProviderServiceResponseComparer>(); var fakeHttpClient = new FakeHttpClient(); var mockHttpRequestMessageMapper = Substitute.For <IHttpRequestMessageMapper>(); var mockProviderServiceResponseMapper = Substitute.For <IProviderServiceResponseMapper>(); var providerServiceValidator = new ProviderServiceValidator( mockProviderServiceResponseComparer, fakeHttpClient, mockHttpRequestMessageMapper, mockProviderServiceResponseMapper, new Reporter(new NoOpReportOutputter())); Assert.Throws <InvalidOperationException>(() => providerServiceValidator.Validate(pact, providerStates)); }
public void Validate_WhenInteractionDefinesAProviderStateAndProviderStateIsSuppliedWithNoSetUpOrTearDown_NoProviderStateSetUpOrTearDownActionsAreInvoked() { var pact = new ProviderServicePactFile { Consumer = new Party { Name = "My client" }, Provider = new Party { Name = "My Provider" }, Interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { ProviderState = "My Provider State", Description = "My interaction" } } }; var providerStates = new ProviderStates(); providerStates.Add(new ProviderState("My Provider State")); var mockProviderServiceResponseComparer = Substitute.For <IProviderServiceResponseComparer>(); var fakeHttpClient = new FakeHttpClient(); var mockHttpRequestMessageMapper = Substitute.For <IHttpRequestMessageMapper>(); var mockProviderServiceResponseMapper = Substitute.For <IProviderServiceResponseMapper>(); var providerServiceValidator = new ProviderServiceValidator( mockProviderServiceResponseComparer, fakeHttpClient, mockHttpRequestMessageMapper, mockProviderServiceResponseMapper, new Reporter(new NoOpReportOutputter())); providerServiceValidator.Validate(pact, providerStates); }