Esempio n. 1
0
        public IEnumerable <string> Get()
        {
            DocumentDb db = new DocumentDb();
            var        rs = db.GetSensorMessages();

            yield return(Newtonsoft.Json.JsonConvert.SerializeObject(rs));
        }
Esempio n. 2
0
        public async Task UpsertDocument_UpdateDocument_IsUpdatedCorrectly()
        {
            // Arrange
            var db         = new DocumentDb(location, key, database);
            var documentId = Guid.NewGuid().ToString();
            var person     = new PersonTest
            {
                Id       = Guid.Parse(documentId),
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87
            };
            var personEnvelope = new DocumentBase <PersonTest> {
                VM = person
            };
            await db.UpsertDocument(CollectionId, documentId, personEnvelope);

            personEnvelope = await db.ReadDocument <PersonTest>(CollectionId, documentId);

            personEnvelope.VM.Name = "Fred Flintstone";
            personEnvelope.VM.Age  = 88;

            await db.UpsertDocument(CollectionId, documentId, personEnvelope);

            // Assert
            var doc = await db.ReadDocument <PersonTest>(CollectionId, documentId);

            doc.VM.Age.Should().Be(88);
            doc.VM.Name.Should().Be("Fred Flintstone");
            doc.VM.PersonId.Should().Be(person.PersonId);
        }
Esempio n. 3
0
        public async Task ReadDocument_DocumentExists_ReturnDocumentIsCorrect()
        {
            // Arrange
            var db         = new DocumentDb(location, key, database);
            var documentId = Guid.NewGuid().ToString();
            var person     = new PersonTest
            {
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87
            };
            var personEnvelope = new DocumentBase <PersonTest> {
                VM = person
            };
            await db.UpsertDocument(CollectionId, documentId, personEnvelope);

            // Act
            var doc = await db.ReadDocument <PersonTest>(CollectionId, documentId);

            // Assert
            doc.VM.Age.Should().Be(person.Age);
            doc.VM.Name.Should().Be(person.Name);
            doc.VM.PersonId.Should().Be(person.PersonId);
            doc.ETag.Should().NotBe(string.Empty);
        }
        public DocumentObserverChatClient(DocumentClientConfig config, DocumentDb <Message> db)
        {
            this.config = config;
            this.db     = db;

            this.settings = IocServices.Resolve <ISettings>();
        }
Esempio n. 5
0
        public async Task UpsertDocument_NewDocument_IsStoredCorrectly()
        {
            // Arrange
            var db     = new DocumentDb(location, key, database);
            var id     = Guid.NewGuid();
            var person = new PersonTest()
            {
                PersonId = id,
                Name     = "Barney Rubble",
                Age      = 87
            };
            var personEnvelope = new DocumentBase <PersonTest> {
                VM = person, ETag = Guid.NewGuid().ToString()
            };

            // Act
            await db.UpsertDocument(CollectionId, id.ToString(), personEnvelope);

            // Assert
            var doc = await db.ReadDocument <PersonTest>(CollectionId, id.ToString());

            doc.VM.Age.Should().Be(87);
            doc.VM.Name.Should().Be("Barney Rubble");
            doc.VM.PersonId.Should().Be(person.PersonId);
        }
Esempio n. 6
0
        public async Task Init(Application app)
        {
            IocServices.AddSingleton <ISettings>(new Settings());

            var config = new DocumentClientConfig
            {
                Collection = "TestCollection",
                Database   = "TestDatabase",
                Url        = "https://localhost:8081",

                //DocumentDb Emulator Token
                Key = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",

                LeaseCollection  = "TestCollectionLease",
                OldMessagesCount = 10,
                PollDelay        = TimeSpan.FromSeconds(1)
            };

            var uri    = new Uri(config.Url);
            var key    = config.Key;
            var client = new DocumentClient(uri, key);

            var db = new DocumentDb <Message>(client, config.Database, config.Collection, config.LeaseCollection);

            var chatCleint = new MockObserverChatClient(config, db);

            await chatCleint.Start();

            IocServices.AddSingleton <IChatClient>(chatCleint);

            app.MainPage = new XamarinChatWithCosmosOnly.MainPage();
        }
