forked from mendixlabs/mxcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_javaactions_mock_test.go
More file actions
143 lines (118 loc) · 4.23 KB
/
cmd_javaactions_mock_test.go
File metadata and controls
143 lines (118 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: Apache-2.0
package executor
import (
"fmt"
"testing"
"github.com/mendixlabs/mxcli/mdl/ast"
"github.com/mendixlabs/mxcli/mdl/backend/mock"
"github.com/mendixlabs/mxcli/mdl/types"
"github.com/mendixlabs/mxcli/model"
"github.com/mendixlabs/mxcli/sdk/javaactions"
)
func TestShowJavaActions_Mock(t *testing.T) {
mod := mkModule("MyModule")
ja := &types.JavaAction{
BaseElement: model.BaseElement{ID: nextID("ja")},
ContainerID: mod.ID,
Name: "DoSomething",
}
h := mkHierarchy(mod)
withContainer(h, ja.ContainerID, mod.ID)
mb := &mock.MockBackend{
IsConnectedFunc: func() bool { return true },
ListJavaActionsFunc: func() ([]*types.JavaAction, error) { return []*types.JavaAction{ja}, nil },
}
ctx, buf := newMockCtx(t, withBackend(mb), withHierarchy(h))
assertNoError(t, listJavaActions(ctx, ""))
out := buf.String()
assertContainsStr(t, out, "Qualified Name")
assertContainsStr(t, out, "MyModule.DoSomething")
}
func TestDescribeJavaAction_Mock(t *testing.T) {
mod := mkModule("MyModule")
mb := &mock.MockBackend{
IsConnectedFunc: func() bool { return true },
ReadJavaActionByNameFunc: func(qn string) (*javaactions.JavaAction, error) {
return &javaactions.JavaAction{
BaseElement: model.BaseElement{ID: nextID("ja")},
ContainerID: mod.ID,
Name: "DoSomething",
}, nil
},
}
ctx, buf := newMockCtx(t, withBackend(mb))
assertNoError(t, describeJavaAction(ctx, ast.QualifiedName{Module: "MyModule", Name: "DoSomething"}))
out := buf.String()
assertContainsStr(t, out, "create java action")
}
// NOTE: listJavaActions has no explicit not-connected guard. It calls
// getHierarchy (which returns nil when disconnected) and is intended to
// be reached through execShow, which enforces a connected backend first.
// A nil hierarchy is only harmless when the backend returns no Java
// actions; if Java actions are returned while disconnected, dereferencing
// the nil hierarchy would panic.
func TestShowJavaActions_BackendError(t *testing.T) {
mod := mkModule("MyModule")
h := mkHierarchy(mod)
mb := &mock.MockBackend{
IsConnectedFunc: func() bool { return true },
ListJavaActionsFunc: func() ([]*types.JavaAction, error) {
return nil, fmt.Errorf("backend unavailable")
},
}
ctx, _ := newMockCtx(t, withBackend(mb), withHierarchy(h))
assertError(t, listJavaActions(ctx, ""))
}
func TestDescribeJavaAction_NotFound(t *testing.T) {
mb := &mock.MockBackend{
IsConnectedFunc: func() bool { return true },
ReadJavaActionByNameFunc: func(qn string) (*javaactions.JavaAction, error) {
return nil, fmt.Errorf("not found")
},
}
ctx, _ := newMockCtx(t, withBackend(mb))
assertError(t, describeJavaAction(ctx, ast.QualifiedName{Module: "MyModule", Name: "Missing"}))
}
func TestShowJavaActions_FilterByModule(t *testing.T) {
mod1 := mkModule("Alpha")
mod2 := mkModule("Beta")
ja1 := &types.JavaAction{
BaseElement: model.BaseElement{ID: nextID("ja")},
ContainerID: mod1.ID,
Name: "ActionA",
}
ja2 := &types.JavaAction{
BaseElement: model.BaseElement{ID: nextID("ja")},
ContainerID: mod2.ID,
Name: "ActionB",
}
h := mkHierarchy(mod1, mod2)
withContainer(h, ja1.ContainerID, mod1.ID)
withContainer(h, ja2.ContainerID, mod2.ID)
mb := &mock.MockBackend{
IsConnectedFunc: func() bool { return true },
ListJavaActionsFunc: func() ([]*types.JavaAction, error) { return []*types.JavaAction{ja1, ja2}, nil },
}
ctx, buf := newMockCtx(t, withBackend(mb), withHierarchy(h))
assertNoError(t, listJavaActions(ctx, "Alpha"))
out := buf.String()
assertContainsStr(t, out, "Alpha.ActionA")
assertNotContainsStr(t, out, "Beta.ActionB")
}
func TestShowJavaActions_JSON(t *testing.T) {
mod := mkModule("MyModule")
ja := &types.JavaAction{
BaseElement: model.BaseElement{ID: nextID("ja")},
ContainerID: mod.ID,
Name: "DoSomething",
}
h := mkHierarchy(mod)
withContainer(h, ja.ContainerID, mod.ID)
mb := &mock.MockBackend{
IsConnectedFunc: func() bool { return true },
ListJavaActionsFunc: func() ([]*types.JavaAction, error) { return []*types.JavaAction{ja}, nil },
}
ctx, buf := newMockCtx(t, withBackend(mb), withHierarchy(h), withFormat(FormatJSON))
assertNoError(t, listJavaActions(ctx, ""))
assertValidJSON(t, buf.String())
}