public async Task DeleteAsyncTest()
        {
            var collectionId = this.rand.NextString();
            var key          = this.rand.NextString();

            var method   = HttpMethod.Delete;
            var response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.OK,
                IsSuccessStatusCode = true,
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(
                       It.IsAny <IHttpRequest>(),
                       It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            await this.client.DeleteAsync(collectionId, key);

            this.mockHttpClient
            .Verify(
                x => x.SendAsync(
                    It.Is <IHttpRequest>(r => r.Check($"{MockServiceUri}/collections/{collectionId}/values/{key}")),
                    It.Is <HttpMethod>(m => m == method)),
                Times.Once);
        }
        public async Task ProcessNoModelRequest()
        {
            string path = this.rand.NextString();
            string url  = $"{MockServiceUri}/{path}";

            HttpMethod method = HttpMethod.Get;

            HttpResponse response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.OK,
                IsSuccessStatusCode = true,
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(It.IsAny <IHttpRequest>(), It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            await this.externalRequestHelper.ProcessRequestAsync(method, url);

            this.mockHttpClient
            .Verify(
                x => x.SendAsync(
                    It.Is <IHttpRequest>(r => r.Check(url)),
                    It.Is <HttpMethod>(r => r == method)),
                Times.Once);
        }
        public async Task GetHealthyStatusAsyncTest()
        {
            var healthyStatus = new StatusResultServiceModel(true, "all good");
            var response      = new HttpResponse
            {
                StatusCode          = HttpStatusCode.OK,
                IsSuccessStatusCode = true,
                Content             = JsonConvert.SerializeObject(healthyStatus),
            };

            this.mockHttpClient
            .Setup(
                x => x.SendAsync(
                    It.IsAny <IHttpRequest>(),
                    It.Is <HttpMethod>(method => method == HttpMethod.Get)))
            .ReturnsAsync(response);

            var result = await this.client.StatusAsync();

            this.mockHttpClient
            .Verify(
                x => x.SendAsync(
                    It.Is <IHttpRequest>(r => r.Check($"{MockServiceUri}/status")),
                    It.Is <HttpMethod>(method => method == HttpMethod.Get)),
                Times.Once);

            Assert.Equal(result.IsHealthy, healthyStatus.IsHealthy);
            Assert.Equal(result.Message, healthyStatus.Message);
        }
        public async Task DeleteRule_QueriesRuleCountAndLogs()
        {
            // Arrange
            ValueListApiModel fakeRules = new ValueListApiModel();

            fakeRules.Items = new List <ValueApiModel>();
            fakeRules.Items.Add(this.CreateFakeRule("rule1"));
            fakeRules.Items.Add(this.CreateFakeRule("rule2"));
            this.storageAdapter.Setup(x => x.GetAllAsync(It.IsAny <string>())).Returns(Task.FromResult(fakeRules));
            IHttpResponse fakeOkResponse = new HttpResponse(HttpStatusCode.OK, string.Empty, null);

            this.httpClientMock.Setup(x => x.PostAsync(It.IsAny <HttpRequest>())).ReturnsAsync(fakeOkResponse);

            Rule test = new Rule
            {
                Enabled = true,
                Deleted = false,
            };

            this.SetUpStorageAdapterGet(test);

            // Act
            await this.rules.DeleteAsync("id");

            // Assert
            this.httpClientMock.Verify(x => x.PostAsync(It.IsAny <HttpRequest>()), Times.Exactly(1));

            this.asaManager
            .Verify(
                x => x.BeginRulesConversionAsync(),
                Times.Once);
        }
예제 #5
0
        public async Task GetAllowedActions_ReturnValues()
        {
            var userObjectId = this.rand.NextString();
            var roles        = new List <string> {
                "Admin"
            };
            var allowedActions = new List <string> {
                "CreateDeviceGroups", "UpdateDeviceGroups"
            };

            var method   = HttpMethod.Post;
            var response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.OK,
                IsSuccessStatusCode = true,
                Content             = JsonConvert.SerializeObject(allowedActions),
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(
                       It.IsAny <IHttpRequest>(),
                       It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            var result = await this.client.GetAllowedActionsAsync(userObjectId, roles);

            this.mockHttpClient
            .Verify(
                x => x.SendAsync(
                    It.Is <IHttpRequest>(r => r.Check($"{MockServiceUri}/users/{userObjectId}/allowedActions")),
                    It.Is <HttpMethod>(m => m == method)),
                Times.Once);

            Assert.Equal(allowedActions, result);
        }
        public async Task GetAllAsyncTest()
        {
            var collectionId = this.rand.NextString();
            var models       = new[]
            {
                new ValueApiModel
                {
                    Key  = this.rand.NextString(),
                    Data = this.rand.NextString(),
                    ETag = this.rand.NextString(),
                },
                new ValueApiModel
                {
                    Key  = this.rand.NextString(),
                    Data = this.rand.NextString(),
                    ETag = this.rand.NextString(),
                },
                new ValueApiModel
                {
                    Key  = this.rand.NextString(),
                    Data = this.rand.NextString(),
                    ETag = this.rand.NextString(),
                },
            };
            var method   = HttpMethod.Get;
            var response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.OK,
                IsSuccessStatusCode = true,
                Content             = JsonConvert.SerializeObject(new ValueListApiModel {
                    Items = models
                }),
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(
                       It.IsAny <IHttpRequest>(),
                       It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            var result = await this.client.GetAllAsync(collectionId);

            this.mockHttpClient
            .Verify(
                x => x.SendAsync(
                    It.Is <IHttpRequest>(r => r.Check($"{MockServiceUri}/collections/{collectionId}/values")),
                    It.Is <HttpMethod>(m => m == method)),
                Times.Once);

            Assert.Equal(result.Items.Count(), models.Length);
            foreach (var item in result.Items)
            {
                var model = models.Single(m => m.Key == item.Key);
                Assert.Equal(model.Data, item.Data);
                Assert.Equal(model.ETag, item.ETag);
            }
        }
        public async Task GetAsyncNotFoundTest()
        {
            var collectionId = this.rand.NextString();
            var key          = this.rand.NextString();

            var method   = HttpMethod.Get;
            var response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.NotFound,
                IsSuccessStatusCode = false,
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(
                       It.IsAny <IHttpRequest>(),
                       It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            await Assert.ThrowsAsync <ResourceNotFoundException>(async() =>
                                                                 await this.client.GetAsync(collectionId, key));
        }
        public async Task UpdateAsyncTest()
        {
            var collectionId = this.rand.NextString();
            var key          = this.rand.NextString();
            var data         = this.rand.NextString();
            var etagOld      = this.rand.NextString();
            var etagNew      = this.rand.NextString();

            var method   = HttpMethod.Put;
            var response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.OK,
                IsSuccessStatusCode = true,
                Content             = JsonConvert.SerializeObject(new ValueApiModel
                {
                    Key  = key,
                    Data = data,
                    ETag = etagNew,
                }),
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(
                       It.IsAny <IHttpRequest>(),
                       It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            var result = await this.client.UpdateAsync(collectionId, key, data, etagOld);

            this.mockHttpClient
            .Verify(
                x => x.SendAsync(
                    It.Is <IHttpRequest>(r => r.Check <ValueApiModel>($"{MockServiceUri}/collections/{collectionId}/values/{key}", m => m.Data == data && m.ETag == etagOld)),
                    It.Is <HttpMethod>(m => m == method)),
                Times.Once);

            Assert.Equal(result.Key, key);
            Assert.Equal(result.Data, data);
            Assert.Equal(result.ETag, etagNew);
        }
예제 #9
0
        public async Task GetToken_ReturnsValue()
        {
            // Arrange
            var token = new TokenApiModel()
            {
                AccessToken     = "1234ExampleToken",
                AccessTokenType = "Bearer",
                Audience        = "https://management.azure.com/",
                Authority       = "https://login.microsoftonline.com/12345/",
            };

            var method   = HttpMethod.Get;
            var response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.OK,
                IsSuccessStatusCode = true,
                Content             = JsonConvert.SerializeObject(token),
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(
                       It.IsAny <IHttpRequest>(),
                       It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            // Act
            var result = await this.client.GetTokenAsync();

            // Assert
            this.mockHttpClient
            .Verify(
                x => x.SendAsync(
                    It.Is <IHttpRequest>(r => r.Check($"{MockServiceUri}/users/default/token")),
                    It.Is <HttpMethod>(m => m == method)),
                Times.Once);

            Assert.Equal(token.AccessToken, result);
        }
        public async Task UpdateAsyncConflictTest()
        {
            var collectionId = this.rand.NextString();
            var key          = this.rand.NextString();
            var data         = this.rand.NextString();
            var etag         = this.rand.NextString();

            var method   = HttpMethod.Put;
            var response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.Conflict,
                IsSuccessStatusCode = false,
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(
                       It.IsAny <IHttpRequest>(),
                       It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            await Assert.ThrowsAsync <ConflictingResourceException>(async() =>
                                                                    await this.client.UpdateAsync(collectionId, key, data, etag));
        }
예제 #11
0
        public async Task GetAllowedActions_ReturnError()
        {
            var userObjectId = this.rand.NextString();
            var roles        = new List <string> {
                "Unknown"
            };

            var method   = HttpMethod.Post;
            var response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.InternalServerError,
                IsSuccessStatusCode = false,
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(
                       It.IsAny <IHttpRequest>(),
                       It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            await Assert.ThrowsAsync <HttpRequestException>(async() =>
                                                            await this.client.GetAllowedActionsAsync(userObjectId, roles));
        }
        public async Task ProcessModelRequest()
        {
            string path = this.rand.NextString();
            string url  = $"{MockServiceUri}/{path}";

            string value = this.rand.NextString();
            ExternalRequestModel content = new ExternalRequestModel
            {
                Value = value,
            };

            HttpMethod method = HttpMethod.Get;

            HttpResponse response = new HttpResponse
            {
                StatusCode          = HttpStatusCode.OK,
                IsSuccessStatusCode = true,
                Content             = JsonConvert.SerializeObject(content),
            };

            this.mockHttpClient
            .Setup(x => x.SendAsync(
                       It.IsAny <IHttpRequest>(),
                       It.IsAny <HttpMethod>()))
            .ReturnsAsync(response);

            ExternalRequestModel processedResponse = await this.externalRequestHelper.ProcessRequestAsync(method, url, content);

            this.mockHttpClient
            .Verify(
                x => x.SendAsync(
                    It.Is <IHttpRequest>(r => r.Check(url)),
                    It.Is <HttpMethod>(r => r == method)),
                Times.Once);

            Assert.Equal(processedResponse.Value, value);
        }