Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 1a1a88f

Browse files
committed
Added RoomMembershipApi and corresponding tests. Also added some some high level comments to other apis.
1 parent d54af34 commit 1a1a88f

File tree

9 files changed

+227
-2
lines changed

9 files changed

+227
-2
lines changed

src/SymphonyOSS.RestApiClient/Api/AgentApi/UtilApi.cs

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public class UtilApi
3434

3535
private readonly IApiExecutor _apiExecutor;
3636

37+
/// <summary>
38+
/// Initializes a new instance of the <see cref="UtilApi" /> class.
39+
/// See <see cref="Factories.PodApiFactory"/> for conveniently constructing
40+
/// an instance.
41+
/// </summary>
42+
/// <param name="authTokens">Authentication tokens.</param>
43+
/// <param name="configuration">Api configuration.</param>
44+
/// <param name="apiExecutor">Execution strategy.</param>
3745
public UtilApi(IAuthTokens authTokens, Configuration configuration, IApiExecutor apiExecutor)
3846
{
3947
_datafeedApi = new Generated.OpenApi.AgentApi.Api.UtilApi(configuration);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Licensed to the Symphony Software Foundation (SSF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SSF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace SymphonyOSS.RestApiClient.Api.PodApi
19+
{
20+
using Authentication;
21+
using Generated.OpenApi.PodApi.Client;
22+
using Generated.OpenApi.PodApi.Model;
23+
24+
/// <summary>
25+
/// Provides methods for operating over multy party chats
26+
/// and chat rooms, by encapsulating <see cref="Generated.OpenApi.PodApi.Api.RoomMembershipApi"/>,
27+
/// adding authentication token management and a custom execution strategy.
28+
/// </summary>
29+
public class RoomMembershipApi
30+
{
31+
private readonly Generated.OpenApi.PodApi.Api.IRoomMembershipApi _roomMembershipApi;
32+
33+
private readonly IAuthTokens _authTokens;
34+
35+
private readonly IApiExecutor _apiExecutor;
36+
37+
/// <summary>
38+
/// Initializes a new instance of the <see cref="RoomMembershipApi" /> class.
39+
/// See <see cref="Factories.PodApiFactory"/> for conveniently constructing
40+
/// an instance.
41+
/// </summary>
42+
/// <param name="authTokens">Authentication tokens.</param>
43+
/// <param name="configuration">Api configuration.</param>
44+
/// <param name="apiExecutor">Execution strategy.</param>
45+
public RoomMembershipApi(IAuthTokens authTokens, Configuration configuration, IApiExecutor apiExecutor)
46+
{
47+
_roomMembershipApi = new Generated.OpenApi.PodApi.Api.RoomMembershipApi(configuration);
48+
_authTokens = authTokens;
49+
_apiExecutor = apiExecutor;
50+
}
51+
52+
/// <summary>
53+
/// Adds a member to an existing room.
54+
/// </summary>
55+
/// <param name="roomId">The id of the room.</param>
56+
/// <param name="userId">The id of the user to add to the room.</param>
57+
/// <returns></returns>
58+
public SuccessResponse AddMemberToRoom(string roomId, long? userId)
59+
{
60+
return _apiExecutor.Execute(_roomMembershipApi.V1RoomIdMembershipAddPost, roomId, new UserId(userId), _authTokens.SessionToken);
61+
}
62+
63+
/// <summary>
64+
/// Removes a member from an existing room.
65+
/// </summary>
66+
/// <param name="roomId">The id of the room.</param>
67+
/// <param name="userId">The id of the user to add to the room.</param>
68+
/// <returns></returns>
69+
public SuccessResponse RemoveMemberFromRoom(string roomId, long? userId)
70+
{
71+
return _apiExecutor.Execute(_roomMembershipApi.V1RoomIdMembershipRemovePost, roomId, new UserId(userId), _authTokens.SessionToken);
72+
}
73+
74+
/// <summary>
75+
/// Promotes a user to owner of the room.
76+
/// </summary>
77+
/// <param name="roomId">The id of the room.</param>
78+
/// <param name="userId">The id of the user to add to the room.</param>
79+
/// <returns></returns>
80+
public SuccessResponse PromoteUserToRoomOwner(string roomId, long? userId)
81+
{
82+
return _apiExecutor.Execute(_roomMembershipApi.V1RoomIdMembershipPromoteOwnerPost, roomId, new UserId(userId), _authTokens.SessionToken);
83+
}
84+
85+
/// <summary>
86+
/// Demotes a user from owner of a room.
87+
/// </summary>
88+
/// <param name="roomId">The id of the room.</param>
89+
/// <param name="userId">The id of the user to add to the room.</param>
90+
/// <returns></returns>
91+
public SuccessResponse DemoteRoomOwner(string roomId, long? userId)
92+
{
93+
return _apiExecutor.Execute(_roomMembershipApi.V1RoomIdMembershipDemoteOwnerPost, roomId, new UserId(userId), _authTokens.SessionToken);
94+
}
95+
96+
/// <summary>
97+
/// Gets the members of a room.
98+
/// </summary>
99+
/// <param name="roomId">The id of the room.</param>
100+
/// <returns></returns>
101+
public MembershipList GetRoomMembers(string roomId)
102+
{
103+
return _apiExecutor.Execute(_roomMembershipApi.V1RoomIdMembershipListGet, roomId, _authTokens.SessionToken);
104+
}
105+
}
106+
}

src/SymphonyOSS.RestApiClient/Factories/AgentApiFactory.cs

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ public MessagesApi CreateMessagesApi(ISessionManager sessionManager, IApiExecuto
7575
return Create<MessagesApi>(sessionManager, apiExecutor);
7676
}
7777

78+
/// <summary>
79+
/// Constructs a UtilApi instance using the provided session manager
80+
/// for authentication.
81+
/// </summary>
82+
/// <param name="sessionManager">Session manager used for authentication.</param>
83+
/// <param name="apiExecutor">The executor, if none is provided <see cref="RetryStrategyApiExecutor"/>
84+
/// with a <see cref="RefreshTokensRetryStrategy"/> will be used.</param>
85+
/// <returns>The UtilApi instance.</returns>
7886
public UtilApi CreateUtilApi(ISessionManager sessionManager, IApiExecutor apiExecutor = null)
7987
{
8088
return Create<UtilApi>(sessionManager, apiExecutor);

src/SymphonyOSS.RestApiClient/Factories/PodApiFactory.cs

+12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ public UsersApi CreateUsersApi(ISessionManager sessionManager, IApiExecutor apiE
114114
return Create<UsersApi>(sessionManager, apiExecutor);
115115
}
116116

117+
/// <summary>
118+
/// Construcs a RoomMembershipApi using the procided session manager for authentication.
119+
/// </summary>
120+
/// <param name="sessionManager">Session manager used for authentication.</param>
121+
/// <param name="apiExecutor">The executor, if none is provided <see cref="RetryStrategyApiExecutor"/>
122+
/// with a <see cref="RefreshTokensRetryStrategy"/> will be used.</param>
123+
/// <returns>The RoomMembershipApi instance.</returns>
124+
public RoomMembershipApi CreateRoomMembershipApi(ISessionManager sessionManager, IApiExecutor apiExecutor = null)
125+
{
126+
return Create<RoomMembershipApi>(sessionManager, apiExecutor);
127+
}
128+
117129
private T Create<T>(ISessionManager sessionManager, IApiExecutor apiExecutor = null)
118130
{
119131
var apiClient = new ApiClient(_baseUrl)

test/SymphonyOSS.RestApiClient.Tests/AgentApiFactoryTest.cs

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public void EnsureConstructs_a_MessagesApi_instance()
5656
var result = agentApiFactory.CreateMessagesApi(_sessionManager);
5757
Assert.NotNull(result);
5858
}
59+
60+
[Fact]
61+
public void EnsureConstructs_a_UtilApi_instance()
62+
{
63+
var agentApiFactory = new AgentApiFactory("https://agent");
64+
var result = agentApiFactory.CreateUtilApi(_sessionManager);
65+
Assert.NotNull(result);
66+
}
5967
}
6068

6169
}

test/SymphonyOSS.RestApiClient.Tests/PodApiFactoryTest.cs

+8
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,13 @@ public void EnsureConstructs_a_UsersApi_instance()
8181
var result = podApiFactory.CreateUsersApi(_sessionManager);
8282
Assert.NotNull(result);
8383
}
84+
85+
[Fact]
86+
public void EnsureConstructs_a_RoomMembershipApi_instance()
87+
{
88+
var podApiFactory = new PodApiFactory("https://pod");
89+
var result = podApiFactory.CreateRoomMembershipApi(_sessionManager);
90+
Assert.NotNull(result);
91+
}
8492
}
8593
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
namespace SymphonyOSS.RestApiClient.Tests
2+
{
3+
using System;
4+
using Api;
5+
using Api.PodApi;
6+
using Authentication;
7+
using Generated.OpenApi.PodApi.Client;
8+
using Generated.OpenApi.PodApi.Model;
9+
using Moq;
10+
using Xunit;
11+
12+
/// <summary>
13+
/// Summary description for RoomMembershipApiTest
14+
/// </summary>
15+
public class RoomMembershipApiTest
16+
{
17+
private readonly RoomMembershipApi _roomMembershipApi;
18+
19+
private readonly Mock<IApiExecutor> _apiExecutorMock;
20+
21+
public RoomMembershipApiTest()
22+
{
23+
var sessionManagerMock = new Mock<IAuthTokens>();
24+
sessionManagerMock.Setup(obj => obj.SessionToken).Returns("sessionToken");
25+
var configuration = new Configuration();
26+
_apiExecutorMock = new Mock<IApiExecutor>();
27+
_roomMembershipApi = new RoomMembershipApi(sessionManagerMock.Object, configuration, _apiExecutorMock.Object);
28+
}
29+
30+
[Fact]
31+
public void EnsureAddMemberToRoom_uses_retry_strategy()
32+
{
33+
var roomId = "some_room";
34+
long? userId = 123456789;
35+
_roomMembershipApi.AddMemberToRoom(roomId, userId);
36+
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, long?, string, SuccessResponse>>(), roomId, userId, "sessionToken"));
37+
}
38+
39+
[Fact]
40+
public void EnsureRemoveMemberFromRoom_uses_retry_strategy()
41+
{
42+
var roomId = "some_room";
43+
long? userId = 123456789;
44+
_roomMembershipApi.RemoveMemberFromRoom(roomId, userId);
45+
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, long?, string, SuccessResponse>>(), roomId, userId, "sessionToken"));
46+
}
47+
48+
[Fact]
49+
public void EnsurePromoteUserToRoomOwner_uses_retry_strategy()
50+
{
51+
var roomId = "some_room";
52+
long? userId = 123456789;
53+
_roomMembershipApi.PromoteUserToRoomOwner(roomId, userId);
54+
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, long?, string, SuccessResponse>>(), roomId, userId, "sessionToken"));
55+
}
56+
57+
[Fact]
58+
public void EnsureDemoteRoomOwner_uses_retry_strategy()
59+
{
60+
var roomId = "some_room";
61+
long? userId = 123456789;
62+
_roomMembershipApi.DemoteRoomOwner(roomId, userId);
63+
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, long?, string, SuccessResponse>>(), roomId, userId, "sessionToken"));
64+
}
65+
66+
[Fact]
67+
public void EnsureGetRoomMembers_uses_retry_strategy()
68+
{
69+
var roomId = "some_room";
70+
_roomMembershipApi.GetRoomMembers(roomId);
71+
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, SuccessResponse>>(), roomId, "sessionToken"));
72+
}
73+
}
74+
}

