From 8f3ee077c7935e8f6b44d8321e0e6fd8ac4f3e45 Mon Sep 17 00:00:00 2001 From: cocoa-dev004 <66989461+cocoa-dev004@users.noreply.github.com> Date: Tue, 26 Jul 2022 10:47:29 +0900 Subject: [PATCH] Changed from Ticks to Epoch milliseconds. --- .../Repository/EventLogRepository.cs | 2 +- .../Repository/EventLogRepositoryTests.cs | 118 +++++++++++++++++- 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs b/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs index 8a723e38f..4442687ea 100644 --- a/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs +++ b/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs @@ -266,7 +266,7 @@ private async Task AddEventNotifiedAsyncInternal(long maxSize) var content = new EventContentExposureNotified() { - NotifiedTimeInMillis = _dateTimeUtility.UtcNow.Ticks + NotifiedTimeInMillis = _dateTimeUtility.UtcNow.ToUnixEpochMillis() }; var eventLog = new EventLog() diff --git a/Covid19Radar/Tests/Covid19Radar.UnitTests/Repository/EventLogRepositoryTests.cs b/Covid19Radar/Tests/Covid19Radar.UnitTests/Repository/EventLogRepositoryTests.cs index a1029b764..90f84d49e 100644 --- a/Covid19Radar/Tests/Covid19Radar.UnitTests/Repository/EventLogRepositoryTests.cs +++ b/Covid19Radar/Tests/Covid19Radar.UnitTests/Repository/EventLogRepositoryTests.cs @@ -22,7 +22,6 @@ public class EventLogRepositoryTests { private readonly MockRepository _mockRepository; private readonly Mock _mockSendEventLogStateRepository; - private readonly Mock _mockDateTimeUtility; private readonly Mock _mockLocalPathService; private readonly Mock _mockLoggerService; private readonly Mock _mockBackupAttributeService; @@ -31,7 +30,6 @@ public EventLogRepositoryTests() { _mockRepository = new MockRepository(MockBehavior.Default); _mockSendEventLogStateRepository = _mockRepository.Create(); - _mockDateTimeUtility = _mockRepository.Create(); _mockLocalPathService = _mockRepository.Create(); _mockLoggerService = _mockRepository.Create(); _mockBackupAttributeService = _mockRepository.Create(); @@ -41,7 +39,7 @@ private EventLogRepository CreateRepository() { return new EventLogRepository( _mockSendEventLogStateRepository.Object, - _mockDateTimeUtility.Object, + new DateTimeUtility(), _mockLocalPathService.Object, _mockLoggerService.Object, _mockBackupAttributeService.Object); @@ -127,6 +125,120 @@ public async Task GetLogsAsyncTest_Expection() Directory.Delete(eventLogsTempPath, true); } + [Fact] + public async Task AddEventNotifiedAsyncTest_Success_Enable() + { + string eventLogsTempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString(), "test_eventlogs"); + + _mockLocalPathService.SetupGet(x => x.EventLogDirPath).Returns(eventLogsTempPath); + _mockSendEventLogStateRepository.Setup(x => x.GetSendEventLogState(EventType.ExposureNotified)).Returns(SendEventLogState.Enable); + + long utcNowEpochMillisecoundsFrom = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + + EventLogRepository unitUnderTest = CreateRepository(); + await unitUnderTest.AddEventNotifiedAsync(long.MaxValue); + + long utcNowEpochMillisecoundsTo = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + + _mockBackupAttributeService.Verify(x => x.SetSkipBackupAttributeToEventLogDir(), Times.Once()); + + Assert.True(Directory.Exists(eventLogsTempPath)); + + string[] files = Directory.GetFiles(eventLogsTempPath); + Assert.Single(files); + + string content = File.ReadAllText(files[0]); + Assert.NotEmpty(content); + + JObject json = JToken.Parse(content) as JObject; + Assert.NotNull(json); + + Assert.Equal(JTokenType.Boolean, json.GetValue("hasConsent").Type); + Assert.True(json.GetValue("hasConsent").Value()); + + Assert.Equal(JTokenType.Integer, json.GetValue("epoch").Type); + Assert.InRange( + json.GetValue("epoch").Value(), + utcNowEpochMillisecoundsFrom / 1000, + utcNowEpochMillisecoundsTo / 1000); + + Assert.Equal(JTokenType.String, json.GetValue("type").Type); + Assert.Equal(EventType.ExposureNotified.Type, json.GetValue("type").Value()); + + Assert.Equal(JTokenType.String, json.GetValue("subtype").Type); + Assert.Equal(EventType.ExposureNotified.SubType, json.GetValue("subtype").Value()); + + Assert.Equal(JTokenType.Object, json.GetValue("content").Type); + + JObject jsonContent = json.GetValue("content") as JObject; + Assert.NotNull(jsonContent); + + Assert.Equal(JTokenType.Integer, jsonContent.GetValue("notifiedTimeInMillis").Type); + Assert.InRange( + jsonContent.GetValue("notifiedTimeInMillis").Value(), + utcNowEpochMillisecoundsFrom, + utcNowEpochMillisecoundsTo); + + Directory.Delete(eventLogsTempPath, true); + } + + [Fact] + public async Task AddEventNotifiedAsyncTest_Success_Disable() + { + string eventLogsTempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString(), "test_eventlogs"); + + _mockLocalPathService.SetupGet(x => x.EventLogDirPath).Returns(eventLogsTempPath); + _mockSendEventLogStateRepository.Setup(x => x.GetSendEventLogState(EventType.ExposureNotified)).Returns(SendEventLogState.Disable); + + long utcNowEpochMillisecoundsFrom = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + + EventLogRepository unitUnderTest = CreateRepository(); + await unitUnderTest.AddEventNotifiedAsync(long.MaxValue); + + long utcNowEpochMillisecoundsTo = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + + _mockBackupAttributeService.Verify(x => x.SetSkipBackupAttributeToEventLogDir(), Times.Once()); + + Assert.True(Directory.Exists(eventLogsTempPath)); + + string[] files = Directory.GetFiles(eventLogsTempPath); + Assert.Single(files); + + string content = File.ReadAllText(files[0]); + Assert.NotEmpty(content); + + JObject json = JToken.Parse(content) as JObject; + Assert.NotNull(json); + + Assert.Equal(JTokenType.Boolean, json.GetValue("hasConsent").Type); + Assert.False(json.GetValue("hasConsent").Value()); + + Assert.Equal(JTokenType.Integer, json.GetValue("epoch").Type); + Assert.InRange( + json.GetValue("epoch").Value(), + utcNowEpochMillisecoundsFrom / 1000, + utcNowEpochMillisecoundsTo / 1000); + + Assert.Equal(JTokenType.String, json.GetValue("type").Type); + Assert.Equal(EventType.ExposureNotified.Type, json.GetValue("type").Value()); + + Assert.Equal(JTokenType.String, json.GetValue("subtype").Type); + Assert.Equal(EventType.ExposureNotified.SubType, json.GetValue("subtype").Value()); + + Assert.Equal(JTokenType.Object, json.GetValue("content").Type); + + JObject jsonContent = json.GetValue("content") as JObject; + Assert.NotNull(jsonContent); + + Assert.Equal(JTokenType.Integer, jsonContent.GetValue("notifiedTimeInMillis").Type); + Assert.InRange( + jsonContent.GetValue("notifiedTimeInMillis").Value(), + utcNowEpochMillisecoundsFrom, + utcNowEpochMillisecoundsTo); + + Directory.Delete(eventLogsTempPath, true); + } + [Fact] public async Task RotateAsyncTest_NoDir() {