Skip to content

Commit 6a54fd7

Browse files
authored
Merge pull request #11 from marcduiker/develop
Develop
2 parents 79509c0 + b17a7a1 commit 6a54fd7

36 files changed

+648
-122
lines changed

src/AzureFunctionsUpdates.UnitTests/Models/LatestReleasesTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using AzureFunctionsUpdates.Models;
2+
using AzureFunctionsUpdates.Models.Releases;
23
using AzureFunctionsUpdates.UnitTests.TestObjectBuilders;
34
using FluentAssertions;
45
using System;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using AzureFunctionsUpdates.Models;
2+
using AzureFunctionsUpdates.Orchestrations;
3+
using AzureFunctionsUpdates.UnitTests.TestObjectBuilders;
4+
using Microsoft.Extensions.Logging;
5+
using Moq;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
12+
namespace AzureFunctionsUpdates.UnitTests.Orchestrations
13+
{
14+
public class PublicationUpdateOrchestrationTests
15+
{
16+
[Fact]
17+
public async Task GivenHistoryIsEmptyAndNewPublicationIsAvailable_WhenOrchcestrationIsRun_ThenSaveAndUpdateShouldBeCalled()
18+
{
19+
// Arrange
20+
Environment.SetEnvironmentVariable(Toggles.DoPostUpdateVariableName, "true");
21+
var mockContext = PublicationUpdateOrchestrationContextBuilder.BuildWithoutHistoryAndWithNewWebPublication();
22+
var logger = new Mock<ILogger>();
23+
var publicationUpdateOrchestration = new PublicationUpdateOrchestration();
24+
25+
// Act
26+
await publicationUpdateOrchestration.Run(mockContext.Object, logger.Object);
27+
28+
// Assert
29+
mockContext.VerifyAll();
30+
}
31+
}
32+
}

src/AzureFunctionsUpdates.UnitTests/Orchestrations/ReleaseUpdateOrchestrationTests.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using AzureFunctionsUpdates.Activities;
2-
using AzureFunctionsUpdates.Models;
1+
using AzureFunctionsUpdates.Models;
32
using AzureFunctionsUpdates.Orchestrations;
43
using AzureFunctionsUpdates.UnitTests.TestObjectBuilders;
5-
using Microsoft.Azure.WebJobs;
64
using Microsoft.Extensions.Logging;
75
using Moq;
6+
using System;
87
using System.Threading.Tasks;
98
using Xunit;
109

