Co-authored-by: tracyboehrer <tracyboehrer@users.noreply.github.com>
This commit is contained in:
Lee Parrish 2021-03-11 14:35:57 -06:00 коммит произвёл GitHub
Родитель 4dd06e088f
Коммит 2d4ef624aa
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 0 добавлений и 349 удалений

Просмотреть файл

@ -11,11 +11,9 @@ import java.util.concurrent.CompletionException;
import org.apache.commons.lang3.StringUtils;
import com.microsoft.bot.connector.ConnectorClient;
import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.ActivityTypes;
import com.microsoft.bot.schema.ChannelAccount;
import com.microsoft.bot.schema.HealthCheckResponse;
import com.microsoft.bot.schema.MessageReaction;
import com.microsoft.bot.schema.ResourceResponse;
import com.microsoft.bot.schema.SignInConstants;
@ -415,10 +413,6 @@ public class ActivityHandler implements Bot {
}
return new InvokeResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, null);
});
} else if (StringUtils.equals(turnContext.getActivity().getName(), "healthCheck")) {
CompletableFuture<InvokeResponse> result = new CompletableFuture<>();
result.complete(new InvokeResponse(HttpURLConnection.HTTP_OK, onHealthCheck(turnContext)));
return result;
}
CompletableFuture<InvokeResponse> result = new CompletableFuture<>();
@ -450,18 +444,6 @@ public class ActivityHandler implements Bot {
return result;
}
/**
* Invoked when a 'healthCheck' event is
* received when the base behavior of onInvokeActivity is used.
*
* @param turnContext The current TurnContext.
* @return A task that represents a HealthCheckResponse.
*/
protected CompletableFuture<HealthCheckResponse> onHealthCheck(TurnContext turnContext) {
ConnectorClient client = turnContext.getTurnState().get(BotFrameworkAdapter.CONNECTOR_CLIENT_KEY);
return CompletableFuture.completedFuture(HealthCheck.createHealthCheckResponse(client));
}
/**
* Creates a success InvokeResponse with the specified body.
*

Просмотреть файл

@ -1,51 +0,0 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.bot.builder;
import java.util.concurrent.ExecutionException;
import com.microsoft.bot.connector.ConnectorClient;
import com.microsoft.bot.connector.authentication.AppCredentials;
import com.microsoft.bot.schema.HealthCheckResponse;
import com.microsoft.bot.schema.HealthResults;
/**
* A class to process a HealthCheck request.
*/
public final class HealthCheck {
private HealthCheck() {
// not called
}
/**
* @param connector the ConnectorClient instance for this request
* @return HealthCheckResponse
*/
public static HealthCheckResponse createHealthCheckResponse(ConnectorClient connector) {
HealthResults healthResults = new HealthResults();
healthResults.setSuccess(true);
if (connector != null) {
healthResults.setUserAgent(connector.getUserAgent());
AppCredentials credentials = (AppCredentials) connector.credentials();
try {
healthResults.setAuthorization(credentials.getToken().get());
} catch (InterruptedException | ExecutionException ignored) {
// An exception here may happen when you have a valid appId but invalid or blank secret.
// No callbacks will be possible, although the bot maybe healthy in other respects.
}
}
if (healthResults.getAuthorization() != null) {
healthResults.setMessages(new String[]{"Health check succeeded."});
} else {
healthResults.setMessages(new String[]{"Health check succeeded.", "Callbacks are not authorized."});
}
HealthCheckResponse response = new HealthCheckResponse();
response.setHealthResults(healthResults);
return response;
}
}

Просмотреть файл