Esempio n. 7
0
        public void OneTimeSetUp()
        {
            _logger      = new Logger();
            _dataCreator = new TestDataCreator();
            var lockDb = new DistributedLockDb(_logger);
            var docDb  = new DocumentDb(_logger);

            lockDb.InitializeDatabase();
            docDb.InitializeDatabase();
        }
Esempio n. 8
0
        private async Task <DocumentDb> SetupMultiplePersons()
        {
            var db = new DocumentDb(location, key, database);

            var personA = new PersonTest
            {
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87,
                Id       = new Guid("C9FCD0EB-E5F3-439D-BA4D-093E692046C0")
            };
            var personAEnvelope = new DocumentBase <PersonTest> {
                VM = personA, ETag = Guid.NewGuid().ToString()
            };
            var personB = new PersonTest
            {
                PersonId = Guid.NewGuid(),
                Name     = "Wilma Flintstone",
                Age      = 88,
                Id       = Guid.NewGuid()
            };
            var personBEnvelope = new DocumentBase <PersonTest> {
                VM = personB, ETag = Guid.NewGuid().ToString()
            };
            var personC = new PersonTest
            {
                PersonId = Guid.NewGuid(),
                Name     = "Wilma Flintstone",
                Age      = 66,
                Id       = Guid.Empty
            };
            var personCEnvelope = new DocumentBase <PersonTest> {
                VM = personC, ETag = Guid.NewGuid().ToString()
            };
            var personD = new PersonTest
            {
                PersonId = Guid.NewGuid(),
                Name     = "Wilma Flintstone",
                Age      = 66,
                Id       = Guid.NewGuid()
            };
            var personDEnvelope = new DocumentBase <PersonTest> {
                VM = personD, ETag = Guid.NewGuid().ToString()
            };

            await db.UpsertDocument(CollectionId, Guid.NewGuid().ToString(), personAEnvelope);

            await db.UpsertDocument(CollectionId, Guid.NewGuid().ToString(), personBEnvelope);

            await db.UpsertDocument(CollectionId, Guid.NewGuid().ToString(), personCEnvelope);

            await db.UpsertDocument(CollectionId, Guid.NewGuid().ToString(), personDEnvelope);

            return(db);
        }
Esempio n. 9
0
        public async Task ReadAll_CollectionIdKnownButEmpty_ReturnsEmptyCollection()
        {
            // Arrange
            var db = new DocumentDb(location, key, database);

            // Act
            var doc = await db.ReadAll <PersonTest>(CollectionId);

            // Assert
            doc.Should().BeEmpty();
        }
Esempio n. 10
0
        public void IsDocumentInCollection_DocumentIdIsNull_ThrowsException(string documentId)
        {
            // Arrange
            var db = new DocumentDb(location, key, database);

            // Act
            var ex = Record.Exception(() => db.DocumentExists(CollectionId, documentId));

            //Assert
            ex.Should().NotBeNull();
            ex.Should().BeOfType <ArgumentNullException>();
        }
Esempio n. 11
0
        public async Task ReadAll_CollectionIdIsNull_ThrowsException(string collectionId)
        {
            // Arrange
            var db = new DocumentDb(location, key, database);

            // Act
            var ex = await Record.ExceptionAsync(() => db.ReadAll <PersonTest>(collectionId));

            //Assert
            ex.Should().NotBeNull();
            ex.Should().BeOfType <ArgumentNullException>();
        }
Esempio n. 12
0
        public async Task DeleteDocument_DocumentNotExists_NoExceptionThrown()
        {
            // Arrange
            var db         = new DocumentDb(location, key, database);
            var documentId = Guid.NewGuid().ToString();

            // Act
            var ex = await Record.ExceptionAsync(() => db.DeleteDocument(CollectionId, documentId));

            // Assert
            ex.Should().BeNull();
        }
Esempio n. 13
0
        public async Task DeleteCollection_NullCollectionId_ThrowsException(string collectionId)
        {
            // Arrange
            var db = new DocumentDb(location, key, database);

            // Act
            var ex = await Record.ExceptionAsync(() => db.DeleteCollection(collectionId));

            //Assert
            ex.Should().NotBeNull();
            ex.Should().BeOfType <ArgumentNullException>();
        }
