Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests for issue #48 #156

Merged
merged 5 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions src/Features/Chatbot/Extensions/WaterfallStepContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,8 @@ public static class WaterfallStepContextExtensions

public static UserProfile CreateUserProfileInstance(this WaterfallStepContext stepContext)
{
var channelData = stepContext.Context.Activity.GetChannelData<ChannelData>();
var idWithoutPrefix = stepContext.Context
.Activity
.From
.Id
.Replace(oldValue: DirectLineService.Prefix,
newValue: string.Empty);

var result = idWithoutPrefix.Split("-");
var userProfile = new UserProfile
{
UserId = int.Parse(result[0]),
PersonId = int.Parse(result[1]),
FullName = channelData?.FullName
};
var channelData = stepContext.Context.Activity.GetChannelData<ChannelData>();
var userProfile = UserProfileFactory.Create(channelData, stepContext.Context.Activity.From.Id);
stepContext.Values[UserInfo] = userProfile;
return userProfile;
}
Expand Down
36 changes: 36 additions & 0 deletions src/Features/Chatbot/Factories/UserProfileFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace DentallApp.Features.Chatbot.Factories;

/// <summary>
/// A factory that can create <see cref="UserProfile" /> instances.
/// </summary>
public class UserProfileFactory
{
public const string IdentifiersCouldNotBeExtractedSeparatelyMessage
= "The 'user ID' and 'person ID' could not be extracted separately from channel ID.\n" +
"This error may be caused by not following the format: " +
"{userID}-{personID}";

/// <summary>
/// Creates an instance of type <see cref="UserProfile" />.
/// </summary>
/// <param name="channelData">The channel data.</param>
/// <param name="channelId">The channel ID.</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException">
/// The identifiers cannot be extracted separately from the channel ID.
/// </exception>
public static UserProfile Create(ChannelData channelData, string channelId)
{
var idWithoutPrefix = channelId.Replace(oldValue: DirectLineService.Prefix, newValue: string.Empty);
var identifiers = idWithoutPrefix.Split("-");
if (identifiers.Length != 2)
throw new InvalidOperationException(IdentifiersCouldNotBeExtractedSeparatelyMessage);
var userProfile = new UserProfile
{
UserId = int.Parse(identifiers[0]),
PersonId = int.Parse(identifiers[1]),
FullName = channelData?.FullName
};
return userProfile;
}
}
3 changes: 3 additions & 0 deletions src/Features/Chatbot/Models/ChannelData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace DentallApp.Features.Chatbot.Models;

/// <summary>
/// Represents the data of a channel such as Direct Line or WebChat.
/// </summary>
public class ChannelData
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

public static class ActivityFactory
{
private const string UserId = "1000";
private const string PersonId = "2000";

public static Activity CreateInitialActivity()
=> new()
{
Text = "Hi",
From = new ChannelAccount { Id = "dl_1000-2000", Name = "[email protected]" },
From = new ChannelAccount { Id = $"dl_{UserId}-{PersonId}", Name = "[email protected]" },
ChannelData = JObject.Parse(@"
{
fullName: 'Dave Roman'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace DentallApp.UnitTests.Features.Chatbot.Factories;

[TestClass]
public class UserProfileFactoryTests
{
[TestMethod]
public void Create_WhenChannelIdStartsWithPrefix_ShouldReturnsUserProfileInstance()
{
var channelData = new ChannelData();
var channelId = "dl_1000-2000";

var userProfile = UserProfileFactory.Create(channelData, channelId);

Assert.AreEqual(expected: 1000, actual: userProfile.UserId);
Assert.AreEqual(expected: 2000, actual: userProfile.PersonId);
Assert.IsNull(userProfile.FullName);
}

[TestMethod]
public void Create_WhenChannelIdHasNotPrefix_ShouldReturnsUserProfileInstance()
{
var channelData = new ChannelData();
var channelId = "1000-2000";

var userProfile = UserProfileFactory.Create(channelData, channelId);

Assert.AreEqual(expected: 1000, actual: userProfile.UserId);
Assert.AreEqual(expected: 2000, actual: userProfile.PersonId);
Assert.IsNull(userProfile.FullName);
}

[DataTestMethod]
[DataRow("dl_")]
[DataRow("dl_1000_2000")]
[DataRow("1000_2000")]
[DataRow("1000")]
[DataRow("dl_1000-2000-1-2")]
[DataRow("1000-2000-1-2")]
[DataRow("--")]
[DataRow("")]
[DataRow(" ")]
public void Create_WhenIdentifiersCouldNotBeExtractedSeparately_ShouldThrowInvalidOperationException(string channelId)
{
var channelData = new ChannelData();

void action() => UserProfileFactory.Create(channelData, channelId);

var exception = Assert.ThrowsException<InvalidOperationException>(action);
StringAssert.Contains(exception.Message, UserProfileFactory.IdentifiersCouldNotBeExtractedSeparatelyMessage);
}

[TestMethod]
public void Create_WhenChannelDataIsNull_ShouldReturnsFullNameWithNullValue()
{
ChannelData channelData = default;
var channelId = "dl_1000-2000";

var userProfile = UserProfileFactory.Create(channelData, channelId);

Assert.IsNull(userProfile.FullName);
}

[DataTestMethod]
[DataRow("")]
[DataRow("Dave Roman")]
public void Create_WhenChannelDataIsNotNull_ShouldReturnsFullNameWithValidValue(string value)
{
var channelData = new ChannelData { FullName = value };
var channelId = "dl_1000-2000";

var userProfile = UserProfileFactory.Create(channelData, channelId);

Assert.AreEqual(expected: value, actual: userProfile.FullName);
}
}