Beispiel #1
0
        public async Task ViewQueryParametersAreApplied()
        {
            UserGenerator.RegisterAndLoginUserWithRole(Role.Analyst, adminDataApiClient, out var dataApiClient);

            // Generate data for test
            var searchDataCount = 5;
            var limit           = 2; // This is our view parameter. If view parameters work as expected, only 2 of the 5 search objects are returned.

            Assume.That(searchDataCount, Is.GreaterThan(limit));
            var searchData = SearchDataGenerator.GenerateAndSubmitSearchData(searchDataCount, dataApiClient);

            ViewInformation viewInformation = null;

            try
            {
                // Setup
                var viewQuery = $"SELECT Data.Id AS Id FROM {nameof(UnitTestSearchObject)} LIMIT {{limit}}";
                var expires   = DateTime.UtcNow.AddMinutes(3);
                Assert.That(() => viewInformation = dataApiClient.CreateViewAsync(viewQuery, expires).Result, Throws.Nothing);
                Assert.That(viewInformation, Is.Not.Null);

                // Test 1: BadRequest response if no parameters provided
                var resultFormat = ResultFormat.Json;
                AssertStatusCode(
                    () => dataApiClient.GetViewAsync(viewInformation.ViewId, resultFormat).Wait(),
                    HttpStatusCode.BadRequest);

                // Test 2: View is correctly generated when all parameters are provided and parameters are correctly applied
                var viewParameters = new Dictionary <string, string> {
                    { "limit", limit.ToString() }
                };
                Stream viewResult = null;
                Assert.That(
                    async() => viewResult = await dataApiClient.GetViewAsync(viewInformation.ViewId, resultFormat, viewParameters),
                    Throws.Nothing);
                var resultTable = await Client.Serialization.SeachResultStreamExtensions.ReadAllSearchResultsAsync(viewResult);

                Assert.That(resultTable.Count, Is.EqualTo(limit));
            }
            finally
            {
                SearchDataGenerator.DeleteData(searchData, dataApiClient);
                if (viewInformation != null)
                {
                    dataApiClient.DeleteViewAsync(viewInformation.ViewId).Wait();
                }
                UserGenerator.DeleteUser(dataApiClient);
            }
        }
Beispiel #2
0
        public void CollectionPermissionsAreUsedForProtectedCollection()
        {
            UserGenerator.RegisterAndLoginUserWithRole(Role.Analyst, adminDataApiClient, out var dataApiClient);

            var dataType = nameof(UnitTestSearchObject);

            adminDataApiClient.SetCollectionOptions(
                new CollectionOptions(dataType)
            {
                IsProtected = true
            });
            var submittedData = SearchDataGenerator.GenerateAndSubmitSearchData(1, adminDataApiClient);

            try
            {
                // Make sure our user is not already permitted to access data from this collection
                AssertStatusCode(
                    () => dataApiClient.GetManyAsync <UnitTestSearchObject>("").Wait(),
                    HttpStatusCode.Unauthorized, "Get many with user without roles");

                adminDataApiClient.AddCollectionRoleToUser(dataApiClient.LoggedInUsername, Role.Analyst, dataType);

                // Check that user has access
                List <UnitTestSearchObject> searchObjects = null;
                AssertStatusCode(
                    () => searchObjects = dataApiClient.GetManyAsync <UnitTestSearchObject>("").Result,
                    HttpStatusCode.OK, "Get many with user with added role");
                Assert.That(searchObjects.Count, Is.EqualTo(1));

                // Remove access
                adminDataApiClient.RemoveCollectionRoleFromUser(dataApiClient.LoggedInUsername, Role.Analyst, dataType);

                // Check that user no longer has access
                AssertStatusCode(
                    () => dataApiClient.GetManyAsync <UnitTestSearchObject>("").Wait(),
                    HttpStatusCode.Unauthorized, "Get many with user with removed role");
            }
            finally
            {
                adminDataApiClient.SetCollectionOptions(
                    new CollectionOptions(dataType)
                {
                    IsProtected = false
                });
                SearchDataGenerator.DeleteData(submittedData, adminDataApiClient);
                UserGenerator.DeleteUser(dataApiClient);
            }
        }
