diff --git a/src/LineBot/GroupSummary/GroupSummary.cs b/src/LineBot/GroupSummary/GroupSummary.cs new file mode 100644 index 0000000..fca51f4 --- /dev/null +++ b/src/LineBot/GroupSummary/GroupSummary.cs @@ -0,0 +1,20 @@ +// Copyright Dirk Lemstra (https://github.com/dlemstra/line-bot-sdk-dotnet). +// Licensed under the Apache License, Version 2.0. + +using System; +using Newtonsoft.Json; + +namespace Line +{ + internal sealed class GroupSummary : IGroupSummary + { + [JsonProperty("groupId")] + public string GroupId { get; set; } = default!; + + [JsonProperty("groupName")] + public string GroupName { get; set; } = default!; + + [JsonProperty("pictureUrl")] + public Uri PictureUrl { get; set; } = default!; + } +} \ No newline at end of file diff --git a/src/LineBot/GroupSummary/IGroupSummary.cs b/src/LineBot/GroupSummary/IGroupSummary.cs new file mode 100644 index 0000000..738022f --- /dev/null +++ b/src/LineBot/GroupSummary/IGroupSummary.cs @@ -0,0 +1,28 @@ +// Copyright Dirk Lemstra (https://github.com/dlemstra/line-bot-sdk-dotnet). +// Licensed under the Apache License, Version 2.0. + +using System; + +namespace Line +{ + /// + /// Encapsulates the interface for the summary of a group. + /// + public interface IGroupSummary + { + /// + /// Gets the Group ID. + /// + string GroupId { get; } + + /// + /// Gets the Group name. + /// + string GroupName { get; } + + /// + /// Gets the Group icon URL. + /// + Uri PictureUrl { get; } + } +} \ No newline at end of file diff --git a/src/LineBot/ILineBot.cs b/src/LineBot/ILineBot.cs index 25fd56d..8a3f96d 100644 --- a/src/LineBot/ILineBot.cs +++ b/src/LineBot/ILineBot.cs @@ -80,6 +80,20 @@ public interface ILineBot /// The events from the specified request. Task GetEvents(HttpRequest request); + /// + /// Returns the summary of the specified group. + /// + /// The group. + /// The summary of the specified group. + Task GetGroupSummmary(IGroup group); + + /// + /// Returns the summary of the specified group. + /// + /// The id of the group. + /// The summary of the specified group. + Task GetGroupSummmary(string groupId); + /// /// Returns the profile of the specified user. /// diff --git a/src/LineBot/LineBot.cs b/src/LineBot/LineBot.cs index 0b6bd05..b643512 100644 --- a/src/LineBot/LineBot.cs +++ b/src/LineBot/LineBot.cs @@ -219,6 +219,33 @@ public async Task GetEvents(HttpRequest request) return eventCollection; } + /// + /// Returns the summary of the specified group. + /// + /// The id of the group. + /// The summary of the specified group. + public async Task GetGroupSummmary(string groupId) + { + Guard.NotNullOrEmpty(nameof(groupId), groupId); + + var response = await _client.GetAsync($"group/{groupId}/summary").ConfigureAwait(false); + await response.CheckResult().ConfigureAwait(false); + + return await response.Content.DeserializeObject().ConfigureAwait(false); + } + + /// + /// Returns the summary of the specified group. + /// + /// The group. + /// The summary of the specified group. + public async Task GetGroupSummmary(IGroup group) + { + Guard.NotNull(nameof(group), group); + + return await GetGroupSummmary(group.Id); + } + /// /// Returns the profile of the specified user. /// diff --git a/tests/LineBot.Tests/GroupProfile/GroupSummaryTests.cs b/tests/LineBot.Tests/GroupProfile/GroupSummaryTests.cs new file mode 100644 index 0000000..dfafbfa --- /dev/null +++ b/tests/LineBot.Tests/GroupProfile/GroupSummaryTests.cs @@ -0,0 +1,88 @@ +// Copyright Dirk Lemstra (https://github.com/dlemstra/line-bot-sdk-dotnet). +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Line.Tests +{ + [TestClass] + public class GroupSummaryTests + { + [TestMethod] + public async Task GetGroupSummmary_GroupIsNulll_ThrowsException() + { + ILineBot bot = TestConfiguration.CreateBot(); + await ExceptionAssert.ThrowsArgumentNullExceptionAsync("group", async () => + { + IGroupSummary summary = await bot.GetGroupSummmary((IGroup)null); + }); + } + + [TestMethod] + public async Task GetGroupSummmary_GroupIdIsNull_ThrowsException() + { + ILineBot bot = TestConfiguration.CreateBot(); + await ExceptionAssert.ThrowsArgumentNullExceptionAsync("groupId", async () => + { + IGroupSummary summary = await bot.GetGroupSummmary((string)null); + }); + } + + [TestMethod] + public async Task GetGroupSummmary_GroupIdIsEmpty_ThrowsException() + { + ILineBot bot = TestConfiguration.CreateBot(); + await ExceptionAssert.ThrowsArgumentEmptyExceptionAsync("groupId", async () => + { + IGroupSummary summary = await bot.GetGroupSummmary(string.Empty); + }); + } + + [TestMethod] + public async Task GetGroupSummmary_ErrorResponse_ThrowsException() + { + TestHttpClient httpClient = TestHttpClient.ThatReturnsAnError(); + + ILineBot bot = TestConfiguration.CreateBot(httpClient); + + await ExceptionAssert.ThrowsUnknownError(async () => + { + await bot.GetGroupSummmary("test"); + }); + } + + [TestMethod] + [DeploymentItem(JsonDocuments.GroupSummary)] + public async Task GetGroupSummary_CorrectResponse_ReturnsGroupSummary() + { + TestHttpClient httpClient = TestHttpClient.Create(JsonDocuments.GroupSummary); + + ILineBot bot = TestConfiguration.CreateBot(httpClient); + IGroupSummary summary = await bot.GetGroupSummmary("test"); + + Assert.AreEqual(HttpMethod.Get, httpClient.RequestMethod); + Assert.AreEqual("/group/test/summary", httpClient.RequestPath); + + Assert.IsNotNull(summary); + Assert.AreEqual("Ca56f94637c...", summary.GroupId); + Assert.AreEqual("Group name", summary.GroupName); + Assert.AreEqual(new Uri("https://profile.line-scdn.net/abcdefghijklmn"), summary.PictureUrl); + } + + [TestMethod] + [DeploymentItem(JsonDocuments.GroupSummary)] + public async Task GetGroupSummary_WithGroup_ReturnsGroupSummary() + { + TestHttpClient httpClient = TestHttpClient.Create(JsonDocuments.GroupSummary); + + ILineBot bot = TestConfiguration.CreateBot(httpClient); + IGroupSummary summary = await bot.GetGroupSummmary(new TestGroup()); + + Assert.AreEqual("/group/testGroup/summary", httpClient.RequestPath); + Assert.IsNotNull(summary); + } + } +} diff --git a/tests/LineBot.Tests/JsonDocuments/GroupSummary.json b/tests/LineBot.Tests/JsonDocuments/GroupSummary.json new file mode 100644 index 0000000..30c691c --- /dev/null +++ b/tests/LineBot.Tests/JsonDocuments/GroupSummary.json @@ -0,0 +1,5 @@ +{ + "groupId": "Ca56f94637c...", + "groupName": "Group name", + "pictureUrl": "https://profile.line-scdn.net/abcdefghijklmn" +} \ No newline at end of file diff --git a/tests/LineBot.Tests/JsonDocuments/JsonDocuments.cs b/tests/LineBot.Tests/JsonDocuments/JsonDocuments.cs index 40be62e..edf4e54 100644 --- a/tests/LineBot.Tests/JsonDocuments/JsonDocuments.cs +++ b/tests/LineBot.Tests/JsonDocuments/JsonDocuments.cs @@ -9,5 +9,6 @@ public static partial class JsonDocuments public const string Error = "JsonDocuments/Error.json"; public const string UserProfile = "JsonDocuments/UserProfile.json"; public const string Whitespace = "JsonDocuments/Whitespace.json"; + public const string GroupSummary = "JsonDocuments/GroupSummary.json"; } }