public void CreateOptions()
        {
            var dbName  = "TEST_DB_" + DateTime.UtcNow;
            var options = new DbContextOptionsBuilder <DigNDB_SmittestopContext>().UseInMemoryDatabase(databaseName: dbName).Options;

            _dbContext = CreateDataBaseContext(options);
        }
Exemple #2
0
        public void CreateDataSet()
        {
            var options = new DbContextOptionsBuilder <DigNDB_SmittestopContext>()
                          .UseInMemoryDatabase(nameof(EuGatewayServiceUploadTest))
                          .Options;

            _dbContext = new DigNDB_SmittestopContext(options);
            _dbContext.Database.EnsureDeleted();

            _temporaryExposureKeyRepositoryLogger = new Mock <ILogger <TemporaryExposureKeyRepository> >(MockBehavior.Loose);
            _loggerGatewayWebContextReader        = new Mock <ILogger <GatewayWebContextReader> >();
            var translationsRepositoryMock = new Mock <IGenericRepository <Translation> >(MockBehavior.Strict);

            _countryRepository = new CountryRepository(_dbContext, translationsRepositoryMock.Object, new AppSettingsConfig());
            _autoMapper        = CreateAutoMapperWithDependencies(_countryRepository);

            _config = new EuGatewayConfig()
            {
                AuthenticationCertificateFingerprint = "AuthenticationCertificateFingerprint",
                SigningCertificateFingerprint        = "SigningCertificateFingerprint",
                Url = "http://netcompany.pl",
            };

            _epochConverter = new EpochConverter();

            _denmark = TestCountryBuilder.Denmark.Build();
            _poland  = TestCountryBuilder.Poland.Build();
            _germany = TestCountryBuilder.Germany.Build();
            _latviaDisabledDownload = TestCountryBuilder.Latvia
                                      .SetIsPullingFromGatewayEnabled(false)
                                      .Build();

            _dbContext.AddRange(_denmark, _poland, _germany, _latviaDisabledDownload);
            _dbContext.SaveChanges();
        }
Exemple #3
0
        public void TestInsert()
        {
            var context = new DigNDB_SmittestopContext(_options);

            context.Database.EnsureDeleted();

            var genericRepository = new GenericRepository <TemporaryExposureKey>(context);

            var entity = new TemporaryExposureKey
            {
                Id                    = Guid.NewGuid(),
                CreatedOn             = DateTime.Now,
                KeyData               = new byte[] { 1, 2, 3 },
                RollingPeriod         = 123,
                RollingStartNumber    = 123,
                TransmissionRiskLevel = RiskLevel.RISK_LEVEL_LOW
            };

            genericRepository.Insert(entity);
            context.SaveChanges();

            var retrievedEntity = context.TemporaryExposureKey.Find(entity.Id);

            Assert.AreEqual(retrievedEntity, entity);
        }
        private DigNDB_SmittestopContext CreateDataBaseContext(DbContextOptions <DigNDB_SmittestopContext> options)
        {
            var context = new DigNDB_SmittestopContext(options);

            context.Database.EnsureDeleted();
            return(context);
        }
        public void CreateDataSet()
        {
            var options = new DbContextOptionsBuilder <DigNDB_SmittestopContext>()
                          .UseInMemoryDatabase(nameof(EuGatewayServiceUploadTest))
                          .Options;

            _dbContext = new DigNDB_SmittestopContext(options);
            _dbContext.Database.EnsureDeleted();

            _config = new EuGatewayConfig()
            {
                AuthenticationCertificateFingerprint = "AuthenticationCertificateFingerprint",
                SigningCertificateFingerprint        = "SigningCertificateFingerprint",
                Url = "http://netcompany.pl",
            };

            _denmark = TestCountryBuilder.Denmark.Build();
            _poland  = TestCountryBuilder.Poland.Build();
            _germany = TestCountryBuilder.Germany.Build();
            _latvia  = TestCountryBuilder.Latvia.Build();

            _dbContext.AddRange(new Country[] { _denmark, _poland, _germany, _latvia });
            _dbContext.SaveChanges();

            _originCountry = _denmark;
        }
        public static void InitializeDbForTests(DigNDB_SmittestopContext db)
        {
            var tokens = GetTokens();

            db.JwtToken.AddRange(tokens);
            db.SaveChanges();
        }
