Skip to content

Commit

Permalink
Added Triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelPuckett2G3 committed Jul 2, 2020
1 parent 3169815 commit fcd1a79
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 28 deletions.
100 changes: 73 additions & 27 deletions IFTTT.Tests/TestVarious.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,11 @@ namespace IFTTT.Tests
[TestClass]
public class TestVarious
{
[TestMethod]
public void JustTestingVarious()
{
//Arrange
IFTTTExpressionGroup expressionGroupA = ArrangeGroupA(); //Meant to pass
IFTTTExpressionGroup expressionGroupB = ArrangeGroupB(); //Meant to fail
IFTTTExpressionGroup allGroups = new IFTTTExpressionGroup
{
Name = "Logical Passing OR Failing Test Group",
ExpressionGroups = new List<IFTTTExpressionGroup> { expressionGroupA, expressionGroupB },
LogicalOperator = LogicalOperator.Or //Passes if one or the other.
};
private const string PassingGroup = "Passing Test Group";
private const string LogicalPassingOrFailingGroup = "Logical Passing OR Failing Test Group";
private const string FailingGroup = "Failing Test Group";

var expressionProcessor = new IFTTTExpressionProcessor(new TraceExpressionLogger());

//Act
var result = expressionProcessor.ProcessExpressionGroup(allGroups);

//Assert
Assert.IsTrue(result);
}

private static IFTTTExpressionGroup ArrangeGroupA()
static IFTTTExpressionGroup GetExpressionGroupA()
{
var person = new Person
{
Expand All @@ -47,16 +29,15 @@ private static IFTTTExpressionGroup ArrangeGroupA()

var expressionGroup = new IFTTTExpressionGroup
{
Name = "Passing Test Group",
Name = PassingGroup,
Circuit = Circuit.Series,
IsShorted = false, //force all checks
Expressions = allExpressions
};

return expressionGroup;
}

private static IFTTTExpressionGroup ArrangeGroupB()
static IFTTTExpressionGroup GetExpressionGroupB()
{
var person = new Person
{
Expand All @@ -67,18 +48,83 @@ private static IFTTTExpressionGroup ArrangeGroupB()

var expression1 = new IFTTTPropertyExpression<string>(person, nameof(person.FirstName), EqualityOperator.NotEquals, "John");
var expression2 = new IFTTTPropertyExpression<string>(person, nameof(person.LastName), EqualityOperator.NotEquals, null);
var expression3 = new IFTTTPropertyExpression<int>(person, nameof(person.Age), EqualityOperator.Equals, 40);
var expression3 = new IFTTTPropertyExpression<int>(person, nameof(person.Age), EqualityOperator.Equals, 40);
var allExpressions = new List<IFTTTExpression> { expression1, expression2, expression3 };

var expressionGroup = new IFTTTExpressionGroup
{
Name = "Failing Test Group",
Name = FailingGroup,
Circuit = Circuit.Series,
IsShorted = false, //force all checks
Expressions = allExpressions
};

return expressionGroup;
}

[TestMethod]
public void TestExpressionGroupsLogic()
{
//TODO Mock all but models and Actor

//Arrange
var expressionGroupA = GetExpressionGroupA(); //Meant to pass
var expressionGroupB = GetExpressionGroupB(); //Meant to fail
var allGroups = new IFTTTExpressionGroup
{
Name = LogicalPassingOrFailingGroup,
ExpressionGroups = new List<IFTTTExpressionGroup> { expressionGroupA, expressionGroupB },
LogicalOperator = LogicalOperator.Or //Passes if one or the other.
};

var traceLogger = new TraceExpressionLogger();
var expressionProcessor = new IFTTTExpressionProcessor(traceLogger);

//Act
var result = expressionProcessor.ProcessExpressionGroup(allGroups);

//Assert
Assert.IsTrue(result);
}

[TestMethod]
public void TestTriggers()
{
//TODO: Mock all but models and Actor

//Arrange

var triggers = new List<IFTTTTrigger>
{
new IFTTTTrigger
{
ListForGroupName = PassingGroup,
TriggerGroupName = FailingGroup
},
new IFTTTTrigger
{
ListForGroupName = FailingGroup,
TriggerGroupName = LogicalPassingOrFailingGroup
}
};

var expressionGroupA = GetExpressionGroupA(); //Meant to pass
var expressionGroupB = GetExpressionGroupB(); //Meant to fail

var traceLogger = new TraceExpressionLogger();
var expressionProcessor = new IFTTTExpressionProcessor(traceLogger);
var actor = new IFTTTExpressionTriggerProcessor(expressionProcessor)
{
Triggers = triggers,
ExpressionGroups = new IFTTTExpressionGroup[] { expressionGroupA, expressionGroupB }
};

//Act
var result = expressionProcessor.ProcessExpressionGroup(expressionGroupA);


//Assert
Assert.IsTrue(result);
}
}
}
15 changes: 15 additions & 0 deletions IFTTT/Interfaces/IIFTTTExpressionProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using IFTTT.Constants;
using IFTTT.Models;
using System;
using System.Collections.Generic;

namespace IFTTT.Interfaces
{
public interface IIFTTTExpressionProcessor
{
event EventHandler<IFTTTExpressionGroup> ExpressionGroupPassed;
bool ProcessExpression(IFTTTExpression expression);
bool ProcessExpressionGroup(IFTTTExpressionGroup expressionGroup);
bool ProcessExpressions(IEnumerable<IFTTTExpression> expressions, bool isShorted, Circuit circuit);
}
}
8 changes: 8 additions & 0 deletions IFTTT/Models/IFTTTTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace IFTTT.Models
{
public class IFTTTTrigger
{
public string ListForGroupName { get; set; }
public string TriggerGroupName { get; set; }
}
}
5 changes: 4 additions & 1 deletion IFTTT/Processors/IFTTTExpressionProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using IFTTT.Constants;
using IFTTT.Interfaces;
using IFTTT.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace IFTTT.Processors
{
public class IFTTTExpressionProcessor
public class IFTTTExpressionProcessor : IIFTTTExpressionProcessor
{
private const int MaxDegreesOfParallism = 8;
private readonly IFTTTLogger expressionLogger;
public event EventHandler<IFTTTExpressionGroup> ExpressionGroupPassed;

public IFTTTExpressionProcessor(IFTTTLogger expressionLogger = null)
{
Expand All @@ -35,6 +37,7 @@ public bool ProcessExpressionGroup(IFTTTExpressionGroup expressionGroup)
}

expressionLogger?.LogAsync(expressionGroup, boolean);
if (boolean) ExpressionGroupPassed?.Invoke(this, expressionGroup);
return boolean;
}

Expand Down
30 changes: 30 additions & 0 deletions IFTTT/Processors/IFTTTExpressionTriggerProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using IFTTT.Interfaces;
using IFTTT.Models;
using System.Collections.Generic;

namespace IFTTT.Processors
{
public class IFTTTExpressionTriggerProcessor
{
private readonly IIFTTTExpressionProcessor expressionProcessor;

public IFTTTExpressionTriggerProcessor(IIFTTTExpressionProcessor expressionProcessor)
{
this.expressionProcessor = expressionProcessor;
this.expressionProcessor.ExpressionGroupPassed += ExpressionProcessor_ExpressionGroupPassed;
}

public IEnumerable<IFTTTTrigger> Triggers { get; set; }
public IEnumerable<IFTTTExpressionGroup> ExpressionGroups { get; set; }

private void ExpressionProcessor_ExpressionGroupPassed(object sender, IFTTTExpressionGroup e)
{
var trigger = Triggers.FirstOrDefault(x => x.ListForGroupName == e.Name);
if (trigger != null)
{
var group = ExpressionGroups.FirstOrDefault(x => x.Name == trigger.TriggerGroupName);
if (group != null) expressionProcessor.ProcessExpressionGroup(group);
}
}
}
}

0 comments on commit fcd1a79

Please sign in to comment.