public async Task WorkerQueueDataCorrectlySerialized()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();

            var managerJob = GetManagerImportJob();

            await CreateValidFile(1, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();
            var originalUserDictionary = await GetDummyUserMetaData();

            var userMetaData = _importHelper.InsertedWorkerQueueRecords.Rows[0][Constant.Sql.ColumnsNames.ImportWorkerQueue.MetaData].ToString();
            var obj          = JsonConvert.DeserializeObject <UserAdminObject>(userMetaData, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Objects
            });
            var processedUserValues = await obj.GetColumnsAsDictionaryAsync();

            // Assert
            Assert.IsTrue(originalUserDictionary.Count == processedUserValues.Count);
            Assert.IsTrue(!originalUserDictionary.Except(processedUserValues).Any());
        }
        public async Task ViolationReportedWhenUnableToInsertBatchIntoWorkerQueue()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();

            _importHelper.MockSqlQueryHelper
            .Setup(x => x.BulkInsertIntoTableAsync(It.IsAny <IDBContext>(), It.IsAny <DataTable>(), It.IsAny <String>(), It.IsAny <Int32>()))
            .Callback <IDBContext, DataTable, String, Int32>((dbContext, sourceDataTable, destinationTableName, batchSize) =>
            {
                _importHelper.InsertedWorkerQueueRecords = sourceDataTable;
            })
            .Throws(new Exception());

            var managerJob = GetManagerImportJob();

            await CreateValidFile(1, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            var expectedMsg = String.Format(Constant.ErrorMessages.ImportQueueManagerPopulatingImportWorkerQueueError, 2, 2);

            Assert.IsTrue(_importHelper.RecordedImportJobErrors.Contains(expectedMsg));
        }
        public async Task ViolationReportedWhenFileHasUnsupportedColumns()
        {
            // Arrange
            var unsupportedColumnName = "ExtraUnsupportedColumn";

            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();
            var user        = new UserAdminObject();
            var userColumns = (await user.GetColumnsAsync()).Select(x => x.ColumnName).ToList();

            userColumns.Add(unsupportedColumnName);
            var fileContents = String.Join(Constant.CommaSeparator, userColumns);

            var managerJob = GetManagerImportJob();

            await CreateFile(fileContents, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            var expectedMsg = String.Format(Constant.ErrorMessages.ExtraColumnsError, _importHelper.ImportObjectType, unsupportedColumnName);

            Assert.IsTrue(_importHelper.RecordedImportJobErrors.Contains(expectedMsg));
        }
        public async Task ExecuteAsync_QueueHasARecord_NotDuringOffHours()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: false);
            _importHelper.Initialize();
            var workerJob = GetImportWorkerJob();

            // Act
            await workerJob.ExecuteAsync();

            // Assert
            AssertRecordWasSkipped();
        }
        public void GetCommaDelimitedListOfResourceIds(IEnumerable <Int32> resourceIdsList, String expectedResult)
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();
            var workerJob = GetImportWorkerJob();

            //Act
            var observedResult = workerJob.GetCommaDelimitedListOfResourceIds(resourceIdsList);

            //Assert
            Assert.AreEqual(expectedResult, observedResult);
        }
        public async Task ExecuteAsync_QueueHasNoRecord_DoNotExecute()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true)
            {
                RecordsReturedFromWorkerBatch = 0
            };
            _importHelper.Initialize();
            var workerJob = GetImportWorkerJob();

            // Act
            await workerJob.ExecuteAsync();

            // Assert
            AssertRecordWasSkipped();
        }
        public async Task JobStatusUpdatedAfterWorkerProcessessAllRecords()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();

            var managerJob = GetManagerImportJob();

            await CreateValidFile(1, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            Assert.AreEqual(Constant.Status.Job.COMPLETED_MANAGER, _importHelper.RDOJobStatus);
        }
        public async Task ExecuteAsync_QueueHasARecord_ExecuteAll()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();

            var managerJob = GetManagerImportJob();

            await CreateValidFile(1, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            AssertRecordWasProcessed();
        }
        public async Task CsvIsDeletedAfterImportManagerJobIsDisposed()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();

            var managerJob = GetManagerImportJob();
            var filePath   = managerJob.LocalTempFilePath;

            await CreateValidFile(1, filePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            Assert.IsTrue(!File.Exists(filePath));
        }
        public async Task WorkerTablePopulatedQueueTableStatusUpdatedRdoStatusUpdatedWhenManagerSuccessfullyProcessesQueueRecord()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();

            var managerJob = GetManagerImportJob();

            await CreateValidFile(1, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            _importHelper.MockSqlQueryHelper.Verify(x => x.BulkInsertIntoTableAsync(It.IsAny <IDBContext>(), It.IsAny <DataTable>(), It.IsAny <String>(), It.IsAny <Int32>()), Times.AtLeastOnce);
            _importHelper.MockSqlQueryHelper.Verify(x => x.UpdateQueueStatusAsync(It.IsAny <IDBContext>(), It.IsAny <Int32>(), It.IsAny <Int32>(), It.IsAny <String>(), It.IsAny <String>(), It.IsAny <Int32>()), Times.AtLeastOnce);
            Assert.AreEqual(Constant.Status.Job.COMPLETED_MANAGER, _importHelper.RDOJobStatus);
        }
        public async Task ViolationReportedWhenFileContainsColumnsButNoMetaData()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();

            var managerJob = GetManagerImportJob();

            await CreateValidFile(0, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            var expectedMsg = Constant.ErrorMessages.FileContainsColumnsButNoMetaDataError;

            Assert.IsTrue(_importHelper.RecordedImportJobErrors.Contains(expectedMsg));
        }
        public async Task ViolationReportedWhenUnsupportedObjectSelected()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true)
            {
                ImportObjectType = "RandomFakeNonExistingObject"
            };
            _importHelper.Initialize();

            var managerJob = GetManagerImportJob();

            await CreateValidFile(1, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            Assert.IsTrue(_importHelper.RecordedImportJobErrors.FirstOrDefault(x => x.ToLower().Contains("unsupported object")) != null);
        }
        public async Task ExecuteAsync_JobContinuesToNextRecordAfterOneCausesException_ExecuteAll()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true)
            {
                RecordsReturedFromWorkerBatch = 2
            };
            _importHelper.Initialize();
            _importHelper.MockClientRepo
            .SetupSequence(x => x.Query(It.IsAny <Query <Client> >(), It.IsAny <Int32>()))
            .Throws(new Exception())
            .Returns(_importHelper.MockClientResult);

            var workerJob = GetImportWorkerJob();

            // Act
            await workerJob.ExecuteAsync();

            // Assert
            AsserUserWasCreated(1);
            AssertClientWasQueried(2);
        }
        public async Task ExecuteAsync_ErrorsAreRecordedWhenOneFailsAndOtherSucceeds_ExecuteAll()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true)
            {
                RecordsReturedFromWorkerBatch = 2
            };
            _importHelper.Initialize();
            _importHelper.MockClientRepo
            .SetupSequence(x => x.Query(It.IsAny <Query <Client> >(), It.IsAny <Int32>()))
            .Throws(new Exception())
            .Returns(_importHelper.MockClientResult);

            var workerJob = GetImportWorkerJob();

            // Act
            await workerJob.ExecuteAsync();

            // Assert
            AssertClientWasQueried(2);
            Assert.IsTrue(_importHelper.RecordedImportJobErrors.Any());
        }
        public async Task WorkerRecordsAreNotWrittenAndStatusUpdatedAndQueueRecordsDeletedWhenFileContainsViolations()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true)
            {
                ImportObjectType = "RandomFakeNonExistingObject"
            };
            _importHelper.Initialize();

            var managerJob = GetManagerImportJob();

            await CreateValidFile(1, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            _importHelper.MockSqlQueryHelper.Verify(x => x.BulkInsertIntoTableAsync(It.IsAny <IDBContext>(), It.IsAny <DataTable>(), It.IsAny <String>(), It.IsAny <Int32>()), Times.Never);
            _importHelper.MockSqlQueryHelper.Verify(x => x.DeleteRecordFromQueueAsync(It.IsAny <IDBContext>(), It.IsAny <String>(), It.IsAny <Int32>(), It.IsAny <String>(), It.IsAny <Int32>(), It.IsAny <String>()), Times.Exactly(2));
            Assert.AreEqual(Constant.Status.Job.ERROR, _importHelper.RDOJobStatus);
        }
        public async Task ManagerContinuesToNextBatchEvenWhenThePreviousBatchErrorsOut()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();

            _importHelper.MockSqlQueryHelper
            .SetupSequence(x => x.BulkInsertIntoTableAsync(It.IsAny <IDBContext>(), It.IsAny <DataTable>(), It.IsAny <String>(), It.IsAny <Int32>()))
            .Throws(new Exception())
            .Returns(Task.FromResult(0));

            var managerJob = GetManagerImportJob();

            // This file should contain twice as many records as the batch size to make sure the logic is executed twice
            await CreateValidFile((Constant.BatchSizes.ImportManagerIntoWorkerQueue * 2), managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            _importHelper.MockSqlQueryHelper.Verify(x => x.BulkInsertIntoTableAsync(It.IsAny <IDBContext>(), It.IsAny <DataTable>(), It.IsAny <String>(), It.IsAny <Int32>()), Times.Exactly(2));
        }
        public async Task QueueRecordsDeletedAndJobStatusUpdatedAfterJobCancellationRequested()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true)
            {
                ManagerQueueRecordStatus = Constant.Status.Queue.CANCELLATION_REQUESTED
            };
            _importHelper.Initialize();

            var managerJob = GetManagerImportJob();

            await CreateValidFile(1, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            var expectedStatus = Constant.Status.Job.CANCELLED;

            _importHelper.MockSqlQueryHelper.Verify(x => x.DeleteRecordFromQueueAsync(It.IsAny <IDBContext>(), It.IsAny <String>(), It.IsAny <Int32>(), It.IsAny <String>(), It.IsAny <Int32>(), It.IsAny <String>()), Times.Exactly(2));
            Assert.AreEqual(expectedStatus, _importHelper.RDOJobStatus);
        }
        public async Task ViolationReportedWhenColumnMissing()
        {
            // Arrange
            _importHelper = new ImportTestHelper(duringOffHours: true);
            _importHelper.Initialize();
            var user          = new UserAdminObject();
            var userColumns   = new Stack <String>((await user.GetColumnsAsync()).Select(x => x.ColumnName).ToList());
            var missingColumn = userColumns.Pop();
            var fileContents  = String.Join(Constant.CommaSeparator, userColumns);

            var managerJob = GetManagerImportJob();

            await CreateFile(fileContents, managerJob.LocalTempFilePath);

            // Act
            await managerJob.ExecuteAsync();

            managerJob.Dispose();

            // Assert
            var expectedMsg = String.Format(Constant.ErrorMessages.MissingColumnsError, missingColumn);

            Assert.IsTrue(_importHelper.RecordedImportJobErrors.Contains(expectedMsg));
        }