Beispiel #3
0
        public void NotificationsAreCreatedForSubscribedData()
        {
            // Setup
            AssertStatusCode(
                () => SubscribeToUnitTestSearchObject(analystDataApiClient, DataModificationType.Created),
                HttpStatusCode.OK, "Subscribe");
            var searchObjectCount = 2;
            var submittedData     = SearchDataGenerator.GenerateAndSubmitSearchData(searchObjectCount, analystDataApiClient);

            // Test
            try
            {
                List <SubscriptionNotification> notifications = null;
                AssertStatusCode(
                    () => notifications = analystDataApiClient.GetSubscribedObjects().Result,
                    HttpStatusCode.OK, "Get notifications (first time)");
                Assert.That(notifications.Count, Is.EqualTo(searchObjectCount), "Notification count (first time)");

                // Test that notifications
                AssertStatusCode(
                    () => notifications = analystDataApiClient.GetSubscribedObjects().Result,
                    HttpStatusCode.OK, "Get notifications (second time)");
                Assert.That(notifications.Count, Is.EqualTo(searchObjectCount), "Notification count (second time)");

                foreach (var notification in notifications)
                {
                    AssertStatusCode(
                        () => analystDataApiClient.DeleteNotificationAsync(notification.Id).Wait(),
                        HttpStatusCode.OK, "Delete notification");
                }
                // Test that notifications have been deleted
                AssertStatusCode(
                    () => notifications = analystDataApiClient.GetSubscribedObjects().Result,
                    HttpStatusCode.OK, "Get notifications after deletion");
                Assert.That(notifications.Count, Is.EqualTo(0), "Notification count after deletion");
            }
            finally
            {
                // Tear down
                SearchDataGenerator.DeleteData(submittedData, analystDataApiClient);
                UnsubscribeFromUnitTestSearchObject(analystDataApiClient);
            }
        }
Beispiel #4
0
        public void CanSearchWithQueryable()
        {
            var submittedData = SearchDataGenerator.GenerateAndSubmitSearchData(5, analystDataApiClient);

            try
            {
                // Test 1:
                List <string> actual     = null;
                var           repository = new GenericDatabase <UnitTestSearchObject>(analystDataApiClient);
                Assert.That(() => actual = repository.OrderBy(x => x.Name).Select(x => x.Id).ToList(), Throws.Nothing);
                CollectionAssert.AreEqual(
                    submittedData.OrderBy(x => x.Name).Select(x => x.Id).ToList(),
                    actual);
            }
            finally
            {
                SearchDataGenerator.DeleteData(submittedData.Select(x => x.Id), analystDataApiClient);
            }
        }
Beispiel #5
0
        public void GetManyReturnsExpectedObjects()
        {
            var submittedData = SearchDataGenerator.GenerateAndSubmitSearchData(5, analystDataApiClient);

            try
            {
                List <UnitTestSearchObject> allSearchObjects = null;
                Assert.That(
                    () => allSearchObjects = analystDataApiClient.GetManyAsync <UnitTestSearchObject>("").Result,
                    Throws.Nothing);
                Assert.That(allSearchObjects, Is.Not.Null);
                Assume.That(allSearchObjects.Count, Is.EqualTo(submittedData.Count), "Search data collection may not be clean. Remove all documents from the collection and run this test again");
                CollectionAssert.AreEquivalent(submittedData.Select(x => x.Id), allSearchObjects.Select(x => x.Id));
            }
            finally
            {
                SearchDataGenerator.DeleteData(submittedData.Select(x => x.Id), analystDataApiClient);
            }
        }
