Skip to content

Commit

Permalink
Pull request #48: SYMPHONYP-1118 test case for File handler
Browse files Browse the repository at this point in the history
Merge in SYMPHONYP/symphony-java-toolkit from feature/SYMPHONYP-1106-download-feature-in-spring-bot-team-framework to spring-bot-master-db

* commit '9129c834f6119ed03b861233bb6c6dc1e1ff4b08':
  SYMPHONYP-1118 test case for File handler
  • Loading branch information
vaibhav-db committed May 15, 2024
2 parents 921cf41 + 9129c83 commit 0f3b4e4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.finos.springbot.tests.controller;



import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -268,5 +270,13 @@ public void testButtonPress() throws Exception {
Assertions.assertEquals("desc", sc.description);

}

@Test
public void testAttachmentResponse() throws Exception {
execute("attachment");
Assertions.assertEquals("attachment", oc.lastMethod);
String data = getMessageContent();
Assertions.assertEquals("somefile.txt", data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public FileActivityHandler(List<ActionConsumer> messageConsumers, TeamsConversat
@Override
protected CompletableFuture<Void> onTeamsFileConsentAccept(TurnContext turnContext,
FileConsentCardResponse fileConsentCardResponse) {

LOG.info("onTeamsFileConsentAccept called...");

return upload(fileConsentCardResponse)
.thenCompose(result -> !result.result() ? fileUploadFailed(turnContext, result.value())
: fileUploadCompleted(turnContext, fileConsentCardResponse));
Expand All @@ -53,6 +54,7 @@ protected CompletableFuture<Void> onTeamsFileConsentAccept(TurnContext turnConte
@Override
protected CompletableFuture<Void> onTeamsFileConsentDecline(TurnContext turnContext,
FileConsentCardResponse fileConsentCardResponse) {
LOG.info("onTeamsFileConsentDecline called...");
Map<String, String> context = (Map<String, String>) fileConsentCardResponse.getContext();

Activity reply = MessageFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.microsoft.bot.schema.Mention;
import com.microsoft.bot.schema.ResourceResponse;
import com.microsoft.bot.schema.teams.ChannelInfo;
import com.microsoft.bot.schema.teams.FileConsentCard;
import com.microsoft.bot.schema.teams.TeamsChannelData;


Expand Down Expand Up @@ -128,7 +129,12 @@ protected String getMessageContent() {

if (a1.getContent() instanceof String) {
return (String) a1.getContent();
} else if (a1.getContent() instanceof ObjectNode) {
} else if (a1.getContent() instanceof FileConsentCard) {
FileConsentCard f = (FileConsentCard)(a1.getContent());
Map<String, String> acceptContext = (Map<String, String>) f.getAcceptContext();
return acceptContext.get("filename");

}else if (a1.getContent() instanceof ObjectNode) {
try {
return new ObjectMapper().writeValueAsString(a1.getContent());
} catch (JsonProcessingException e) {
Expand All @@ -152,7 +158,7 @@ protected void execute(String s) throws Exception {
s = "<span itemscope=\"\" itemtype=\"http://schema.skype.com/Mention\" itemid=\"0\">"+BOT_NAME+"</span>" + s;

mockSetup();
mockTurnContext(s, null);
mockTurnContext(s, null, false);
mah.onTurn(tc);
}

Expand Down Expand Up @@ -210,19 +216,19 @@ public static <R> CompletableFuture<R> failed(Throwable error) {
return future;
}

private void mockTurnContext(String s, Map<String, Object> formData) {
private void mockTurnContext(String s, Map<String, Object> formData, boolean isAttachement) {
tc = Mockito.mock(TurnContext.class);
CurrentTurnContext.CURRENT_CONTEXT.set(tc);

msg = ArgumentCaptor.forClass(Activity.class);
Mockito.when(tc.sendActivity(msg.capture())).thenReturn(CompletableFuture.completedFuture(null));

Activity out = createActivity(s, formData);
Activity out = createActivity(s, formData, isAttachement);
Mockito.when(tc.getActivity()).thenReturn(out);
}


private Activity createActivity(String s, Map<String, Object> formData) {
private Activity createActivity(String s, Map<String, Object> formData, boolean isAttachement) {
Activity out = new Activity(ActivityTypes.MESSAGE);

ConversationAccount conv = new ConversationAccount(CHAT_ID);
Expand All @@ -243,7 +249,22 @@ private Activity createActivity(String s, Map<String, Object> formData) {

out.setEntities(Arrays.asList(botEntity(), gauravEntity()));

if (formData != null) {

if(isAttachement) {
Attachment a = new Attachment();
String fileName = s;
Map<String, String> consentContext = new HashMap<>();
consentContext.put("filename", fileName);
FileConsentCard fileCard = new FileConsentCard();
fileCard.setDescription("This is the file I want to send you");
fileCard.setAcceptContext(consentContext);
fileCard.setDeclineContext(consentContext);

a.setContent(fileCard);
a.setName(fileName);
a.setContentType(FileConsentCard.CONTENT_TYPE);
out.setAttachment(a);
} else if (formData != null) {
formData.put("action", s);
out.setValue(formData);
} else {
Expand Down Expand Up @@ -287,7 +308,7 @@ private Entity botEntity() {
@Override
protected void pressButton(String s, Map<String, Object> formData) {
mockSetup();
mockTurnContext(s, formData);
mockTurnContext(s, formData, false);
mah.onTurn(tc);
}

Expand Down Expand Up @@ -321,5 +342,5 @@ protected void assertThrowsResponse() {
Assertions.assertTrue(message.contains("Error123"));
}


}

0 comments on commit 0f3b4e4

Please sign in to comment.