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

Commit

Permalink
Add additional unit test coverage (#1185)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LeeParrishMSFT authored and tracyboehrer committed Apr 26, 2021
1 parent 2daa350 commit b476ff6
Show file tree
Hide file tree
Showing 20 changed files with 1,933 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
public class ComponentRegistration {

private static final ConcurrentHashMap<Class<?>, ComponentRegistration> COMPONENTS =
new ConcurrentHashMap<Class<?>, ComponentRegistration>();
private static final ConcurrentHashMap<Class<?>, Object> COMPONENTS =
new ConcurrentHashMap<Class<?>, Object>();

/**
* Add a component which implements registration methods.
Expand All @@ -28,7 +28,7 @@ public static void add(ComponentRegistration componentRegistration) {
*
* @return A array of ComponentRegistration objects.
*/
public static Iterable<ComponentRegistration> getComponents() {
public static Iterable<Object> getComponents() {
return COMPONENTS.values();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,26 @@ public class ChannelServiceHandlerTests {
@Test
public void AuthenticateSetsAnonymousSkillClaim() {
TestChannelServiceHandler sut = new TestChannelServiceHandler();
sut.handleReplyToActivity(null, "123", "456", new Activity(ActivityTypes.MESSAGE));
sut.handleReplyToActivity(null, "123", "456", new Activity(ActivityTypes.MESSAGE));

Assert.assertEquals(AuthenticationConstants.ANONYMOUS_AUTH_TYPE,
sut.getClaimsIdentity().getType());
Assert.assertEquals(AuthenticationConstants.ANONYMOUS_SKILL_APPID,
JwtTokenValidation.getAppIdFromClaims(sut.getClaimsIdentity().claims()));
}

@Test
public void testHandleSendToConversation() {
TestChannelServiceHandler sut = new TestChannelServiceHandler();
sut.handleSendToConversation(null, "456", new Activity(ActivityTypes.MESSAGE));

Assert.assertEquals(AuthenticationConstants.ANONYMOUS_AUTH_TYPE,
sut.getClaimsIdentity().getType());
Assert.assertEquals(AuthenticationConstants.ANONYMOUS_SKILL_APPID,
JwtTokenValidation.getAppIdFromClaims(sut.getClaimsIdentity().claims()));
}


/**
* A {@link ChannelServiceHandler} with overrides for testings.
*/
Expand All @@ -50,6 +62,17 @@ protected CompletableFuture<ResourceResponse> onReplyToActivity(
this.claimsIdentity = claimsIdentity;
return CompletableFuture.completedFuture(new ResourceResponse());
}

@Override
protected CompletableFuture<ResourceResponse> onSendToConversation(
ClaimsIdentity claimsIdentity,
String activityId,
Activity activity
) {
this.claimsIdentity = claimsIdentity;
return CompletableFuture.completedFuture(new ResourceResponse());
}

/**
* Gets the {@link ClaimsIdentity} sent to the different methods after
* auth is done.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public TestAdapter(ConversationReference reference) {
setConversationReference(conversationReference);
}
}
public TestAdapter(ConversationReference reference, boolean sendTraceActivity) {
this(reference);
this.sendTraceActivity = sendTraceActivity;
}

public Queue<Activity> activeQueue() {
return botReplies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public TestFlow send(String userSays) throws IllegalArgumentException {
* Creates a conversation update activity and process it the activity.
* @return A new TestFlow Object
*/
public TestFlow sendConverationUpdate() {
public TestFlow sendConversationUpdate() {
return new TestFlow(testTask.thenCompose(result -> {
Activity cu = Activity.createConversationUpdateActivity();
cu.getMembersAdded().add(this.adapter.conversationReference().getUser());
Expand Down
6 changes: 5 additions & 1 deletion libraries/bot-dialogs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
<artifactId>snakeyaml</artifactId>
<version>1.27</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down Expand Up @@ -157,7 +162,6 @@
</plugin>
</plugins>
</reporting>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.bot.builder.TurnContextStateCollection;
import com.microsoft.bot.schema.Serialization;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -599,6 +600,8 @@ private static Object resolveSegment(Object current, Object segment) {

if (current instanceof List) {
current = ((List<Object>) current).get(index);
} else if (current instanceof ArrayNode) {
current = ((ArrayNode) current).get(index);
} else {
current = Array.get(current, index);
}
Expand Down Expand Up @@ -633,6 +636,22 @@ private static Object getObjectProperty(Object obj, String property) {
return null;
}

// Because TurnContextStateCollection is not implemented as a Map<String, Object> we need to
// set obj to the Map<String, Object> which holds the state values which is retrieved from calling
// getTurnStateServices()
if (obj instanceof TurnContextStateCollection) {
Map<String, Object> dict = ((TurnContextStateCollection) obj).getTurnStateServices();
List<Entry<String, Object>> matches = dict.entrySet().stream()
.filter(key -> key.getKey().equalsIgnoreCase(property))
.collect(Collectors.toList());

if (matches.size() > 0) {
return matches.get(0).getValue();
}

return null;
}

if (obj instanceof Map) {
Map<String, Object> dict = (Map<String, Object>) obj;
List<Entry<String, Object>> matches = dict.entrySet().stream()
Expand All @@ -652,7 +671,7 @@ private static Object getObjectProperty(Object obj, String property) {
while (fields.hasNext()) {
String field = fields.next();
if (field.equalsIgnoreCase(property)) {
return node.findValue(property);
return node.findValue(field);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.AbstractMap.SimpleEntry;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -91,7 +93,17 @@ public DialogStateManager(DialogContext dc, DialogStateManagerConfiguration conf
if (this.configuration == null) {
this.configuration = new DialogStateManagerConfiguration();

Iterable<ComponentRegistration> components = ComponentRegistration.getComponents();
Map<String, Object> turnStateServices = dc.getContext().getTurnState().getTurnStateServices();
for (Map.Entry<String, Object> entry : turnStateServices.entrySet()) {
if (entry.getValue() instanceof MemoryScope[]) {
this.configuration.getMemoryScopes().addAll(Arrays.asList((MemoryScope[]) entry.getValue()));
}
if (entry.getValue() instanceof PathResolver[]) {
this.configuration.getPathResolvers().addAll(Arrays.asList((PathResolver[]) entry.getValue()));
}
}

Iterable<Object> components = ComponentRegistration.getComponents();

components.forEach((component) -> {
if (component instanceof ComponentMemoryScopes) {
Expand Down Expand Up @@ -166,7 +178,7 @@ public void setElement(String key, Object element) {
e.printStackTrace();
}
} else {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getBadScopeMessage(key));
}
}
}
Expand All @@ -181,8 +193,10 @@ public MemoryScope getMemoryScope(String name) {
if (name == null) {
throw new IllegalArgumentException("name cannot be null.");
}
return configuration.getMemoryScopes().stream().filter((scope) -> scope.getName().equalsIgnoreCase(name))
.findFirst().get();
Optional<MemoryScope> result = configuration.getMemoryScopes().stream()
.filter((scope) -> scope.getName().equalsIgnoreCase(name))
.findFirst();
return result.isPresent() ? result.get() : null;
}

/**
Expand Down Expand Up @@ -638,12 +652,13 @@ public Iterable<SimpleEntry<String, Object>> getEnumerator() {
*/
public List<String> trackPaths(Iterable<String> paths) {
List<String> allPaths = new ArrayList<String>();
for (String path : allPaths) {
for (String path : paths) {
String tpath = transformPath(path);
// Track any path that resolves to a constant path
Object resolved = ObjectPath.tryResolvePath(this, tpath);
ArrayList<Object> resolved = ObjectPath.tryResolvePath(this, tpath);
String[] segments = resolved.toArray(new String[resolved.size()]);
if (resolved != null) {
String npath = String.join("_", resolved.toString());
String npath = String.join("_", segments);
setValue(pathTracker + "." + npath, 0);
allPaths.add(npath);
}
Expand Down Expand Up @@ -721,7 +736,7 @@ private Boolean trackChange(String path, Object value) {
// Convert to a simple path with _ between segments
String pathName = String.join("_", stringSegments);
String trackedPath = String.format("%s.%s", pathTracker, pathName);
Integer counter = getValue(DialogPath.EVENTCOUNTER, 0, Integer.class);
Integer counter = null;
/**
*
*/
Expand Down Expand Up @@ -777,7 +792,6 @@ private void checkChildren(String property, Object instance, String path, Intege
checkChildren(field, node.findValue(field), trackedPath, counter);
}
}

}

@Override
Expand Down Expand Up @@ -807,12 +821,13 @@ public final Object get(Object key) {

@Override
public final Object put(String key, Object value) {
return null;
setElement(key, value);
return value;
}

@Override
public final Object remove(Object key) {
return null;
throw new UnsupportedOperationException();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final Object getMemory(DialogContext dialogContext) {
return dialogContext.getParent().getActiveDialog().getState();
}
} else if (dialogContext.getActiveDialog() != null) {
return dialogContext.getActiveDialog().getStackIndex();
return dialogContext.getActiveDialog().getState();
}
return null;
}
Expand Down
Loading

0 comments on commit b476ff6

Please sign in to comment.