diff --git a/libs/teams/teams-chat-workflow-spring-boot-starter/src/main/java/org/finos/springbot/teams/conversations/TeamsErrorResourceResponse.java b/libs/teams/teams-chat-workflow-spring-boot-starter/src/main/java/org/finos/springbot/teams/conversations/TeamsErrorResourceResponse.java new file mode 100644 index 00000000..0e895b78 --- /dev/null +++ b/libs/teams/teams-chat-workflow-spring-boot-starter/src/main/java/org/finos/springbot/teams/conversations/TeamsErrorResourceResponse.java @@ -0,0 +1,18 @@ +package org.finos.springbot.teams.conversations; + +import com.microsoft.bot.schema.ResourceResponse; + +public class TeamsErrorResourceResponse extends ResourceResponse { + + Throwable e; + + public TeamsErrorResourceResponse(String id, Throwable e ) { + super(id); + this.e = e; + } + + public Throwable getThrowable() { + return e; + } +} + diff --git a/libs/teams/teams-chat-workflow-spring-boot-starter/src/main/java/org/finos/springbot/teams/handlers/TeamsResponseHandler.java b/libs/teams/teams-chat-workflow-spring-boot-starter/src/main/java/org/finos/springbot/teams/handlers/TeamsResponseHandler.java index 838397bd..01f7d04b 100644 --- a/libs/teams/teams-chat-workflow-spring-boot-starter/src/main/java/org/finos/springbot/teams/handlers/TeamsResponseHandler.java +++ b/libs/teams/teams-chat-workflow-spring-boot-starter/src/main/java/org/finos/springbot/teams/handlers/TeamsResponseHandler.java @@ -9,6 +9,7 @@ import org.finos.springbot.teams.TeamsException; import org.finos.springbot.teams.content.TeamsAddressable; +import org.finos.springbot.teams.conversations.TeamsErrorResourceResponse; import org.finos.springbot.teams.history.StorageIDResponseHandler; import org.finos.springbot.teams.history.TeamsHistory; import org.finos.springbot.teams.response.templating.EntityMarkupTemplateProvider; @@ -194,7 +195,7 @@ private BiFunction handle } catch (JsonProcessingException e1) { } } else { - LOG.error("message:\n"+out); + LOG.error("message:\n"+out); } if(!(t instanceof ErrorResponse)) { @@ -204,6 +205,7 @@ private BiFunction handle initErrorHandler(); eh.handleError(e); Action.CURRENT_ACTION.set(Action.NULL_ACTION); + rr = new TeamsErrorResourceResponse(address.getKey(), e); } else if(rr != null) { performStorage(address, data, teamsState); } diff --git a/libs/teams/teams-chat-workflow-spring-boot-starter/src/test/java/org/finos/springbot/teams/handlers/TeamsResponseHandlerTest.java b/libs/teams/teams-chat-workflow-spring-boot-starter/src/test/java/org/finos/springbot/teams/handlers/TeamsResponseHandlerTest.java new file mode 100644 index 00000000..bc9c7809 --- /dev/null +++ b/libs/teams/teams-chat-workflow-spring-boot-starter/src/test/java/org/finos/springbot/teams/handlers/TeamsResponseHandlerTest.java @@ -0,0 +1,80 @@ +package org.finos.springbot.teams.handlers; + +import java.util.concurrent.CompletableFuture; + +import org.finos.springbot.teams.MockTeamsConfiguration; +import org.finos.springbot.teams.TeamsWorkflowConfig; +import org.finos.springbot.teams.content.TeamsUser; +import org.finos.springbot.teams.conversations.TeamsConversations; +import org.finos.springbot.teams.conversations.TeamsErrorResourceResponse; +import org.finos.springbot.workflow.data.DataHandlerConfig; +import org.finos.springbot.workflow.response.MessageResponse; +import org.finos.springbot.workflow.response.handlers.ResponseHandlers; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import com.microsoft.bot.connector.rest.ErrorResponseException; +import com.microsoft.bot.schema.Activity; +import com.microsoft.bot.schema.ResourceResponse; + +import okhttp3.MediaType; +import okhttp3.Protocol; +import okhttp3.Request; +import okhttp3.ResponseBody; + +import retrofit2.Response; + +@SpringBootTest(classes = { MockTeamsConfiguration.class, TeamsWorkflowConfig.class, DataHandlerConfig.class }) +@ActiveProfiles("teams") +@ExtendWith(SpringExtension.class) +public class TeamsResponseHandlerTest { + + @MockBean + ActivityHandler ah; + + @MockBean + ResponseHandlers eh; + + @Autowired + TeamsResponseHandler teamsResponseHandler; + + @Test + public void testErrorIsReturned() { + + ArgumentCaptor msg = ArgumentCaptor.forClass(Activity.class); + + Mockito.when(ah.handleActivity(msg.capture(), Mockito.any())).thenAnswer(a -> { + // fails + ResponseBody out = ResponseBody.create("{}", MediaType.get("application/json")); + Response r = Response.error(out, + new okhttp3.Response.Builder().code(HttpStatus.NOT_FOUND.value()) + .message("{\"error\":{\"code\": \"ConversationNotFound\",\"message\":\"Conversation not found.\"}}") + .protocol(Protocol.HTTP_1_1).request(new Request.Builder().url("http://localhost/").build()) + .build()); + return failed(new ErrorResponseException("Failed", r)); + }); + + TeamsUser tu = new TeamsUser("made", "up", "thing"); + MessageResponse r = new MessageResponse(tu, "Some object"); + ResourceResponse rr = teamsResponseHandler.apply(r); + + Assertions.assertTrue(rr instanceof TeamsErrorResourceResponse); + Assertions.assertNotNull(((TeamsErrorResourceResponse) rr).getThrowable()); + } + + public static CompletableFuture failed(Throwable error) { + CompletableFuture future = new CompletableFuture<>(); + future.completeExceptionally(error); + return future; + } + +}