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

Commit 8a0a153

Browse files
Add additional unit test coverage (#1185) (#1186)
* Activity Unit Tests * additional unit tests * Remove surefire entry from dialogs pom.xml * Update surefire entry in pom.xml * Additional tests and fixes. * Additional unit tests and fixes. * Additional tests Co-authored-by: Lee Parrish <[email protected]>
1 parent 2daa350 commit 8a0a153

File tree

20 files changed

+1933
-118
lines changed

20 files changed

+1933
-118
lines changed

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/ComponentRegistration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
1212
public class ComponentRegistration {
1313

14-
private static final ConcurrentHashMap<Class<?>, ComponentRegistration> COMPONENTS =
15-
new ConcurrentHashMap<Class<?>, ComponentRegistration>();
14+
private static final ConcurrentHashMap<Class<?>, Object> COMPONENTS =
15+
new ConcurrentHashMap<Class<?>, Object>();
1616

1717
/**
1818
* Add a component which implements registration methods.
@@ -28,7 +28,7 @@ public static void add(ComponentRegistration componentRegistration) {
2828
*
2929
* @return A array of ComponentRegistration objects.
3030
*/
31-
public static Iterable<ComponentRegistration> getComponents() {
31+
public static Iterable<Object> getComponents() {
3232
return COMPONENTS.values();
3333
}
3434
}

libraries/bot-builder/src/test/java/com/microsoft/bot/builder/ChannelServiceHandlerTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,26 @@ public class ChannelServiceHandlerTests {
2222
@Test
2323
public void AuthenticateSetsAnonymousSkillClaim() {
2424
TestChannelServiceHandler sut = new TestChannelServiceHandler();
25-
sut.handleReplyToActivity(null, "123", "456", new Activity(ActivityTypes.MESSAGE));
25+
sut.handleReplyToActivity(null, "123", "456", new Activity(ActivityTypes.MESSAGE));
2626

2727
Assert.assertEquals(AuthenticationConstants.ANONYMOUS_AUTH_TYPE,
2828
sut.getClaimsIdentity().getType());
2929
Assert.assertEquals(AuthenticationConstants.ANONYMOUS_SKILL_APPID,
3030
JwtTokenValidation.getAppIdFromClaims(sut.getClaimsIdentity().claims()));
3131
}
3232

33+
@Test
34+
public void testHandleSendToConversation() {
35+
TestChannelServiceHandler sut = new TestChannelServiceHandler();
36+
sut.handleSendToConversation(null, "456", new Activity(ActivityTypes.MESSAGE));
37+
38+
Assert.assertEquals(AuthenticationConstants.ANONYMOUS_AUTH_TYPE,
39+
sut.getClaimsIdentity().getType());
40+
Assert.assertEquals(AuthenticationConstants.ANONYMOUS_SKILL_APPID,
41+
JwtTokenValidation.getAppIdFromClaims(sut.getClaimsIdentity().claims()));
42+
}
43+
44+
3345
/**
3446
* A {@link ChannelServiceHandler} with overrides for testings.
3547
*/
@@ -50,6 +62,17 @@ protected CompletableFuture<ResourceResponse> onReplyToActivity(
5062
this.claimsIdentity = claimsIdentity;
5163
return CompletableFuture.completedFuture(new ResourceResponse());
5264
}
65+
66+
@Override
67+
protected CompletableFuture<ResourceResponse> onSendToConversation(
68+
ClaimsIdentity claimsIdentity,
69+
String activityId,
70+
Activity activity
71+
) {
72+
this.claimsIdentity = claimsIdentity;
73+
return CompletableFuture.completedFuture(new ResourceResponse());
74+
}
75+
5376
/**
5477
* Gets the {@link ClaimsIdentity} sent to the different methods after
5578
* auth is done.

libraries/bot-builder/src/test/java/com/microsoft/bot/builder/adapters/TestAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ public TestAdapter(ConversationReference reference) {
139139
setConversationReference(conversationReference);
140140
}
141141
}
142+
public TestAdapter(ConversationReference reference, boolean sendTraceActivity) {
143+
this(reference);
144+
this.sendTraceActivity = sendTraceActivity;
145+
}
142146

143147
public Queue<Activity> activeQueue() {
144148
return botReplies;

libraries/bot-builder/src/test/java/com/microsoft/bot/builder/adapters/TestFlow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public TestFlow send(String userSays) throws IllegalArgumentException {
7676
* Creates a conversation update activity and process it the activity.
7777
* @return A new TestFlow Object
7878
*/
79-
public TestFlow sendConverationUpdate() {
79+
public TestFlow sendConversationUpdate() {
8080
return new TestFlow(testTask.thenCompose(result -> {
8181
Activity cu = Activity.createConversationUpdateActivity();
8282
cu.getMembersAdded().add(this.adapter.conversationReference().getUser());

libraries/bot-dialogs/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@
101101
<artifactId>snakeyaml</artifactId>
102102
<version>1.27</version>
103103
</dependency>
104+
<dependency>
105+
<groupId>org.mockito</groupId>
106+
<artifactId>mockito-core</artifactId>
107+
<scope>test</scope>
108+
</dependency>
104109
</dependencies>
105110

106111
<profiles>
@@ -157,7 +162,6 @@
157162
</plugin>
158163
</plugins>
159164
</reporting>
160-
161165
<build>
162166
<plugins>
163167
<plugin>

libraries/bot-dialogs/src/main/java/com/microsoft/bot/dialogs/ObjectPath.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
99
import com.fasterxml.jackson.databind.node.NullNode;
1010
import com.fasterxml.jackson.databind.node.ObjectNode;
11+
import com.microsoft.bot.builder.TurnContextStateCollection;
1112
import com.microsoft.bot.schema.Serialization;
1213
import java.lang.reflect.Array;
1314
import java.lang.reflect.Field;
@@ -599,6 +600,8 @@ private static Object resolveSegment(Object current, Object segment) {
599600

600601
if (current instanceof List) {
601602
current = ((List<Object>) current).get(index);
603+
} else if (current instanceof ArrayNode) {
604+
current = ((ArrayNode) current).get(index);
602605
} else {
603606
current = Array.get(current, index);
604607
}
@@ -633,6 +636,22 @@ private static Object getObjectProperty(Object obj, String property) {
633636
return null;
634637
}
635638

639+
// Because TurnContextStateCollection is not implemented as a Map<String, Object> we need to
640+
// set obj to the Map<String, Object> which holds the state values which is retrieved from calling
641+
// getTurnStateServices()
642+
if (obj instanceof TurnContextStateCollection) {
643+
Map<String, Object> dict = ((TurnContextStateCollection) obj).getTurnStateServices();
644+
List<Entry<String, Object>> matches = dict.entrySet().stream()
645+
.filter(key -> key.getKey().equalsIgnoreCase(property))
646+
.collect(Collectors.toList());
647+
648+
if (matches.size() > 0) {
649+
return matches.get(0).getValue();
650+
}
651+
652+
return null;
653+
}
654+
636655
if (obj instanceof Map) {
637656
Map<String, Object> dict = (Map<String, Object>) obj;
638657
List<Entry<String, Object>> matches = dict.entrySet().stream()
@@ -652,7 +671,7 @@ private static Object getObjectProperty(Object obj, String property) {
652671
while (fields.hasNext()) {
653672
String field = fields.next();
654673
if (field.equalsIgnoreCase(property)) {
655-
return node.findValue(property);
674+
return node.findValue(field);
656675
}
657676
}
658677
return null;

libraries/bot-dialogs/src/main/java/com/microsoft/bot/dialogs/memory/DialogStateManager.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
import java.util.AbstractMap;
77
import java.util.ArrayList;
8+
import java.util.Arrays;
89
import java.util.Collection;
910
import java.util.HashSet;
1011
import java.util.Iterator;
1112
import java.util.List;
1213
import java.util.Locale;
1314
import java.util.Map;
1415
import java.util.Objects;
16+
import java.util.Optional;
1517
import java.util.Set;
1618
import java.util.AbstractMap.SimpleEntry;
1719
import java.util.concurrent.CompletableFuture;
@@ -91,7 +93,17 @@ public DialogStateManager(DialogContext dc, DialogStateManagerConfiguration conf
9193
if (this.configuration == null) {
9294
this.configuration = new DialogStateManagerConfiguration();
9395

94-
Iterable<ComponentRegistration> components = ComponentRegistration.getComponents();
96+
Map<String, Object> turnStateServices = dc.getContext().getTurnState().getTurnStateServices();
97+
for (Map.Entry<String, Object> entry : turnStateServices.entrySet()) {
98+
if (entry.getValue() instanceof MemoryScope[]) {
99+
this.configuration.getMemoryScopes().addAll(Arrays.asList((MemoryScope[]) entry.getValue()));
100+
}
101+
if (entry.getValue() instanceof PathResolver[]) {
102+
this.configuration.getPathResolvers().addAll(Arrays.asList((PathResolver[]) entry.getValue()));
103+
}
104+
}
105+
106+
Iterable<Object> components = ComponentRegistration.getComponents();
95107

96108
components.forEach((component) -> {
97109
if (component instanceof ComponentMemoryScopes) {
@@ -166,7 +178,7 @@ public void setElement(String key, Object element) {
166178
e.printStackTrace();
167179
}
168180
} else {
169-
throw new IllegalArgumentException();
181+
throw new IllegalArgumentException(getBadScopeMessage(key));
170182
}
171183
}
172184
}
@@ -181,8 +193,10 @@ public MemoryScope getMemoryScope(String name) {
181193
if (name == null) {
182194
throw new IllegalArgumentException("name cannot be null.");
183195
}
184-
return configuration.getMemoryScopes().stream().filter((scope) -> scope.getName().equalsIgnoreCase(name))
185-
.findFirst().get();
196+
Optional<MemoryScope> result = configuration.getMemoryScopes().stream()
197+
.filter((scope) -> scope.getName().equalsIgnoreCase(name))
198+
.findFirst();
199+
return result.isPresent() ? result.get() : null;
186200
}
187201

188202
/**
@@ -638,12 +652,13 @@ public Iterable<SimpleEntry<String, Object>> getEnumerator() {
638652
*/
639653
public List<String> trackPaths(Iterable<String> paths) {
640654
List<String> allPaths = new ArrayList<String>();
641-
for (String path : allPaths) {
655+
for (String path : paths) {
642656
String tpath = transformPath(path);
643657
// Track any path that resolves to a constant path
644-
Object resolved = ObjectPath.tryResolvePath(this, tpath);
658+
ArrayList<Object> resolved = ObjectPath.tryResolvePath(this, tpath);
659+
String[] segments = resolved.toArray(new String[resolved.size()]);
645660
if (resolved != null) {
646-
String npath = String.join("_", resolved.toString());
661+
String npath = String.join("_", segments);
647662
setValue(pathTracker + "." + npath, 0);
648663
allPaths.add(npath);
649664
}
@@ -721,7 +736,7 @@ private Boolean trackChange(String path, Object value) {
721736
// Convert to a simple path with _ between segments
722737
String pathName = String.join("_", stringSegments);
723738
String trackedPath = String.format("%s.%s", pathTracker, pathName);
724-
Integer counter = getValue(DialogPath.EVENTCOUNTER, 0, Integer.class);
739+
Integer counter = null;
725740
/**
726741
*
727742
*/
@@ -777,7 +792,6 @@ private void checkChildren(String property, Object instance, String path, Intege
777792
checkChildren(field, node.findValue(field), trackedPath, counter);
778793
}
779794
}
780-
781795
}
782796

783797
@Override
@@ -807,12 +821,13 @@ public final Object get(Object key) {
807821

808822
@Override
809823
public final Object put(String key, Object value) {
810-
return null;
824+
setElement(key, value);
825+
return value;
811826
}
812827

813828
@Override
814829
public final Object remove(Object key) {
815-
return null;
830+
throw new UnsupportedOperationException();
816831
}
817832

818833
@Override

libraries/bot-dialogs/src/main/java/com/microsoft/bot/dialogs/memory/scopes/DialogMemoryScope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final Object getMemory(DialogContext dialogContext) {
4242
return dialogContext.getParent().getActiveDialog().getState();
4343
}
4444
} else if (dialogContext.getActiveDialog() != null) {
45-
return dialogContext.getActiveDialog().getStackIndex();
45+
return dialogContext.getActiveDialog().getState();
4646
}
4747
return null;
4848
}

0 commit comments

Comments
 (0)