|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MT License. |
| 3 | + |
| 4 | +package com.microsoft.bot.builder; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 10 | +import com.fasterxml.jackson.databind.JsonNode; |
| 11 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 12 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 13 | +import com.microsoft.bot.builder.adapters.TestAdapter; |
| 14 | +import com.microsoft.bot.schema.Activity; |
| 15 | +import com.microsoft.bot.schema.ActivityTypes; |
| 16 | +import com.microsoft.bot.schema.ChannelAccount; |
| 17 | +import com.microsoft.bot.schema.ConversationAccount; |
| 18 | +import com.microsoft.bot.schema.HandoffEventNames; |
| 19 | +import com.microsoft.bot.schema.Serialization; |
| 20 | +import com.microsoft.bot.schema.Transcript; |
| 21 | + |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +public class EventFactoryTests { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void HandoffInitiationNullTurnContext() { |
| 29 | + Assert.assertThrows(IllegalArgumentException.class, |
| 30 | + () -> EventFactory.createHandoffInitiation(null, "some text")); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void HandoffStatusNullConversation() { |
| 35 | + Assert.assertThrows(IllegalArgumentException.class, () -> EventFactory.createHandoffStatus(null, "accepted")); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void HandoffStatusNullStatus() { |
| 40 | + Assert.assertThrows(IllegalArgumentException.class, |
| 41 | + () -> EventFactory.createHandoffStatus(new ConversationAccount(), null)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void TestCreateHandoffInitiation() { |
| 46 | + TestAdapter adapter = new TestAdapter( |
| 47 | + TestAdapter.createConversationReference("TestCreateHandoffInitiation", "User1", "Bot")); |
| 48 | + String fromD = "test"; |
| 49 | + Activity activity = new Activity(ActivityTypes.MESSAGE); |
| 50 | + activity.setText(""); |
| 51 | + activity.setConversation(new ConversationAccount()); |
| 52 | + activity.setRecipient(new ChannelAccount()); |
| 53 | + activity.setFrom(new ChannelAccount(fromD)); |
| 54 | + activity.setChannelId("testchannel"); |
| 55 | + activity.setServiceUrl("http://myservice"); |
| 56 | + TurnContext context = new TurnContextImpl(adapter, activity); |
| 57 | + List<Activity> activities = new ArrayList<Activity>(); |
| 58 | + activities.add(MessageFactory.text("hello")); |
| 59 | + Transcript transcript = new Transcript(); |
| 60 | + transcript.setActivities(activities); |
| 61 | + |
| 62 | + Assert.assertNull(transcript.getActivities().get(0).getChannelId()); |
| 63 | + Assert.assertNull(transcript.getActivities().get(0).getServiceUrl()); |
| 64 | + Assert.assertNull(transcript.getActivities().get(0).getConversation()); |
| 65 | + |
| 66 | + ObjectNode handoffContext = JsonNodeFactory.instance.objectNode(); |
| 67 | + handoffContext.set("Skill", JsonNodeFactory.instance.textNode("any")); |
| 68 | + |
| 69 | + Activity handoffEvent = EventFactory.createHandoffInitiation(context, handoffContext, transcript); |
| 70 | + Assert.assertEquals(handoffEvent.getName(), HandoffEventNames.INITIATEHANDOFF); |
| 71 | + ObjectNode node = (ObjectNode) handoffEvent.getValue(); |
| 72 | + String skill = node.get("Skill").asText(); |
| 73 | + Assert.assertEquals("any", skill); |
| 74 | + Assert.assertEquals(handoffEvent.getFrom().getId(), fromD); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void TestCreateHandoffInitiationNoTranscript() { |
| 79 | + TestAdapter adapter = new TestAdapter( |
| 80 | + TestAdapter.createConversationReference("TestCreateHandoffInitiation", "User1", "Bot")); |
| 81 | + String fromD = "test"; |
| 82 | + Activity activity = new Activity(ActivityTypes.MESSAGE); |
| 83 | + activity.setText(""); |
| 84 | + activity.setConversation(new ConversationAccount()); |
| 85 | + activity.setRecipient(new ChannelAccount()); |
| 86 | + activity.setFrom(new ChannelAccount(fromD)); |
| 87 | + activity.setChannelId("testchannel"); |
| 88 | + activity.setServiceUrl("http://myservice"); |
| 89 | + TurnContext context = new TurnContextImpl(adapter, activity); |
| 90 | + List<Activity> activities = new ArrayList<Activity>(); |
| 91 | + activities.add(MessageFactory.text("hello")); |
| 92 | + |
| 93 | + ObjectNode handoffContext = JsonNodeFactory.instance.objectNode(); |
| 94 | + handoffContext.set("Skill", JsonNodeFactory.instance.textNode("any")); |
| 95 | + |
| 96 | + Activity handoffEvent = EventFactory.createHandoffInitiation(context, handoffContext); |
| 97 | + Assert.assertEquals(handoffEvent.getName(), HandoffEventNames.INITIATEHANDOFF); |
| 98 | + ObjectNode node = (ObjectNode) handoffEvent.getValue(); |
| 99 | + String skill = node.get("Skill").asText(); |
| 100 | + Assert.assertEquals("any", skill); |
| 101 | + Assert.assertEquals(handoffEvent.getFrom().getId(), fromD); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void TestCreateHandoffStatus() throws JsonProcessingException { |
| 106 | + String state = "failed"; |
| 107 | + String message = "timed out"; |
| 108 | + Activity handoffEvent = EventFactory.createHandoffStatus(new ConversationAccount(), state, message); |
| 109 | + Assert.assertEquals(handoffEvent.getName(), HandoffEventNames.HANDOFFSTATUS); |
| 110 | + |
| 111 | + ObjectNode node = (ObjectNode) handoffEvent.getValue(); |
| 112 | + |
| 113 | + String stateFormEvent = node.get("state").asText(); |
| 114 | + Assert.assertEquals(stateFormEvent, state); |
| 115 | + |
| 116 | + String messageFormEvent = node.get("message").asText(); |
| 117 | + Assert.assertEquals(messageFormEvent, message); |
| 118 | + |
| 119 | + String status = Serialization.toString(node); |
| 120 | + Assert.assertEquals(status, String.format("{\"state\":\"%s\",\"message\":\"%s\"}", state, message)); |
| 121 | + Assert.assertNotNull(handoffEvent.getAttachments()); |
| 122 | + Assert.assertNotNull(handoffEvent.getId()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void TestCreateHandoffStatusNoMessage() { |
| 127 | + String state = "failed"; |
| 128 | + Activity handoffEvent = EventFactory.createHandoffStatus(new ConversationAccount(), state); |
| 129 | + |
| 130 | + ObjectNode node = (ObjectNode) handoffEvent.getValue(); |
| 131 | + |
| 132 | + String stateFormEvent = node.get("state").asText(); |
| 133 | + Assert.assertEquals(stateFormEvent, state); |
| 134 | + |
| 135 | + JsonNode messageFormEvent = node.get("message"); |
| 136 | + Assert.assertNull(messageFormEvent); |
| 137 | + } |
| 138 | +} |
0 commit comments