Exemple #7
0
        public void SetUp()
        {
            GenerateData();
            DbContextOptions <DigNDB_SmittestopContext> options =
                new DbContextOptionsBuilder <DigNDB_SmittestopContext>()
                .UseInMemoryDatabase(nameof(ApplicationStatisticsRepositoryTests)).Options;

            _context = new DigNDB_SmittestopContext(options);
        }
Exemple #8
0
        private DigNDB_SmittestopContext CreatePopulatedContext <T>(IEnumerable <T> entities)
            where T : class
        {
            var context = new DigNDB_SmittestopContext(_options);

            context.Database.EnsureDeleted();
            context.AddRange(entities);
            context.SaveChanges();

            return(context);
        }
Exemple #9
0
        public void SetUp()
        {
            GenerateData();
            DbContextOptions <DigNDB_SmittestopContext> options =
                new DbContextOptionsBuilder <DigNDB_SmittestopContext>()
                .UseInMemoryDatabase(nameof(ApplicationStatisticsRepositoryTests)).Options;

            _context = new DigNDB_SmittestopContext(options);
            _context.Database.EnsureDeleted();
            _context.AddRange(_covidStatisticsMockData);
            _context.SaveChanges();
        }
        public void TestGetByKey()
        {
            var entities = CreateMockedListSettings();

            using DigNDB_SmittestopContext context = CreatePopulatedContext(entities);

            var settingRepository = new SettingRepository(context);

            var insertedEntities = settingRepository.FindSettingByKey("key");

            Assert.AreEqual(insertedEntities, entities.First());
        }
Exemple #11
0
        public void TestGetAllAsync()
        {
            var expectDate = DateTime.UtcNow;
            var entities   = CreateMockedListExposureKeys(expectDate, 2);

            using DigNDB_SmittestopContext context = CreatePopulatedContext(entities);

            var genericRepository = new GenericRepository <TemporaryExposureKey>(context);

            var insertedEntities = genericRepository.GetAllAsync().Result;

            CollectionAssert.AreEquivalent(insertedEntities, entities);
        }
Exemple #12
0
        public void RemoveKeyDuplicates_ShouldOnlyWorkOnKeysCreatedUpTo14DaysAgo()
        {
            var options = new DbContextOptionsBuilder <DigNDB_SmittestopContext>()
                          .UseInMemoryDatabase(nameof(EuGatewayServiceUploadTest))
                          .Options;

            var dbContext = new DigNDB_SmittestopContext(options);

            dbContext.Database.EnsureDeleted();
            var translationsRepositoryMock = new Mock <IGenericRepository <Translation> >(MockBehavior.Strict);

            var originSpecificSettings = new AppSettingsConfig()
            {
                OriginCountryCode = "dk"
            };
            var countryRepository = new CountryRepository(dbContext, translationsRepositoryMock.Object, originSpecificSettings);
            var keysRepository    = new TemporaryExposureKeyRepository(dbContext, countryRepository);

            _keyFilter = new KeyFilter(_keyMapper, _keyValidator.Object, new ExposureKeyMapper(), _logger.Object, keysRepository);

            var rollingStartNumberNewerThan14Days = DateTimeOffset.Now.Subtract(new TimeSpan(DaysOffset - 1, 0, 0, 0)).ToUnixTimeSeconds();
            var rollingStartNumberOlderThan14Days = DateTimeOffset.Now.Subtract(new TimeSpan(DaysOffset + 3, 0, 0, 0)).ToUnixTimeSeconds();
            var origin = new Country {
                Id = 1
            };
            var keysNewerThan14Days = new List <TemporaryExposureKey>
            {
                new TemporaryExposureKey {
                    Id = Guid.NewGuid(), RollingStartNumber = rollingStartNumberNewerThan14Days, Origin = origin, KeyData = new byte[] { 1 }
                },
            };
            var keysOlderThan14Days = new List <TemporaryExposureKey>
            {
                new TemporaryExposureKey {
                    Id = Guid.NewGuid(), RollingStartNumber = rollingStartNumberOlderThan14Days, Origin = origin, KeyData = new byte[] { 2 }
                },
                new TemporaryExposureKey {
                    Id = Guid.NewGuid(), RollingStartNumber = rollingStartNumberOlderThan14Days, Origin = origin, KeyData = new byte[] { 3 }
                },
            };
            var keyList = keysNewerThan14Days.Concat(keysOlderThan14Days).ToList();

            dbContext.TemporaryExposureKey.AddRange(keyList);
            dbContext.SaveChanges();

            var filteredList = _keyFilter.RemoveKeyDuplicatesAsync(keyList).Result;

            filteredList.Count.Should().Be(keysOlderThan14Days.Count);
        }
