Adding missing null check on TelemetryClient and RecognizerOptions (#996)

* Adding first Iteration of Luis Recognizer

* Adding tests, comments and fixing bugs

* Enabling live testing, commenting Dialog Recognizer dependency

* Removing spaces and fixing not implemented try catch

* Fixing linting errors

* Fixing style check errors

* Fixing style checks

* Adding package-info.java file

* Adding missing Doc on Constructor

* Adding missing javadoc at classes

* Adding test on send trace Activity

* Adding missing test

* Added Dialogs Recognizer

* Uncommented ExternalEntityRecognizer accessors

* Added missing Dialog.Recognizer.recognize method

* Adding test for DialogContext scenarios

* Adding missing javadoc

* Throwing Exception

* Fixing style

* Missing error message

* Adding missing null check on TelemetryClient and RecognizerOptions

Co-authored-by: tracyboehrer <tracyboehrer@users.noreply.github.com>
This commit is contained in:
Emilio Munoz 2021-02-17 05:57:46 -08:00 коммит произвёл GitHub
Родитель ca5d557525
Коммит bc2f483af8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 78 добавлений и 12 удалений

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

@ -5,6 +5,7 @@ package com.microsoft.bot.ai.luis;
import com.fasterxml.jackson.databind.JsonNode;
import com.microsoft.bot.builder.IntentScore;
import com.microsoft.bot.builder.NullBotTelemetryClient;
import com.microsoft.bot.builder.RecognizerConvert;
import com.microsoft.bot.builder.RecognizerResult;
import com.microsoft.bot.builder.TurnContext;
@ -32,11 +33,18 @@ public class LuisRecognizer extends TelemetryRecognizer {
/**
* Initializes a new instance of the Luis Recognizer .
* @param recognizerOptions Luis Recognizer options to use when calling th LUIS Service.
* @throws IllegalArgumentException if null is passed as recognizerOptions.
*/
public LuisRecognizer(LuisRecognizerOptions recognizerOptions) {
if (recognizerOptions == null) {
throw new IllegalArgumentException("Recognizer Options cannot be null");
}
this.luisRecognizerOptions = recognizerOptions;
this.setLogPersonalInformation(recognizerOptions.isLogPersonalInformation());
this.setTelemetryClient(recognizerOptions.getTelemetryClient());
this.setTelemetryClient(recognizerOptions.getTelemetryClient() != null
? recognizerOptions.getTelemetryClient()
: new NullBotTelemetryClient());
}
/**

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

@ -4,6 +4,7 @@
package com.microsoft.bot.ai.luis;
import com.microsoft.bot.builder.BotTelemetryClient;
import com.microsoft.bot.builder.NullBotTelemetryClient;
import com.microsoft.bot.builder.RecognizerResult;
import com.microsoft.bot.builder.TurnContext;
import com.microsoft.bot.dialogs.DialogContext;
@ -35,7 +36,7 @@ public abstract class LuisRecognizerOptions {
/**
* Bot Telemetry Client instance.
*/
private BotTelemetryClient telemetryClient = null;
private BotTelemetryClient telemetryClient = new NullBotTelemetryClient();
/**
* Controls if personal information should be sent as telemetry.
@ -56,16 +57,6 @@ public abstract class LuisRecognizerOptions {
return application;
}
/**
* Sets the Luis Application.
*
* @param application A Luis Application instance which sets the Luis specifics to work with
*/
public void setApplication(
LuisApplication application) {
this.application = application;
}
/**
* Gets the currently configured Bot Telemetry Client that logs the LuisResult event.
*

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

@ -106,6 +106,16 @@ public class LuisRecognizerTests {
assertEquals(defaultIntent, "Greeting");
}
@Test
public void throwExceptionOnNullOptions() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
LuisRecognizer lR = new LuisRecognizer(null);
});
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("Recognizer Options cannot be null"));
}
@Test
public void recognizerResult() {
setMockObjectsForTelemetry();
@ -137,6 +147,62 @@ public class LuisRecognizerTests {
assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual));
} catch (InterruptedException | ExecutionException | JsonProcessingException e) {
e.printStackTrace();
assertTrue(false);
}
}
@Test
public void recognizerResult_nullTelemetryClient() {
when(turnContext.getActivity())
.thenReturn(new Activity() {{
setText("Random Message");
setType(ActivityTypes.MESSAGE);
setChannelId("EmptyContext");
setFrom(new ChannelAccount(){{
setId("Activity-from-ID");
}});
}});
when(luisApplication.getApplicationId())
.thenReturn("b31aeaf3-3511-495b-a07f-571fc873214b");
when(options.getApplication())
.thenReturn(luisApplication);
mockedResult.setText("Random Message");
doReturn(CompletableFuture.supplyAsync(() -> mockedResult))
.when(options)
.recognizeInternal(
any(TurnContext.class));
LuisRecognizer recognizer = new LuisRecognizer(options);
RecognizerResult expected = new RecognizerResult(){{
setText("Random Message");
setIntents(new HashMap<String, IntentScore>(){{
put("Test",
new IntentScore(){{
setScore(0.2);
}});
put("Greeting",
new IntentScore(){{
setScore(0.4);
}});
}});
setEntities(JsonNodeFactory.instance.objectNode());
setProperties(
"sentiment",
JsonNodeFactory.instance.objectNode()
.put(
"label",
"neutral"));
}};
RecognizerResult actual = null;
try {
actual = recognizer.recognize(turnContext).get();
ObjectMapper mapper = new ObjectMapper();
assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual));
} catch (InterruptedException | ExecutionException | JsonProcessingException e) {
e.printStackTrace();
assertTrue(false);
}
}
@ -195,6 +261,7 @@ public class LuisRecognizerTests {
assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual));
} catch (InterruptedException | ExecutionException | JsonProcessingException e) {
e.printStackTrace();
assertTrue(false);
}
}