@@ -16,7 +15,8 @@ public class ReleaseUpdateOrchestrationTests
1615
public async Task GivenNoReleasesAreAvailableInHistoryAndNewGithubReleasesAreRetrieved_WhenOrchestrationIsRunForTwoRepos_ThenSaveAndPostShouldBeCalled()
1716
{
1817
// Arrange
19-
var mockContext = OrchestrationContextBuilder.BuildWithoutHistoryAndWithGitHubRelease();
18+
Environment.SetEnvironmentVariable(Toggles.DoPostUpdateVariableName, "true");
19+
var mockContext = ReleaseUpdateOrchestrationContextBuilder.BuildWithoutHistoryAndWithGitHubRelease();
2020
var logger = new Mock<ILogger>();
2121
var releaseUpdateOrchestration = new ReleaseUpdateOrchestration();
2222

@@ -31,7 +31,8 @@ public async Task GivenNoReleasesAreAvailableInHistoryAndNewGithubReleasesAreRet
3131
public async Task GivenNoReleasesAreAvailableInHistoryAndNewGithubReleaseReturnsNullRelease_WhenOrchestrationIsRunForTwoRepos_ThenSaveAndPostShouldBeCalledForTheReleaseWhichWasReturnedFromGitHub()
3232
{
3333
// Arrange
34-
var mockContext = OrchestrationContextBuilder.BuildWithoutHistoryAndGitHubReturnsNullRelease();
34+
Environment.SetEnvironmentVariable(Toggles.DoPostUpdateVariableName, "true");
35+
var mockContext = ReleaseUpdateOrchestrationContextBuilder.BuildWithoutHistoryAndGitHubReturnsNullRelease();
3536
var logger = new Mock<ILogger>();
3637
var releaseUpdateOrchestration = new ReleaseUpdateOrchestration();
3738

@@ -47,7 +48,7 @@ public async Task GivenNoReleasesAreAvailableInHistoryAndNewGithubReleaseReturns
4748
public async Task GivenReleasesAreAvailableInHistoryAndNewGithubReleasesAreTheSame_WhenOrchestrationIsRun_ThenSaveAndPostShouldNotBeCalled()
4849
{
4950
// Arrange
50-
var mockContext = OrchestrationContextBuilder.BuildWithHistoryAndWithGitHubWithEqualReleases();
51+
var mockContext = ReleaseUpdateOrchestrationContextBuilder.BuildWithHistoryAndWithGitHubWithEqualReleases();
5152
var logger = new Mock<ILogger>();
5253
var releaseUpdateOrchestration = new ReleaseUpdateOrchestration();
5354

@@ -62,7 +63,8 @@ public async Task GivenReleasesAreAvailableInHistoryAndNewGithubReleasesAreTheSa
6263
public async Task GivenReleasesAreAvailableInHistoryAndOneGithubReleaseIsEqualAndOneIsDifferent_WhenOrchestrationIsRun_ThenSaveAndPostShouldBeCalledOnce()
6364
{
6465
// Arrange
65-
var mockContext = OrchestrationContextBuilder.BuildWithHistoryAndWithGitHubWithOneEqualAndOneDifferentRelease();
66+
Environment.SetEnvironmentVariable(Toggles.DoPostUpdateVariableName, "true");
67+
var mockContext = ReleaseUpdateOrchestrationContextBuilder.BuildWithHistoryAndWithGitHubWithOneEqualAndOneDifferentRelease();
6668
var logger = new Mock<ILogger>();
6769
var releaseUpdateOrchestration = new ReleaseUpdateOrchestration();
6870

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using AutoFixture;
2+
using AzureFunctionsUpdates.Models.Publications;
3+
using System;
4+
5+
namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders
6+
{
7+
public static class PublicationBuilder
8+
{
9+
private static Fixture _fixture = new Fixture();
10+
11+
public static Publication BuildNullPublication(string publicationSourceName)
12+
{
13+
return new NullPublication(publicationSourceName);
14+
}
15+
16+
public static Publication BuildPublicationFromWeb(string publicationSourceName)
17+
{
18+
return _fixture.Build<Publication>()
19+
.With(p => p.PublicationSourceName, publicationSourceName)
20+
.With(p => p.PublicationDate, DateTimeOffset.Now)
21+
.Create();
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using AutoFixture;
2+
using AzureFunctionsUpdates.Models.Publications;
3+
using System.Collections.Generic;
4+
5+
namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders
6+
{
7+
public static class PublicationConfigurationBuilder
8+
{
9+
private static Fixture _fixture = new Fixture();
10+
11+
private static PublicationConfiguration BuildOne(string publicationSourceName)
12+
{
13+
return _fixture.Build<PublicationConfiguration>()
14+
.With(p => p.PublicationSourceName, publicationSourceName)
15+
.Create();
16+
}
17+
18+
public static IReadOnlyList<PublicationConfiguration> BuildListWithOne(string publicationSourceName)
19+
{
20+
return new List<PublicationConfiguration> { BuildOne(publicationSourceName) };
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using AzureFunctionsUpdates.Activities;
2+
using AzureFunctionsUpdates.Activities.Publications;
3+
using AzureFunctionsUpdates.Models;
4+
using AzureFunctionsUpdates.Models.Publications;
5+
using Microsoft.Azure.WebJobs;
6+
using Moq;
7+
using System.Collections.Generic;
8+
using System.Threading.Tasks;
9+
10+
namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders
11+
{
12+
public static class PublicationUpdateOrchestrationContextBuilder
13+
{
14+
public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndWithNewWebPublication()
15+
{
16+
var mockContext = new Mock<DurableOrchestrationContextBase>(MockBehavior.Strict);
17+
const string publicationSourceName = "Azure Service updates";
18+
19+
mockContext
20+
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<PublicationConfiguration>>(
21+
nameof(GetPublicationConfigurations),
22+
It.IsAny<RetryOptions>(),
23+
null))
24+
.ReturnsAsync(PublicationConfigurationBuilder.BuildListWithOne(publicationSourceName));
25+
26+
mockContext
27+
.Setup(c => c.CallActivityWithRetryAsync<Publication>(
28+
nameof(GetLatestPublicationFromHistory),
29+
It.IsAny<RetryOptions>(),
30+
It.IsAny<PublicationConfiguration>()))
31+
.ReturnsAsync(PublicationBuilder.BuildNullPublication(publicationSourceName));
32+
33+
mockContext
34+
.Setup(c => c.CallActivityWithRetryAsync<Publication>(
35+
nameof(GetLatestPublicationFromWeb),
36+
It.IsAny<RetryOptions>(),
37+
It.IsAny<PublicationConfiguration>()))
38+
.ReturnsAsync(PublicationBuilder.BuildPublicationFromWeb(publicationSourceName));
39+
40+
mockContext
41+
.Setup(c => c.CallActivityWithRetryAsync(
42+
nameof(SaveLatestPublication),
43+
It.IsAny<RetryOptions>(),
44+
It.IsAny<Publication>()))
45+
.Returns(Task.CompletedTask);
46+
47+
mockContext
48+
.Setup(c => c.CallActivityWithRetryAsync(
49+
nameof(PostUpdate),
50+
It.IsAny<RetryOptions>(),
51+
It.IsAny<UpdateMessage>()))
52+
.Returns(Task.CompletedTask);
53+
54+
return mockContext;
55+
}
56+
}
57+
}

src/AzureFunctionsUpdates.UnitTests/TestObjectBuilders/OrchestrationContextBuilder.cs renamed to src/AzureFunctionsUpdates.UnitTests/TestObjectBuilders/ReleaseUpdateOrchestrationContextBuilder.cs

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
using System.Collections.Generic;
66
using System;
77
using System.Threading.Tasks;
8+
using AzureFunctionsUpdates.Models.Releases;
9+
using AzureFunctionsUpdates.Activities.Releases;
810

911
namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders
1012
{
11-
public static class OrchestrationContextBuilder
13+
public static class ReleaseUpdateOrchestrationContextBuilder
1214
{
1315
public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndWithGitHubRelease()
1416
{
@@ -23,7 +25,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndWithGi
2325
// Setup GetRepositoryConfigurations
2426
mockContext
2527
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<RepositoryConfiguration>>(
26-
nameof(GetConfigurations),
28+
nameof(GetRepositoryConfigurations),
2729
It.IsAny<RetryOptions>(),
2830
null))
2931
.ReturnsAsync(repoConfigurations);
@@ -78,14 +80,14 @@ public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndWithGi
7880
.Setup(c => c.CallActivityWithRetryAsync(
7981
nameof(PostUpdate),
8082
It.IsAny<RetryOptions>(),
81-
It.Is<RepositoryRelease>(r => r.RepositoryName.Equals(repository1Name))))
83+
It.Is<UpdateMessage>(message => message.Topic.Contains(repository1Name))))
8284
.Returns(Task.CompletedTask);
8385

8486
mockContext
8587
.Setup(c => c.CallActivityWithRetryAsync(
8688
nameof(PostUpdate),
8789
It.IsAny<RetryOptions>(),
88-
It.Is<RepositoryRelease>(r => r.RepositoryName.Equals(repository2Name))))
90+
It.Is<UpdateMessage>(message => message.Topic.Contains(repository2Name))))
8991
.Returns(Task.CompletedTask);
9092

9193
return mockContext;
@@ -104,7 +106,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndGitHub
104106
// Setup GetRepositoryConfigurations
105107
mockContext
106108
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<RepositoryConfiguration>>(
107-
nameof(GetConfigurations),
109+
nameof(GetRepositoryConfigurations),
108110
It.IsAny<RetryOptions>(),
109111
null))
110112
.ReturnsAsync(repoConfigurations);
@@ -154,7 +156,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndGitHub
154156
.Setup(c => c.CallActivityWithRetryAsync(
155157
nameof(PostUpdate),
156158
It.IsAny<RetryOptions>(),
157-
It.Is<RepositoryRelease>(r => r.RepositoryName.Equals(repository1Name))))
159+
It.Is<UpdateMessage>(message => message.Topic.Contains(repository1Name))))
158160
.Returns(Task.CompletedTask);
159161

160162
return mockContext;
@@ -175,7 +177,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithHistoryAndWithGitHu
175177
// Setup GetRepositoryConfigurations
176178
mockContext
177179
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<RepositoryConfiguration>>(
178-
nameof(GetConfigurations),
180+
nameof(GetRepositoryConfigurations),
179181
It.IsAny<RetryOptions>(),
180182
null))
181183
.ReturnsAsync(repoConfigurations);
@@ -230,7 +232,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithHistoryAndWithGitHu
230232
// Setup GetRepositoryConfigurations
231233
mockContext
232234
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<RepositoryConfiguration>>(
233-
nameof(GetConfigurations),
235+
nameof(GetRepositoryConfigurations),
234236
It.IsAny<RetryOptions>(),
235237
null))
236238
.ReturnsAsync(repoConfigurations);
@@ -278,7 +280,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithHistoryAndWithGitHu
278280
.Setup(c => c.CallActivityWithRetryAsync(
279281
nameof(PostUpdate),
280282
It.IsAny<RetryOptions>(),
281-
It.Is<RepositoryRelease>(r => r.RepositoryName.Equals(repository2Name))))
283+
It.Is<UpdateMessage>(message => message.Topic.Contains(repository2Name))))
282284
.Returns(Task.CompletedTask);
283285