Beispiel #6
0
        public void CollectionCanBeProtectedAndUnprotected()
        {
            var submittedData = SearchDataGenerator.GenerateAndSubmitSearchData(1, analystDataApiClient);

            try
            {
                List <UnitTestSearchObject> searchObjects = null;
                Assume.That(
                    () => searchObjects = analystDataApiClient.GetManyAsync <UnitTestSearchObject>("").Result,
                    Throws.Nothing);
                Assume.That(searchObjects.Count, Is.EqualTo(1));

                AssertStatusCode(
                    () => adminDataApiClient.SetCollectionOptions(
                        new CollectionOptions(nameof(UnitTestSearchObject))
                {
                    IsProtected = true
                }),
                    HttpStatusCode.OK, "Protect collection");

                AssertStatusCode(
                    () => analystDataApiClient.GetManyAsync <UnitTestSearchObject>("").Wait(),
                    HttpStatusCode.Unauthorized, "Get many from protected collection");

                AssertStatusCode(
                    () => adminDataApiClient.SetCollectionOptions(
                        new CollectionOptions(nameof(UnitTestSearchObject))
                {
                    IsProtected = false
                }),
                    HttpStatusCode.OK, "Unprotect collection");

                AssertStatusCode(
                    () => searchObjects = analystDataApiClient.GetManyAsync <UnitTestSearchObject>("").Result,
                    HttpStatusCode.OK, "Get many from unprotected collection");
                Assert.That(searchObjects.Count, Is.EqualTo(1));
            }
            finally
            {
                SearchDataGenerator.DeleteData(submittedData, analystDataApiClient);
            }
        }
Beispiel #7
0
        public void GetManyCanLimitNumberOfObjects()
        {
            var submittedData = SearchDataGenerator.GenerateAndSubmitSearchData(5, analystDataApiClient);

            var limit = 3u;

            try
            {
                List <UnitTestSearchObject> allSearchObjects = null;
                Assert.That(
                    () => allSearchObjects = analystDataApiClient.GetManyAsync <UnitTestSearchObject>(limit: limit).Result,
                    Throws.Nothing);
                Assert.That(allSearchObjects, Is.Not.Null);
                Assert.That(allSearchObjects.Count, Is.EqualTo(limit));
            }
            finally
            {
                SearchDataGenerator.DeleteData(submittedData.Select(x => x.Id), analystDataApiClient);
            }
        }
Beispiel #8
0
        public async Task SqlQueryReturnsExpectedResult()
        {
            var submittedData = SearchDataGenerator.GenerateAndSubmitSearchData(5, analystDataApiClient);

            var resultFormat = ResultFormat.Json;

            try
            {
                // Test 1:
                var    query1       = $"SELECT Data.Id AS Id FROM {nameof(UnitTestSearchObject)}";
                Stream resultStream = null;
                Assert.That(async() => resultStream = await analystDataApiClient.SearchAsync(query1, resultFormat), Throws.Nothing);
                var results = await Client.Serialization.SeachResultStreamExtensions.ReadAllSearchResultsAsync(resultStream);

                Assert.That(results.All(x => x.ContainsKey("_id")), "One or more results do not contain '_id' field");
                if (results.Count > submittedData.Count)
                {
                    Assert.Inconclusive($"WARNING: Collection '{nameof(UnitTestSearchObject)}' contains data from previous tests. " +
                                        "Delete all documents in these collections manually");
                }
                CollectionAssert.IsSubsetOf(submittedData.Select(x => x.Id), results.Select(row => row.Value <string>("_id")), "Test 1 failed");

                // Test 2:
                var query2 = $"SELECT Data.Id AS Id, Data.Products.Price AS Price FROM {nameof(UnitTestSearchObject)} ORDER BY Price";
                Assert.That(() => resultStream = analystDataApiClient.SearchAsync(query2, resultFormat).Result, Throws.Nothing);
                results = await Client.Serialization.SeachResultStreamExtensions.ReadAllSearchResultsAsync(resultStream);

                Assert.That(results.All(x => x.ContainsKey("_id")), "One or more results do not contain '_id' field");
                Assert.That(results.All(x => x.ContainsKey("Price")), "Results do not contain 'Price'-field");
                var expectedPrices = submittedData.SelectMany(x => x.Products).Select(product => product.Price).OrderBy(price => price).ToList();
                var actualPrices   = results.Select(x => x.Value <double>("Price")).ToList();
                CollectionAssert.AreEqual(expectedPrices, actualPrices, "Test 2 failed");
            }
            finally
            {
                SearchDataGenerator.DeleteData(submittedData.Select(x => x.Id), analystDataApiClient);
            }
        }
