Exemple #1
0
 internal static void SetupRequestTimeoutException(
     Mock <TransportClient> mockTransportClient,
     TransportAddressUri physicalUri)
 {
     mockTransportClient.Setup(x => x.InvokeResourceOperationAsync(physicalUri, It.IsAny <DocumentServiceRequest>()))
     .Returns(() => throw new RequestTimeoutException($"Mock request timeout exception on URI:{physicalUri}", physicalUri.Uri));
 }
Exemple #2
0
 internal static void SetupServiceUnavailableException(
     Mock <TransportClient> mockTransportClient,
     TransportAddressUri physicalUri)
 {
     mockTransportClient.Setup(x => x.InvokeResourceOperationAsync(physicalUri, It.IsAny <DocumentServiceRequest>()))
     .Returns(() => throw new ServiceUnavailableException($"Mock write forbidden exception on URI:{physicalUri}", physicalUri.Uri));
 }
 internal static void SetupWriteForbiddenException(
     Mock <TransportClient> mockTransportClient,
     TransportAddressUri physicalUri)
 {
     mockTransportClient.Setup(x => x.InvokeResourceOperationAsync(physicalUri, It.IsAny <DocumentServiceRequest>()))
     .Returns(() =>
     {
         Console.WriteLine($"WriteForbiddenThrown: {physicalUri}");
         throw new ForbiddenException($"Mock write forbidden exception on URI:{physicalUri}", SubStatusCodes.WriteForbidden);
     });
 }
