using System.Collections.Generic;
public class HealthCheckTest
public void HandleExceptionFromCloud()
var sesnorDataMock = new Mock<ISensorData>();
sesnorDataMock.Setup(x => x.GetSensorDataFromSource("V1")).Throws<System.NullReferenceException>();
var sensorRangeMock = new Mock<ISensorRange>();
sensorRangeMock.Setup(x => x.GetSensorRange("S1")).Returns( new List<float> {20, 35});
sensorRangeMock.Setup(x => x.GetSensorRange("S2")).Returns( new List<float> {44, 64});
var sensorToCompMock = new Mock<ISensorToComponentMap>();
sensorToCompMock.Setup( x => x.GetCompListForSensor("S1")).Returns( new List<string> {"C1", "C2"});
sensorToCompMock.Setup( x => x.GetCompListForSensor("S2")).Returns( new List<string> {"C3"});
var componentCost = new Mock<IComponentCost>();
componentCost.Setup(x => x.GetComponentCost("C1")).Returns(10000);
componentCost.Setup(x => x.GetComponentCost("C2")).Returns(50000);
componentCost.Setup(x => x.GetComponentCost("C3")).Returns(25000);
var sendEmail = new Mock<IEmail>();
MachineHealthChecker machineHealthChecker = new MachineHealthChecker(sesnorDataMock.Object, sensorRangeMock.Object, sensorToCompMock.Object, componentCost.Object, sendEmail.Object );
float maintainenceCost = machineHealthChecker.GetMaintainenceCost("V1");
catch (NullReferenceException ex)
Assert.Throws<NullReferenceException>(
() => { throw new NullReferenceException(); });
public void VerifyEmailSent()
MailMessage mail = new MailMessage();
mail.FromAddress = "admin@plant.com";
mail.Subject = "Failure";
mail.toAddress = "operator@plant.com";
var sesnorDataMock = new FakeSensorData();
var sensorRangeMock = new Mock<ISensorRange>();
sensorRangeMock.Setup(x => x.GetSensorRange("S1")).Returns( new List<float> {20, 35});
sensorRangeMock.Setup(x => x.GetSensorRange("S2")).Returns( new List<float> {44, 64});
var sensorToCompMock = new Mock<ISensorToComponentMap>();
sensorToCompMock.Setup( x => x.GetCompListForSensor("S1")).Returns( new List<string> {"C1", "C2"});
sensorToCompMock.Setup( x => x.GetCompListForSensor("S2")).Returns( new List<string> {"C3"});
var componentCost = new Mock<IComponentCost>();
componentCost.Setup(x => x.GetComponentCost("C1")).Returns(10000);
componentCost.Setup(x => x.GetComponentCost("C2")).Returns(50000);
componentCost.Setup(x => x.GetComponentCost("C3")).Returns(25000);
var sendEmail = new Mock<IEmail>();
MachineHealthChecker machineHealthChecker = new MachineHealthChecker(sesnorDataMock, sensorRangeMock.Object, sensorToCompMock.Object, componentCost.Object, sendEmail.Object );
float maintainenceCost = machineHealthChecker.GetMaintainenceCost("V1");
Assert.AreEqual(0, maintainenceCost);
sendEmail.Verify(x => x.SendEmail(), Times.Once());
public void Returns_ZeroCost_When_SesnorsAreInRange()
var sesnorDataMock = new FakeSensorData();
var sensorRangeMock = new Mock<ISensorRange>();
sensorRangeMock.Setup(x => x.GetSensorRange("S1")).Returns( new List<float> {20, 35});
sensorRangeMock.Setup(x => x.GetSensorRange("S2")).Returns( new List<float> {44, 64});
var sensorToCompMock = new Mock<ISensorToComponentMap>();
sensorToCompMock.Setup( x => x.GetCompListForSensor("S1")).Returns( new List<string> {"C1", "C2"});
sensorToCompMock.Setup( x => x.GetCompListForSensor("S2")).Returns( new List<string> {"C3"});
var componentCost = new Mock<IComponentCost>();
componentCost.Setup(x => x.GetComponentCost("C1")).Returns(10000);
componentCost.Setup(x => x.GetComponentCost("C2")).Returns(50000);
componentCost.Setup(x => x.GetComponentCost("C3")).Returns(25000);
public void Returns_EmptyList_When_SensorsAreInRange()
var sesnorDataMock = new Mock<ISensorData>();
sesnorDataMock.Setup(x => x.GetSensorDataFromSource("V1")).Returns(GetMockSensorData());
var sensorRangeMock = new Mock<ISensorRange>();
sensorRangeMock.Setup(x => x.GetSensorRange("S1")).Returns( new List<float> {20, 35});
var sensorToCompMock = new Mock<ISensorToComponentMap>();
sensorToCompMock.Setup( x => x.GetCompListForSensor("S1")).Returns( new List<string> {"C1", "C2"});
public void Returns_FaultyList_When_SensorsAreOutOfRange()
private List<SensorData> GetMockSensorData()
List<SensorData> sensorData = new List<SensorData>();
SensorData sensorDataItem = new SensorData();
sensorDataItem.MahineiD = "M01";
sensorDataItem.VendoriD = "V01";
sensorDataItem.SensoriD = "S1";
sensorDataItem.SesnorValue = 25;
sensorDataItem.DateCaptured = "11:00:00";
sensorData.Add(sensorDataItem);
private List<SensorData> GetMockSensorData1()
List<SensorData> sensorData = new List<SensorData>();
SensorData sensorDataItem = new SensorData();
sensorDataItem.MahineiD = "M01";
sensorDataItem.VendoriD = "V01";
sensorDataItem.SensoriD = "S1";
sensorDataItem.SesnorValue = 25;
sensorDataItem.DateCaptured = "11:00:00";
SensorData sensorDataItem1 = new SensorData();
sensorDataItem1.MahineiD = "M01";
sensorDataItem1.VendoriD = "V01";
sensorDataItem1.SensoriD = "S1";
sensorDataItem1.SesnorValue = 19;
sensorDataItem1.DateCaptured = "11:00:00";
SensorData sensorDataItem2 = new SensorData();
sensorDataItem2.MahineiD = "M01";
sensorDataItem2.VendoriD = "V01";
sensorDataItem2.SensoriD = "S2";
sensorDataItem2.SesnorValue = 40;
sensorDataItem2.DateCaptured = "11:00:00";
SensorData sensorDataItem3 = new SensorData();
sensorDataItem3.MahineiD = "M01";
sensorDataItem3.VendoriD = "V01";
sensorDataItem3.SensoriD = "S2";
sensorDataItem3.SesnorValue = 30;
sensorDataItem3.DateCaptured = "11:00:00";
sensorData.Add(sensorDataItem3);
sensorData.Add(sensorDataItem2);
sensorData.Add(sensorDataItem1);
sensorData.Add(sensorDataItem);
public class FakeSensorData : ISensorData
public List<SensorData> GetSensorDataFromSource(string VendoriD)
return GetMockSensorData();
private List<SensorData> GetMockSensorData()
List<SensorData> sensorData = new List<SensorData>();
SensorData sensorDataItem = new SensorData();
sensorDataItem.MahineiD = "M01";
sensorDataItem.VendoriD = "V01";
sensorDataItem.SensoriD = "S1";
sensorDataItem.SesnorValue = 18;
sensorDataItem.DateCaptured = "11:00:00";
sensorData.Add(sensorDataItem);
public class MailNotification
public MailNotification(IEmail email)
public class MachineHealthChecker
private ISensorData _sensorData;
private ISensorRange _sensorRange;
private ISensorToComponentMap _sensorToComponentMap;
private IComponentCost _componentCost;
public MachineHealthChecker(ISensorData sensorData, ISensorRange sensorRange, ISensorToComponentMap sensorToComponentMap, IComponentCost componentCost, IEmail email )
_sensorRange = sensorRange;
_sensorToComponentMap = sensorToComponentMap;
_componentCost = componentCost;
public float GetMaintainenceCost(string VendorID)
List<String> faultyComponentList = new List<String>();
faultyComponentList = GetFaultyComponents();
foreach (string component in faultyComponentList)
cost += _componentCost.GetComponentCost(component);
public void DeserializeJSONData()
string json = @"{'VendoriD': '1',
List<SensorData> sensorData = new List<SensorData>();
SensorData sensorDataItem = JsonConvert.DeserializeObject<SensorData>(json);
Console.WriteLine("JSON" + sensorDataItem.SensoriD);
public List<String> GetFaultyComponents()
List<String> faultyComponentList = new List<String>();
Dictionary<string, int> dictInstanceCount = new Dictionary<string, int>();
List<SensorData> sensorData = new List<SensorData>();
sensorData = _sensorData.GetSensorDataFromSource("V1");
foreach (SensorData sensordata in sensorData)
dictInstanceCount = InitializeFaultsCountDict(sensordata.SensoriD, dictInstanceCount);
if (!IsSensorInRange(sensordata.SensoriD, sensordata.SesnorValue, _sensorRange))
int OutOfRangeCount = UpdateOutOfRangeCountForSensor(sensordata.SensoriD, dictInstanceCount);
Console.WriteLine(OutOfRangeCount);
faultyComponentList = GetComponentList(sensordata.SensoriD, _sensorToComponentMap);
Console.WriteLine(faultyComponentList.Count);
Console.WriteLine("111");
return faultyComponentList;
private int UpdateOutOfRangeCountForSensor(string SensorID, Dictionary<string, int> result)
result[SensorID] = result[SensorID] + 1;
private Dictionary<string, int> InitializeFaultsCountDict(string SensorId, Dictionary<string, int> result)
bool dictHasSensoriD = result.ContainsKey(SensorId);
private bool IsSensorInRange(string SensoriD, float SensorValue, ISensorRange sensorRange)
List<float> SensorRange = sensorRange.GetSensorRange(SensoriD);
Console.WriteLine(SensorValue);
Console.WriteLine( SensorRange[0]);
if ((SensorValue < SensorRange[0]))
private List<string> GetComponentList(string SensoriD, ISensorToComponentMap sensorToComponentMap)
List<string> SensorCompMap = sensorToComponentMap.GetCompListForSensor(SensoriD);
public interface ISensorRange
public List<float> GetSensorRange(string SesnorID);
public string toAddress {set; get;}
public string FromAddress {set; get;}
public string Subject {set; get;}
public string Body {set; get;}
public interface ISensorToComponentMap
public List<string> GetCompListForSensor(string SesnorID);
public interface ISensorData
public List<SensorData> GetSensorDataFromSource(string VendoriD);
public interface IComponentCost
public float GetComponentCost(string ComponentID);
public string VendoriD {get; set;}
public string MahineiD {get; set;}
public string SensoriD {get; set;}
public float SesnorValue {get; set;}
public string DateCaptured {get; set;}
public static void Main()
var x = new NUnitLite.AutoRun().Execute(new string[]{"--test:HealthCheckTest", "--noc"});
Console.WriteLine("----------------------------------------------");
Console.WriteLine(x==0?"All Test Passed... :¬)": string.Format("{0} tests failed... :¬(", x));
Console.WriteLine("----------------------------------------------");