Exemple #13
0
        public void TestGetById()
        {
            var expectDate = DateTime.UtcNow;
            var entities   = CreateMockedListExposureKeys(expectDate, 2);

            using DigNDB_SmittestopContext context = CreatePopulatedContext(entities);
            var genericRepository = new GenericRepository <TemporaryExposureKey>(context);

            var firstEntity   = entities.First();
            var firstEntityId = firstEntity.Id;

            var insertedEntity = genericRepository.GetById(firstEntityId);

            Assert.AreEqual(insertedEntity, firstEntity);
        }
        public void HasNoPendingModelChanges()
        {
            var optionsBuilder = new DbContextOptionsBuilder <DigNDB_SmittestopContext>();

            optionsBuilder
            .UseSqlServer(new SqlConnection())
            .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning));
            var databaseMigrationContext = new DigNDB_SmittestopContext(optionsBuilder.Options);

            var modelDiffer        = databaseMigrationContext.GetService <IMigrationsModelDiffer>();
            var migrationsAssembly = databaseMigrationContext.GetService <IMigrationsAssembly>();

            var modelDifferences = modelDiffer.GetDifferences(migrationsAssembly.ModelSnapshot.Model, databaseMigrationContext.Model);

            Assert.AreEqual(0, modelDifferences.Count);
        }
        public void AddTemporaryExposureKeys_ProvideKeys_ShouldAddNewKeysToDB()
        {
            var data = CreateMockedListExposureKeys(DateTime.UtcNow, 4, false);

            using (var context = new DigNDB_SmittestopContext(_options))
            {
                context.Database.EnsureDeleted();
                _repo = new TemporaryExposureKeyRepository(context, _countryRepository.Object);
                _repo.AddTemporaryExposureKeys(data).Wait();
            }
            using (var context = new DigNDB_SmittestopContext(_options))
            {
                var keyInDB = context.TemporaryExposureKey.ToList();
                Assert.AreEqual(4, keyInDB.Count);
            }
        }
        public void GetAll_HaveData_ShouldReturnCorrectNumberOfRecord()
        {
            var expectKeys = 4;
            var data       = CreateMockedListExposureKeys(DateTime.UtcNow, expectKeys, true);

            using (var context = new DigNDB_SmittestopContext(_options))
            {
                context.Database.EnsureDeleted();
                //add data to context
                context.TemporaryExposureKey.AddRange(data);
                context.SaveChanges();
                _repo = new TemporaryExposureKeyRepository(context, _countryRepository.Object);
                var keys = _repo.GetAll().Result;
                Assert.AreEqual(expectKeys, keys.Count);
            }
        }
        public void GetAllKeyData_HaveData_ShouldReturnCorrectResult()
        {
            var data           = CreateMockedListExposureKeys(DateTime.UtcNow, 4, true);
            var expectKeysData = data.Select(x => x.KeyData).ToList();

            using (var context = new DigNDB_SmittestopContext(_options))
            {
                context.Database.EnsureDeleted();
                //add data to context
                context.TemporaryExposureKey.AddRange(data);
                context.SaveChanges();
                _repo = new TemporaryExposureKeyRepository(context, _countryRepository.Object);
                var keys = _repo.GetAllKeyData().Result;
                CollectionAssert.AreEqual(expectKeysData, keys);
            }
        }