Exemple #4
0
 internal static void SetupCreateItemResponse(
     Mock <TransportClient> mockTransportClient,
     TransportAddressUri physicalUri)
 {
     mockTransportClient.Setup(x => x.InvokeResourceOperationAsync(physicalUri, It.IsAny <DocumentServiceRequest>()))
     .Returns <TransportAddressUri, DocumentServiceRequest>(
         (uri, documentServiceRequest) =>
     {
         Stream createdObject = documentServiceRequest.CloneableBody.Clone();
         return(Task.FromResult(new StoreResponse()
         {
             Status = 201,
             Headers = new StoreResponseNameValueCollection()
             {
                 ActivityId = Guid.NewGuid().ToString(),
                 LSN = "58593",
                 PartitionKeyRangeId = "1",
                 GlobalCommittedLSN = "58593",
             },
             ResponseBody = createdObject
         }));
     });
 }
        private static void SetupAccountAndCacheOperations(
            out string secondaryRegionNameForUri,
            out string globalEndpoint,
            out string secondaryRegionEndpiont,
            out string databaseName,
            out string containerName,
            out ResourceId containerResourceId,
            out Mock <IHttpHandler> mockHttpHandler,
            out IReadOnlyList <string> primaryRegionPartitionKeyRangeIds,
            out TransportAddressUri primaryRegionprimaryReplicaUri)
        {
            string accountName             = "testAccount";
            string primaryRegionNameForUri = "eastus";

            secondaryRegionNameForUri = "westus";
            globalEndpoint            = $"https://{accountName}.documents.azure.com:443/";
            Uri    globalEndpointUri     = new Uri(globalEndpoint);
            string primaryRegionEndpoint = $"https://{accountName}-{primaryRegionNameForUri}.documents.azure.com";

            secondaryRegionEndpiont = $"https://{accountName}-{secondaryRegionNameForUri}.documents.azure.com";
            databaseName            = "testDb";
            containerName           = "testContainer";
            string containerRid = "ccZ1ANCszwk=";

            containerResourceId = ResourceId.Parse(containerRid);

            List <AccountRegion> writeRegion = new List <AccountRegion>()
            {
                new AccountRegion()
                {
                    Name     = "East US",
                    Endpoint = $"{primaryRegionEndpoint}:443/"
                }
            };

            List <AccountRegion> readRegions = new List <AccountRegion>()
            {
                new AccountRegion()
                {
                    Name     = "East US",
                    Endpoint = $"{primaryRegionEndpoint}:443/"
                },
                new AccountRegion()
                {
                    Name     = "West US",
                    Endpoint = $"{secondaryRegionEndpiont}:443/"
                }
            };

            // Create a mock http handler to inject gateway responses.
            // MockBehavior.Strict ensures that only the mocked APIs get called
            mockHttpHandler = new Mock <IHttpHandler>(MockBehavior.Strict);
            MockSetupsHelper.SetupStrongAccountProperties(
                mockHttpClientHandler: mockHttpHandler,
                endpoint: globalEndpointUri.ToString(),
                accountName: accountName,
                writeRegions: writeRegion,
                readRegions: readRegions);

            MockSetupsHelper.SetupContainerProperties(
                mockHttpHandler: mockHttpHandler,
                regionEndpoint: primaryRegionEndpoint,
                databaseName: databaseName,
                containerName: containerName,
                containerRid: containerRid);

            MockSetupsHelper.SetupPartitionKeyRanges(
                mockHttpHandler: mockHttpHandler,
                regionEndpoint: primaryRegionEndpoint,
                containerResourceId: containerResourceId,
                partitionKeyRangeIds: out primaryRegionPartitionKeyRangeIds);

            MockSetupsHelper.SetupAddresses(
                mockHttpHandler: mockHttpHandler,
                partitionKeyRangeId: primaryRegionPartitionKeyRangeIds.First(),
                regionEndpoint: primaryRegionEndpoint,
                regionName: primaryRegionNameForUri,
                containerResourceId: containerResourceId,
                primaryReplicaUri: out primaryRegionprimaryReplicaUri);
        }
        internal static void SetupAddresses(
            Mock <IHttpHandler> mockHttpHandler,
            string partitionKeyRangeId,
            string regionEndpoint,
            string regionName,
            ResourceId containerResourceId,
            out TransportAddressUri primaryReplicaUri)
        {
            string basePhysicalUri = $"rntbd://cdb-ms-prod-{regionName}-fd4.documents.azure.com:14382/apps/9dc0394e-d25f-4c98-baa5-72f1c700bf3e/services/060067c7-a4e9-4465-a412-25cb0104cb58/partitions/2cda760c-f81f-4094-85d0-7bcfb2acc4e6/replicas/";

            // Use the partition key range id at the end of each replica id to avoid conflicts when setting up multiple partition key ranges
            List <Address> addresses = new List <Address>()
            {
                new Address()
                {
                    IsPrimary           = true,
                    PartitionKeyRangeId = partitionKeyRangeId,
                    PhysicalUri         = basePhysicalUri + $"13260893385949999{partitionKeyRangeId}p/",
                    Protocol            = "rntbd",
                    PartitionIndex      = "7718513@164605136"
                },
                new Address()
                {
                    IsPrimary           = false,
                    PartitionKeyRangeId = partitionKeyRangeId,
                    PhysicalUri         = basePhysicalUri + $"13260893385947000{partitionKeyRangeId}s/",
                    Protocol            = "rntbd",
                    PartitionIndex      = "7718513@164605136"
                },
                new Address()
                {
                    IsPrimary           = false,
                    PartitionKeyRangeId = partitionKeyRangeId,
                    PhysicalUri         = basePhysicalUri + $"13260893385947111{partitionKeyRangeId}s/",
                    Protocol            = "rntbd",
                    PartitionIndex      = "7718513@164605136"
                },
                new Address()
                {
                    IsPrimary           = false,
                    PartitionKeyRangeId = partitionKeyRangeId,
                    PhysicalUri         = basePhysicalUri + $"13260893385947222{partitionKeyRangeId}s/",
                    Protocol            = "rntbd",
                    PartitionIndex      = "7718513@164605136"
                }
            };

            primaryReplicaUri = new TransportAddressUri(new Uri(addresses.First(x => x.IsPrimary).PhysicalUri));

            string  databaseRid  = containerResourceId.DatabaseId.ToString();
            string  containerRid = containerResourceId.DocumentCollectionId.ToString();
            JObject jObject      = new JObject
            {
                { "_rid", containerRid },
                { "_count", addresses.Count },
                { "Addresss", JArray.FromObject(addresses) }
            };

            Uri addressUri = new Uri($"{regionEndpoint }//addresses/?$resolveFor=dbs{HttpUtility.UrlEncode($"/{databaseRid}/colls/{containerRid}/docs")}&$filter=protocol eq rntbd&$partitionKeyRangeIds={partitionKeyRangeId}");

            mockHttpHandler.Setup(x => x.SendAsync(It.Is <HttpRequestMessage>(x => x.RequestUri == addressUri), It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(jObject.ToString())
            }));
        }