Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 99cab6f

Browse files
authored
Merge pull request #416 from microsoft/trboehre/teamsinfotests
Added TeamsInfoTests and TeamsActivityHandlerTests
2 parents 078c4d3 + b1b0c92 commit 99cab6f

13 files changed

Lines changed: 1975 additions & 41 deletions

File tree

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/ActivityHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.net.HttpURLConnection;
1515
import java.util.List;
1616
import java.util.concurrent.CompletableFuture;
17+
import java.util.concurrent.CompletionException;
1718

1819
/**
1920
* An implementation of the {@link Bot} interface intended for further subclassing.
@@ -309,7 +310,10 @@ protected CompletableFuture<InvokeResponse> onInvokeActivity(TurnContext turnCon
309310
return onSignInInvoke(turnContext)
310311
.thenApply(aVoid -> createInvokeResponse(null))
311312
.exceptionally(ex -> {
312-
if (ex instanceof InvokeResponseExcetion) {
313+
if (ex instanceof CompletionException && ex.getCause() instanceof InvokeResponseExcetion) {
314+
InvokeResponseExcetion ire = (InvokeResponseExcetion) ex.getCause();
315+
return new InvokeResponse(ire.statusCode, ire.body);
316+
} else if (ex instanceof InvokeResponseExcetion) {
313317
InvokeResponseExcetion ire = (InvokeResponseExcetion) ex;
314318
return new InvokeResponse(ire.statusCode, ire.body);
315319
}

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public class BotFrameworkAdapter extends BotAdapter implements AdapterIntegratio
8989
*/
9090
public static final String CONNECTOR_CLIENT_KEY = "ConnectorClient";
9191

92+
/**
93+
* Key to store TeamsConnectorClient.
94+
* For testing only.
95+
*/
96+
public static final String TEAMSCONNECTOR_CLIENT_KEY = "TeamsConnectorClient";
97+
9298
private AppCredentials appCredentials;
9399

