Skip to content

Commit

Permalink
Merge pull request #246 from corgibytes/manifest-detected-dispatches-…
Browse files Browse the repository at this point in the history
…generate-bom

ManifestDetectedEvent dispatches GenerateBillOfMaterialsActivity
  • Loading branch information
mscottford authored Sep 13, 2022
2 parents 0045d1b + 13a5e06 commit de7d42c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Corgibytes.Freshli.Cli.Functionality.Analysis;
using Corgibytes.Freshli.Cli.Functionality.BillOfMaterials;
using Corgibytes.Freshli.Cli.Functionality.Engine;
using Corgibytes.Freshli.Cli.Services;
using Moq;
using Xunit;

namespace Corgibytes.Freshli.Cli.Test.Functionality.Analysis;

[UnitTest]
public class ManifestDetectedEventTest
{
[Fact]
public void CorrectlyDispatchesGenerateBillOfMaterialsActivity()
{
var manifestPath = "/path/to/manifest";
var billOfMaterialsPath = "/path/to/bom";
var analysisLocation = new AnalysisLocation("/cache/directory", "2dbc2fd2358e1ea1b7a6bc08ea647b9a337ac92d",
"da39a3ee5e6b4b0d3255bfef95601890afd80709");

var javaAgentReader = new Mock<IAgentReader>();

javaAgentReader.Setup(mock => mock.ProcessManifest(manifestPath, It.IsAny<DateTime>())).Returns(billOfMaterialsPath);

var engine = new Mock<IApplicationActivityEngine>();

var manifestEvent = new ManifestDetectedEvent(analysisLocation, javaAgentReader.Object, manifestPath);
manifestEvent.Handle(engine.Object);

engine.Verify(mock => mock.Dispatch(It.Is<GenerateBillOfMaterialsActivity>(value =>
value.AnalysisLocation == analysisLocation &&
value.ManifestPath == manifestPath &&
value.AgentReader == javaAgentReader.Object
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ public class GenerateBillOfMaterialsActivityTest
public void Handle()
{
// Arrange
var agentManager = new Mock<IAgentManager>();
var javaAgentReader = new Mock<IAgentReader>();
var eventEngine = new Mock<IApplicationEventEngine>();
var analysisLocation = new Mock<IAnalysisLocation>();

agentManager.Setup(mock => mock.GetReader("/usr/local/bin/freshli-agent-java")).Returns(javaAgentReader.Object);
javaAgentReader.Setup(mock => mock.ProcessManifest("/path/to/manifest", It.IsAny<DateTime>()))
.Returns("/path/to/bill-of-materials");

// Act
var activity = new GenerateBillOfMaterialsActivity(
agentManager.Object, analysisLocation.Object, "/path/to/manifest", "/usr/local/bin/freshli-agent-java"
);
var activity = new GenerateBillOfMaterialsActivity(javaAgentReader.Object, analysisLocation.Object, "/path/to/manifest");
activity.Handle(eventEngine.Object);

// Assert
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using Corgibytes.Freshli.Cli.Functionality.BillOfMaterials;
using Corgibytes.Freshli.Cli.Functionality.Engine;
using Corgibytes.Freshli.Cli.Services;

Expand All @@ -17,5 +17,9 @@ public ManifestDetectedEvent(IAnalysisLocation analysisLocation, IAgentReader ag
public IAgentReader AgentReader { get; }
public string ManifestPath { get; }

public void Handle(IApplicationActivityEngine eventClient) => throw new NotImplementedException();
public void Handle(IApplicationActivityEngine eventClient) => eventClient.Dispatch(new GenerateBillOfMaterialsActivity(
AgentReader,
AnalysisLocation,
ManifestPath
));
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,22 @@ namespace Corgibytes.Freshli.Cli.Functionality.BillOfMaterials;

public class GenerateBillOfMaterialsActivity : IApplicationActivity
{
private readonly IAgentManager _agentManager;
private readonly string _agentPath;
private readonly IAnalysisLocation _analysisLocation;
private readonly string _manifestPath;
public readonly IAgentReader AgentReader;
public readonly IAnalysisLocation AnalysisLocation;
public readonly string ManifestPath;

public GenerateBillOfMaterialsActivity(
IAgentManager agentManager, IAnalysisLocation analysisLocation, string manifestPath, string agentPath
)
public GenerateBillOfMaterialsActivity(IAgentReader agentReader, IAnalysisLocation analysisLocation, string manifestPath)
{
_agentManager = agentManager;
_analysisLocation = analysisLocation;
_manifestPath = manifestPath;
_agentPath = agentPath;
AgentReader = agentReader;
AnalysisLocation = analysisLocation;
ManifestPath = manifestPath;
}

public void Handle(IApplicationEventEngine eventClient)
{
var agent = _agentManager.GetReader(_agentPath);
var asOfDate = DateTime.Now;
var pathToBillOfMaterials = agent.ProcessManifest(_manifestPath, asOfDate);
var pathToBillOfMaterials = AgentReader.ProcessManifest(ManifestPath, asOfDate);

eventClient.Fire(new BillOfMaterialsGeneratedEvent(_analysisLocation, pathToBillOfMaterials));
eventClient.Fire(new BillOfMaterialsGeneratedEvent(AnalysisLocation, pathToBillOfMaterials));
}
}

0 comments on commit de7d42c

Please sign in to comment.