66
loading...
This website collects cookies to deliver better user experience
GraphServiceClient
AddBatchRequestStep
to control requestId
public class GraphService
{
private readonly GraphServiceClient graphServiceClient;
public GraphService(GraphServiceClient graphServiceClient)
{
this.graphServiceClient = graphServiceClient;
}
public async Task<List<User>> GetUsersAsync(List<string> userIds)
{
List<User> users = new();
BatchRequestContent batchRequestContent = new();
userIds.ForEach(x =>
{
batchRequestContent.AddBatchRequestStep(new BatchRequestStep(x, graphServiceClient.Users[x]
.Request().Select(u => new
{
u.Id,
u.GivenName,
u.Mail,
u.Surname,
u.UserPrincipalName
}).GetHttpRequestMessage()));
});
BatchResponseContent returnedResponse = await graphServiceClient.Batch.Request().PostAsync(batchRequestContent);
userIds.ForEach(async x =>
{
User user = await returnedResponse.GetResponseByIdAsync<Microsoft.Graph.User>(x);
users.Add(user);
});
return users;
}
}
IHttpProvider
to handle SendAsync
request. Return the json string as StringContent
GraphServiceClient
ahd pass mocked IHttpProvider
as constructorGetHttpRequestMessage
method as this is called in AddBatchRequestStep
[Fact]
public async Task ReturnsUsers()
{
// Arrenge
string userId = "dummy_id1";
string userId2 = "dummy_id2";
string batchResponse = @$"{{
""responses"":[{{
""id"": ""{userId}"",
""status"":200,
""headers"":
{{
""Content-Type"":""application/json""
}},
""body"":
{{
""@odata.context"":""https://graph.microsoft.com/v1.0/$metadata#users/$entity"",
""surName"":""Test"",
""givenName"":""User1"",
""id"":""{userId}""
}}
}},
{{
""id"": ""{userId2}"",
""status"":200,
""headers"":
{{
""Content-Type"":""application/json""
}},
""body"":
{{
""@odata.context"":""https://graph.microsoft.com/v1.0/$metadata#users/$entity"",
""surName"":""Test"",
""givenName"":""User2"",
""id"":""{userId2}""
}}
}}]
}}";
Mock<IHttpProvider> mockedIHttpProvider = new();
mockedIHttpProvider.Setup(x => x.SendAsync(
It.IsAny<HttpRequestMessage>(),
It.IsAny<HttpCompletionOption>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(batchResponse),
});
Mock<IAuthenticationProvider> mockedIAuthenticationProvider = new();
Mock<GraphServiceClient> mockedGraphServiceClient = new(
mockedIAuthenticationProvider.Object, mockedIHttpProvider.Object);
mockedGraphServiceClient.Setup(x => x.Users[It.IsAny<string>()]
.Request()
.Select(It.IsAny<Expression<Func<Microsoft.Graph.User, object>>>()).GetHttpRequestMessage())
.Returns(new HttpRequestMessage() { RequestUri = new Uri("http://localhost/v1.0/users") });
GraphService graphService = new(mockedGraphServiceClient.Object);
// Act
List<User> users = await graphService.GetUsersAsync(new List<string>() { userId, userId2 }).ConfigureAwait(false);
// Assert
Assert.True(users.Count == 2);
}