public async Task CanGetNetworksWithOkResponse()
        {
            var payload = @"{
                ""networks"": [
                    {
                        ""status"": ""ACTIVE"",
                        ""subnets"": [
                            ""d3839504-ec4c-47a4-b7c7-07af079a48bb""
                        ],
                        ""name"": ""myNetwork"",
                        ""router:external"": false,
                        ""tenant_id"": ""ffe683d1060449d09dac0bf9d7a371cd"",
                        ""admin_state_up"": true,
                        ""shared"": false,
                        ""id"": ""12345""
                    }
                ]
            }";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.NetworkServiceRestClient.Responses.Enqueue(restResp);

            var client = new NetworkServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetNetworks();

            Assert.IsNotNull(result);

            var networks = result.ToList();
            Assert.AreEqual(1, networks.Count());

            var network = networks.First();
            Assert.AreEqual("myNetwork", network.Name);
            Assert.AreEqual("12345", network.Id);
            Assert.AreEqual(NetworkStatus.Active, network.Status);
        }
        public async Task CannotAuthenticateWithNotFoundResponse()
        {
            var creds = GetValidCredentials();

            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.NotFound);
            this.RestClient.Response = restResp;

            var client = new IdentityServicePocoClient(creds, "Swift", CancellationToken.None, this.ServiceLocator);
            await client.Authenticate();
        }
        public async Task CanCreateFloatingIpWithCreatedResponse()
        {
            var payload = @"{
                ""floatingip"":
                    {
                        ""router_id"": ""fafac59b-a94a-4525-8700-f4f448e0ac97"",
                        ""status"": ""ACTIVE"",
                        ""tenant_id"": ""ffe683d1060449d09dac0bf9d7a371cd"",
                        ""floating_network_id"": ""3eaab3f7-d3f2-430f-aa73-d07f39aae8f4"",
                        ""fixed_ip_address"": ""10.0.0.2"",
                        ""floating_ip_address"": ""172.0.0.1"",
                        ""port_id"": ""9da94672-6e6b-446c-9579-3dd5484b31fd"",
                        ""id"": ""12345""
                    }
            }";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.Created);
            this.NetworkServiceRestClient.Responses.Enqueue(restResp);

            var client = new NetworkServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.CreateFloatingIp("12345");

            Assert.IsNotNull(result);

            Assert.AreEqual("12345", result.Id);
            Assert.AreEqual("172.0.0.1", result.FloatingIpAddress);
            Assert.AreEqual(FloatingIpStatus.Active, result.Status);
        }
        public async Task CanDeleteFloatingIpWithNoContentResponse()
        {
            
            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.NoContent);
            this.NetworkServiceRestClient.Responses.Enqueue(restResp);

            var client = new NetworkServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.DeleteFloatingIp("12345");
        }
        public async Task CanDeleteComputeServerMetadataWithOkResponse()
        {
            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.DeleteServerMetadata("1", "item1");
        }
        public async Task CanGetComputeImageWithOkResponse()
        {
            var created = DateTime.Parse("2014-05-30T16:56:32Z").ToUniversalTime();
            var updated = DateTime.Parse("2014-06-30T16:56:32Z").ToUniversalTime();
            var payload = @"{
                                    ""image"" : {
                                        ""name"": ""image1"",
                                        ""status"": ""ACTIVE"",
                                        ""updated"": ""2014-06-30T16:56:32Z"",
                                        ""created"": ""2014-05-30T16:56:32Z"",
                                        ""minRam"": 512,
                                        ""minDisk"": 10,
                                        ""progress"": 100,
                                        ""links"": [
                                            {
                                                ""href"": ""http://someuri.com/v2/images/12345"",
                                                ""rel"": ""self""
                                            },
                                            {
                                                ""href"": ""http://someuri.com/images/12345"",
                                                ""rel"": ""bookmark""
                                            }
                                        ],
                                        ""id"": ""12345""
                                    }
                                }";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetImage("12345");

            Assert.IsNotNull(result);
            Assert.AreEqual("image1", result.Name);
            Assert.AreEqual("ACTIVE", result.Status);
            Assert.AreEqual("12345", result.Id);
            Assert.AreEqual(512, result.MinimumRamSize);
            Assert.AreEqual(10, result.MinimumDiskSize);
            Assert.AreEqual(100, result.UploadProgress);
            Assert.AreEqual(created.ToLongTimeString(), result.CreateDate.ToLongTimeString());
            Assert.AreEqual(updated.ToLongTimeString(), result.LastUpdated.ToLongTimeString());
            Assert.AreEqual(new Uri("http://someuri.com/images/12345"), result.PermanentUri);
            Assert.AreEqual(new Uri("http://someuri.com/v2/images/12345"), result.PublicUri);
        }
        public async Task CanGetComputeFlavorsWithNonAuthoritativeResponse()
        {
            var payload = @"{
                            ""flavors"": [
                                {
                                    ""id"": ""1"",
                                    ""links"": [
                                        {
                                            ""href"": ""http://someuri.com/v2/flavors/1"",
                                            ""rel"": ""self""
                                        },
                                        {
                                            ""href"": ""http://someuri.com/flavors/1"",
                                            ""rel"": ""bookmark""
                                        }
                                    ],
                                    ""name"": ""m1.tiny""
                                }
                            ]
                        }";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.NonAuthoritativeInformation);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetFlavors();

            Assert.IsNotNull(result);

            var flavors = result.ToList();
            Assert.AreEqual(1, flavors.Count());

            var flavor = flavors.First();
            Assert.AreEqual("m1.tiny", flavor.Name);
            Assert.AreEqual("1", flavor.Id);
            Assert.AreEqual(new Uri("http://someuri.com/v2/flavors/1"), flavor.PublicUri);
            Assert.AreEqual(new Uri("http://someuri.com/flavors/1"), flavor.PermanentUri);
        }
        public async Task CanUpdateComputeServerMetadataWithOkResponse()
        {
            var metadata = new Dictionary<string, string>() { { "item1", "value1" }, { "item2", "value2" } };

            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.UpdateServerMetadata("1", metadata);
        }
        public async Task CanGetComputeKeyPairsWithOkResponse()
        {
            var content = TestHelper.CreateStream(KeyPairsPayload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetKeyPairs();

            Assert.IsNotNull(result);

            var pairs = result.ToList();
            Assert.AreEqual(1, pairs.Count());

            var keyPair = pairs.First();
            Assert.AreEqual("MyKey", keyPair.Name);
            Assert.AreEqual("ABCDEF", keyPair.PublicKey);
            Assert.AreEqual("12345", keyPair.Fingerprint);
        }
        public async Task CanGetComputeKeyPairWithNonAuthoritativeResponse()
        {
            var content = TestHelper.CreateStream(KeyPairPayload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.NonAuthoritativeInformation);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var keyPair = await client.GetKeyPair("MyKey");

            Assert.IsNotNull(keyPair);
            Assert.AreEqual("MyKey", keyPair.Name);
            Assert.AreEqual("ABCDEF", keyPair.PublicKey);
            Assert.AreEqual("12345", keyPair.Fingerprint);
        }
        public async Task CanGetComputeImagesWithOkResponse()
        {
            var payload = @"{
                            ""images"": [
                                {
                                    ""id"": ""12345"",
                                    ""links"": [
                                        {
                                            ""href"": ""http://someuri.com/v2/images/12345"",
                                            ""rel"": ""self""
                                        },
                                        {
                                            ""href"": ""http://someuri.com/images/12345"",
                                            ""rel"": ""bookmark""
                                        }
                                    ],
                                    ""name"": ""image1""
                                }
                            ]
                        }";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetImages();

            Assert.IsNotNull(result);

            var images = result.ToList();
            Assert.AreEqual(1, images.Count());

            var image = images.First();
            Assert.AreEqual("image1", image.Name);
            Assert.AreEqual("12345", image.Id);
            Assert.AreEqual(new Uri("http://someuri.com/v2/images/12345"), image.PublicUri);
            Assert.AreEqual(new Uri("http://someuri.com/images/12345"), image.PermanentUri);
        }
        public async Task ExceptionthrownWhenGettingAStorageFolderAndServerError()
        {
            var containerName = "TestContainer";
            var fodlerName = "a/b/b/";

            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.InternalServerError);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.GetStorageFolder(containerName, fodlerName);
        }
        public async Task ExceptionthrownWhenGettingAStorageFolderThatDoesNotExistAndCannotBeInferred()
        {
            var containerName = "TestContainer";
            var folderName = "a/b/c/";
            var headers = new HttpHeadersAbstraction()
            {
                {"X-Container-Bytes-Used", "1234"},
                {"X-Container-Object-Count", "1"}
            };

            var payload = @"[]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClientFactory().Create(GetValidContext(), this.ServiceLocator) as StorageServicePocoClient;
            var resp = await client.GetStorageFolder(containerName, folderName);
        }
        public async Task CanGetStorageFolderWithOkResponseAndSubFolders()
        {
            var containerName = "TestContainer";
            var folderName = "a/b/c/";
            var headers = new HttpHeadersAbstraction()
            {
                {"X-Container-Bytes-Used", "1234"},
                {"X-Container-Object-Count", "1"}
            };

            var payload = @"[
                                {
                                    ""hash"": ""d41d8cd98f00b204e9800998ecf8427e"",
                                    ""last_modified"": ""2014-03-07T21:31:31.588170"",
                                    ""bytes"": 0,
                                    ""name"": ""a/b/c/"",
                                    ""content_type"": ""application/octet-stream""
                                },
                                {
                                    ""hash"": ""d41d8cd98f00b204e9800998ecf8427e"",
                                    ""last_modified"": ""2014-03-07T21:31:31.588170"",
                                    ""bytes"": 0,
                                    ""name"": ""a/b/c/BLAH"",
                                    ""content_type"": ""application/octet-stream""
                                },
                                {
                                        ""subdir"": ""a/b/c/d/""
                                },
                                {
                                        ""subdir"": ""a/b/c/x/""
                                }
                            ]";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, headers, HttpStatusCode.OK);
            this.StorageServiceRestClient.Responses.Enqueue(restResp);

            var client = new StorageServicePocoClientFactory().Create(GetValidContext(), this.ServiceLocator) as StorageServicePocoClient;
            var resp = await client.GetStorageFolder(containerName, folderName);

            Assert.AreEqual("c", resp.Name);
            Assert.AreEqual("a/b/c/", resp.FullName);
            Assert.AreEqual(1, resp.Objects.Count);
            Assert.AreEqual(2, resp.Folders.Count);

            var obj = resp.Objects.First();
            Assert.AreEqual("a/b/c/BLAH", obj.FullName);

            var dNode = resp.Folders.First(f => f.FullName == "a/b/c/d/");
            var xNode = resp.Folders.First(f => f.FullName == "a/b/c/x/");

            Assert.AreEqual("d", dNode.Name);
            Assert.AreEqual(0, dNode.Folders.Count);
            Assert.AreEqual(0, dNode.Objects.Count);

            Assert.AreEqual("x", xNode.Name);
            Assert.AreEqual(0, xNode.Folders.Count);
            Assert.AreEqual(0, xNode.Objects.Count);
        }
        public async Task CanGetComputeServerMetadataWithOkResponse()
        {
            var metadata = new Dictionary<string, string>() { { "item1", "value1" }, { "item2", "value2" } };
            var content = TestHelper.CreateStream(GenerateMetadataPayload(metadata));

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var respData = await client.GetServerMetadata("1");

            Assert.AreEqual(2, respData.Count);
            Assert.AreEqual("value1", respData["item1"]);
            Assert.AreEqual("value2", respData["item2"]);
        }
        public async Task ExceptionthrownWhennAssigningAFloatingIpAndServerError()
        {
            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.InternalServerError);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.AssignFloatingIp("12345", "172.0.0.1");
        }
        public async Task CanCreateComputeServerWithOkResponse()
        {
            var serverId = "98765";
            var keyName = "MyKey";
            var publicUrl = "http://15.125.87.81:8774/v2/ffe683d1060449d09dac0bf9d7a371cd/servers/" + serverId;
            var permUrl = "http://15.125.87.81:8774/ffe683d1060449d09dac0bf9d7a371cd/servers/" + serverId;
            var adminPassword = "******";
            var serverFixture = @"{{
                ""server"": {{
                    ""security_groups"": [
                        {{
                            ""name"": ""default""
                        }},
                        {{
                            ""name"": ""MyGroup""
                        }}
                    ],
                    ""OS-DCF:diskConfig"": ""MANUAL"",
                    ""id"": ""{0}"",
                    ""links"": [
                        {{
                            ""href"": ""{1}"",
                            ""rel"": ""self""
                        }},
                        {{
                            ""href"": ""{2}"",
                            ""rel"": ""bookmark""
                        }}
                    ],
                    ""adminPass"": ""{3}""
                }}
            }}";

            var payload = string.Format(serverFixture, serverId, publicUrl, permUrl, adminPassword);
            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.CreateServer("MyServer", "12345", "1", "54321", keyName, new List<string>() { "MyGroup" });

            Assert.IsNotNull(result);

            Assert.AreEqual(serverId, result.Id);
            Assert.AreEqual(adminPassword, result.AdminPassword);
            Assert.AreEqual(new Uri(publicUrl), result.PublicUri);
            Assert.AreEqual(new Uri(permUrl), result.PermanentUri);
        }
        public async Task ExceptionthrownWhenUpdatingComputeServerMetadataAndServerError()
        {
            var metadata = new Dictionary<string, string>() { { "item1", "value1" }, { "item2", "value2" } };

            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.InternalServerError);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.UpdateServerMetadata("12345", metadata);
        }
        public async Task ExceptionthrownWhenCreatingAComputeServerAndServerError()
        {
            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.InternalServerError);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.CreateServer("MyServer", "12345", "1", "54321", string.Empty, new List<string>() { "MyGroup" });
        }
        public async Task ExceptionthrownWhenDeletingComputeServerMetadataAndServerError()
        {
            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.InternalServerError);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.DeleteServerMetadata("1", "item1");
        }
        public async Task CanGetComputeServersWithNonAuthoritativeResponse()
        {
            var content = TestHelper.CreateStream(ServersPayload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.NonAuthoritativeInformation);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetServers();

            Assert.IsNotNull(result);

            var servers = result.ToList();
            Assert.AreEqual(1, servers.Count());

            var server = servers.First();
            Assert.AreEqual("server1", server.Name);
            Assert.AreEqual("1", server.Id);
            Assert.AreEqual(new Uri("http://someuri.com/v2/servers/1"), server.PublicUri);
            Assert.AreEqual(new Uri("http://someuri.com/servers/1"), server.PermanentUri);
        }
        public async Task CanGetComputeFlavorWithOkResponse()
        {
            var payload = @"{
                            ""flavor"": {
                                ""name"": ""m1.tiny"",
                                ""id"": ""1"",
                                ""links"": [
                                    {
                                        ""href"": ""http://someuri.com/v2/flavors/1"",
                                        ""rel"": ""self""
                                    },
                                    {
                                        ""href"": ""http://someuri.com/flavors/1"",
                                        ""rel"": ""bookmark""
                                    }
                                ],
                                ""ram"" : 512,
                                ""vcpus"": 2,
                                ""disk"": 10
                            }
                        }";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetFlavor("1");

            Assert.IsNotNull(result);
            Assert.AreEqual("m1.tiny", result.Name);
            Assert.AreEqual("1", result.Id);
            Assert.AreEqual("512", result.Ram);
            Assert.AreEqual("2", result.Vcpus);
            Assert.AreEqual("10", result.Disk);
            Assert.AreEqual(new Uri("http://someuri.com/v2/flavors/1"), result.PublicUri);
            Assert.AreEqual(new Uri("http://someuri.com/flavors/1"), result.PermanentUri);
        }
        public async Task CanGetComputeServerWithNonAuthoritativeResponse()
        {
            var content = TestHelper.CreateStream(ServerPayload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.NonAuthoritativeInformation);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            var result = await client.GetServer("1");

            Assert.IsNotNull(result);
            Assert.AreEqual("wfoley1", result.Name);
            Assert.AreEqual("1", result.Id);
            Assert.AreEqual(ComputeServerStatus.Active, result.Status);
            Assert.AreEqual(0, result.Progress);
            Assert.AreEqual(new Uri("http://someuri.com/v2/servers/1"), result.PublicUri);
            Assert.AreEqual(new Uri("http://someuri.com/servers/1"), result.PermanentUri);
        }
        public async Task ExceptionthrownWhenCreatingFloatingIpAndNotAuthed()
        {
            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.Unauthorized);
            this.NetworkServiceRestClient.Responses.Enqueue(restResp);

            var client = new NetworkServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.CreateFloatingIp("12345");
        }
        public async Task CannotGetComputeServerWithNoContent()
        {

            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.NoContent);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.GetServer("1");
        }
        public async Task ExceptionthrownWhenDeletingFloatingIpAndServerError()
        {
            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.InternalServerError);
            this.NetworkServiceRestClient.Responses.Enqueue(restResp);

            var client = new NetworkServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.DeleteFloatingIp("12345");
        }
        public async Task ExceptionthrownWhenGettingAComputeServerAndNotAuthed()
        {
            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.Unauthorized);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.GetServer("1");
        }
        public async Task AuthenticationDoesNotResolveRegionIfCredRegionSupplied()
        {
            var expectedRegion = "Some region";
            var creds = GetValidCredentials();
            creds.SetRegion(expectedRegion);
            var payload = @"{
                                                ""access"": {
                                                    ""token"": {
                                                        ""expires"": ""2014-03-18T10:59:46.355Z"",
                                                        ""id"": ""HPAuth10_af3d1bfe456d18e8d4793e54922f839fa051d9f60f115aca52c9a44f9e3d96fb"",
                                                        ""tenant"": {
                                                            ""id"": ""10244656540440"",
                                                            ""name"": ""10255892528404-Project""
                                                        }
                                                    },
                                                    ""serviceCatalog"":[{
                                                        ""name"": ""Object Storage"",
                                                        ""type"": ""object-store"",
                                                        ""endpoints"": [
                                                            {
                                                                ""tenantId"": ""10244656540440"",
                                                                ""publicURL"": ""https://region-a.geo-1.objects.hpcloudsvc.com/v1/10244656540440"",
                                                                ""region"": ""region-a.geo-1"",
                                                                ""versionId"": ""1.0"",
                                                                ""versionInfo"": ""https://region-a.geo-1.objects.hpcloudsvc.com/v1.0/"",
                                                                ""versionList"": ""https://region-a.geo-1.objects.hpcloudsvc.com""
                                                            },
                                                            {
                                                                ""tenantId"": ""10244656540440"",
                                                                ""publicURL"": ""https://region-b.geo-1.objects.hpcloudsvc.com:443/v1/10244656540440"",
                                                                ""region"": ""region-b.geo-1"",
                                                                ""versionId"": ""1"",
                                                                ""versionInfo"": ""https://region-b.geo-1.objects.hpcloudsvc.com:443/v1/"",
                                                                ""versionList"": ""https://region-b.geo-1.objects.hpcloudsvc.com:443""
                                                            }
                                                        ]
                                                    }]
                                                }
                                            }";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.NonAuthoritativeInformation);
            this.RestClient.Response = restResp;

            var client = new IdentityServicePocoClient(creds, "Swift", CancellationToken.None, this.ServiceLocator);
            var result = await client.Authenticate();

            Assert.AreEqual(expectedRegion, result.Region);
        }
        public async Task CanDAssignFloatingIpWithAcceptedResponse()
        {
            var restResp = new HttpResponseAbstraction(new MemoryStream(), new HttpHeadersAbstraction(), HttpStatusCode.Accepted);
            this.ComputeServiceRestClient.Responses.Enqueue(restResp);

            var client = new ComputeServicePocoClient(GetValidContext(), this.ServiceLocator);
            await client.AssignFloatingIp("12345", "172.0.0.1");
        }
        public async Task CanAuthenticateWithOkResponse()
        {
            var creds = GetValidCredentials();

            var expectedToken = "HPAuth10_af3d1bfe456d18e8d4793e54922f839fa051d9f60f115aca52c9a44f9e3d96fb";

            var payload = @"{
                                                ""access"": {
                                                    ""token"": {
                                                        ""expires"": ""2014-03-18T10:59:46.355Z"",
                                                        ""id"": ""HPAuth10_af3d1bfe456d18e8d4793e54922f839fa051d9f60f115aca52c9a44f9e3d96fb"",
                                                        ""tenant"": {
                                                            ""id"": ""10244656540440"",
                                                            ""name"": ""10255892528404-Project""
                                                        }
                                                    },
                                                    ""serviceCatalog"":[{
                                                        ""name"": ""Object Storage"",
                                                        ""type"": ""object-store"",
                                                        ""endpoints"": [
                                                            {
                                                                ""tenantId"": ""10244656540440"",
                                                                ""publicURL"": ""https://region-a.geo-1.objects.hpcloudsvc.com/v1/10244656540440"",
                                                                ""region"": ""region-a.geo-1"",
                                                                ""versionId"": ""1.0"",
                                                                ""versionInfo"": ""https://region-a.geo-1.objects.hpcloudsvc.com/v1.0/"",
                                                                ""versionList"": ""https://region-a.geo-1.objects.hpcloudsvc.com""
                                                            },
                                                            {
                                                                ""tenantId"": ""10244656540440"",
                                                                ""publicURL"": ""https://region-b.geo-1.objects.hpcloudsvc.com:443/v1/10244656540440"",
                                                                ""region"": ""region-b.geo-1"",
                                                                ""versionId"": ""1"",
                                                                ""versionInfo"": ""https://region-b.geo-1.objects.hpcloudsvc.com:443/v1/"",
                                                                ""versionList"": ""https://region-b.geo-1.objects.hpcloudsvc.com:443""
                                                            }
                                                        ]
                                                    }]
                                                }
                                            }";

            var content = TestHelper.CreateStream(payload);

            var restResp = new HttpResponseAbstraction(content, new HttpHeadersAbstraction(), HttpStatusCode.OK);
            this.RestClient.Response = restResp;

            var client =
                new IdentityServicePocoClientFactory().Create(creds, "Swift", CancellationToken.None, this.ServiceLocator) as
                    IdentityServicePocoClient;
            var result = await client.Authenticate();

            Assert.IsNotNull(result);
            Assert.AreEqual(creds.UserName, result.UserName);
            Assert.AreEqual(creds.Password, result.Password);
            Assert.AreEqual(creds.TenantId, result.TenantId);
            Assert.AreEqual(expectedToken, result.AccessTokenId);
            Assert.AreEqual(1,result.ServiceCatalog.Count());
        }
        public async Task <IHttpResponseAbstraction> SendAsync()
        {
            var requestMessage = new HttpRequestMessage {
                Method = this.Method, RequestUri = this.Uri
            };

            if (this.Method == HttpMethod.Post || this.Method == HttpMethod.Put)
            {
                if (this.Content != null)
                {
                    requestMessage.Content = new StreamContent(this.Content);
                    if (this.ContentType != string.Empty)
                    {
                        requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(this.ContentType);
                    }
                }
            }

            requestMessage.Headers.Clear();
            foreach (var header in this.Headers)
            {
                requestMessage.Headers.Add(header.Key, header.Value);
            }

            var startTime = DateTime.Now;

            try
            {
                var result = await this._client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, this._cancellationToken);

                var headers = new HttpHeadersAbstraction(result.Headers);

                Stream content = null;
                if (result.Content != null)
                {
                    headers.AddRange(result.Content.Headers);
                    content = this.WaitForResult(result.Content.ReadAsStreamAsync(), new TimeSpan(0, 0, 0, 0, int.MaxValue));
                }

                var retval = new HttpResponseAbstraction(content, headers, result.StatusCode);

                //TODO: Add logging code

                return(retval);
            }
            catch (Exception ex)
            {
                //TODO: Add logging code

                var tcex = ex as TaskCanceledException;
                if (tcex == null)
                {
                    throw;
                }

                if (this._cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException("The operation was canceled by user request.", tcex, this._cancellationToken);
                }

                if (DateTime.Now - startTime > this.Timeout)
                {
                    throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "The task failed to complete in the given timeout period ({0}).", this.Timeout));
                }

                throw;
            }
        }