public SubscriptionClient GetSubscriptionClientVerLatest() { var tenantMock = new Mock <ITenantsOperations>(); tenantMock.Setup(t => t.ListWithHttpMessagesAsync(It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .Returns( (Dictionary <string, List <string> > ch, CancellationToken token) => { if (TenantListQueueVerLatest != null && TenantListQueueVerLatest.Any()) { return(ListTenantQueueDequeueVerLatest()); } var tenants = _tenants.Select((k) => new TenantIdDescription(id: k, tenantId: k)); var mockPage = new MockPage <TenantIdDescription>(tenants.ToList()); AzureOperationResponse <IPage <TenantIdDescription> > r = new AzureOperationResponse <IPage <TenantIdDescription> > { Body = mockPage }; return(Task.FromResult(r)); } ); var subscriptionMock = new Mock <ISubscriptionsOperations>(); subscriptionMock.Setup( s => s.GetWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())).Returns( (string subId, Dictionary <string, List <string> > ch, CancellationToken token) => { if (SubGetQueueVerLatest != null && SubGetQueueVerLatest.Any()) { return(GetSubQueueDequeueVerLatest()); } AzureOperationResponse <Subscription> result = new AzureOperationResponse <Subscription> { RequestId = Guid.NewGuid().ToString() }; if (_subscriptionSet.Contains(subId)) { result.Body = new Subscription( id: subId, subscriptionId: subId, tenantId: null, displayName: GetSubscriptionNameFromId(subId), state: SubscriptionState.Enabled, subscriptionPolicies: null, authorizationSource: null); } return(Task.FromResult(result)); }); subscriptionMock.Setup( (s) => s.ListWithHttpMessagesAsync(null, It.IsAny <CancellationToken>())).Returns( (Dictionary <string, List <string> > ch, CancellationToken token) => { if (SubListQueueVerLatest != null && SubListQueueVerLatest.Any()) { return(ListSubQueueDequeueVerLatest()); } AzureOperationResponse <IPage <Subscription> > result = null; if (_subscriptions.Count > 0) { var subscriptionList = _subscriptions.Dequeue(); result = new AzureOperationResponse <IPage <Subscription> > { RequestId = Guid.NewGuid().ToString(), Body = new MockPage <Subscription>( new List <Subscription>( subscriptionList.Select( sub => new Subscription( id: sub, subscriptionId: sub, tenantId: null, displayName: GetSubscriptionNameFromId(sub), state: SubscriptionState.Enabled, subscriptionPolicies: null, authorizationSource: null))), "LinkToNextPage") }; } return(Task.FromResult(result)); }); subscriptionMock.Setup( (s) => s.ListNextWithHttpMessagesAsync("LinkToNextPage", null, It.IsAny <CancellationToken>())).Returns( (string nextLink, Dictionary <string, List <string> > ch, CancellationToken token) => { AzureOperationResponse <IPage <Subscription> > result = null; if (_subscriptions.Count > 0) { var subscriptionList = _subscriptions.Dequeue(); result = new AzureOperationResponse <IPage <Subscription> > { RequestId = Guid.NewGuid().ToString(), Body = new MockPage <Subscription>( new List <Subscription>( subscriptionList.Select( sub => new Subscription( id: sub, subscriptionId: sub, tenantId: null, displayName: GetSubscriptionNameFromId(sub), state: SubscriptionState.Disabled, subscriptionPolicies: null, authorizationSource: null)))) }; } return(Task.FromResult(result)); }); var client = new Mock <SubscriptionClient>() { CallBase = true }; client.SetupGet(c => c.Subscriptions).Returns(subscriptionMock.Object); client.SetupGet(c => c.Tenants).Returns(tenantMock.Object); return(client.Object); }
private ISubscriptionsOperations GetSubscriptionMock(List <Subscription> subscriptionGetList, List <List <Subscription> > subscriptionListLists, List <bool> HasNextPage) { if (HasNextPage == null && subscriptionListLists != null) { HasNextPage = Enumerable.Repeat(false, subscriptionListLists.Count).ToList(); } var subscriptionMock = new Mock <ISubscriptionsOperations>(); if (subscriptionGetList != null) { subscriptionMock.Setup( s => s.GetWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())).Returns( (string subId, Dictionary <string, List <string> > ch, CancellationToken token) => { if (SubGetQueueVerLatest != null && SubGetQueueVerLatest.Any()) { return(GetSubQueueDequeueVerLatest()); } AzureOperationResponse <Subscription> result = new AzureOperationResponse <Subscription>() { RequestId = Guid.NewGuid().ToString() }; if (subscriptionGetList.Any()) { result.Body = subscriptionGetList.First(); subscriptionGetList.RemoveAt(0); } return(Task.FromResult(result)); }); } subscriptionMock.Setup( (s) => s.ListWithHttpMessagesAsync(null, It.IsAny <CancellationToken>())).Returns( (Dictionary <string, List <string> > ch, CancellationToken token) => { if (SubListQueueVerLatest != null && SubListQueueVerLatest.Any()) { return(ListSubQueueDequeueVerLatest()); } AzureOperationResponse <IPage <Subscription> > result = null; if (subscriptionListLists.Any() && HasNextPage.Any()) { result = new AzureOperationResponse <IPage <Subscription> > { RequestId = Guid.NewGuid().ToString(), Body = HasNextPage.First() ? new MockPage <Subscription>(subscriptionListLists.FirstOrDefault(), "LinkToNextPage") : new MockPage <Subscription>(subscriptionListLists.FirstOrDefault()) }; subscriptionListLists.RemoveAt(0); HasNextPage.RemoveAt(0); } return(Task.FromResult(result)); }); subscriptionMock.Setup( (s) => s.ListNextWithHttpMessagesAsync("LinkToNextPage", null, It.IsAny <CancellationToken>())).Returns( (string nextLink, Dictionary <string, List <string> > ch, CancellationToken token) => { AzureOperationResponse <IPage <Subscription> > result = null; if (subscriptionListLists.Any() && HasNextPage.Any()) { result = new AzureOperationResponse <IPage <Subscription> > { RequestId = Guid.NewGuid().ToString(), Body = new MockPage <Subscription>(subscriptionListLists.LastOrDefault()) }; subscriptionListLists.RemoveAt(0); HasNextPage.RemoveAt(0); } return(Task.FromResult(result)); }); return(subscriptionMock.Object); }