public void Default_Timestamp_ISDateTimeMinValue()
        {
            // Arrange
            var object1 = new SystemInformation();

            // Assert
            Assert.AreEqual(DateTime.MinValue, object1.Timestamp);
        }
        public void Put(SystemInformation systemInformation)
        {
            this.systemInformationArchiveAccessor.Store(systemInformation);
            var systemStatusViewModel = this.systemStatusOrchestrator.GetSystemStatusViewModel(systemInformation);

            var context = SignalR.GlobalHost.ConnectionManager.GetHubContext<SystemInformationHub>();
            context.Clients.displaySystemStatus(systemStatusViewModel);
        }
        public void Default_MachineName_IsNull()
        {
            // Arrange
            var object1 = new SystemInformation();

            // Assert
            Assert.IsNull(object1.MachineName);
        }
        public void EnqueuCount_IsZeroWhenToObjectIsFirstCreated()
        {
            // Arrange
            var item = new SystemInformation();

            // Act
            var systemInformationQueueItem = new SystemInformationQueueItem(item);

            // Assert
            Assert.AreEqual(0, systemInformationQueueItem.EnqueuCount);
        }
        public void Constructor_SuppliedItemIsSet_ObjectIsInstantiated()
        {
            // Arrange
            var item = new SystemInformation();

            // Act
            var systemInformationQueueItem = new SystemInformationQueueItem(item);

            // Assert
            Assert.IsNotNull(systemInformationQueueItem);
        }
        public void EnqueuCount_CanBeSet_ValueIsTheValueThatHasBeenAssigned()
        {
            // Arrange
            var newEnqueueCount = 5;
            var item = new SystemInformation();
            var systemInformationQueueItem = new SystemInformationQueueItem(item);

            // Act
            systemInformationQueueItem.EnqueuCount = newEnqueueCount;

            // Assert
            Assert.AreEqual(newEnqueueCount, systemInformationQueueItem.EnqueuCount);
        }
        public void Send(SystemInformation systemInformation)
        {
            if (systemInformation == null)
            {
                throw new ArgumentNullException("systemInformation");
            }

            // get service configuration
            IRESTServiceConfiguration serviceConfiguration = this.systemInformationSenderConfigurationProvider.GetConfiguration();
            if (serviceConfiguration == null)
            {
                throw new SystemInformationSenderSetupException("Service configuration is null.");
            }

            if (!serviceConfiguration.IsValid())
            {
                throw new SystemInformationSenderSetupException("Service configuration \"{0}\" is invalid.", serviceConfiguration);
            }

            // create REST client
            var restClient = this.restClientFactory.GetRESTClient(serviceConfiguration.Hostaddress);
            if (restClient == null)
            {
                throw new SystemInformationSenderSetupException(
                    "Could not create a REST client using the supplied hostaddress \"{0}\".", serviceConfiguration.Hostaddress);
            }

            // create request
            var request = this.requestFactory.CreatePutRequest(serviceConfiguration.ResourcePath, serviceConfiguration.Hostname);
            if (request == null)
            {
                throw new FatalSystemInformationSenderException(
                    "Could not create a request object for the hostaddress \"{0}\", the hostname \"{1}\" and the resource path \"{2}\".",
                    serviceConfiguration.Hostaddress,
                    serviceConfiguration.Hostname,
                    serviceConfiguration.ResourcePath);
            }

            request.AddBody(systemInformation);

            // Send request
            var response = restClient.Execute<SystemInformation>(request);

            // Evaluate response
            if (response.ErrorException != null)
            {
                throw new SendSystemInformationFailedException(
                    string.Format("Sending object \"{0}\" via a REST call to \"{1}\" failed. Please try again later.", systemInformation, serviceConfiguration),
                    response.ErrorException);
            }
        }
        public void GetSystemStatusViewModel_ParameterIsNotInitialized_ResultIsUninitializedViewModel()
        {
            // Arrage
            var sytemInformation = new SystemInformation();

            // Act
            var result = this.systemStatusOrchestrator.GetSystemStatusViewModel(sytemInformation);

            // Assert
            var emptyViewModel = new SystemStatusViewModel();
            Assert.AreEqual(emptyViewModel.MachineName, result.MachineName);
            Assert.AreEqual(emptyViewModel.Timestamp, result.Timestamp);
            Assert.AreEqual(emptyViewModel.DataPoints, result.DataPoints);
        }
        public static SystemInformation[] GetSystemInformationObjects(int count)
        {
            var items = new SystemInformation[count];
            var machineName = Environment.MachineName;
            DateTime timestamp = DateTime.UtcNow;
            var incremet = new TimeSpan(0, 0, 0, 1, 0);

            for (int i = 0; i < count; i++)
            {
                items[i] = new SystemInformation { MachineName = machineName, Timestamp = timestamp, };
                timestamp += incremet;
            }

            return items;
        }
        public void Dequeue_FirstItemThatIsAddedIsReturned()
        {
            // Arrange
            var queue = new SystemInformationMessageQueue();

            var firstItem = new SystemInformation { MachineName = "1", Timestamp = DateTime.UtcNow };
            queue.Enqueue(new SystemInformationQueueItem(firstItem));
            queue.Enqueue(new SystemInformationQueueItem(new SystemInformation { MachineName = "2", Timestamp = DateTime.UtcNow }));
            queue.Enqueue(new SystemInformationQueueItem(new SystemInformation { MachineName = "3", Timestamp = DateTime.UtcNow }));

            // Act
            var dequeuedItem = queue.Dequeue();

            // Assert
            Assert.AreEqual(firstItem, dequeuedItem.Item);
        }
        public void Equals_SuppliedObjectIsOfOtherType_ResultIsFalse()
        {
            // Arrange
            var object1 = new SystemInformation
                {
                    MachineName = Environment.MachineName,
                    Timestamp = DateTime.UtcNow,
                    SystemPerformance = new SystemPerformanceData()
                };
            var object2 = new object();

            // Act
            bool result = object1.Equals(object2);

            // Assert
            Assert.IsFalse(result);
        }
        public SystemStatusViewModel GetSystemStatusViewModel(SystemInformation systemInformation)
        {
            if (systemInformation == null)
            {
                throw new ArgumentNullException("systemInformation");
            }

            var systemStatusViewModel = new SystemStatusViewModel { MachineName = systemInformation.MachineName, Timestamp = systemInformation.Timestamp, };

            // add data series
            var dataSerieses = new List<SystemStatusPointViewModel>();

            // systerm performance
            if (systemInformation.SystemPerformance != null)
            {
                // Processor status data series
                if (systemInformation.SystemPerformance.ProcessorStatus != null)
                {
                    dataSerieses.Add(this.processorStatusOrchestrator.GetProcessorUtilizationInPercent(systemInformation.SystemPerformance.ProcessorStatus));
                }

                // Memory status data series
                if (systemInformation.SystemPerformance.MemoryStatus != null)
                {
                    dataSerieses.Add(this.memoryStatusOrchestrator.GetMemoryUtilizationInPercent(systemInformation.SystemPerformance.MemoryStatus));
                }

                // Storage status data series
                if (systemInformation.SystemPerformance.StorageStatus != null)
                {
                    dataSerieses.AddRange(this.storageStatusOrchestrator.GetStorageUtilizationInPercent(systemInformation.SystemPerformance.StorageStatus));
                }
            }

            systemStatusViewModel.DataPoints = dataSerieses.ToArray();

            return systemStatusViewModel;
        }
        public void GetSystemStatusViewModel_MemoryStatusIsNotNull_ResultContainsMemoryStatusDataPoint()
        {
            // Arrage
            var memoryStatus = new SystemMemoryInformation { UsedMemoryInGB = 1d, AvailableMemoryInGB = 10d };
            var systemPerformanceInformation = new SystemPerformanceData { MemoryStatus = memoryStatus };
            var systemInformation = new SystemInformation { SystemPerformance = systemPerformanceInformation };
            var memoryStatusViewModel = new SystemStatusPointViewModel { Name = "Memory Status", Value = 10d };

            var processorStatusOrchestrator = new Mock<IProcessorStatusOrchestrator>();
            var memoryStatusOrchestrator = new Mock<IMemoryStatusOrchestrator>();
            memoryStatusOrchestrator.Setup(p => p.GetMemoryUtilizationInPercent(It.IsAny<SystemMemoryInformation>())).Returns(memoryStatusViewModel);

            var storageStatusOrchestrator = new Mock<IStorageStatusOrchestrator>();

            var systemStatusOrchestrator = new SystemStatusOrchestrator(
                processorStatusOrchestrator.Object, memoryStatusOrchestrator.Object, storageStatusOrchestrator.Object);

            // Act
            var result = systemStatusOrchestrator.GetSystemStatusViewModel(systemInformation);

            // Assert
            Assert.AreEqual(memoryStatusViewModel.Value, result.DataPoints.First(d => d.Name.Equals(memoryStatusViewModel.Name)).Value);
        }
        public void GetHashCode_TwoIdenticalObjects_BothUninitialized_HashCodesAreEqual()
        {
            // Arrange
            var package1 = new SystemInformation();
            var package2 = new SystemInformation();

            // Act
            int hashCodeObject1 = package1.GetHashCode();
            int hashCodeObject2 = package2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
        public void GetSystemStatusViewModel_StorageStatusIsNotNull_ResultContainsStorageStatusDataPoint()
        {
            // Arrage
            var deviceInfo1 = new SystemStorageDeviceInformation { DeviceName = "C:", FreeDiscSpaceInPercent = 20d };
            var deviceInfos = new[] { deviceInfo1 };
            var storageStatus = new SystemStorageInformation { StorageDeviceInfos = deviceInfos };
            var systemPerformanceInformation = new SystemPerformanceData { StorageStatus = storageStatus };
            var systemInformation = new SystemInformation { SystemPerformance = systemPerformanceInformation };
            var storageStatusViewModel = new SystemStatusPointViewModel { Name = "Storage Status" + deviceInfo1.DeviceName, Value = deviceInfo1.FreeDiscSpaceInPercent };

            var processorStatusOrchestrator = new Mock<IProcessorStatusOrchestrator>();
            var memoryStatusOrchestrator = new Mock<IMemoryStatusOrchestrator>();
            var storageStatusOrchestrator = new Mock<IStorageStatusOrchestrator>();
            storageStatusOrchestrator.Setup(s => s.GetStorageUtilizationInPercent(It.IsAny<SystemStorageInformation>())).Returns(
                new[] { storageStatusViewModel });

            var systemStatusOrchestrator = new SystemStatusOrchestrator(
                processorStatusOrchestrator.Object, memoryStatusOrchestrator.Object, storageStatusOrchestrator.Object);

            // Act
            var result = systemStatusOrchestrator.GetSystemStatusViewModel(systemInformation);

            // Assert
            Assert.AreEqual(storageStatusViewModel.Value, result.DataPoints.First(d => d.Name.Equals(storageStatusViewModel.Name)).Value);
        }
        public void GetSystemStatusViewModel_ParameterIsValid_SystemStatusViewModelContainsSuppliedMachineName(string machineName)
        {
            // Arrage
            var sytemInformation = new SystemInformation { MachineName = machineName };

            var processorStatusOrchestrator = new Mock<IProcessorStatusOrchestrator>();
            var memoryStatusOrchestrator = new Mock<IMemoryStatusOrchestrator>();
            var storageStatusOrchestrator = new Mock<IStorageStatusOrchestrator>();

            var systemStatusOrchestrator = new SystemStatusOrchestrator(
                processorStatusOrchestrator.Object, memoryStatusOrchestrator.Object, storageStatusOrchestrator.Object);

            // Act
            var result = systemStatusOrchestrator.GetSystemStatusViewModel(sytemInformation);

            // Assert
            Assert.AreEqual(machineName, result.MachineName);
        }
        public void GetSystemStatusViewModel_ParameterIsValid_SystemStatusViewModelContainsSuppliedTimestamp()
        {
            // Arrage
            var timestamp = DateTime.UtcNow;
            var sytemInformation = new SystemInformation { Timestamp = timestamp };

            var processorStatusOrchestrator = new Mock<IProcessorStatusOrchestrator>();
            var memoryStatusOrchestrator = new Mock<IMemoryStatusOrchestrator>();
            var storageStatusOrchestrator = new Mock<IStorageStatusOrchestrator>();

            var systemStatusOrchestrator = new SystemStatusOrchestrator(
                processorStatusOrchestrator.Object, memoryStatusOrchestrator.Object, storageStatusOrchestrator.Object);

            // Act
            var result = systemStatusOrchestrator.GetSystemStatusViewModel(sytemInformation);

            // Assert
            Assert.AreEqual(timestamp, result.Timestamp);
        }
        public void Send_ServiceConfigurationProviderReturnsNull_SystemInformationSenderSetupExceptionIsThrown()
        {
            // Arrange
            var systemInformation = new SystemInformation();
            IRESTServiceConfiguration serviceConfiguration = null;

            var systemInformationSenderConfigurationProvider =
                new Mock<IRESTBasedSystemInformationSenderConfigurationProvider>();

            systemInformationSenderConfigurationProvider.Setup(s => s.GetConfiguration()).Returns(serviceConfiguration);

            var restClientFactory = new Mock<IRESTClientFactory>();
            var requestFactory = new Mock<IRESTRequestFactory>();

            var systemInformationSender = new RESTBasedSystemInformationSender(systemInformationSenderConfigurationProvider.Object, restClientFactory.Object, requestFactory.Object);

            // Act
            systemInformationSender.Send(systemInformation);
        }
        public void Send_RequestCannotBeCreated_FatalSystemInformationSenderExceptionIsThrown()
        {
            // Arrange
            var systemInformation = new SystemInformation();

            var configuration = new Mock<IRESTServiceConfiguration>();
            configuration.Setup(c => c.Hostaddress).Returns("127.0.0.1");
            configuration.Setup(c => c.Hostname).Returns("localhost");
            configuration.Setup(c => c.ResourcePath).Returns("api/systeminformation");
            configuration.Setup(c => c.IsValid()).Returns(true);

            var systemInformationSenderConfigurationProvider =
                new Mock<IRESTBasedSystemInformationSenderConfigurationProvider>();

            systemInformationSenderConfigurationProvider.Setup(s => s.GetConfiguration()).Returns(configuration.Object);

            var client = new Mock<IRestClient>();
            var restClientFactory = new Mock<IRESTClientFactory>();
            restClientFactory.Setup(r => r.GetRESTClient(It.IsAny<string>())).Returns(client.Object);

            IRestRequest request = null;
            var requestFactory = new Mock<IRESTRequestFactory>();
            requestFactory.Setup(r => r.CreatePutRequest(It.IsAny<string>(), It.IsAny<string>())).Returns(request);

            var systemInformationSender = new RESTBasedSystemInformationSender(
                systemInformationSenderConfigurationProvider.Object, restClientFactory.Object, requestFactory.Object);

            // Act
            systemInformationSender.Send(systemInformation);
        }
        public void QueueIsFilled_SendCausesExceptionWhichJustifiesARetry_RetryCountHasNotBeenExceeded_ItemIsAddedToQueue()
        {
            // Arrange
            int maxRuntime = SystemInformationMessageQueueWorker.WorkIntervalInMilliseconds * 2;

            var workQueue = new Mock<IMessageQueue<SystemInformation>>();
            workQueue.Setup(q => q.IsEmpty()).Returns(false);

            bool itemHasBeenAccessed = false;
            var systemInfo = new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
            var queueItem = new SystemInformationQueueItem(systemInfo);
            workQueue.Setup(q => q.Dequeue()).Returns(() =>
                {
                    if (!itemHasBeenAccessed)
                    {
                        itemHasBeenAccessed = true;
                        return queueItem;
                    }

                    return null;
                });

            var errorQueue = new Mock<IMessageQueue<SystemInformation>>();
            var systemInformationSender = new Mock<ISystemInformationSender>();
            systemInformationSender.Setup(s => s.Send(systemInfo)).Throws(
                new SendSystemInformationFailedException("Some minor exception which justifies a retry", null));

            using (var messageQueueWorker = new SystemInformationMessageQueueWorker(systemInformationSender.Object, workQueue.Object, errorQueue.Object))
            {
                // Act
                var worker = new Task(messageQueueWorker.Start);
                worker.Start();
                Task.WaitAll(new[] { worker }, maxRuntime);

                // Assert
                workQueue.Verify(q => q.Enqueue(It.IsAny<SystemInformationQueueItem>()), Times.Once());
            }
        }
        public void Item_Get_ReturnsTheSameItemThatHasBeenAssignedViaTheConstructor()
        {
            // Arrange
            var assignedItem = new SystemInformation();
            var systemInformationQueueItem = new SystemInformationQueueItem(assignedItem);

            // Act
            var result = systemInformationQueueItem.Item;

            // Assert
            Assert.AreEqual(assignedItem, result);
        }
        public void Equals_TwoIdenticalInitializedObjects_ResultIsTrue()
        {
            // Arrange
            var timestamp = DateTime.UtcNow;
            var object1 = new SystemInformation
                {
                    MachineName = Environment.MachineName,
                    Timestamp = timestamp,
                    SystemPerformance = new SystemPerformanceData()
                };
            var object2 = new SystemInformation
                {
                    MachineName = Environment.MachineName,
                    Timestamp = timestamp,
                    SystemPerformance = new SystemPerformanceData()
                };

            // Act
            bool result = object1.Equals(object2);

            // Assert
            Assert.IsTrue(result);
        }
        public void GetHashCode_TwoDistinctObjects_HashCodesAreDifferent()
        {
            // Arrange
            var object1 = new SystemInformation
                {
                    MachineName = Environment.MachineName,
                    Timestamp = DateTime.UtcNow,
                    SystemPerformance = new SystemPerformanceData()
                };
            var object2 = new SystemInformation
            {
                MachineName = "Different Machine",
                Timestamp = DateTime.UtcNow,
                SystemPerformance = new SystemPerformanceData()
            };

            // Act
            int hashCodeObject1 = object1.GetHashCode();
            int hashCodeObject2 = object2.GetHashCode();

            // Assert
            Assert.AreNotEqual(hashCodeObject1, hashCodeObject2);
        }
        public void ToString_Contains_SystemPerformance()
        {
            // Arrange
            string expectedString = "SystemPerformanceData";
            var systemPerformanceMock = new Mock<SystemPerformanceData>();
            systemPerformanceMock.Setup(s => s.ToString()).Returns(expectedString);

            var object1 = new SystemInformation
            {
                MachineName = Environment.MachineName,
                Timestamp = DateTime.UtcNow,
                SystemPerformance = new SystemPerformanceData()
            };

            // Act
            string result = object1.ToString();

            // Assert
            Assert.IsTrue(result.Contains(expectedString));
        }
        public void ToString_DoesNotContain_UsedMemoryInGB()
        {
            // Arrange
            var systemInformationItem = new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
            var object1 = new SystemInformationQueueItem(systemInformationItem) { EnqueuCount = 15 };

            // Act
            string result = object1.ToString();

            // Assert
            Assert.IsFalse(result.Contains(object1.EnqueuCount.ToString()));
        }
        public void ToString_Contains_Item()
        {
            // Arrange
            var systemInformationItem = new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
            var object1 = new SystemInformationQueueItem(systemInformationItem);

            // Act
            string result = object1.ToString();

            // Assert
            Assert.IsTrue(result.Contains(object1.Item.ToString()));
        }
        public void Enqueue_Item_DequeueReturnsSameItem()
        {
            // Arrange
            var item = new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
            var queue = new SystemInformationMessageQueue();

            // Act
            queue.Enqueue(new SystemInformationQueueItem(item));
            var dequeuedItem = queue.Dequeue();

            // Assert
            Assert.AreEqual(item, dequeuedItem.Item);
        }
        public void GetHashCode_TwoIdenticalObjects_BothInitialized_HashCodesAreEqual()
        {
            // Arrange
            var timestamp = DateTime.UtcNow;
            var object1 = new SystemInformation
                {
                    MachineName = Environment.MachineName,
                    Timestamp = timestamp,
                    SystemPerformance = new SystemPerformanceData()
                };
            var object2 = new SystemInformation
                {
                    MachineName = Environment.MachineName,
                    Timestamp = timestamp,
                    SystemPerformance = new SystemPerformanceData()
                };

            // Act
            int hashCodeObject1 = object1.GetHashCode();
            int hashCodeObject2 = object2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
        public void Send_ResponseContainsException_SendSystemInformationFailedExceptionIsThrown()
        {
            // Arrange
            var systemInformation = new SystemInformation();

            var configuration = new Mock<IRESTServiceConfiguration>();
            configuration.Setup(c => c.Hostaddress).Returns("127.0.0.1");
            configuration.Setup(c => c.Hostname).Returns("localhost");
            configuration.Setup(c => c.ResourcePath).Returns("api/systeminformation");
            configuration.Setup(c => c.IsValid()).Returns(true);

            var systemInformationSenderConfigurationProvider =
                new Mock<IRESTBasedSystemInformationSenderConfigurationProvider>();

            systemInformationSenderConfigurationProvider.Setup(s => s.GetConfiguration()).Returns(configuration.Object);

            var response = new Mock<IRestResponse<SystemInformation>>();
            response.Setup(r => r.ErrorException).Returns(new Exception("Some exception"));

            var client = new Mock<IRestClient>();
            client.Setup(c => c.Execute<SystemInformation>(It.IsAny<IRestRequest>())).Returns(response.Object);
            var restClientFactory = new Mock<IRESTClientFactory>();
            restClientFactory.Setup(r => r.GetRESTClient(It.IsAny<string>())).Returns(client.Object);

            var request = new Mock<IRestRequest>();
            var requestFactory = new Mock<IRESTRequestFactory>();
            requestFactory.Setup(r => r.CreatePutRequest(It.IsAny<string>(), It.IsAny<string>())).Returns(request.Object);

            var systemInformationSender = new RESTBasedSystemInformationSender(
                systemInformationSenderConfigurationProvider.Object, restClientFactory.Object, requestFactory.Object);

            // Act
            systemInformationSender.Send(systemInformation);
        }
        public void ToString_Contains_Timestamp()
        {
            // Arrange
            var object1 = new SystemInformation
            {
                MachineName = Environment.MachineName,
                Timestamp = DateTime.UtcNow,
                SystemPerformance = new SystemPerformanceData()
            };

            // Act
            string result = object1.ToString();

            // Assert
            Assert.IsTrue(result.Contains(object1.Timestamp.ToString()));
        }