@ -10,9 +10,6 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import org.mockito.internal.matchers.Not;
public class ActivityHandlerTests {
@Test
@ -428,98 +425,6 @@ public class ActivityHandlerTests {
Assert.assertEquals("onUnrecognizedActivityType", bot.getRecord().get(0));
}
@Test
public void TestHealthCheckAsyncOverride() {
Activity activity = new Activity() {
{
setType(ActivityTypes.INVOKE);
setName("healthCheck");
}
};
TurnContext turnContext = new TurnContextImpl(new TestInvokeAdapter(), activity);
TestActivityHandler bot = new TestActivityHandler();
bot.onTurn(turnContext).join();
Assert.assertEquals(2, bot.getRecord().size());
Assert.assertEquals("onInvokeActivity", bot.getRecord().get(0));
Assert.assertEquals("onHealthCheck", bot.getRecord().get(1));
}
@Test
public void TestHealthCheckAsync() {
Activity activity = new Activity() {
{
setType(ActivityTypes.INVOKE);
setName("healthCheck");
}
};
AtomicReference<List<Activity>> activitiesToSend = new AtomicReference<>();
TurnContext turnContext = new TurnContextImpl(new SimpleAdapter(activitiesToSend::set), activity);
ActivityHandler bot = new ActivityHandler();
bot.onTurn(turnContext).join();
Assert.assertNotNull(activitiesToSend.get());
Assert.assertEquals(1, activitiesToSend.get().size());
Assert.assertTrue(activitiesToSend.get().get(0).getValue() instanceof InvokeResponse);
Assert.assertEquals(200, ((InvokeResponse) activitiesToSend.get().get(0).getValue()).getStatus());
CompletableFuture future = ((CompletableFuture) ((InvokeResponse) activitiesToSend.get().get(0).getValue())
.getBody());
HealthCheckResponse result = new HealthCheckResponse();
result = (HealthCheckResponse) future.join();
Assert.assertTrue(result.getHealthResults().getSuccess());
String[] messages = result.getHealthResults().getMessages();
Assert.assertEquals(messages[0], "Health check succeeded.");
}
@Test
public void TestHealthCheckWithConnectorAsync() {
Activity activity = new Activity() {
{
setType(ActivityTypes.INVOKE);
setName("healthCheck");
}
};
AtomicReference<List<Activity>> activitiesToSend = new AtomicReference<>();
TurnContext turnContext = new TurnContextImpl(new SimpleAdapter(activitiesToSend::set), activity);
MockConnectorClient mockConnector = new MockConnectorClient("Windows/3.1", new MockAppCredentials("awesome"));
turnContext.getTurnState().add(BotFrameworkAdapter.CONNECTOR_CLIENT_KEY, mockConnector);
ActivityHandler bot = new ActivityHandler();
bot.onTurn(turnContext).join();
Assert.assertNotNull(activitiesToSend.get());
Assert.assertEquals(1, activitiesToSend.get().size());
Assert.assertTrue(activitiesToSend.get().get(0).getValue() instanceof InvokeResponse);
Assert.assertEquals(
200,
((InvokeResponse) activitiesToSend.get().get(0).getValue()).getStatus()
);
CompletableFuture<HealthCheckResponse> future =
((CompletableFuture<HealthCheckResponse>)
((InvokeResponse) activitiesToSend.get().get(0).getValue()).getBody());
HealthCheckResponse result = new HealthCheckResponse();
result = (HealthCheckResponse) future.join();
Assert.assertTrue(result.getHealthResults().getSuccess());
Assert.assertEquals(result.getHealthResults().getAuthorization(), "awesome");
Assert.assertEquals(result.getHealthResults().getUserAgent(), "Windows/3.1");
String[] messages = result.getHealthResults().getMessages();
Assert.assertEquals(messages[0], "Health check succeeded.");
}
private static class TestInvokeAdapter extends NotImplementedAdapter {
@Override
public CompletableFuture<ResourceResponse[]> sendActivities(
TurnContext context,
List<Activity> activities
) {
return CompletableFuture.completedFuture(new ResourceResponse[0]);
}
}
private static class NotImplementedAdapter extends BotAdapter {
@Override
public CompletableFuture<ResourceResponse[]> sendActivities(
@ -635,12 +540,6 @@ public class ActivityHandlerTests {
return super.onInvokeActivity(turnContext);
}
@Override
protected CompletableFuture<HealthCheckResponse> onHealthCheck(TurnContext turnContext) {
record.add("onHealthCheck");
return super.onHealthCheck(turnContext);
}
@Override
protected CompletableFuture onInstallationUpdate(TurnContext turnContext) {
record.add("onInstallationUpdate");

Просмотреть файл

@ -1,40 +0,0 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.bot.schema;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Defines the structure that is returned as the result of a health check on the bot.
* The health check is sent to the bot as an {@link Activity} of type "invoke" and this class along
* with {@link HealthResults} defines the structure of the body of the response.
* The name of the invoke Activity is "healthCheck".
*/
public class HealthCheckResponse {
/**
* The health check results.
*/
@JsonProperty(value = "healthResults")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private HealthResults healthResults;
/**
* Gets the healthResults value.
*
* @return The healthResults value.
*/
public HealthResults getHealthResults() {
return this.healthResults;
}
/**
* Sets the healthResults value.
*
* @param withHealthResults The healthResults value to set.
*/
public void setHealthResults(HealthResults withHealthResults) {
this.healthResults = withHealthResults;
}
}

Просмотреть файл

@ -1,139 +0,0 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.bot.schema;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Defines the structure that is returned as the result of a health check on the bot.
* The health check is sent to the bot as an InvokeActivity and this class along with {@link HealthCheckResponse}
* defines the structure of the body of the response.
*/
public class HealthResults {
/**
* A value indicating whether the health check has succeeded and the bot is healthy.
*/
@JsonProperty(value = "success")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Boolean success;
/**
* A value that is exactly the same as the Authorization header that would have been added to an HTTP POST back.
*/
@JsonProperty(value = "authorization")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String authorization;
/**
* A value that is exactly the same as the User-Agent header that would have been added to an HTTP POST back.
*/
@JsonProperty(value = "user-agent")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String userAgent;
/**
* Informational messages that can be optionally included in the health check response.
*/
@JsonProperty(value = "messages")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String[] messages;
/**
* Diagnostic data that can be optionally included in the health check response.
*/
@JsonProperty(value = "diagnostics")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Object diagnostics;
/**
* Gets the success value.
*
* @return The success value.
*/
public Boolean getSuccess() {
return this.success;
}
/**
* Sets the success value.
*
* @param withSuccess The success value to set.
*/
public void setSuccess(Boolean withSuccess) {
this.success = withSuccess;
}
/**
* Get the authorization value.
*
* @return the authorization value
*/
public String getAuthorization() {
return this.authorization;
}
/**
* Set the authorization value.
*
* @param withAuthorization the authization value to set
*/
public void setAuthorization(String withAuthorization) {
this.authorization = withAuthorization;
}
/**
* Get the userAgent value.
*
* @return the userAgent value
*/
public String getUserAgent() {
return this.userAgent;
}
/**
* Set the userAgent value.
*
* @param withUserAgent the userAgent value to set
*/
public void setUserAgent(String withUserAgent) {
this.userAgent = withUserAgent;
}
/**
* Get the messages value.
*
* @return the messages value
*/
public String[] getMessages() {
return this.messages;
}
/**
* Set the messages value.
*
* @param withMessages the messages value to set
*/
public void setMessages(String[] withMessages) {
this.messages = withMessages;
}
/**
* Get the diagnostics value.
*
* @return the diagnostics value
*/
public Object getDiagnostics() {
return this.diagnostics;
}
/**
* Set the diagnostics value.
*
* @param withDiagnostics the diagnostics value to set
*/
public void setDiagnostics(Object withDiagnostics) {
this.diagnostics = withDiagnostics;
}
}