Exemple #18
0
        public void TestDeleteById()
        {
            var expectDate = DateTime.UtcNow;
            var entities   = CreateMockedListExposureKeys(expectDate, 2);

            using DigNDB_SmittestopContext context = CreatePopulatedContext(entities);
            var genericRepository = new GenericRepository <TemporaryExposureKey>(context);

            var firstEntity = entities.First();

            genericRepository.Delete(firstEntity.Id);
            context.SaveChanges();

            var retrievedEntity = genericRepository.GetById(firstEntity.Id);

            Assert.AreEqual(retrievedEntity, null);
        }
Exemple #19
0
        public void TestEdit()
        {
            var expectDate = DateTime.UtcNow;
            var entities   = CreateMockedListExposureKeys(expectDate, 2);

            using DigNDB_SmittestopContext context = CreatePopulatedContext(entities);
            var genericRepository = new GenericRepository <TemporaryExposureKey>(context);

            var entity = entities.First();

            entity.RollingPeriod = 567;

            genericRepository.Edit(entity);
            context.SaveChanges();

            var retrievedEntity = context.TemporaryExposureKey.Find(entity.Id);

            Assert.AreEqual(retrievedEntity.RollingPeriod, 567);
        }
        public void HasValidTargetModel()
        {
            var optionsBuilder = new DbContextOptionsBuilder <DigNDB_SmittestopContext>();

            optionsBuilder
            .UseSqlServer(new SqlConnection())
            .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning));
            var databaseMigrationContext = new DigNDB_SmittestopContext(optionsBuilder.Options);

            var modelDiffer        = databaseMigrationContext.GetService <IMigrationsModelDiffer>();
            var migrationsAssembly = databaseMigrationContext.GetService <IMigrationsAssembly>();

            var latestMigration = migrationsAssembly.Migrations.Last();
            var targetModel     = migrationsAssembly.CreateMigration(latestMigration.Value, databaseMigrationContext.Database.ProviderName).TargetModel;

            var modelDifferences = modelDiffer.GetDifferences(migrationsAssembly.ModelSnapshot.Model, targetModel);

            Assert.AreEqual(0, modelDifferences.Count);
        }
        public void GetById_HaveRecord_ShouldReturnCorrectRecord()
        {
            var data = CreateMockedListExposureKeys(DateTime.UtcNow, 4, true);

            using (var context = new DigNDB_SmittestopContext(_options))
            {
                context.Database.EnsureDeleted();
                context.TemporaryExposureKey.AddRange(data);
                context.SaveChanges();
                _repo = new TemporaryExposureKeyRepository(context, _countryRepository.Object);
                var expectKey = data[0];
                var actualKey = _repo.GetById(expectKey.Id).Result;

                Assert.AreEqual(expectKey.Id, actualKey.Id);
                Assert.AreEqual(expectKey.TransmissionRiskLevel, actualKey.TransmissionRiskLevel);
                Assert.AreEqual(expectKey.CreatedOn, actualKey.CreatedOn);
                Assert.AreEqual(expectKey.KeyData, actualKey.KeyData);
            }
        }
        public void GetTemporaryExposureKeys_HaveRecord_ShouldReturnCorrectRecordMatchedRequirement()
        {
            var expectDate         = DateTime.UtcNow;
            var dataForCurrentDate = CreateMockedListExposureKeys(expectDate, 2, true);
            var dataForOtherDate   = CreateMockedListExposureKeys(expectDate.AddDays(-12), 3, true);
            var dataForNotDK       = CreateMockedListExposureKeys(expectDate.AddDays(-12), 3, false);

            using (var context = new DigNDB_SmittestopContext(_options))
            {
                context.Database.EnsureDeleted();
                //add data to context
                context.TemporaryExposureKey.AddRange(dataForCurrentDate);
                context.TemporaryExposureKey.AddRange(dataForOtherDate);
                context.TemporaryExposureKey.AddRange(dataForNotDK);
                context.SaveChanges();
                _repo = new TemporaryExposureKeyRepository(context, _countryRepository.Object);
                var keys = _repo.GetKeysOnlyFromApiOriginCountry(expectDate, 0);
                Assert.AreEqual(dataForCurrentDate.Count, keys.Count);
            }
        }
        public void AddTemporaryExposureKey_ProvideKey_ShouldAddNewKeyToDB()
        {
            TemporaryExposureKey key = new TemporaryExposureKey()
            {
                CreatedOn             = DateTime.UtcNow.Date,
                Id                    = Guid.NewGuid(),
                KeyData               = Encoding.ASCII.GetBytes("keyData"),
                TransmissionRiskLevel = RiskLevel.RISK_LEVEL_LOW,
            };

            using (var context = new DigNDB_SmittestopContext(_options))
            {
                context.Database.EnsureDeleted();
                _repo = new TemporaryExposureKeyRepository(context, _countryRepository.Object);
                _repo.AddTemporaryExposureKey(key).Wait();
            }
            using (var context = new DigNDB_SmittestopContext(_options))
            {
                var keyInDB = context.TemporaryExposureKey.ToList();
                Assert.AreEqual(1, keyInDB.Count);
                Assert.IsNotNull(keyInDB.FirstOrDefault(k => k.Id == key.Id));
            }
        }
        private CountryRepository CreateCountryRepository(Country[] fakeCountries)
        {
            DbContextOptions <DigNDB_SmittestopContext> options =
                new DbContextOptionsBuilder <DigNDB_SmittestopContext>()
                .UseInMemoryDatabase(nameof(CountryRepositoryTests)).Options;
            var context = new DigNDB_SmittestopContext(options);

            context.Database.EnsureDeleted();

            fakeCountries[0].Code = ExampleCountryCodePl;
            fakeCountries[1].Code = ExampleCountryCodeDk;

            var exposureKeyCountries = new List <TemporaryExposureKeyCountry>
            {
                new TemporaryExposureKeyCountry
                {
                    CountryId = 1
                },
                new TemporaryExposureKeyCountry
                {
                    CountryId = 2
                },
            };

            context.AddRange(fakeCountries);
            context.AddRange(exposureKeyCountries);
            context.SaveChanges();

            var translationRepositoryMock = new Mock <IGenericRepository <Translation> >();

            IOriginSpecificSettings config = new AppSettingsConfig()
            {
                OriginCountryCode = ExampleCountryCodeDk
            };

            return(new CountryRepository(context, translationRepositoryMock.Object, config));
        }
        public void Test()
        {
            var context  = new DigNDB_SmittestopContext(_options);
            var entities = CreateMockedListSettings();

            context.Database.EnsureDeleted();
            var settingRepository = new SettingRepository(context);

            var key   = "key";
            var value = "value";

            settingRepository.SetSetting(key, value);
            var retrievedEntity = context.Setting.Where(x => x.Key == key).Single();
            var retrievedValue  = retrievedEntity.Value;

            var value2 = "value2";

            settingRepository.SetSetting(key, value2);
            var retrievedEntity2 = context.Setting.Where(x => x.Key == key).Single();
            var retrievedValue2  = retrievedEntity2.Value;

            Assert.AreEqual(value, retrievedValue);
            Assert.AreEqual(value2, retrievedValue2);
        }
 public JwtTokenRepository(DigNDB_SmittestopContext context) : base(context)
 {
 }
Exemple #27
0
 public ApplicationStatisticsRepository(DigNDB_SmittestopContext context) : base(context)
 {
 }
Exemple #28
0
 // constructor used for unit tests
 public TemporaryExposureKeyRepository(DigNDB_SmittestopContext dbContext, ICountryRepository countryRepository, ILogger <TemporaryExposureKeyRepository> logger)
 {
     _logger            = logger;
     _countryRepository = countryRepository;
     _dbContext         = dbContext;
 }
Exemple #29
0
 public SettingRepository(DigNDB_SmittestopContext context) : base(context)
 {
 }
 public CountryRepository(DigNDB_SmittestopContext context, IGenericRepository <Translation> translationRepository, IOriginSpecificSettings originSpecificSettings) : base(context)
 {
     _translationRepository  = translationRepository;
     _originSpecificSettings = originSpecificSettings;
 }