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

Commit

Permalink
fixup! fixup! Add unified broker tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Garagatyi committed Mar 22, 2019
1 parent c7c92f0 commit e3b76bb
Showing 1 changed file with 240 additions and 67 deletions.
307 changes: 240 additions & 67 deletions brokers/unified/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
"github.com/stretchr/testify/mock"
)

const TestChePluginType = "che plugin"
const TestEditorPluginType = "che editor"
const TestTheiaPluginType = "theia plugin"
const TestVscodePluginType = "vs code extension"
const TestChePluginType = "Che Plugin"
const TestEditorPluginType = "Che Editor"
const TestTheiaPluginType = "Theia plugin"
const TestVscodePluginType = "VS Code extension"

type mocks struct {
cb *cmock.Broker
Expand All @@ -42,14 +42,16 @@ type mocks struct {
cheBroker *cheBrokerMocks.ChePluginBroker
}

func initMocks() *mocks {
func createMocks() *mocks {
cb := &cmock.Broker{}
u := &fmock.IoUtil{}
randMock := &cmock.Random{}
theiaBroker := &theiaBrokerMocks.Broker{}
vscodeBroker := &vscodeBrokerMocks.Broker{}
cheBroker := &cheBrokerMocks.ChePluginBroker{}

cb.On("PrintInfo", mock.AnythingOfType("string"))

return &mocks{
cb: cb,
u: u,
Expand All @@ -68,109 +70,280 @@ func initMocks() *mocks {
}
}

func setUpMocks(m *mocks) {
m.cb.On("PubStarted").Once()
m.cb.On("PrintDebug", mock.AnythingOfType("string"))
m.cb.On("PrintInfo", mock.AnythingOfType("string"))
m.cb.On("PrintPlan", mock.AnythingOfType("[]model.PluginMeta")).Once()
}

func createVSCodeMeta() model.PluginMeta {
return model.PluginMeta{
Type: TestVscodePluginType,
}
}

func createTheiaMeta() model.PluginMeta {
return model.PluginMeta{
Type: TestTheiaPluginType,
}
}

func createCheMeta() model.PluginMeta {
return model.PluginMeta{
Type: TestChePluginType,
}
}

func TestBroker_processPlugins(t *testing.T) {
type args struct {
metas []model.PluginMeta
}
tests := []struct {
name string
args args
vsCodeError error
theiaError error
cheBrokerError error
wantErr string
type want struct {
err string
theiaMetas []model.PluginMeta
vscodeMetas []model.PluginMeta
cheBrokerMetas []model.PluginMeta
}
type mocks struct {
vsCodeError error
theiaError error
cheBrokerError error
}
tests := []struct {
name string
args args
mocks mocks
want want
}{
{
name: "Sends error on VS Code broker error",
vsCodeError: errors.New("test vscode error"),
wantErr: "test vscode error",
name: "Sends error on VS Code broker error",
mocks: mocks{
vsCodeError: errors.New("test vscode error"),
},
args: args{
metas: []model.PluginMeta{createVSCodeMeta()},
metas: []model.PluginMeta{createDefaultTheiaMeta(), createDefaultVSCodeMeta(), createDefaultChePluginMeta()},
},
want: want{
err: "test vscode error",
},
},
{
name: "Returns error on Theia broker error",
theiaError: errors.New("test theia error"),
wantErr: "test theia error",
name: "Returns error on Theia broker error",
mocks: mocks{
theiaError: errors.New("test theia error"),
},
args: args{
metas: []model.PluginMeta{createTheiaMeta()},
metas: []model.PluginMeta{createDefaultVSCodeMeta(), createDefaultTheiaMeta(), createDefaultChePluginMeta()},
},
want: want{
err: "test theia error",
},
},
{
name: "Returns error on Che plugin broker error",
theiaError: errors.New("test che plugin broker error"),
wantErr: "test che plugin broker error",
name: "Returns error on Che plugin broker error",
mocks: mocks{
theiaError: errors.New("test che plugin broker error"),
},
args: args{
metas: []model.PluginMeta{createCheMeta()},
metas: []model.PluginMeta{createDefaultVSCodeMeta(), createDefaultChePluginMeta(), createDefaultTheiaMeta()},
},
want: want{
err: "test che plugin broker error",
},
},
{
name: "Sorts metas by type",
//mocks: mocks{},
args: args{
metas: []model.PluginMeta{
createVSCodeMeta("id1"),
createChePluginMeta("id2"),
createVSCodeMeta("id4"),
createTheiaMeta("id3"),
createTheiaMeta("id5"),
createChePluginMeta("id6"),
},
},
want: want{
cheBrokerMetas: []model.PluginMeta{createChePluginMeta("id2"), createChePluginMeta("id6")},
vscodeMetas: []model.PluginMeta{createVSCodeMeta("id1"), createVSCodeMeta("id4")},
theiaMetas: []model.PluginMeta{createTheiaMeta("id3"), createTheiaMeta("id5"),},
},
},
{
name: "Processes metas of type Che Editor with those of type Che Plugin",
name: "Processes metas of type Che Editor with those of type Che Plugin",
mocks: mocks{},
args: args{
metas: []model.PluginMeta{createChePluginMeta("id1"), createCheEditorMeta("id2")},
},
want: want{
cheBrokerMetas: []model.PluginMeta{createChePluginMeta("id1"), createCheEditorMeta("id2")},
},
},
{
name: "Meta type checking is case insensitive",
args: args{
metas: []model.PluginMeta{
{
Type: "che plugin",
ID: "id11",
},
{
Type: "Che Plugin",
ID: "id12",
},
{
Type: "cHE plugIN",
ID: "id13",
},
{
Type: "vs code extension",
ID: "id21",
},
{
Type: "VS CODE EXTENSION",
ID: "id22",
},
{
Type: "vs cODE EXTENSION",
ID: "id23",
},
{
Type: "theia plugin",
ID: "id31",
},
{
Type: "Theia Plugin",
ID: "id32",
},
{
Type: "THEIA PLUGIN",
ID: "id33",
},
},
},
want: want{
theiaMetas: []model.PluginMeta{
{
Type: "theia plugin",
ID: "id31",
},
{
Type: "Theia Plugin",
ID: "id32",
},
{
Type: "THEIA PLUGIN",
ID: "id33",
},
},
vscodeMetas: []model.PluginMeta{
{
Type: "vs code extension",
ID: "id21",
},
{
Type: "VS CODE EXTENSION",
ID: "id22",
},
{
Type: "vs cODE EXTENSION",
ID: "id23",
},
},
cheBrokerMetas: []model.PluginMeta{
{
Type: "che plugin",
ID: "id11",
},
{
Type: "Che Plugin",
ID: "id12",
},
{
Type: "cHE plugIN",
ID: "id13",
},
},
},
},
{
name: "Returns error when type not supported",
args: args{
metas: []model.PluginMeta{
{
Type: "Unsupported type",
ID: "test id",
Version: "test version",
},
},
},
want: want{
err: "Type 'Unsupported type' of plugin 'test id:test version' is unsupported",
},
},
{
name: "Returns error when type is empty",
args: args{
metas: []model.PluginMeta{
{
Type: "",
ID: "test id",
Version: "test version",
},
},
},
want: want{
err: "Type field is missing in meta information of plugin 'test id:test version'",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := initMocks()
setUpMocks(m)
if tt.wantErr == "" {
m.cb.On("PubDone", "null").Once()
}
m := createMocks()

m.cheBroker.On("ProcessPlugin", mock.AnythingOfType("model.PluginMeta")).Return(tt.cheBrokerError)
m.theiaBroker.On("ProcessPlugin", mock.AnythingOfType("model.PluginMeta")).Return(tt.theiaError)
m.vscodeBroker.On("ProcessPlugin", mock.AnythingOfType("model.PluginMeta")).Return(tt.vsCodeError)
m.cheBroker.On("ProcessPlugin", mock.AnythingOfType("model.PluginMeta")).Return(tt.mocks.cheBrokerError)
m.theiaBroker.On("ProcessPlugin", mock.AnythingOfType("model.PluginMeta")).Return(tt.mocks.theiaError)
m.vscodeBroker.On("ProcessPlugin", mock.AnythingOfType("model.PluginMeta")).Return(tt.mocks.vsCodeError)

err := m.b.processPlugins(tt.args.metas)
err := m.b.ProcessPlugins(tt.args.metas)

if err != nil || tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
if err != nil || tt.want.err != "" {
assert.EqualError(t, err, tt.want.err)
} else if tt.want.cheBrokerMetas == nil && tt.want.theiaMetas == nil && tt.want.vscodeMetas == nil {
assert.Fail(t, "Neither expected error nor expected ProcessPlugin method arguments are set in test")
}
if tt.cheBrokerMetas != nil {
m.cheBroker.AssertCalled(t, "ProcessPlugin", )
if tt.want.cheBrokerMetas != nil {
for _, meta := range tt.want.cheBrokerMetas {
m.cheBroker.AssertCalled(t, "ProcessPlugin", meta)
}
}
if tt.theiaMetas != nil {
m.theiaBroker.AssertCalled(t, "ProcessPlugin", )
if tt.want.theiaMetas != nil {
for _, meta := range tt.want.theiaMetas {
m.theiaBroker.AssertCalled(t, "ProcessPlugin", meta)
}
}
if tt.vscodeMetas != nil {
m.vscodeBroker.AssertCalled(t, "ProcessPlugin", )
if tt.want.vscodeMetas != nil {
for _, meta := range tt.want.vscodeMetas {
m.vscodeBroker.AssertCalled(t, "ProcessPlugin", meta)
}
}
})
}
}

func createDefaultVSCodeMeta() model.PluginMeta {
return createVSCodeMeta("test ID")
}

func createDefaultTheiaMeta() model.PluginMeta {
return createTheiaMeta("test ID")
}

func createDefaultChePluginMeta() model.PluginMeta {
return createChePluginMeta("test ID")
}

func createVSCodeMeta(ID string) model.PluginMeta {
return model.PluginMeta{
Type: TestVscodePluginType,
ID: ID,
}
}

func createTheiaMeta(ID string) model.PluginMeta {
return model.PluginMeta{
Type: TestTheiaPluginType,
ID: ID,
}
}

func createChePluginMeta(ID string) model.PluginMeta {
return model.PluginMeta{
Type: TestChePluginType,
ID: ID,
}
}

func createCheEditorMeta(ID string) model.PluginMeta {
return model.PluginMeta{
Type: TestEditorPluginType,
ID: ID,
}
}

0 comments on commit e3b76bb

Please sign in to comment.