Esempio n. 14
0
        public DocumentDbTest()
        {
            location = ConfigurationManager.AppSettings["DocumentDbUrl"];
            key      = ConfigurationManager.AppSettings["DocumentDbAuthKey"];
            database = ConfigurationManager.AppSettings["DocumentDbDatabaseId"];

            var db = new DocumentDb(location, key, database);

            db.ClearCollection(NewCollectionId).Wait();
            db.ClearCollection(CollectionId).Wait();
            db.CreateCollection(CollectionId).Wait();
        }
Esempio n. 15
0
        public void IsDocumentInCollection_DocumentDoesNotExist_ReturnsFalse()
        {
            // Arrange
            var db         = new DocumentDb(location, key, database);
            var documentId = Guid.NewGuid().ToString();

            // Act
            var exists = db.DocumentExists(CollectionId, documentId);

            // Assert
            exists.Should().BeFalse();
        }
Esempio n. 16
0
 public async Task <bool> Delete(int id)
 {
     if (id == 214213232)
     {
         DocumentDb db = new DocumentDb();
         return(await db.ClearDocumentCollectionAsync(CollectionId));
     }
     else
     {
         return(false);
     }
 }
Esempio n. 17
0
        public async Task ReadDocument_DocumentNotExists_ReturnsNull()
        {
            // Arrange
            var db         = new DocumentDb(location, key, database);
            var documentId = Guid.NewGuid().ToString();

            // Act
            var doc = await db.ReadDocument <PersonTest>(CollectionId, documentId);

            // Assert
            doc.Should().BeNull();
        }
Esempio n. 18
0
        public async Task DeleteDocument_CollectionIdIsNull_ThrowsException(string collectionId)
        {
            // Arrange
            var db         = new DocumentDb(location, key, database);
            var documentId = Guid.NewGuid().ToString();

            // Act
            var ex = await Record.ExceptionAsync(() => db.DeleteDocument(collectionId, documentId));

            //Assert
            ex.Should().NotBeNull();
            ex.Should().BeOfType <ArgumentNullException>();
        }
Esempio n. 19
0
        public async Task CreateCollection_NewCollectionNameProvided_NewCollectionCreated()
        {
            // Arrange
            var db = new DocumentDb(location, key, database);

            // Act
            await db.CreateCollection(NewCollectionId);

            // Assert
            var exists = await db.CollectionExists(NewCollectionId);

            exists.Should().BeTrue();
        }
        public async static Task RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "SendGridEventWebhook.Controllers" }
                );

            // Create stored procedure on DocumentDb
            DocumentDb ddb = DocumentDb.GetInstance();
            await ddb.Init();
        }
Esempio n. 21
0
        public async Task UpsertDocument_NullItem_ThrowsException()
        {
            // Arrange
            var db         = new DocumentDb(location, key, database);
            var documentId = Guid.NewGuid().ToString();
            DocumentBase <PersonTest> personEnvelope = null;

            // Act
            var ex = await Record.ExceptionAsync(() => db.UpsertDocument(CollectionId, documentId, personEnvelope));

            // Assert
            ex.Should().NotBeNull();
            ex.Should().BeOfType <ArgumentNullException>();
        }
Esempio n. 22
0
        public async Task UpsertDocument_NullDocumentId_ThrowsException(string documentId)
        {
            // Arrange
            var db     = new DocumentDb(location, key, database);
            var person = new PersonTest
            {
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87
            };
            var personEnvelope = new DocumentBase <PersonTest> {
                VM = person, ETag = Guid.NewGuid().ToString()
            };

            // Act
            var ex = await Record.ExceptionAsync(() => db.UpsertDocument(CollectionId, documentId, personEnvelope));

            //Assert
            ex.Should().NotBeNull();
            ex.Should().BeOfType <ArgumentNullException>();
        }
Esempio n. 23
0
        public async Task IsDocumentInCollection_DocumentDoesExist_ReturnsTrue()
        {
            // Arrange
            var db         = new DocumentDb(location, key, database);
            var documentId = Guid.NewGuid().ToString();
            var person     = new PersonTest
            {
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87
            };
            var personEnvelope = new DocumentBase <PersonTest> {
                VM = person
            };
            await db.UpsertDocument(CollectionId, documentId, personEnvelope);

            // Act
            var exists = db.DocumentExists(CollectionId, documentId);

            // Assert
            exists.Should().BeTrue();
        }
