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

Commit bc2f483

Browse files
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>
1 parent ca5d557 commit bc2f483

3 files changed

Lines changed: 78 additions & 12 deletions

File tree

libraries/bot-ai-luis-v3/src/main/java/com/microsoft/bot/ai/luis/LuisRecognizer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.microsoft.bot.builder.IntentScore;
8+
import com.microsoft.bot.builder.NullBotTelemetryClient;
89
import com.microsoft.bot.builder.RecognizerConvert;
910
import com.microsoft.bot.builder.RecognizerResult;
1011
import com.microsoft.bot.builder.TurnContext;
@@ -32,11 +33,18 @@ public class LuisRecognizer extends TelemetryRecognizer {
3233
/**
3334
* Initializes a new instance of the Luis Recognizer .
3435
* @param recognizerOptions Luis Recognizer options to use when calling th LUIS Service.
36+
* @throws IllegalArgumentException if null is passed as recognizerOptions.
3537
*/
3638
public LuisRecognizer(LuisRecognizerOptions recognizerOptions) {
39+
if (recognizerOptions == null) {
40+
throw new IllegalArgumentException("Recognizer Options cannot be null");
41+
}
42+
3743
this.luisRecognizerOptions = recognizerOptions;
3844
this.setLogPersonalInformation(recognizerOptions.isLogPersonalInformation());
39-
this.setTelemetryClient(recognizerOptions.getTelemetryClient());
45+
this.setTelemetryClient(recognizerOptions.getTelemetryClient() != null
46+
? recognizerOptions.getTelemetryClient()
47+
: new NullBotTelemetryClient());
4048
}
4149

4250
/**

libraries/bot-ai-luis-v3/src/main/java/com/microsoft/bot/ai/luis/LuisRecognizerOptions.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.microsoft.bot.ai.luis;
55

66
import com.microsoft.bot.builder.BotTelemetryClient;
7+
import com.microsoft.bot.builder.NullBotTelemetryClient;
78
import com.microsoft.bot.builder.RecognizerResult;
89
import com.microsoft.bot.builder.TurnContext;
910
import com.microsoft.bot.dialogs.DialogContext;
@@ -35,7 +36,7 @@ protected LuisRecognizerOptions(LuisApplication application) {
3536
/**
3637
* Bot Telemetry Client instance.
3738
*/
38-
private BotTelemetryClient telemetryClient = null;
39+
private BotTelemetryClient telemetryClient = new NullBotTelemetryClient();
3940

4041
/**
4142
* Controls if personal information should be sent as telemetry.
@@ -56,16 +57,6 @@ public LuisApplication getApplication() {
5657
return application;
5758
}
5859

59-
/**
60-
* Sets the Luis Application.
61-
*
62-
* @param application A Luis Application instance which sets the Luis specifics to work with
63-
*/
64-
public void setApplication(
65-
LuisApplication application) {
66-
this.application = application;
67-
}
68-
6960
/**
7061
* Gets the currently configured Bot Telemetry Client that logs the LuisResult event.
7162
*

libraries/bot-ai-luis-v3/src/test/java/com/microsoft/bot/ai/luis/LuisRecognizerTests.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ public void TopIntentReturnsTopIntentIfScoreEqualsMinScore() {
106106
assertEquals(defaultIntent, "Greeting");
107107
}
108108

109+
@Test
110+
public void throwExceptionOnNullOptions() {
111+
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
112+
LuisRecognizer lR = new LuisRecognizer(null);
113+
});
114+
115+
String actualMessage = exception.getMessage();
116+
assertTrue(actualMessage.contains("Recognizer Options cannot be null"));
117+
}
118+
109119
@Test
110120
public void recognizerResult() {
111121
setMockObjectsForTelemetry();
@@ -137,6 +147,62 @@ public void recognizerResult() {
137147
assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual));
138148
} catch (InterruptedException | ExecutionException | JsonProcessingException e) {
139149
e.printStackTrace();
150+
assertTrue(false);
151+
}
152+
}
153+
154+
@Test
155+
public void recognizerResult_nullTelemetryClient() {
156+
when(turnContext.getActivity())
157+
.thenReturn(new Activity() {{
158+
setText("Random Message");
159+
setType(ActivityTypes.MESSAGE);
160+
setChannelId("EmptyContext");
161+
setFrom(new ChannelAccount(){{
162+
setId("Activity-from-ID");
163+
}});
164+
}});
165+
166+
when(luisApplication.getApplicationId())
167+
.thenReturn("b31aeaf3-3511-495b-a07f-571fc873214b");
168+
169+
when(options.getApplication())
170+
.thenReturn(luisApplication);
171+
mockedResult.setText("Random Message");
172+
doReturn(CompletableFuture.supplyAsync(() -> mockedResult))
173+
.when(options)
174+
.recognizeInternal(
175+
any(TurnContext.class));
176+
177+
LuisRecognizer recognizer = new LuisRecognizer(options);
178+
RecognizerResult expected = new RecognizerResult(){{
179+
setText("Random Message");
180+
setIntents(new HashMap<String, IntentScore>(){{
181+
put("Test",
182+
new IntentScore(){{
183+
setScore(0.2);
184+
}});
185+
put("Greeting",
186+
new IntentScore(){{
187+
setScore(0.4);
188+
}});
189+
}});
190+
setEntities(JsonNodeFactory.instance.objectNode());
191+
setProperties(
192+
"sentiment",
193+
JsonNodeFactory.instance.objectNode()
194+
.put(
195+
"label",
196+
"neutral"));
197+
}};
198+
RecognizerResult actual = null;
199+
try {
200+
actual = recognizer.recognize(turnContext).get();
201+
ObjectMapper mapper = new ObjectMapper();
202+
assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual));
203+
} catch (InterruptedException | ExecutionException | JsonProcessingException e) {
204+
e.printStackTrace();
205+
assertTrue(false);
140206
}
141207
}
142208

@@ -195,6 +261,7 @@ public void recognizerResultDialogContext() {
195261
assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual));
196262
} catch (InterruptedException | ExecutionException | JsonProcessingException e) {
197263
e.printStackTrace();
264+
assertTrue(false);
198265
}
199266
}
200267

0 commit comments

Comments
 (0)