From 76c1d226c15984d5ed8d69d30e1842016e34d286 Mon Sep 17 00:00:00 2001 From: Devis Lucato Date: Tue, 7 Nov 2017 00:42:14 -0800 Subject: [PATCH] Code style (#40) --- .../DocumentDbKeyValueContainerTest.cs | 186 +++++++++--------- Services.Test/Services.Test.csproj | 3 - Services.Test/helpers/Constants.cs | 10 +- Services.Test/helpers/MockFactory.cs | 2 +- Services/KeyValueDocument.cs | 8 +- Services/Wrappers/DocumentClientFactory.cs | 6 +- .../Controllers/KeyValueControllerTest.cs | 156 +++++++-------- .../IntegrationTests/ServiceStatusTest.cs | 4 +- WebService.Test/helpers/Constants.cs | 10 +- WebService.Test/helpers/Http/HttpResponse.cs | 4 +- WebService/Program.cs | 2 +- WebService/Runtime/Config.cs | 26 +-- WebService/v1/Controllers/StatusController.cs | 2 +- WebService/v1/Controllers/ValuesController.cs | 40 ++-- WebService/v1/Models/StatusApiModel.cs | 10 +- WebService/v1/Models/ValueApiModel.cs | 4 +- WebService/v1/Models/ValueListApiModel.cs | 8 +- WebService/v1/Version.cs | 6 +- pcs-storage-adapter.sln.DotSettings | 1 + 19 files changed, 242 insertions(+), 246 deletions(-) diff --git a/Services.Test/DocumentDbKeyValueContainerTest.cs b/Services.Test/DocumentDbKeyValueContainerTest.cs index 27f52c6..59a8f4a 100644 --- a/Services.Test/DocumentDbKeyValueContainerTest.cs +++ b/Services.Test/DocumentDbKeyValueContainerTest.cs @@ -18,9 +18,9 @@ namespace Services.Test { public class DocumentDbKeyValueContainerTest { - private const string MockDbId = "mockdb"; - private const string MockCollId = "mockcoll"; - private static readonly string mockCollectionLink = $"/dbs/{MockDbId}/colls/{MockCollId}"; + private const string MOCK_DB_ID = "mockdb"; + private const string MOCK_COLL_ID = "mockcoll"; + private static readonly string mockCollectionLink = $"/dbs/{MOCK_DB_ID}/colls/{MOCK_COLL_ID}"; private readonly Mock mockClient; private readonly DocumentDbKeyValueContainer container; @@ -28,30 +28,30 @@ public class DocumentDbKeyValueContainerTest public DocumentDbKeyValueContainerTest() { - mockClient = new Mock(); + this.mockClient = new Mock(); - container = new DocumentDbKeyValueContainer( - new MockFactory(mockClient), + this.container = new DocumentDbKeyValueContainer( + new MockFactory(this.mockClient), new MockExceptionChecker(), new ServicesConfig { StorageType = "documentDb", DocumentDbConnString = "", - DocumentDbDatabase = MockDbId, - DocumentDbCollection = MockCollId, + DocumentDbDatabase = MOCK_DB_ID, + DocumentDbCollection = MOCK_COLL_ID, DocumentDbRUs = 567 }, new Logger("UnitTest", LogLevel.Debug)); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task GetAsyncTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); - var data = rand.NextString(); - var etag = rand.NextString(); - var timestamp = rand.NextDateTimeOffset(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); + var data = this.rand.NextString(); + var etag = this.rand.NextString(); + var timestamp = this.rand.NextDateTimeOffset(); var document = new Document(); document.SetPropertyValue("CollectionId", collectionId); @@ -61,13 +61,13 @@ public async Task GetAsyncTest() document.SetTimestamp(timestamp); var response = new ResourceResponse(document); - mockClient + this.mockClient .Setup(x => x.ReadDocumentAsync( It.IsAny(), It.IsAny())) .ReturnsAsync(response); - var result = await container.GetAsync(collectionId, key); + var result = await this.container.GetAsync(collectionId, key); Assert.Equal(result.CollectionId, collectionId); Assert.Equal(result.Key, key); @@ -75,52 +75,52 @@ public async Task GetAsyncTest() Assert.Equal(result.ETag, etag); Assert.Equal(result.Timestamp, timestamp); - mockClient + this.mockClient .Verify(x => x.ReadDocumentAsync( - It.Is(s => s == $"{mockCollectionLink}/docs/{collectionId.ToLowerInvariant()}.{key.ToLowerInvariant()}"), - It.IsAny()), + It.Is(s => s == $"{mockCollectionLink}/docs/{collectionId.ToLowerInvariant()}.{key.ToLowerInvariant()}"), + It.IsAny()), Times.Once); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task GetAsyncNotFoundTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); - mockClient + this.mockClient .Setup(x => x.ReadDocumentAsync( It.IsAny(), It.IsAny())) .ThrowsAsync(new ResourceNotFoundException()); await Assert.ThrowsAsync(async () => - await container.GetAsync(collectionId, key)); + await this.container.GetAsync(collectionId, key)); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task GetAllAsyncTest() { - var collectionId = rand.NextString(); + var collectionId = this.rand.NextString(); var documents = new[] { - new KeyValueDocument(collectionId, rand.NextString(), rand.NextString()), - new KeyValueDocument(collectionId, rand.NextString(), rand.NextString()), - new KeyValueDocument(collectionId, rand.NextString(), rand.NextString()), + new KeyValueDocument(collectionId, this.rand.NextString(), this.rand.NextString()), + new KeyValueDocument(collectionId, this.rand.NextString(), this.rand.NextString()), + new KeyValueDocument(collectionId, this.rand.NextString(), this.rand.NextString()), }; foreach (var doc in documents) { - doc.SetETag(rand.NextString()); - doc.SetTimestamp(rand.NextDateTimeOffset()); + doc.SetETag(this.rand.NextString()); + doc.SetTimestamp(this.rand.NextDateTimeOffset()); } - mockClient + this.mockClient .Setup(x => x.CreateDocumentQuery( It.IsAny(), It.IsAny())) .Returns(documents.AsQueryable().OrderBy(doc => doc.Id)); - var result = (await container.GetAllAsync(collectionId)).ToList(); + var result = (await this.container.GetAllAsync(collectionId)).ToList(); Assert.Equal(result.Count(), documents.Length); foreach (var model in result) @@ -132,21 +132,21 @@ public async Task GetAllAsyncTest() Assert.Equal(model.Timestamp, doc.Timestamp); } - mockClient + this.mockClient .Verify(x => x.CreateDocumentQuery( - It.Is(s => s == mockCollectionLink), - It.IsAny()), + It.Is(s => s == mockCollectionLink), + It.IsAny()), Times.Once); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task CreateAsyncTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); - var data = rand.NextString(); - var etag = rand.NextString(); - var timestamp = rand.NextDateTimeOffset(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); + var data = this.rand.NextString(); + var etag = this.rand.NextString(); + var timestamp = this.rand.NextDateTimeOffset(); var document = new Document(); document.SetPropertyValue("CollectionId", collectionId); @@ -156,7 +156,7 @@ public async Task CreateAsyncTest() document.SetTimestamp(timestamp); var response = new ResourceResponse(document); - mockClient + this.mockClient .Setup(x => x.CreateDocumentAsync( It.IsAny(), It.IsAny(), @@ -164,7 +164,7 @@ public async Task CreateAsyncTest() It.IsAny())) .ReturnsAsync(response); - var result = await container.CreateAsync(collectionId, key, new ValueServiceModel + var result = await this.container.CreateAsync(collectionId, key, new ValueServiceModel { Data = data }); @@ -175,23 +175,23 @@ public async Task CreateAsyncTest() Assert.Equal(result.ETag, etag); Assert.Equal(result.Timestamp, timestamp); - mockClient + this.mockClient .Verify(x => x.CreateDocumentAsync( - It.Is(s => s == mockCollectionLink), - It.Is(doc => doc.Id == $"{collectionId.ToLowerInvariant()}.{key.ToLowerInvariant()}" && doc.CollectionId == collectionId && doc.Key == key && doc.Data == data), - It.IsAny(), - It.IsAny()), + It.Is(s => s == mockCollectionLink), + It.Is(doc => doc.Id == $"{collectionId.ToLowerInvariant()}.{key.ToLowerInvariant()}" && doc.CollectionId == collectionId && doc.Key == key && doc.Data == data), + It.IsAny(), + It.IsAny()), Times.Once); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task CreateAsyncConflictTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); - var data = rand.NextString(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); + var data = this.rand.NextString(); - mockClient + this.mockClient .Setup(x => x.CreateDocumentAsync( It.IsAny(), It.IsAny(), @@ -200,21 +200,21 @@ public async Task CreateAsyncConflictTest() .ThrowsAsync(new ConflictingResourceException()); await Assert.ThrowsAsync(async () => - await container.CreateAsync(collectionId, key, new ValueServiceModel - { - Data = data - })); + await this.container.CreateAsync(collectionId, key, new ValueServiceModel + { + Data = data + })); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task UpsertAsyncTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); - var data = rand.NextString(); - var etagOld = rand.NextString(); - var etagNew = rand.NextString(); - var timestamp = rand.NextDateTimeOffset(); + 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 timestamp = this.rand.NextDateTimeOffset(); var document = new Document(); document.SetPropertyValue("CollectionId", collectionId); @@ -224,7 +224,7 @@ public async Task UpsertAsyncTest() document.SetTimestamp(timestamp); var response = new ResourceResponse(document); - mockClient + this.mockClient .Setup(x => x.UpsertDocumentAsync( It.IsAny(), It.IsAny(), @@ -232,7 +232,7 @@ public async Task UpsertAsyncTest() It.IsAny())) .ReturnsAsync(response); - var result = await container.UpsertAsync(collectionId, key, new ValueServiceModel + var result = await this.container.UpsertAsync(collectionId, key, new ValueServiceModel { Data = data, ETag = etagOld @@ -244,24 +244,24 @@ public async Task UpsertAsyncTest() Assert.Equal(result.ETag, etagNew); Assert.Equal(result.Timestamp, timestamp); - mockClient + this.mockClient .Verify(x => x.UpsertDocumentAsync( - It.Is(s => s == mockCollectionLink), - It.Is(doc => doc.Id == $"{collectionId.ToLowerInvariant()}.{key.ToLowerInvariant()}" && doc.CollectionId == collectionId && doc.Key == key && doc.Data == data), - It.IsAny(), - It.IsAny()), + It.Is(s => s == mockCollectionLink), + It.Is(doc => doc.Id == $"{collectionId.ToLowerInvariant()}.{key.ToLowerInvariant()}" && doc.CollectionId == collectionId && doc.Key == key && doc.Data == data), + It.IsAny(), + It.IsAny()), Times.Once); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task UpsertAsyncConflictTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); - var data = rand.NextString(); - var etag = rand.NextString(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); + var data = this.rand.NextString(); + var etag = this.rand.NextString(); - mockClient + this.mockClient .Setup(x => x.UpsertDocumentAsync( It.IsAny(), It.IsAny(), @@ -270,47 +270,47 @@ public async Task UpsertAsyncConflictTest() .ThrowsAsync(new ConflictingResourceException()); await Assert.ThrowsAsync(async () => - await container.UpsertAsync(collectionId, key, new ValueServiceModel + await this.container.UpsertAsync(collectionId, key, new ValueServiceModel { Data = data, ETag = etag })); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task DeleteAsyncTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); - mockClient + this.mockClient .Setup(x => x.DeleteDocumentAsync( It.IsAny(), It.IsAny())) - .ReturnsAsync((ResourceResponse)null); + .ReturnsAsync((ResourceResponse) null); - await container.DeleteAsync(collectionId, key); + await this.container.DeleteAsync(collectionId, key); - mockClient + this.mockClient .Verify(x => x.DeleteDocumentAsync( - It.Is(s => s == $"{mockCollectionLink}/docs/{collectionId.ToLowerInvariant()}.{key.ToLowerInvariant()}"), - It.IsAny()), + It.Is(s => s == $"{mockCollectionLink}/docs/{collectionId.ToLowerInvariant()}.{key.ToLowerInvariant()}"), + It.IsAny()), Times.Once); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task DeleteAsyncNotFoundTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); - mockClient + this.mockClient .Setup(x => x.DeleteDocumentAsync( It.IsAny(), It.IsAny())) .ThrowsAsync(new ResourceNotFoundException()); - await container.DeleteAsync(collectionId, key); + await this.container.DeleteAsync(collectionId, key); } } } diff --git a/Services.Test/Services.Test.csproj b/Services.Test/Services.Test.csproj index 467cc9b..e87eaa3 100644 --- a/Services.Test/Services.Test.csproj +++ b/Services.Test/Services.Test.csproj @@ -19,7 +19,4 @@ - - - \ No newline at end of file diff --git a/Services.Test/helpers/Constants.cs b/Services.Test/helpers/Constants.cs index d2d8d25..1b103db 100644 --- a/Services.Test/helpers/Constants.cs +++ b/Services.Test/helpers/Constants.cs @@ -8,11 +8,11 @@ namespace Services.Test.helpers /// public class Constants { - public const string Type = "Type"; - public const string UnitTest = "UnitTest"; - public const string IntegrationTest = "IntegrationTest"; + public const string TYPE = "Type"; + public const string UNIT_TEST = "UnitTest"; + public const string INTEGRATION_TEST = "IntegrationTest"; - public const string Speed = "Speed"; - public const string SlowTest = "SlowTest"; + public const string SPEED = "Speed"; + public const string SLOW_TEST = "SlowTest"; } } diff --git a/Services.Test/helpers/MockFactory.cs b/Services.Test/helpers/MockFactory.cs index 869df73..2a523d8 100644 --- a/Services.Test/helpers/MockFactory.cs +++ b/Services.Test/helpers/MockFactory.cs @@ -16,7 +16,7 @@ public MockFactory(Mock mock) public T Create() { - return mock.Object; + return this.mock.Object; } } } diff --git a/Services/KeyValueDocument.cs b/Services/KeyValueDocument.cs index 7205e30..688c54b 100644 --- a/Services/KeyValueDocument.cs +++ b/Services/KeyValueDocument.cs @@ -16,10 +16,10 @@ internal sealed class KeyValueDocument : Resource public KeyValueDocument(string collectionId, string key, string data) { - Id = DocumentIdHelper.GenerateId(collectionId, key); - CollectionId = collectionId; - Key = key; - Data = data; + this.Id = DocumentIdHelper.GenerateId(collectionId, key); + this.CollectionId = collectionId; + this.Key = key; + this.Data = data; } } } diff --git a/Services/Wrappers/DocumentClientFactory.cs b/Services/Wrappers/DocumentClientFactory.cs index 913aa7c..a0ed79b 100644 --- a/Services/Wrappers/DocumentClientFactory.cs +++ b/Services/Wrappers/DocumentClientFactory.cs @@ -25,13 +25,13 @@ public DocumentClientFactory(IServicesConfig config, ILogger logger) throw new InvalidConfigurationException(message); } - docDbEndpoint = new Uri(match.Groups["endpoint"].Value); - docDbKey = match.Groups["key"].Value; + this.docDbEndpoint = new Uri(match.Groups["endpoint"].Value); + this.docDbKey = match.Groups["key"].Value; } public IDocumentClient Create() { - return new DocumentClient(docDbEndpoint, docDbKey); + return new DocumentClient(this.docDbEndpoint, this.docDbKey); } } } diff --git a/WebService.Test/Controllers/KeyValueControllerTest.cs b/WebService.Test/Controllers/KeyValueControllerTest.cs index f0ffd3f..f39f741 100644 --- a/WebService.Test/Controllers/KeyValueControllerTest.cs +++ b/WebService.Test/Controllers/KeyValueControllerTest.cs @@ -26,23 +26,23 @@ public class KeyValueControllerTest public KeyValueControllerTest() { - mockContainer = new Mock(); - mockGenerator = new Mock(); + this.mockContainer = new Mock(); + this.mockGenerator = new Mock(); - controller = new ValuesController( - mockContainer.Object, - mockGenerator.Object, + this.controller = new ValuesController( + this.mockContainer.Object, + this.mockGenerator.Object, new Logger("UnitTest", LogLevel.Debug)); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task GetTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); - var data = rand.NextString(); - var etag = rand.NextString(); - var timestamp = rand.NextDateTimeOffset(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); + var data = this.rand.NextString(); + var etag = this.rand.NextString(); + var timestamp = this.rand.NextDateTimeOffset(); var model = new ValueServiceModel { @@ -53,13 +53,13 @@ public async Task GetTest() Timestamp = timestamp }; - mockContainer + this.mockContainer .Setup(x => x.GetAsync( It.IsAny(), It.IsAny())) .ReturnsAsync(model); - var result = await controller.Get(collectionId, key); + var result = await this.controller.Get(collectionId, key); Assert.Equal(result.Key, key); Assert.Equal(result.Data, data); @@ -68,52 +68,52 @@ public async Task GetTest() Assert.Equal(result.Metadata["$modified"], timestamp.ToString(CultureInfo.InvariantCulture)); Assert.Equal(result.Metadata["$uri"], $"/v1/collections/{collectionId}/values/{key}"); - mockContainer + this.mockContainer .Verify(x => x.GetAsync( - It.Is(s => s == collectionId), - It.Is(s => s == key)), + It.Is(s => s == collectionId), + It.Is(s => s == key)), Times.Once); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task GetAllTest() { - var collectionId = rand.NextString(); + var collectionId = this.rand.NextString(); var models = new[] { new ValueServiceModel { CollectionId = collectionId, - Key = rand.NextString(), - Data = rand.NextString(), - ETag = rand.NextString(), - Timestamp = rand.NextDateTimeOffset() + Key = this.rand.NextString(), + Data = this.rand.NextString(), + ETag = this.rand.NextString(), + Timestamp = this.rand.NextDateTimeOffset() }, new ValueServiceModel { CollectionId = collectionId, - Key = rand.NextString(), - Data = rand.NextString(), - ETag = rand.NextString(), - Timestamp = rand.NextDateTimeOffset() + Key = this.rand.NextString(), + Data = this.rand.NextString(), + ETag = this.rand.NextString(), + Timestamp = this.rand.NextDateTimeOffset() }, new ValueServiceModel { CollectionId = collectionId, - Key = rand.NextString(), - Data = rand.NextString(), - ETag = rand.NextString(), - Timestamp = rand.NextDateTimeOffset() + Key = this.rand.NextString(), + Data = this.rand.NextString(), + ETag = this.rand.NextString(), + Timestamp = this.rand.NextDateTimeOffset() } }; - mockContainer + this.mockContainer .Setup(x => x.GetAllAsync( It.IsAny())) .ReturnsAsync(models); - var result = await controller.Get(collectionId); + var result = await this.controller.Get(collectionId); var jsonResponse = JObject.FromObject(result); Assert.True(jsonResponse.TryGetValue("Items", out JToken value)); @@ -131,19 +131,19 @@ public async Task GetAllTest() Assert.Equal(result.Metadata["$type"], "ValueList;1"); Assert.Equal(result.Metadata["$uri"], $"/v1/collections/{collectionId}/values"); - mockContainer + this.mockContainer .Verify(x => x.GetAllAsync( It.Is(s => s == collectionId))); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task PostTest() { - var collectionId = rand.NextString(); + var collectionId = this.rand.NextString(); var key = Guid.NewGuid().ToString(); - var data = rand.NextString(); - var etag = rand.NextString(); - var timestamp = rand.NextDateTimeOffset(); + var data = this.rand.NextString(); + var etag = this.rand.NextString(); + var timestamp = this.rand.NextDateTimeOffset(); var modelIn = new ValueServiceModel { @@ -159,18 +159,18 @@ public async Task PostTest() Timestamp = timestamp }; - mockGenerator + this.mockGenerator .Setup(x => x.Generate()) .Returns(key); - mockContainer + this.mockContainer .Setup(x => x.CreateAsync( It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(modelOut); - var result = await controller.Post(collectionId, modelIn); + var result = await this.controller.Post(collectionId, modelIn); Assert.Equal(result.Key, key); Assert.Equal(result.Data, data); @@ -179,21 +179,21 @@ public async Task PostTest() Assert.Equal(result.Metadata["$modified"], modelOut.Timestamp.ToString(CultureInfo.InvariantCulture)); Assert.Equal(result.Metadata["$uri"], $"/v1/collections/{collectionId}/values/{key}"); - mockContainer + this.mockContainer .Verify(x => x.CreateAsync( It.Is(s => s == collectionId), It.Is(s => s == key), It.Is(m => m.Equals(modelIn)))); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task PutNewTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); - var data = rand.NextString(); - var etag = rand.NextString(); - var timestamp = rand.NextDateTimeOffset(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); + var data = this.rand.NextString(); + var etag = this.rand.NextString(); + var timestamp = this.rand.NextDateTimeOffset(); var modelIn = new ValueServiceModel { @@ -209,14 +209,14 @@ public async Task PutNewTest() Timestamp = timestamp }; - mockContainer + this.mockContainer .Setup(x => x.CreateAsync( It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(modelOut); - var result = await controller.Put(collectionId, key, modelIn); + var result = await this.controller.Put(collectionId, key, modelIn); Assert.Equal(result.Key, key); Assert.Equal(result.Data, data); @@ -225,23 +225,23 @@ public async Task PutNewTest() Assert.Equal(result.Metadata["$modified"], modelOut.Timestamp.ToString(CultureInfo.InvariantCulture)); Assert.Equal(result.Metadata["$uri"], $"/v1/collections/{collectionId}/values/{key}"); - mockContainer + this.mockContainer .Verify(x => x.CreateAsync( - It.Is(s => s == collectionId), - It.Is(s => s == key), - It.Is(m => m.Equals(modelIn))), + It.Is(s => s == collectionId), + It.Is(s => s == key), + It.Is(m => m.Equals(modelIn))), Times.Once); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task PutUpdateTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); - var data = rand.NextString(); - var etagOld = rand.NextString(); - var etagNew = rand.NextString(); - var timestamp = rand.NextDateTimeOffset(); + 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 timestamp = this.rand.NextDateTimeOffset(); var modelIn = new ValueServiceModel { @@ -258,14 +258,14 @@ public async Task PutUpdateTest() Timestamp = timestamp }; - mockContainer + this.mockContainer .Setup(x => x.UpsertAsync( It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(modelOut); - var result = await controller.Put(collectionId, key, modelIn); + var result = await this.controller.Put(collectionId, key, modelIn); Assert.Equal(result.Key, key); Assert.Equal(result.Data, data); @@ -274,43 +274,43 @@ public async Task PutUpdateTest() Assert.Equal(result.Metadata["$modified"], modelOut.Timestamp.ToString(CultureInfo.InvariantCulture)); Assert.Equal(result.Metadata["$uri"], $"/v1/collections/{collectionId}/values/{key}"); - mockContainer + this.mockContainer .Verify(x => x.UpsertAsync( - It.Is(s => s == collectionId), - It.Is(s => s == key), - It.Is(m => m.Equals(modelIn))), + It.Is(s => s == collectionId), + It.Is(s => s == key), + It.Is(m => m.Equals(modelIn))), Times.Once); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task DeleteTest() { - var collectionId = rand.NextString(); - var key = rand.NextString(); + var collectionId = this.rand.NextString(); + var key = this.rand.NextString(); - mockContainer + this.mockContainer .Setup(x => x.DeleteAsync( It.IsAny(), It.IsAny())) .Returns(Task.FromResult(0)); - await controller.Delete(collectionId, key); + await this.controller.Delete(collectionId, key); - mockContainer + this.mockContainer .Verify(x => x.DeleteAsync( - It.Is(s => s == collectionId), - It.Is(s => s == key)), + It.Is(s => s == collectionId), + It.Is(s => s == key)), Times.Once); } - [Fact, Trait(Constants.Type, Constants.UnitTest)] + [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public async Task ValidateKeyTest() { await Assert.ThrowsAsync(async () => - await controller.Delete("collection", "*")); + await this.controller.Delete("collection", "*")); await Assert.ThrowsAsync(async () => - await controller.Delete("collection", new string('a', 256))); + await this.controller.Delete("collection", new string('a', 256))); } } } diff --git a/WebService.Test/IntegrationTests/ServiceStatusTest.cs b/WebService.Test/IntegrationTests/ServiceStatusTest.cs index bb732c8..70102a6 100644 --- a/WebService.Test/IntegrationTests/ServiceStatusTest.cs +++ b/WebService.Test/IntegrationTests/ServiceStatusTest.cs @@ -27,7 +27,7 @@ public ServiceStatusTest(ITestOutputHelper log) /// Bootstrap a real HTTP server and test a request to the /// status endpoint. /// - [Fact, Trait(Constants.Type, Constants.IntegrationTest)] + [Fact, Trait(Constants.TYPE, Constants.INTEGRATION_TEST)] public void TheServiceIsHealthyViaHttpServer() { // Arrange @@ -53,7 +53,7 @@ public void TheServiceIsHealthyViaHttpServer() /// Bootstrap a test server and test a request to the /// status endpoint /// - [Fact, Trait(Constants.Type, Constants.IntegrationTest)] + [Fact, Trait(Constants.TYPE, Constants.INTEGRATION_TEST)] public void TheServiceIsHealthyViaTestServer() { // Arrange diff --git a/WebService.Test/helpers/Constants.cs b/WebService.Test/helpers/Constants.cs index b4b0ac7..0e63594 100644 --- a/WebService.Test/helpers/Constants.cs +++ b/WebService.Test/helpers/Constants.cs @@ -8,11 +8,11 @@ namespace WebService.Test.helpers /// public class Constants { - public const string Type = "Type"; - public const string UnitTest = "UnitTest"; - public const string IntegrationTest = "IntegrationTest"; + public const string TYPE = "Type"; + public const string UNIT_TEST = "UnitTest"; + public const string INTEGRATION_TEST = "IntegrationTest"; - public const string Speed = "Speed"; - public const string SlowTest = "SlowTest"; + public const string SPEED = "Speed"; + public const string SLOW_TEST = "SlowTest"; } } diff --git a/WebService.Test/helpers/Http/HttpResponse.cs b/WebService.Test/helpers/Http/HttpResponse.cs index ae21e39..abb4761 100644 --- a/WebService.Test/helpers/Http/HttpResponse.cs +++ b/WebService.Test/helpers/Http/HttpResponse.cs @@ -15,7 +15,7 @@ public interface IHttpResponse public class HttpResponse : IHttpResponse { - private const int TooManyRequests = 429; + private const int TOO_MANY_REQUESTS = 429; public HttpResponse() { @@ -37,6 +37,6 @@ public HttpResponse( public bool IsRetriableError => this.StatusCode == HttpStatusCode.NotFound || this.StatusCode == HttpStatusCode.RequestTimeout || - (int) this.StatusCode == TooManyRequests; + (int) this.StatusCode == TOO_MANY_REQUESTS; } } diff --git a/WebService/Program.cs b/WebService/Program.cs index 135bf21..f27eb9b 100644 --- a/WebService/Program.cs +++ b/WebService/Program.cs @@ -19,7 +19,7 @@ runtime and configuration settings */ Console.WriteLine($"[{Uptime.ProcessId}] Starting web service started, process ID: " + Uptime.ProcessId); Console.WriteLine($"[{Uptime.ProcessId}] Web service listening on port " + config.Port); - Console.WriteLine($"[{Uptime.ProcessId}] Web service health check at: http://127.0.0.1:" + config.Port + "/" + v1.Version.Path + "/status"); + Console.WriteLine($"[{Uptime.ProcessId}] Web service health check at: http://127.0.0.1:" + config.Port + "/" + v1.Version.PATH + "/status"); /* Kestrel is a cross-platform HTTP server based on libuv, a diff --git a/WebService/Runtime/Config.cs b/WebService/Runtime/Config.cs index 1255310..05dc6d9 100644 --- a/WebService/Runtime/Config.cs +++ b/WebService/Runtime/Config.cs @@ -17,13 +17,13 @@ public interface IConfig /// Web service configuration public class Config : IConfig { - private const string ApplicationKey = "StorageAdapter:"; - private const string PortKey = ApplicationKey + "webservice_port"; - private const string StorageTypeKey = ApplicationKey + "storageType"; - private const string DocumentDbConnectionStringKey = ApplicationKey + "documentdb_connstring"; - private const string DocumentDbDatabaseKey = ApplicationKey + "documentdb_database"; - private const string DocumentDbCollectionKey = ApplicationKey + "documentdb_collection"; - private const string DocumentDbRUsKey = ApplicationKey + "documentdb_RUs"; + private const string APPLICATION_KEY = "StorageAdapter:"; + private const string PORT_KEY = APPLICATION_KEY + "webservice_port"; + private const string STORAGE_TYPE_KEY = APPLICATION_KEY + "storageType"; + private const string DOCUMENT_DB_CONNECTION_STRING_KEY = APPLICATION_KEY + "documentdb_connstring"; + private const string DOCUMENT_DB_DATABASE_KEY = APPLICATION_KEY + "documentdb_database"; + private const string DOCUMENT_DB_COLLECTION_KEY = APPLICATION_KEY + "documentdb_collection"; + private const string DOCUMENT_DB_RUS_KEY = APPLICATION_KEY + "documentdb_RUs"; /// Web service listening port public int Port { get; } @@ -33,10 +33,10 @@ public class Config : IConfig public Config(IConfigData configData) { - this.Port = configData.GetInt(PortKey); + this.Port = configData.GetInt(PORT_KEY); - var storageType = configData.GetString(StorageTypeKey).ToLowerInvariant(); - var documentDbConnString = configData.GetString(DocumentDbConnectionStringKey); + var storageType = configData.GetString(STORAGE_TYPE_KEY).ToLowerInvariant(); + var documentDbConnString = configData.GetString(DOCUMENT_DB_CONNECTION_STRING_KEY); if (storageType == "documentdb" && (string.IsNullOrEmpty(documentDbConnString) || documentDbConnString.StartsWith("${") @@ -59,9 +59,9 @@ public Config(IConfigData configData) { StorageType = storageType, DocumentDbConnString = documentDbConnString, - DocumentDbDatabase = configData.GetString(DocumentDbDatabaseKey), - DocumentDbCollection = configData.GetString(DocumentDbCollectionKey), - DocumentDbRUs = configData.GetInt(DocumentDbRUsKey), + DocumentDbDatabase = configData.GetString(DOCUMENT_DB_DATABASE_KEY), + DocumentDbCollection = configData.GetString(DOCUMENT_DB_COLLECTION_KEY), + DocumentDbRUs = configData.GetInt(DOCUMENT_DB_RUS_KEY), }; } } diff --git a/WebService/v1/Controllers/StatusController.cs b/WebService/v1/Controllers/StatusController.cs index d842ce9..dc68bc4 100644 --- a/WebService/v1/Controllers/StatusController.cs +++ b/WebService/v1/Controllers/StatusController.cs @@ -7,7 +7,7 @@ namespace Microsoft.Azure.IoTSolutions.StorageAdapter.WebService.v1.Controllers { - [Route(Version.Path + "/[controller]"), TypeFilter(typeof(ExceptionsFilterAttribute))] + [Route(Version.PATH + "/[controller]"), TypeFilter(typeof(ExceptionsFilterAttribute))] public sealed class StatusController : Controller { private readonly ILogger log; diff --git a/WebService/v1/Controllers/ValuesController.cs b/WebService/v1/Controllers/ValuesController.cs index 0dcaa9d..7d64797 100644 --- a/WebService/v1/Controllers/ValuesController.cs +++ b/WebService/v1/Controllers/ValuesController.cs @@ -15,12 +15,12 @@ namespace Microsoft.Azure.IoTSolutions.StorageAdapter.WebService.v1.Controllers { - [Route(Version.Path), TypeFilter(typeof(ExceptionsFilterAttribute))] + [Route(Version.PATH), TypeFilter(typeof(ExceptionsFilterAttribute))] public class ValuesController : Controller { private readonly IKeyValueContainer container; private readonly IKeyGenerator keyGenerator; - private readonly ILogger logger; + private readonly ILogger log; public ValuesController( IKeyValueContainer container, @@ -29,15 +29,15 @@ public ValuesController( { this.container = container; this.keyGenerator = keyGenerator; - this.logger = logger; + this.log = logger; } [HttpGet("collections/{collectionId}/values/{key}")] public async Task Get(string collectionId, string key) { - EnsureValidId(collectionId, key); + this.EnsureValidId(collectionId, key); - var result = await container.GetAsync(collectionId, key); + var result = await this.container.GetAsync(collectionId, key); return new ValueApiModel(result); } @@ -45,42 +45,40 @@ public async Task Get(string collectionId, string key) [HttpGet("collections/{collectionId}/values")] public async Task Get(string collectionId) { - EnsureValidId(collectionId); + this.EnsureValidId(collectionId); - var result = await container.GetAllAsync(collectionId); + var result = await this.container.GetAllAsync(collectionId); return new ValueListApiModel(result, collectionId); } [HttpPost("collections/{collectionId}/values")] - public async Task Post(string collectionId, [FromBody]ValueServiceModel model) + public async Task Post(string collectionId, [FromBody] ValueServiceModel model) { if (model == null) { throw new InvalidInputException("The request is empty"); } - string key = keyGenerator.Generate(); - EnsureValidId(collectionId, key); + string key = this.keyGenerator.Generate(); + this.EnsureValidId(collectionId, key); - var result = await container.CreateAsync(collectionId, key, model); + var result = await this.container.CreateAsync(collectionId, key, model); return new ValueApiModel(result); } [HttpPut("collections/{collectionId}/values/{key}")] - public async Task Put(string collectionId, string key, [FromBody]ValueServiceModel model) + public async Task Put(string collectionId, string key, [FromBody] ValueServiceModel model) { if (model == null) { throw new InvalidInputException("The request is empty"); } - EnsureValidId(collectionId, key); + this.EnsureValidId(collectionId, key); - var result = model.ETag == null ? - await container.CreateAsync(collectionId, key, model) : - await container.UpsertAsync(collectionId, key, model); + var result = model.ETag == null ? await this.container.CreateAsync(collectionId, key, model) : await this.container.UpsertAsync(collectionId, key, model); return new ValueApiModel(result); } @@ -88,9 +86,9 @@ await container.CreateAsync(collectionId, key, model) : [HttpDelete("collections/{collectionId}/values/{key}")] public async Task Delete(string collectionId, string key) { - EnsureValidId(collectionId, key); + this.EnsureValidId(collectionId, key); - await container.DeleteAsync(collectionId, key); + await this.container.DeleteAsync(collectionId, key); } private void EnsureValidId(string collectionId, string key = "") @@ -102,14 +100,14 @@ private void EnsureValidId(string collectionId, string key = "") if (!collectionId.All(c => char.IsLetterOrDigit(c) || validCharacters.Contains(c))) { var message = $"Invalid collectionId: '{collectionId}'"; - logger.Info(message, () => new { collectionId }); + this.log.Info(message, () => new { collectionId }); throw new BadRequestException(message); } if (key.Any() && !key.All(c => char.IsLetterOrDigit(c) || validCharacters.Contains(c))) { var message = $"Invalid key: '{key}'"; - logger.Info(message, () => new { key }); + this.log.Info(message, () => new { key }); throw new BadRequestException(message); } @@ -121,7 +119,7 @@ private void EnsureValidId(string collectionId, string key = "") if (id.Length > 255) { var message = $"The collectionId/Key are too long: '{collectionId}', '{key}'"; - logger.Info(message, () => new { collectionId, key, id }); + this.log.Info(message, () => new { collectionId, key, id }); throw new BadRequestException(message); } } diff --git a/WebService/v1/Models/StatusApiModel.cs b/WebService/v1/Models/StatusApiModel.cs index a94de6b..6086c3c 100644 --- a/WebService/v1/Models/StatusApiModel.cs +++ b/WebService/v1/Models/StatusApiModel.cs @@ -9,7 +9,7 @@ namespace Microsoft.Azure.IoTSolutions.StorageAdapter.WebService.v1.Models { public sealed class StatusApiModel { - private const string DateFormat = "yyyy-MM-dd'T'HH:mm:sszzz"; + private const string DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:sszzz"; [JsonProperty(PropertyName = "Name", Order = 10)] public string Name => "StorageAdapter"; @@ -18,10 +18,10 @@ public sealed class StatusApiModel public string Status { get; set; } [JsonProperty(PropertyName = "CurrentTime", Order = 30)] - public string CurrentTime => DateTimeOffset.UtcNow.ToString(DateFormat); + public string CurrentTime => DateTimeOffset.UtcNow.ToString(DATE_FORMAT); [JsonProperty(PropertyName = "StartTime", Order = 40)] - public string StartTime => Uptime.Start.ToString(DateFormat); + public string StartTime => Uptime.Start.ToString(DATE_FORMAT); [JsonProperty(PropertyName = "UpTime", Order = 50)] public long UpTime => Convert.ToInt64(Uptime.Duration.TotalSeconds); @@ -49,8 +49,8 @@ public sealed class StatusApiModel [JsonProperty(PropertyName = "$metadata", Order = 1000)] public Dictionary Metadata => new Dictionary { - { "$type", "Status;" + Version.Number }, - { "$uri", "/" + Version.Path + "/status" } + { "$type", "Status;" + Version.NUMBER }, + { "$uri", "/" + Version.PATH + "/status" } }; public StatusApiModel(bool isOk, string msg) diff --git a/WebService/v1/Models/ValueApiModel.cs b/WebService/v1/Models/ValueApiModel.cs index 9cdbb01..7ce84ca 100644 --- a/WebService/v1/Models/ValueApiModel.cs +++ b/WebService/v1/Models/ValueApiModel.cs @@ -29,9 +29,9 @@ public ValueApiModel(ValueServiceModel model) this.Metadata = new Dictionary { - { "$type", $"Value;{Version.Number}" }, + { "$type", $"Value;{Version.NUMBER}" }, { "$modified", model.Timestamp.ToString(CultureInfo.InvariantCulture) }, - { "$uri", $"/{Version.Path}/collections/{model.CollectionId}/values/{model.Key}" } + { "$uri", $"/{Version.PATH}/collections/{model.CollectionId}/values/{model.Key}" } }; } } diff --git a/WebService/v1/Models/ValueListApiModel.cs b/WebService/v1/Models/ValueListApiModel.cs index 2a9bc3d..7b25819 100644 --- a/WebService/v1/Models/ValueListApiModel.cs +++ b/WebService/v1/Models/ValueListApiModel.cs @@ -17,12 +17,12 @@ public class ValueListApiModel public ValueListApiModel(IEnumerable models, string collectionId) { - Items = models.Select(m => new ValueApiModel(m)); + this.Items = models.Select(m => new ValueApiModel(m)); - Metadata = new Dictionary + this.Metadata = new Dictionary { - { "$type", $"ValueList;{Version.Number}" }, - { "$uri", $"/{Version.Path}/collections/{collectionId}/values" } + { "$type", $"ValueList;{Version.NUMBER}" }, + { "$uri", $"/{Version.PATH}/collections/{collectionId}/values" } }; } } diff --git a/WebService/v1/Version.cs b/WebService/v1/Version.cs index d224362..30303e0 100644 --- a/WebService/v1/Version.cs +++ b/WebService/v1/Version.cs @@ -6,12 +6,12 @@ namespace Microsoft.Azure.IoTSolutions.StorageAdapter.WebService.v1 public static class Version { /// Number used for routing HTTP requests - public const string Number = "1"; + public const string NUMBER = "1"; /// Full path used in the URL - public const string Path = "v" + Number; + public const string PATH = "v" + NUMBER; /// Date when the API version has been published - public const string Date = "201706"; + public const string DATE = "201711"; } } diff --git a/pcs-storage-adapter.sln.DotSettings b/pcs-storage-adapter.sln.DotSettings index d7dd9e8..a3e6444 100644 --- a/pcs-storage-adapter.sln.DotSettings +++ b/pcs-storage-adapter.sln.DotSettings @@ -63,6 +63,7 @@ <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + True True True True