Beispiel #9
0
        public void DataModificationTypeAsExpected()
        {
            var    testObject     = SearchDataGenerator.GenerateUnitTestSearchObject();
            string subscriptionId = null;

            AssertStatusCode(
                () => subscriptionId = SubscribeToUnitTestSearchObject(analystDataApiClient, DataModificationType.Created, DataModificationType.Replaced),
                HttpStatusCode.OK, "Subscribe");

            try
            {
                // Create object
                analystDataApiClient.InsertAsync(testObject, testObject.Id).Wait();
                var notifications = analystDataApiClient.GetSubscribedObjects().Result;
                Assert.That(notifications.Count, Is.EqualTo(1));
                Assert.That(notifications.Single().ModificationType, Is.EqualTo(DataModificationType.Created));
                analystDataApiClient.DeleteNotificationAsync(notifications.Single().Id).Wait();

                // Replace object
                analystDataApiClient.ReplaceAsync(testObject, testObject.Id).Wait();
                notifications = analystDataApiClient.GetSubscribedObjects().Result;
                Assert.That(notifications.Count, Is.EqualTo(1));
                Assert.That(notifications.Single().ModificationType, Is.EqualTo(DataModificationType.Replaced));
                analystDataApiClient.DeleteNotificationAsync(notifications.Single().Id).Wait();

                // Delete object
                analystDataApiClient.DeleteAsync <UnitTestSearchObject>(testObject.Id).Wait();
                notifications = analystDataApiClient.GetSubscribedObjects().Result;
                Assert.That(notifications.Count, Is.EqualTo(1));
                Assert.That(notifications.Single().ModificationType, Is.EqualTo(DataModificationType.Deleted));
                analystDataApiClient.DeleteNotificationAsync(notifications.Single().Id).Wait();
            }
            finally
            {
                UnsubscribeFromUnitTestSearchObject(analystDataApiClient);
            }
        }
Beispiel #10
0
        public void GlobalPermissionsAreUsedForUnprotectedCollection()
        {
            UserGenerator.RegisterAndLoginUserWithRole(Role.Analyst, adminDataApiClient, out var dataApiClient);

            var dataType = nameof(UnitTestSearchObject);

            // Make sure collection is not protected
            adminDataApiClient.SetCollectionOptions(
                new CollectionOptions(dataType)
            {
                IsProtected = false
            });
            var submittedData = SearchDataGenerator.GenerateAndSubmitSearchData(1, adminDataApiClient);

            try
            {
                // Check that user has access
                List <UnitTestSearchObject> searchObjects = null;
                AssertStatusCode(
                    () => searchObjects = dataApiClient.GetManyAsync <UnitTestSearchObject>("").Result,
                    HttpStatusCode.OK, "Get many with authorized user");
                Assert.That(searchObjects.Count, Is.EqualTo(1));

                // Remove role
                adminDataApiClient.RemoveGlobalRoleFromUser(dataApiClient.LoggedInUsername, Role.Analyst);

                AssertStatusCode(
                    () => dataApiClient.GetManyAsync <UnitTestSearchObject>("").Wait(),
                    HttpStatusCode.Unauthorized, "Get many with unauthorized user");
            }
            finally
            {
                SearchDataGenerator.DeleteData(submittedData, adminDataApiClient);
                UserGenerator.DeleteUser(dataApiClient);
            }
        }