-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): notification sdk enhancement for .NET
- Loading branch information
Showing
14 changed files
with
583 additions
and
93 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
packages/dotnet-sdk/src/TeamsFx.Test/Conversation/DefaultConversationReferenceStoreTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
namespace Microsoft.TeamsFx.Test.Conversation | ||
{ | ||
using Microsoft.Bot.Schema; | ||
using Microsoft.TeamsFx.Conversation; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
[Obsolete] | ||
internal class DefaultConversationReferenceStoreTest | ||
{ | ||
private readonly InMemoryStorage _storage; | ||
private readonly DefaultConversationReferenceStore _store; | ||
|
||
public DefaultConversationReferenceStoreTest() | ||
{ | ||
_storage = new InMemoryStorage(); | ||
_store = new DefaultConversationReferenceStore(_storage); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Add_NonExistItem() | ||
{ | ||
var storage = new InMemoryStorage(); | ||
var store = new DefaultConversationReferenceStore(storage); | ||
|
||
const string key = "key"; | ||
const string activityId = "add_nonexistitem"; | ||
var reference = new ConversationReference | ||
{ | ||
ActivityId = activityId | ||
}; | ||
var isAdded = await store.Add(key, reference, new ConversationReferenceStoreAddOptions { }); | ||
|
||
Assert.IsTrue(isAdded); | ||
Assert.Equals(activityId, storage.Items[key].ActivityId); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Add_OverwriteExistItem() | ||
{ | ||
var storage = new InMemoryStorage(); | ||
var store = new DefaultConversationReferenceStore(storage); | ||
|
||
const string key = "key"; | ||
const string oldActivityId = "add_existitem_old"; | ||
const string newActivityId = "add_existitem_new"; | ||
|
||
storage.Items[key] = new ConversationReference | ||
{ | ||
ActivityId = oldActivityId | ||
}; | ||
|
||
var reference = new ConversationReference | ||
{ | ||
ActivityId = newActivityId | ||
}; | ||
var isAdded = await store.Add(key, reference, new ConversationReferenceStoreAddOptions { Overwrite = true }); | ||
|
||
Assert.IsTrue(isAdded); | ||
Assert.Equals(newActivityId, storage.Items[key].ActivityId); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Add_NotOverwriteExistItem() | ||
{ | ||
var storage = new InMemoryStorage(); | ||
var store = new DefaultConversationReferenceStore(storage); | ||
|
||
const string key = "key"; | ||
const string oldActivityId = "add_existitem_old"; | ||
const string newActivityId = "add_existitem_new"; | ||
|
||
storage.Items[key] = new ConversationReference | ||
{ | ||
ActivityId = oldActivityId | ||
}; | ||
|
||
var reference = new ConversationReference | ||
{ | ||
ActivityId = newActivityId | ||
}; | ||
var isAdded = await store.Add(key, reference, new ConversationReferenceStoreAddOptions { Overwrite = false }); | ||
|
||
Assert.IsFalse(isAdded); | ||
Assert.Equals(oldActivityId, storage.Items[key].ActivityId); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Add_NotOverwriteExistItemByDefault() | ||
{ | ||
var storage = new InMemoryStorage(); | ||
var store = new DefaultConversationReferenceStore(storage); | ||
|
||
const string key = "key"; | ||
const string oldActivityId = "add_existitem_old"; | ||
const string newActivityId = "add_existitem_new"; | ||
|
||
storage.Items[key] = new ConversationReference | ||
{ | ||
ActivityId = oldActivityId | ||
}; | ||
|
||
var reference = new ConversationReference | ||
{ | ||
ActivityId = newActivityId | ||
}; | ||
var isAdded = await store.Add(key, reference, new ConversationReferenceStoreAddOptions { }); | ||
|
||
Assert.IsFalse(isAdded); | ||
Assert.Equals(oldActivityId, storage.Items[key].ActivityId); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Remove_ExistItem() | ||
{ | ||
var storage = new InMemoryStorage(); | ||
var store = new DefaultConversationReferenceStore(storage); | ||
|
||
const string key = "key"; | ||
const string activityId = "remove_existitem"; | ||
|
||
var reference = new ConversationReference | ||
{ | ||
ActivityId = activityId | ||
}; | ||
storage.Items[key] = reference; | ||
|
||
var isRemoved = await store.Remove(key, reference); | ||
Assert.IsTrue(isRemoved); | ||
Assert.IsTrue(!storage.Items.ContainsKey(key)); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Remove_NonExistItem() | ||
{ | ||
var storage = new InMemoryStorage(); | ||
var store = new DefaultConversationReferenceStore(storage); | ||
|
||
const string key = "key"; | ||
const string activityId = "remove_existitem"; | ||
|
||
var reference = new ConversationReference | ||
{ | ||
ActivityId = activityId | ||
}; | ||
|
||
var isRemoved = await store.Remove(key, reference); | ||
Assert.IsFalse(isRemoved); | ||
} | ||
|
||
[TestMethod] | ||
public async Task List() | ||
{ | ||
var storage = new InMemoryStorage(); | ||
var store = new DefaultConversationReferenceStore(storage); | ||
|
||
storage.Items.Add("key1", new ConversationReference { ActivityId = "activity-1" }); | ||
storage.Items.Add("key2", new ConversationReference { ActivityId = "activity-2" }); | ||
storage.Items.Add("key3", new ConversationReference { ActivityId = "activity-3" }); | ||
|
||
var items = await store.List(); | ||
Assert.AreEqual(3, items.Data.Length); | ||
Assert.IsNull(items.ContinuationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/dotnet-sdk/src/TeamsFx/Conversation/DefaultConversationReferenceStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.TeamsFx.Conversation | ||
{ | ||
using Microsoft.Bot.Schema; | ||
|
||
[Obsolete] | ||
internal sealed class DefaultConversationReferenceStore : IConversationReferenceStore | ||
{ | ||
private readonly INotificationTargetStorage _storage; | ||
|
||
public DefaultConversationReferenceStore(INotificationTargetStorage storage) | ||
{ | ||
_storage = storage; | ||
} | ||
|
||
public async Task<bool> Add( | ||
string key, | ||
ConversationReference reference, | ||
ConversationReferenceStoreAddOptions options, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (string.IsNullOrEmpty(key)) | ||
{ | ||
throw new ArgumentException($"{nameof(key)} can't be null or empty."); | ||
} | ||
|
||
if (reference == null) | ||
{ | ||
throw new ArgumentNullException(nameof(reference)); | ||
} | ||
|
||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
var overwrite = options.Overwrite ?? false; | ||
if (overwrite) | ||
{ | ||
await _storage.Write(key, reference, cancellationToken).ConfigureAwait(false); | ||
return true; | ||
} | ||
|
||
var result = await _storage.Read(key, cancellationToken).ConfigureAwait(false); | ||
if (result == null) | ||
{ | ||
await _storage.Write(key, reference, cancellationToken).ConfigureAwait(false); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public async Task<bool> Remove( | ||
string key, | ||
ConversationReference reference, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (string.IsNullOrEmpty(key)) | ||
{ | ||
throw new ArgumentException($"{nameof(key)} can't be null or empty."); | ||
} | ||
|
||
if (reference == null) | ||
{ | ||
throw new ArgumentNullException(nameof(reference)); | ||
} | ||
|
||
var result = await _storage.Read(key, cancellationToken).ConfigureAwait(false); | ||
if (result == null) | ||
{ | ||
return false; | ||
} | ||
|
||
await _storage.Delete(key, cancellationToken).ConfigureAwait(false); | ||
return true; | ||
} | ||
|
||
public async Task<PagedData<ConversationReference>> List( | ||
int? pageSize = null, | ||
string continuationToken = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var data = await _storage.List(cancellationToken).ConfigureAwait(false); | ||
return new PagedData<ConversationReference> { | ||
Data = data, | ||
ContinuationToken = null, | ||
}; | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/dotnet-sdk/src/TeamsFx/Conversation/IConversationReferenceStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.TeamsFx.Conversation | ||
{ | ||
using Microsoft.Bot.Schema; | ||
|
||
/// <summary> | ||
/// Interface for a store provider that manages notification target references. | ||
/// </summary> | ||
public interface IConversationReferenceStore | ||
{ | ||
/// <summary> | ||
/// Add a conversation reference to the store. If overwrite, update the existing one, otherwise add when not exist. | ||
/// </summary> | ||
/// <param name="key">The target key.</param> | ||
/// <param name="reference">The conversation reference to be added.</param> | ||
/// <param name="options">The options to add the conversation reference.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>true if added or updated, false if not changed.</returns> | ||
public Task<bool> Add( | ||
string key, | ||
ConversationReference reference, | ||
ConversationReferenceStoreAddOptions options, | ||
CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Remove a conversation reference from the store. | ||
/// </summary> | ||
/// <param name="key">The target key.</param> | ||
/// <param name="reference">The conversation reference to be removed.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>true if exist and removed, false if not changed.</returns> | ||
public Task<bool> Remove( | ||
string key, | ||
ConversationReference reference, | ||
CancellationToken cancellationToken = default); | ||
|
||
|
||
/// <summary> | ||
/// List stored conversation reference by page. | ||
/// </summary> | ||
/// <param name="pageSize">The page size.</param> | ||
/// <param name="continuationToken">The continuation token.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>A paged list of conversation references.</returns> | ||
public Task<PagedData<ConversationReference>> List( | ||
int? pageSize = default, | ||
string continuationToken = default, | ||
CancellationToken cancellationToken = default); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/dotnet-sdk/src/TeamsFx/Conversation/IConversationReferenceStoreAddOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.TeamsFx.Conversation | ||
{ | ||
/// <summary> | ||
/// The options to add a conversation reference. | ||
/// </summary> | ||
public class ConversationReferenceStoreAddOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether to overwrite the existing conversation reference. | ||
/// </summary> | ||
public bool? Overwrite { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.