284286
return mockContext;

src/AzureFunctionsUpdates.UnitTests/TestObjectBuilders/RepositoryConfigurationBuilder.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AutoFixture;
2-
using AzureFunctionsUpdates.Models;
2+
using AzureFunctionsUpdates.Models.Releases;
3+
using System;
34
using System.Collections.Generic;
45

56
namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders

src/AzureFunctionsUpdates.UnitTests/TestObjectBuilders/RepositoryReleaseBuilder.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using AutoFixture;
2-
using AzureFunctionsUpdates.Models;
2+
using AzureFunctionsUpdates.Models.Releases;
33
using System;
44
using System.Collections.Generic;
55

@@ -13,13 +13,15 @@ public static RepositoryRelease BuildOne(string repositoryName)
1313
{
1414
return _fixture.Build<RepositoryRelease>()
1515
.With(r => r.RepositoryName, repositoryName)
16+
.With(r => r.ReleaseCreatedAt, DateTimeOffset.Now)
1617
.Create();
1718
}
1819

1920
public static RepositoryRelease BuildOneWithReleaseId(string repositoryName, int id)
2021
{
2122
return _fixture.Build<RepositoryRelease>()
2223
.With(r => r.RepositoryName, repositoryName)
24+
.With(r => r.ReleaseCreatedAt, DateTimeOffset.Now)
2325
.With(r => r.ReleaseId, id)
2426
.Create();
2527
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using AzureFunctionsUpdates.Models;
2-
using Microsoft.Azure.WebJobs;
1+
using Microsoft.Azure.WebJobs;
32
using Microsoft.Extensions.Logging;
43
using System;
54
using Tweetinvi;
65
using Tweetinvi.Models;
6+
using UpdateMessage = AzureFunctionsUpdates.Models.UpdateMessage;
77

88
namespace AzureFunctionsUpdates.Activities
99
{
@@ -16,39 +16,17 @@ public class PostUpdate
1616

1717
[FunctionName(nameof(PostUpdate))]
1818
public void Run(
19-
[ActivityTrigger] RepositoryRelease newRelease,
19+
[ActivityTrigger] UpdateMessage message,
2020
ILogger logger)
2121
{
22-
logger.LogInformation($"Started {nameof(PostUpdate)} for { newRelease.RepositoryName} { newRelease.ReleaseName}.");
22+
logger.LogInformation($"Started {nameof(PostUpdate)} for { message.Topic}.");
2323

2424
var creds = new TwitterCredentials(consumerApiKey, consumerApiSecret, accessToken, accessTokenSecret);
25-
var tweetMessage = CreateMessage(newRelease);
26-
25+
2726
var tweet = Auth.ExecuteOperationWithCredentials(creds, () =>
2827
{
29-
return Tweet.PublishTweet(tweetMessage);
28+
return Tweet.PublishTweet(message.Content);
3029
});
3130
}
32-
33-
private static string CreateMessage(RepositoryRelease release)
34-
{
35-
string firstLine;
36-
if (string.IsNullOrEmpty(release.ReleaseName) || release.ReleaseName == release.TagName)
37-
{
38-
firstLine = $"A new {release.RepositoryName} release, tagged {release.TagName}, is available on GitHub since {release.ReleaseCreatedAt.ToString("D")}.";
39-
}
40-
else
41-
{
42-
firstLine = $"A new {release.RepositoryName} release, {release.ReleaseName} (tagged {release.TagName}), is available on GitHub since {release.ReleaseCreatedAt.ToString("D")}.";
43-
}
44-
45-
return firstLine +
46-
$"{Environment.NewLine}" +
47-
$"{Environment.NewLine}" +
48-
$"See {release.HtmlUrl} for more information." +
49-
$"{Environment.NewLine}" +
50-
$"{Environment.NewLine}" +
51-
$"{release.HashTags}";
52-
}
5331
}
5432
}

0 commit comments

Comments
 (0)