Esempio n. 24
0
        public async Task UpsertDocument_TwoNewDocumentsSameIdDifferentEtag_OnlyOneIsStored()
        {
            // Arrange
            var db      = new DocumentDb(location, key, database);
            var id      = Guid.NewGuid();
            var personA = new PersonTest()
            {
                Id       = id,
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87
            };
            var personB = new PersonTest()
            {
                Id       = id,
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87
            };
            var personAEnvelope = new DocumentBase <PersonTest> {
                VM = personA
            };
            var personBEnvelope = new DocumentBase <PersonTest> {
                VM = personB
            };

            // Act
            var taskForPersonA = db.UpsertDocument(CollectionId, id.ToString(), personAEnvelope);
            var taskForPersonB = db.UpsertDocument(CollectionId, id.ToString(), personBEnvelope);

            // Assert
            //We don't care about exception now we just waiting for tasks to finish
            await Record.ExceptionAsync(() => Task.WhenAll(taskForPersonA, taskForPersonB));

            var persons = await db.Query <PersonTest>(CollectionId, new Dictionary <string, object>()
                                                      { { "Id", personA.Id } });

            persons.Count().Should().Be(1);
        }
Esempio n. 25
0
        public async Task UpsertDocument_UpdateDocumentConncurrently_ThrowsException()

        {
            // Arrange
            var db         = new DocumentDb(location, key, database);
            var documentId = Guid.NewGuid().ToString();
            var person     = new PersonTest
            {
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87,
            };
            var personEnvelope = new DocumentBase <PersonTest> {
                VM = person
            };
            await db.UpsertDocument(CollectionId, documentId, personEnvelope);

            personEnvelope = await db.ReadDocument <PersonTest>(CollectionId, documentId);

            var newChange = new DocumentBase <PersonTest> {
                VM = person
            };

            newChange.VM.Name = "Change";
            newChange.ETag    = personEnvelope.ETag;

            personEnvelope.VM.Name = "Fred Flintstone";
            personEnvelope.VM.Age  = 88;

            await db.UpsertDocument(CollectionId, documentId, personEnvelope);

            var exception = await Record.ExceptionAsync(() => db.UpsertDocument(CollectionId, documentId, newChange));

            // Assert
            exception.Should().NotBeNull();
            exception.Should().BeOfType <ConcurrencyException>();
            exception.Message.Should().Contain("One of the specified pre-condition is not met");
        }
Esempio n. 26
0
        public async Task UpsertDocument_TwoNewDocuments_OneThrowsConcurrencyException()
        {
            // Arrange
            var db      = new DocumentDb(location, key, database);
            var id      = Guid.NewGuid();
            var personA = new PersonTest()
            {
                Id       = id,
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87
            };
            var personB = new PersonTest()
            {
                Id       = id,
                PersonId = Guid.NewGuid(),
                Name     = "Barney Rubble",
                Age      = 87
            };
            var personAEnvelope = new DocumentBase <PersonTest> {
                VM = personA, ETag = id.ToString()
            };
            var personBEnvelope = new DocumentBase <PersonTest> {
                VM = personB, ETag = id.ToString()
            };

            // Act
            var taskForPersonA = db.UpsertDocument(CollectionId, id.ToString(), personAEnvelope);
            var taskForPersonB = db.UpsertDocument(CollectionId, id.ToString(), personBEnvelope);

            // Assert
            var exception = await Record.ExceptionAsync(() => Task.WhenAll(taskForPersonA, taskForPersonB));

            exception.Should().NotBeNull();
            exception.Should().BeOfType <ConcurrencyException>();
        }
Esempio n. 27
0
 public Consumer(DistributedLockDb lockDb, DocumentDb documentDb, Logger logger)
 {
     _lockDb     = lockDb;
     _documentDb = documentDb;
     _logger     = logger;
 }
Esempio n. 28
0
 public MockObserverChatClient(DocumentClientConfig config, DocumentDb <Message> db) : base(config, db)
 {
 }
Esempio n. 29
0
 private void SetupDatabases()
 {
     _documentDb = new DocumentDb(_config, "Grouper Service");
     _logDb      = new LogDb(_config);
 }
Esempio n. 30
0
 public RepositoryBase(DocumentDb db)
 {
     _db = db;
 }