test/SymphonyOSS.RestApiClient.Tests/SymphonyOSS.RestApiClient.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="StreamsApiTest.cs" />
9999
<Compile Include="SystemApiTest.cs" />
100100
<Compile Include="UsersApiTest.cs" />
101+
<Compile Include="UtilApiTest.cs" />
101102
</ItemGroup>
102103
<ItemGroup>
103104
<ProjectReference Include="..\..\src\SymphonyOSS.RestApiClient.Generated\SymphonyOSS.RestApiClient.Generated.csproj">

test/SymphonyOSS.RestApiClient.Tests/UtilApiTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public void EnsureEcho_uses_retry_strategy()
3636
{
3737
const string msg = "Hello!";
3838
_utilApi.Echo(msg);
39-
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, SimpleMessage>>(), msg));
39+
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, string, SimpleMessage>>(), "sessionToken", "keyManagerToken", msg));
4040
}
4141

4242
[Fact]
4343
public void EnsureObsolete_uses_retry_strategy()
4444
{
4545
const string msg = "Obsolete!";
4646
_utilApi.Obsolete(msg);
47-
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, SimpleMessage>>(), msg));
47+
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, string, SimpleMessage>>(), "sessionToken", "keyManagerToken", msg));
4848
}
4949
}
5050
}

0 commit comments

Comments
 (0)