94100
/**

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/teams/TeamsActivityHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ protected CompletableFuture<InvokeResponse> onInvokeActivity(TurnContext turnCon
153153
}
154154

155155
return result.exceptionally(e -> {
156-
if (e instanceof InvokeResponseExcetion) {
156+
if (e instanceof CompletionException && e.getCause() instanceof InvokeResponseExcetion) {
157+
return ((InvokeResponseExcetion) e.getCause()).createInvokeResponse();
158+
} else if (e instanceof InvokeResponseExcetion) {
157159
return ((InvokeResponseExcetion) e).createInvokeResponse();
158160
}
159161
return new InvokeResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, e.getLocalizedMessage());
@@ -340,7 +342,7 @@ protected CompletableFuture<Void> onConversationUpdateActivity(TurnContext turnC
340342

341343
if (turnContext.getActivity().getMembersRemoved() != null) {
342344
return onTeamsMembersRemovedDispatch(
343-
turnContext.getActivity().getMembersAdded(),
345+
turnContext.getActivity().getMembersRemoved(),
344346
channelData.result() ? channelData.value().getTeam() : null,
345347
turnContext
346348
);

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/teams/TeamsInfo.java

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
package com.microsoft.bot.builder.teams;
55

6-
import com.fasterxml.jackson.core.JsonProcessingException;
7-
import com.fasterxml.jackson.databind.JsonNode;
8-
import com.fasterxml.jackson.databind.ObjectMapper;
96
import com.microsoft.bot.builder.BotFrameworkAdapter;
107
import com.microsoft.bot.builder.TurnContext;
118
import com.microsoft.bot.connector.ConnectorClient;
@@ -17,6 +14,7 @@
1714
import com.microsoft.bot.schema.ConversationReference;
1815
import com.microsoft.bot.schema.PagedMembersResult;
1916
import com.microsoft.bot.schema.Pair;
17+
import com.microsoft.bot.schema.Serialization;
2018
import com.microsoft.bot.schema.teams.ChannelInfo;
2119
import com.microsoft.bot.schema.teams.ConversationList;
2220
import com.microsoft.bot.schema.teams.TeamDetails;
@@ -186,22 +184,8 @@ private static CompletableFuture<List<TeamsChannelAccount>> getMembers(Connector
186184

187185
return connectorClient.getConversations().getConversationMembers(conversationId)
188186
.thenApply(teamMembers -> {
189-
ObjectMapper objectMapper = new ObjectMapper();
190-
objectMapper.findAndRegisterModules();
191-
192187
List<TeamsChannelAccount> members = teamMembers.stream()
193-
.map(channelAccount -> {
194-
try {
195-
// convert fro ChannelAccount to TeamsChannelAccount by going to JSON then back
196-
// to TeamsChannelAccount.
197-
JsonNode node = objectMapper.valueToTree(channelAccount);
198-
return objectMapper.treeToValue(node, TeamsChannelAccount.class);
199-
} catch (JsonProcessingException jpe) {
200-
// this would be a conversion error. for now, return null and filter the results
201-
// below. there is probably a more elegant way to handle this.
202-
return null;
203-
}
204-
})
188+
.map(channelAccount -> Serialization.convert(channelAccount, TeamsChannelAccount.class))
205189
.collect(Collectors.toCollection(ArrayList::new));
206190

207191
members.removeIf(Objects::isNull);
@@ -216,25 +200,7 @@ private static CompletableFuture<TeamsChannelAccount> getMember(ConnectorClient
216200
}
217201

218202
return connectorClient.getConversations().getConversationMember(userId, conversationId)
219-
.thenApply(teamMember -> {
220-
if (teamMember == null) {
221-
return null;
222-
}
223-
224-
ObjectMapper objectMapper = new ObjectMapper();
225-
objectMapper.findAndRegisterModules();
226-
227-
try {
228-
// convert fro ChannelAccount to TeamsChannelAccount by going to JSON then back
229-
// to TeamsChannelAccount.
230-
JsonNode node = objectMapper.valueToTree(teamMember);
231-
return objectMapper.treeToValue(node, TeamsChannelAccount.class);
232-
} catch (JsonProcessingException jpe) {
233-
// this would be a conversion error.
234-
return null;
235-
}
236-
237-
});
203+
.thenApply(teamMember -> Serialization.convert(teamMember, TeamsChannelAccount.class));
238204
}
239205

240206
private static CompletableFuture<TeamsPagedMembersResult> getPagedMembers(ConnectorClient connectorClient,
@@ -265,6 +231,13 @@ private static ConnectorClient getConnectorClient(TurnContext turnContext) {
265231
}
266232

267233
private static TeamsConnectorClient getTeamsConnectorClient(TurnContext turnContext) {
234+
// for testing to be able to provide a custom client.
235+
TeamsConnectorClient teamsClient = turnContext.getTurnState()
236+
.get(BotFrameworkAdapter.TEAMSCONNECTOR_CLIENT_KEY);
237+
if (teamsClient != null) {
238+
return teamsClient;
239+
}
240+
268241
ConnectorClient client = getConnectorClient(turnContext);
269242
return new RestTeamsConnectorClient(client.baseUrl(), client.credentials());
270243
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.bot.builder.teams;
5+
6+
import com.microsoft.bot.builder.InvokeResponse;
7+
import com.microsoft.bot.builder.SimpleAdapter;
8+
import com.microsoft.bot.builder.TurnContext;
9+
import com.microsoft.bot.builder.TurnContextImpl;
10+
import com.microsoft.bot.schema.Activity;
11+
import com.microsoft.bot.schema.ActivityTypes;
12+
import com.microsoft.bot.schema.teams.FileConsentCardResponse;
13+
import com.microsoft.bot.schema.teams.FileUploadInfo;
14+
import com.microsoft.bot.schema.teams.MessagingExtensionAction;
15+
import org.junit.Assert;
16+
import org.junit.Test;
17+
18+
import java.util.List;
19+
import java.util.concurrent.atomic.AtomicReference;
20+
21+
public class TeamsActivityHandlerBadRequestTests {
22+
@Test
23+
public void TestFileConsentBadAction() {
24+
Activity activity = new Activity(ActivityTypes.INVOKE) {{
25+
setName("fileConsent/invoke");
26+
setValue(new FileConsentCardResponse() {{
27+
setAction("this.is.a.bad.action");
28+
setUploadInfo(new FileUploadInfo() {{
29+
setUniqueId("uniqueId");
30+
setFileType("fileType");
31+
setUploadUrl("uploadUrl");
32+
}});
33+
}});
34+
}};
35+
36+
AtomicReference<List<Activity>> activitiesToSend = new AtomicReference<>();
37+
38+
TurnContext turnContext = new TurnContextImpl(
39+
new SimpleAdapter(activitiesToSend::set),
40+
activity
41+
);
42+
43+
TeamsActivityHandler bot = new TeamsActivityHandler();
44+
bot.onTurn(turnContext).join();
45+
46+
Assert.assertNotNull(activitiesToSend.get());
47+
Assert.assertEquals(1, activitiesToSend.get().size());
48+
Assert.assertTrue(activitiesToSend.get().get(0).getValue() instanceof InvokeResponse);
49+
Assert.assertEquals(400, ((InvokeResponse) activitiesToSend.get().get(0).getValue()).getStatus());
50+
}
51+
52+
@Test
53+
public void TestMessagingExtensionSubmitActionPreviewBadAction() {
54+
Activity activity = new Activity(ActivityTypes.INVOKE) {{
55+
setName("composeExtension/submitAction");
56+
setValue(new MessagingExtensionAction() {{
57+
setBotMessagePreviewAction("this.is.a.bad.action");
58+
}});
59+
}};
60+
61+
AtomicReference<List<Activity>> activitiesToSend = new AtomicReference<>();
62+
63+
TurnContext turnContext = new TurnContextImpl(
64+
new SimpleAdapter(activitiesToSend::set),
65+
activity
66+
);
67+
68+
TeamsActivityHandler bot = new TeamsActivityHandler();
69+
bot.onTurn(turnContext).join();
70+
71+
Assert.assertNotNull(activitiesToSend.get());
72+
Assert.assertEquals(1, activitiesToSend.get().size());
73+
Assert.assertTrue(activitiesToSend.get().get(0).getValue() instanceof InvokeResponse);
74+
Assert.assertEquals(400, ((InvokeResponse) activitiesToSend.get().get(0).getValue()).getStatus());
75+
}
76+
}

0 commit comments

Comments
 (0)