Ejemplo n.º 1
0
            public void WhenMainframeFileDoesNotContainOrderHistory_HasValidHeadersAndOrderDate()
            {
                // arrange
                MockDependents     mockDependents = new MockDependents();
                IOrderHistoryLogic testunit       = MakeUnitToBeTested(true, mockDependents);

                // act
                var mockDataReader = new StringReader(string.Empty);
                OrderHistoryFileReturn orderHistoryFiles = testunit.ParseMainframeFile(mockDataReader);

                mockDataReader.Close();

                // assert
                orderHistoryFiles.Files.Count.Should().Be(0);
            }
Ejemplo n.º 2
0
            public void OrderHistory_HasValidHeadersAndOrderDate()
            {
                // arrange
                IOrderHistoryLogic testunit = MakeUnitToBeTested();

                // act
                var mockDataReader = GetMockDataReader("OT780FDF-2018206-10273884.TXT");
                OrderHistoryFileReturn orderHistoryFiles = testunit.ParseMainframeFile(mockDataReader);

                mockDataReader.Close();

                // assert
                orderHistoryFiles.Files.Count.Should().Be(40);
                orderHistoryFiles.Files.ForEach(file => CheckFile(file));
            }
Ejemplo n.º 3
0
        public void ProcessOrderUpdatesTick(object state)
        {
            if (!_orderUpdateProcessing)
            {
                _orderUpdateProcessing = true;

                try
                {
                    // if update path does not exist, send an email then suppress messages
                    // disallow processing here until the folder exists, but do not kill the service
                    // to keep other threads running
                    if (Directory.Exists(Configuration.OrderUpdateWatchPath))
                    {
                        _allowOrderUpdateProcessing = true;
                        _silenceOrderUpdateMessages = false;
                    }
                    else
                    {
                        _allowOrderUpdateProcessing = false;

                        if (!_silenceOrderUpdateMessages)
                        {
                            HandleMissingOrderUpdateWatchPath();

                            _silenceOrderUpdateMessages = true;
                        }
                    }

                    if (_allowOrderUpdateProcessing)
                    {
                        string[] files = Directory.GetFiles(Configuration.OrderUpdateWatchPath);
                        _orderScope = _diContainer.BeginLifetimeScope();

                        IGenericQueueRepository repo = _orderScope.Resolve <IGenericQueueRepository>();

                        Parallel.ForEach(files, filePath =>
                        {
                            IOrderHistoryLogic logic = _orderScope.Resolve <IOrderHistoryLogic>();

                            if (CanOpenFile(filePath))
                            {
                                var logMessage = "Processing " + filePath + ".";
                                _log.WriteInformationLog(logMessage);

                                OrderHistoryFileReturn parsedFile = null;
                                using (var reader = File.OpenText(filePath))
                                {
                                    parsedFile = logic.ParseMainframeFile(reader);
                                }

                                if (parsedFile.Files.Count == 0)
                                {
                                    logMessage = "No instances of OrderHistoryFile were extracted from " + filePath + ".";
                                    _log.WriteWarningLog(logMessage);
                                }

                                var serializedFiles = new BlockingCollection <string>();

                                Parallel.ForEach(parsedFile.Files, file =>
                                {
                                    // do not upload an order file with an invalid header
                                    if (file.ValidHeader)
                                    {
                                        file.SenderApplicationName = Configuration.ApplicationName;
                                        file.SenderProcessName     = "Process Order History Updates From Mainframe (Flat File)";

                                        try
                                        {
                                            var serializedFile = JsonConvert.SerializeObject(file);
                                            serializedFiles.Add(serializedFile);

                                            //StringBuilder logMsg = new StringBuilder();
                                            //logMsg.AppendLine(string.Format("Serializing order history message ({0}).", file.MessageId));
                                            //logMsg.AppendLine();
                                            //logMsg.AppendLine(serializedFile);

                                            //_log.WriteInformationLog(logMsg.ToString());

                                            _silenceOrderUpdateMessages = false;
                                        }
                                        catch (Exception ex)
                                        {
                                            if (!_silenceOrderUpdateMessages)
                                            {
                                                HandleException(ex);
                                                _silenceOrderUpdateMessages = true;
                                            }
                                        }
                                    }
                                });

                                if (serializedFiles.Count > 0)
                                {
                                    logMessage = "Publishing " + serializedFiles.Count + " order history messages to queue.";
                                    _log.WriteInformationLog(logMessage);

                                    repo.BulkPublishToQueue(serializedFiles.ToList(), Configuration.RabbitMQConfirmationServer, Configuration.RabbitMQUserNamePublisher, Configuration.RabbitMQUserPasswordPublisher, Configuration.RabbitMQVHostConfirmation, Configuration.RabbitMQExchangeHourlyUpdates);
                                }

                                File.Delete(filePath);

                                logMessage = "Deleted " + filePath + ".";
                                _log.WriteInformationLog(logMessage);
                            } // end if CanOpenFile
                        });

                        _orderScope.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }

                _orderUpdateProcessing = false;
            }
        }