Ejemplo n.º 1
0
        public void TestQueryAsync_ServerReturnsQuery_ReturnsListWithExternalApis()
        {
            var header1 = new ExternalApiHeader
            {
                HeaderId = 1,
                Name     = "Header1",
                Value    = "Api 0 header 0"
            };
            var header2 = new ExternalApiHeader
            {
                HeaderId = 2,
                Name     = "Header2",
                Value    = "Api 1 header 1"
            };
            var expectedExternalApis = new[]
            {
                new ExternalApi
                {
                    Name        = "TestExternalApi",
                    Description = "Description",
                    Headers     = new List <ExternalApiHeader>
                    {
                        header1
                    }
                },
                new ExternalApi
                {
                    Name        = "AnotherTestExternalApi",
                    Description = "Another Description",
                    Headers     = new List <ExternalApiHeader>
                    {
                        header2
                    }
                }
            };
            var mockedNfieldConnection = new Mock <INfieldConnectionClient>();
            var mockedHttpClient       = CreateHttpClientMock(mockedNfieldConnection);

            mockedHttpClient
            .Setup(client => client.GetAsync(new Uri(ServiceAddress, "externalapis/")))
            .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(expectedExternalApis))));

            var target = new NfieldExternalApisService();

            target.InitializeNfieldConnection(mockedNfieldConnection.Object);

            var actualExternalApis = target.QueryAsync().Result.ToArray();

            Assert.Equal(expectedExternalApis[0].Name, actualExternalApis[0].Name);
            Assert.Equal(expectedExternalApis[0].Description, actualExternalApis[0].Description);
            Assert.Equal(expectedExternalApis[0].Headers.First().HeaderId, header1.HeaderId);
            Assert.Equal(expectedExternalApis[0].Headers.First().Name, header1.Name);
            Assert.Equal(expectedExternalApis[0].Headers.First().Value, header1.Value);
            Assert.Equal(expectedExternalApis[1].Name, actualExternalApis[1].Name);
            Assert.Equal(expectedExternalApis[1].Description, actualExternalApis[1].Description);
            Assert.Equal(expectedExternalApis[1].Headers.First().HeaderId, header2.HeaderId);
            Assert.Equal(expectedExternalApis[1].Headers.First().Name, header2.Name);
            Assert.Equal(expectedExternalApis[1].Headers.First().Value, header2.Value);
            Assert.Equal(2, actualExternalApis.Count());
        }
Ejemplo n.º 2
0
        public void TestAddAsync_ServerAccepts_ReturnsExternalApi()
        {
            var header = new ExternalApiHeader
            {
                Name  = "Header",
                Value = "Api header"
            };
            var externalApi = new ExternalApi
            {
                Name        = "New ExternalApi",
                Description = "New Description",
                Headers     = new List <ExternalApiHeader>
                {
                    header
                }
            };
            var mockedNfieldConnection = new Mock <INfieldConnectionClient>();
            var mockedHttpClient       = CreateHttpClientMock(mockedNfieldConnection);
            var content = new StringContent(JsonConvert.SerializeObject(externalApi));

            mockedHttpClient
            .Setup(client => client.PostAsJsonAsync(new Uri(ServiceAddress, "externalapis/"), externalApi))
            .Returns(CreateTask(HttpStatusCode.OK, content));

            var target = new NfieldExternalApisService();

            target.InitializeNfieldConnection(mockedNfieldConnection.Object);

            var actual = target.AddAsync(externalApi).Result;

            Assert.Equal(externalApi.Name, actual.Name);
            Assert.Equal(externalApi.Description, actual.Description);
            Assert.Equal(externalApi.Headers.First().Name, header.Name);
            Assert.Equal(externalApi.Headers.First().Value, header.Value);
        }
Ejemplo n.º 3
0
        public void TestRemoveAsync_ServerRemovedExternalApi_DoesNotThrow()
        {
            const string ExternalApiName = "ApiToDelete";
            var          externalApi     = new ExternalApi {
                Name = ExternalApiName
            };
            var mockedNfieldConnection = new Mock <INfieldConnectionClient>();
            var mockedHttpClient       = CreateHttpClientMock(mockedNfieldConnection);

            mockedHttpClient
            .Setup(client => client.DeleteAsync(new Uri(ServiceAddress, "externalapis/" + ExternalApiName)))
            .Returns(CreateTask(HttpStatusCode.OK));

            var target = new NfieldExternalApisService();

            target.InitializeNfieldConnection(mockedNfieldConnection.Object);

            // assert: no throw
            target.RemoveAsync(externalApi).Wait();
        }
Ejemplo n.º 4
0
        public void TestUpdateAsync_ExternalApiExists_ReturnsExternalApi()
        {
            const string externalApiName = "ApiToUpdate";
            var          externalApi     = new ExternalApi
            {
                Name        = externalApiName,
                Description = "updated description"
            };
            var mockedNfieldConnection = new Mock <INfieldConnectionClient>();
            var mockedHttpClient       = CreateHttpClientMock(mockedNfieldConnection);

            mockedHttpClient
            .Setup(client => client.PutAsJsonAsync(new Uri(ServiceAddress, "externalapis/"), externalApi))
            .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(externalApi))));

            var target = new NfieldExternalApisService();

            target.InitializeNfieldConnection(mockedNfieldConnection.Object);

            var actual = target.UpdateAsync(externalApi).Result;

            Assert.Equal(externalApi.Description, actual.Description);
        }
Ejemplo n.º 5
0
        public void TestUpdateAsync_ExternalApiArgumentIsNull_ThrowsArgumentNullException()
        {
            var target = new NfieldExternalApisService();

            Assert.Throws <ArgumentNullException>(() => UnwrapAggregateException(target.UpdateAsync(null)));
        }