Skip to content

Commit 3e41009

Browse files
CROSSLINK-303 Add state model validation for closing action
1 parent 5b2989d commit 3e41009

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

broker/patron_request/service/statemodel.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ func ValidateStateModel(stateModel *proapi.StateModel) error {
184184
return fmt.Errorf("primary action %s undefined in state %s side %s", *state.PrimaryAction, state.Name, state.Side)
185185
}
186186

187+
if state.ClosingAction != nil && (state.Actions == nil || !slices.ContainsFunc(*state.Actions, func(a proapi.ModelAction) bool {
188+
return a.Name == string(*state.ClosingAction)
189+
})) {
190+
return fmt.Errorf("closing action %s undefined in state %s side %s", *state.ClosingAction, state.Name, state.Side)
191+
}
187192
if state.Events != nil {
188193
for _, event := range *state.Events {
189194
if !slices.Contains(allowedEvents, event.Name) {

broker/patron_request/service/statemodel_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,55 @@ func TestValidateStateModelPrimaryActionNoActionsDefined(t *testing.T) {
200200
assert.Equal(t, "primary action other undefined in state NEW side REQUESTER", err.Error())
201201
}
202202

203+
func TestValidateStateModelClosingActionUndefined(t *testing.T) {
204+
s := "ship"
205+
valid := "will-supply"
206+
tt := true
207+
model := &proapi.StateModel{
208+
Type: proapi.StateModelTypeStateModel,
209+
Name: "test",
210+
Version: "1.0.0",
211+
States: []proapi.ModelState{
212+
{
213+
Name: "VALIDATED",
214+
Side: proapi.SUPPLIER,
215+
Initial: &tt,
216+
Actions: &[]proapi.ModelAction{
217+
{Name: valid},
218+
},
219+
PrimaryAction: &valid,
220+
ClosingAction: &s,
221+
},
222+
},
223+
}
224+
225+
err := ValidateStateModel(model)
226+
assert.Error(t, err)
227+
assert.Equal(t, "closing action ship undefined in state VALIDATED side SUPPLIER", err.Error())
228+
}
229+
230+
func TestValidateStateModelClosingActionNoActionsDefined(t *testing.T) {
231+
s := "ship"
232+
tt := true
233+
model := &proapi.StateModel{
234+
Type: proapi.StateModelTypeStateModel,
235+
Name: "test",
236+
Version: "1.0.0",
237+
States: []proapi.ModelState{
238+
{
239+
Name: "VALIDATED",
240+
Side: proapi.SUPPLIER,
241+
Initial: &tt,
242+
ClosingAction: &s,
243+
},
244+
},
245+
}
246+
247+
err := ValidateStateModel(model)
248+
assert.Error(t, err)
249+
assert.Equal(t, "closing action ship undefined in state VALIDATED side SUPPLIER", err.Error())
250+
}
251+
203252
func TestValidateStateModelManualCloseTerminal(t *testing.T) {
204253
tt := true
205254
model := &proapi.StateModel{

0 commit comments

Comments
 (0)