Skip to content

Commit 0e7b228

Browse files
committed
fixes #950 story context not loaded: story builder regression
Signed-off-by: Julien Buret <jburet@voyages-sncf.com>
1 parent 867a477 commit 0e7b228

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

bot/engine/src/main/kotlin/definition/BotDefinition.kt

+5
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ interface BotDefinition : I18nKeyProvider {
130130
}
131131
}
132132

133+
/**
134+
* Search story by storyId.
135+
*/
136+
fun findStoryDefinitionById(storyId: String): StoryDefinition = stories.find { it.id == storyId } ?: unknownStory
137+
133138
/**
134139
* Finds a [StoryDefinition] from an intent name.
135140
*

bot/engine/src/main/kotlin/engine/ConnectorController.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,7 @@ interface ConnectorController {
117117
/**
118118
* Return a story definition provider for this controller.
119119
*/
120-
fun storyDefinitionLoader(): (String) -> StoryDefinition = { botDefinition.findStoryDefinition(it) }
120+
fun storyDefinitionLoader(): (String) -> StoryDefinition = {
121+
botDefinition.findStoryDefinitionById(it)
122+
}
121123
}

bot/engine/src/main/kotlin/engine/config/BotDefinitionWrapper.kt

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ internal class BotDefinitionWrapper(val botDefinition: BotDefinition) : BotDefin
4040
@Volatile
4141
private var configuredStories: Map<String, List<ConfiguredStoryDefinition>> = emptyMap()
4242

43+
@Volatile
44+
private var allStoriesById: Map<String, StoryDefinition> = botDefinition.stories.associateBy { it.id }
45+
4346
//all stories
4447
@Volatile
4548
private var allStories: List<StoryDefinition> = botDefinition.stories
@@ -80,6 +83,8 @@ internal class BotDefinitionWrapper(val botDefinition: BotDefinition) : BotDefin
8083
.groupBy { it.id }
8184
)
8285
.values.flatten()
86+
87+
this.allStoriesById = allStories.associateBy { it.id }
8388
}
8489

8590
override val stories: List<StoryDefinition>
@@ -160,6 +165,9 @@ internal class BotDefinitionWrapper(val botDefinition: BotDefinition) : BotDefin
160165
}
161166
}
162167

168+
override fun findStoryDefinitionById(storyId: String): StoryDefinition =
169+
allStoriesById[storyId] ?: builtInStoriesMap[storyId] ?: findStoryDefinition(storyId)
170+
163171
override fun toString(): String {
164172
return "Wrapper($botDefinition)"
165173
}

bot/engine/src/test/kotlin/definition/BotDefinitionTest.kt

+16-26
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import java.util.Locale
3434
import kotlin.test.assertEquals
3535
import kotlin.test.assertFalse
3636
import kotlin.test.assertNotNull
37-
import kotlin.test.assertNull
3837
import kotlin.test.assertTrue
3938

4039
/**
@@ -44,12 +43,6 @@ class BotDefinitionTest {
4443

4544
private val botDef = BotDefinitionTest()
4645

47-
/**
48-
* Finds a [StoryDefinition] from a story [id]
49-
*/
50-
private fun BotDefinition.findStoryDefinitionById(id: String): StoryDefinition? =
51-
stories.find { it.id == id || (it as? ConfiguredStoryDefinition)?.configuration?.storyId == id }
52-
5346
@Test
5447
fun `i18nTranslator() returns an I18nTranslator that use BotDefinition#i18nKeyFromLabel`() {
5548
val result = botDef.i18nTranslator(Locale.ENGLISH, ConnectorType.none).i18n("test")
@@ -82,27 +75,24 @@ class BotDefinitionTest {
8275
@Test
8376
fun `GIVEN stories containing configured story with specific id WHEN findStoryDefinitionById THEN story is found`() {
8477
// Given
85-
val storyConfiguration = ConfiguredStoryDefinition(
86-
BotDefinitionWrapper(botDef),
87-
StoryDefinitionConfiguration(
88-
botDefinition = botDef,
89-
storyDefinition = SimpleStoryDefinition(
90-
id = "disable_bot",
91-
storyHandler = StoryHandlerTest,
92-
starterIntents = setOf(Intent("starter_intent"))
93-
),
94-
configurationName = "toto"
78+
val wrap = BotDefinitionWrapper(botDef)
79+
80+
wrap.updateStories(
81+
listOf(
82+
StoryDefinitionConfiguration(
83+
botDefinition = botDef,
84+
storyDefinition = SimpleStoryDefinition(
85+
id = "disable_bot",
86+
storyHandler = StoryHandlerTest,
87+
starterIntents = setOf(Intent("starter_intent"))
88+
),
89+
configurationName = "toto"
90+
)
9591
)
9692
)
9793

98-
val botDef = BotDefinitionBase(
99-
botId = "test",
100-
namespace = "namespace",
101-
stories = listOf(storyConfiguration)
102-
)
103-
10494
// When
105-
val result = botDef.findStoryDefinitionById("disable_bot")
95+
val result = wrap.findStoryDefinitionById("disable_bot")
10696

10797
// Then
10898
assertNotNull(result)
@@ -143,7 +133,7 @@ class BotDefinitionTest {
143133
val result = botDef.findStoryDefinitionById("disable_bot")
144134

145135
// Then
146-
assertNull(result)
136+
assertEquals(botDef.unknownStory, result)
147137
}
148138

149139
@Test
@@ -178,7 +168,7 @@ class BotDefinitionTest {
178168
val result = botDef.findStoryDefinitionById("should_not_find_id")
179169

180170
// Then
181-
assertNull(result)
171+
assertEquals(botDef.unknownStory, result)
182172
}
183173

184174
@Test

0 commit comments

Comments
 (0)