import (
"errors"
"testing"
+ "reflect"
"gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
"gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
return a.Error(0)
}
-func (m *mockDB) MSetPub(ns, message string, pairs ...interface{}) error {
- a := m.Called(ns, message, pairs)
+func (m *mockDB) MSetMPub(channelsAndEvents []string, pairs ...interface{}) error {
+ a := m.Called(channelsAndEvents, pairs)
return a.Error(0)
}
return a.Error(0)
}
-func (m *mockDB) DelPub(channel, message string, keys []string) error {
- a := m.Called(channel, message, keys)
+func (m *mockDB) DelMPub(channelsAndEvents []string, keys []string) error {
+ a := m.Called(channelsAndEvents, keys)
return a.Error(0)
}
return m, i
}
+func verifySliceInOrder(a, b []string) bool {
+ for i, v := range a {
+ found := false
+ if i%2 == 0 {
+ for j, x := range b {
+ if j%2 == 0 && x == v && a[i+1] == b[j+1] {
+ found = true
+ break
+ }
+ }
+ if !found {
+ return false
+ }
+ }
+ }
+ return true
+
+}
+
func TestSubscribeChannel(t *testing.T) {
m, i := setup()
m.AssertExpectations(t)
}
+func TestWriteMapAsInput(t *testing.T){
+ m, i := setup()
+
+ setExpected := []interface{}{"{namespace},key1", "string123",
+ "{namespace},key22", 12,
+ "{namespace},key333", []byte{1,2,3,4,5}}
+ inputMap := map[string]interface{}{
+ "key1": "string123",
+ "key22": 12,
+ "key333": []byte{1,2,3,4,5},
+ }
+
+ m.On("MSet", mock.MatchedBy(func(input []interface{}) bool {
+ for _, v := range input {
+ found := false
+ for _, x := range setExpected {
+ found = reflect.DeepEqual(x, v)
+ if found == true {
+ break
+ }
+ }
+ if found == false {
+ return false
+ }
+ }
+ return true
+ })).Return(nil)
+
+ err := i.Set(inputMap)
+ assert.Nil(t, err)
+ m.AssertExpectations(t)
+}
+
func TestWriteMixed(t *testing.T) {
m, i := setup()
func TestWriteAndPublishOneKeyOneChannel(t *testing.T) {
m, i := setup()
- expectedChannel := "{namespace},channel"
- expectedMessage := "event"
+ expectedChannelAndEvent := []string{"{namespace},channel", "event"}
expectedKeyVal := []interface{}{"{namespace},key1", "data1"}
- m.On("MSetPub", expectedChannel, expectedMessage, expectedKeyVal).Return(nil)
+ m.On("MSetMPub", expectedChannelAndEvent, expectedKeyVal).Return(nil)
m.AssertNotCalled(t, "MSet", expectedKeyVal)
err := i.SetAndPublish([]string{"channel", "event"}, "key1", "data1")
assert.Nil(t, err)
m.AssertExpectations(t)
}
+
+func TestWriteAndPublishSeveralChannelsAndEvents(t *testing.T) {
+ m , i := setup()
+
+ expectedChannelsAndEvents := []string{"{namespace},channel1", "event1___event2",
+ "{namespace},channel2", "event3___event4"}
+ expectedKeyVal := []interface{}{"{namespace},key", "data"}
+
+ verifyChannelAndEvent := func(input []string) bool {
+ return verifySliceInOrder(input, expectedChannelsAndEvents)
+ }
+ m.On("MSetMPub", mock.MatchedBy(verifyChannelAndEvent), expectedKeyVal).Return(nil)
+ m.AssertNotCalled(t, "MSet", expectedKeyVal)
+ err := i.SetAndPublish([]string{"channel1", "event1", "channel2", "event3", "channel1", "event2", "channel2", "event4"},
+ "key", "data")
+ assert.Nil(t, err)
+ m.AssertExpectations(t)
+}
+
func TestWriteAndPublishOneKeyOneChannelTwoEvents(t *testing.T) {
m, i := setup()
- expectedChannel := "{namespace},channel"
- expectedMessage := "event1___event2"
+ expectedChannelAndEvents := []string{"{namespace},channel", "event1___event2"}
expectedKeyVal := []interface{}{"{namespace},key1", "data1"}
- m.On("MSetPub", expectedChannel, expectedMessage, expectedKeyVal).Return(nil)
+ m.On("MSetMPub", expectedChannelAndEvents, expectedKeyVal).Return(nil)
m.AssertNotCalled(t, "MSet", expectedKeyVal)
err := i.SetAndPublish([]string{"channel", "event1", "channel", "event2"}, "key1", "data1")
assert.Nil(t, err)
func TestWriteAndPublishIncorrectChannelAndEvent(t *testing.T) {
m, i := setup()
- expectedChannel := "{namespace},channel"
- expectedMessage := "event1___event2"
+ expectedChannelAndEvent := []string{}
expectedKeyVal := []interface{}{"{namespace},key1", "data1"}
- m.AssertNotCalled(t, "MSetPub", expectedChannel, expectedMessage, expectedKeyVal)
+ m.AssertNotCalled(t, "MSetMPub", expectedChannelAndEvent, expectedKeyVal)
m.AssertNotCalled(t, "MSet", expectedKeyVal)
err := i.SetAndPublish([]string{"channel", "event1", "channel"}, "key1", "data1")
assert.NotNil(t, err)
func TestWriteAndPublishNotAllowedCharactersInEvents(t *testing.T) {
m, i := setup()
- expectedChannel := "{namespace},channel"
- expectedMessage := "event1___event2"
+ expectedChannelAndEvent := []string{}
expectedKeyVal := []interface{}{"{namespace},key1", "data1"}
- m.AssertNotCalled(t, "MSetPub", expectedChannel, expectedMessage, expectedKeyVal)
+ m.AssertNotCalled(t, "MSetMPub", expectedChannelAndEvent, expectedKeyVal)
m.AssertNotCalled(t, "MSet", expectedKeyVal)
err := i.SetAndPublish([]string{"channel", "event1___event2"}, "key1", "data1")
assert.NotNil(t, err)
func TestWriteAndPublishNoData(t *testing.T) {
m, i := setup()
- expectedChannel := "{namespace},channel"
- expectedMessage := "event"
+ expectedChannelAndEvent := []string{}
expectedKeyVal := []interface{}{"key"}
- m.AssertNotCalled(t, "MSetPub", expectedChannel, expectedMessage, expectedKeyVal)
+ m.AssertNotCalled(t, "MSetMPub", expectedChannelAndEvent, expectedKeyVal)
m.AssertNotCalled(t, "MSet", expectedKeyVal)
err := i.SetAndPublish([]string{"channel", "event"}, []interface{}{"key"})
assert.NotNil(t, err)
expectedKeyVal := []interface{}{"{namespace},key1", "data1"}
m.On("MSet", expectedKeyVal).Return(nil)
- m.AssertNotCalled(t, "MSetPub", "", "", expectedKeyVal)
+ m.AssertNotCalled(t, "MSetMPub", "", "", expectedKeyVal)
err := i.SetAndPublish([]string{}, "key1", "data1")
assert.Nil(t, err)
m.AssertExpectations(t)
func TestRemoveAndPublishSuccessfully(t *testing.T) {
m, i := setup()
- expectedChannel := "{namespace},channel"
- expectedEvent := "event"
+ expectedChannelAndEvent := []string{"{namespace},channel", "event"}
expectedKeys := []string{"{namespace},key1", "{namespace},key2"}
- m.On("DelPub", expectedChannel, expectedEvent, expectedKeys).Return(nil)
+ m.On("DelMPub", expectedChannelAndEvent, expectedKeys).Return(nil)
err := i.RemoveAndPublish([]string{"channel", "event"}, []string{"key1", "key2"})
assert.Nil(t, err)
m.AssertExpectations(t)
}
+
+func TestRemoveAndPublishSeveralChannelsAndEventsSuccessfully(t *testing.T) {
+ m, i := setup()
+
+ expectedChannelAndEvent := []string{"{namespace},channel1", "event1___event2",
+ "{namespace},channel2", "event3___event4"}
+ expectedKeys := []string{"{namespace},key1", "{namespace},key2"}
+
+ verifyChannelAndEvent := func(input []string) bool {
+ return verifySliceInOrder(input, expectedChannelAndEvent)
+ }
+ m.On("DelMPub", mock.MatchedBy(verifyChannelAndEvent), expectedKeys).Return(nil)
+ err := i.RemoveAndPublish([]string{"channel1", "event1", "channel2", "event3",
+ "channel1", "event2", "channel2", "event4"},
+ []string{"key1", "key2"})
+ assert.Nil(t, err)
+ m.AssertExpectations(t)
+}
func TestRemoveAndPublishFail(t *testing.T) {
m, i := setup()
- expectedChannel := "{namespace},channel"
- expectedEvent := "event"
+ expectedChannelAndEvent := []string{"{namespace},channel", "event"}
expectedKeys := []string{"{namespace},key1", "{namespace},key2"}
- m.On("DelPub", expectedChannel, expectedEvent, expectedKeys).Return(errors.New("Some error"))
+ m.On("DelMPub", expectedChannelAndEvent, expectedKeys).Return(errors.New("Some error"))
err := i.RemoveAndPublish([]string{"channel", "event"}, []string{"key1", "key2"})
assert.NotNil(t, err)
m.AssertExpectations(t)
func TestRemoveAndPublishIncorrectChannel(t *testing.T) {
m, i := setup()
- notExpectedChannel := "{namespace},channel"
- notExpectedEvent := "event"
+ notExpectedChannelAndEvent := []string{}
notExpectedKeys := []string{"{namespace},key"}
- m.AssertNotCalled(t, "DelPub", notExpectedChannel, notExpectedEvent, notExpectedKeys)
+ m.AssertNotCalled(t, "DelMPub", notExpectedChannelAndEvent, notExpectedKeys)
m.AssertNotCalled(t, "Del", notExpectedKeys)
err := i.RemoveAndPublish([]string{"channel", "event", "channel2"}, []string{})
assert.Nil(t, err)
func TestRemoveAndPublishNoKeys(t *testing.T) {
m, i := setup()
- notExpectedChannel := "{namespace},channel"
- notExpectedEvent := "event"
+ notExpectedChannelAndEvent := []string{}
notExpectedKeys := []string{"{namespace},key"}
- m.AssertNotCalled(t, "DelPub", notExpectedChannel, notExpectedEvent, notExpectedKeys)
+ m.AssertNotCalled(t, "DelMPub", notExpectedChannelAndEvent, notExpectedKeys)
m.AssertNotCalled(t, "Del", notExpectedKeys)
err := i.RemoveAndPublish([]string{"channel", "event"}, []string{})
assert.Nil(t, err)
mKeysExpected := string("{namespace},*")
mKeysReturn := []string{"{namespace},key1", "{namespace},key2"}
mDelExpected := mKeysReturn
- expectedChannel := "{namespace},channel"
- expectedEvent := "event"
+ expectedChannelAndEvent := []string{"{namespace},channel", "event"}
m.On("Keys", mKeysExpected).Return(mKeysReturn, nil)
- m.On("DelPub", expectedChannel, expectedEvent, mDelExpected).Return(nil)
+ m.On("DelMPub", expectedChannelAndEvent, mDelExpected).Return(nil)
err := i.RemoveAllAndPublish([]string{"channel", "event"})
assert.Nil(t, err)
m.AssertExpectations(t)
mKeysExpected := string("{namespace},*")
mKeysReturn := []string{"{namespace},key1", "{namespace},key2"}
mDelExpected := mKeysReturn
- expectedChannel := "{namespace},channel"
- expectedEvent := "event"
+ expectedChannelAndEvent := []string{"{namespace},channel", "event" }
m.On("Keys", mKeysExpected).Return(mKeysReturn, errors.New("Some error"))
- m.AssertNotCalled(t, "DelPub", expectedChannel, expectedEvent, mDelExpected)
+ m.AssertNotCalled(t, "DelMPub", expectedChannelAndEvent, mDelExpected)
err := i.RemoveAllAndPublish([]string{"channel", "event"})
assert.NotNil(t, err)
m.AssertExpectations(t)
mKeysExpected := string("{namespace},*")
mKeysReturn := []string{"{namespace},key1", "{namespace},key2"}
mDelExpected := mKeysReturn
- expectedChannel := "{namespace},channel"
- expectedEvent := "event"
+ expectedChannelAndEvent := []string{"{namespace},channel", "event"}
m.On("Keys", mKeysExpected).Return(mKeysReturn, nil)
- m.On("DelPub", expectedChannel, expectedEvent, mDelExpected).Return(errors.New("Some error"))
+ m.On("DelMPub", expectedChannelAndEvent, mDelExpected).Return(errors.New("Some error"))
err := i.RemoveAllAndPublish([]string{"channel", "event"})
assert.NotNil(t, err)
m.AssertExpectations(t)
mKeysExpected := string("{namespace},*")
mKeysReturn := []string{"{namespace},key1", "{namespace},key2"}
mDelExpected := mKeysReturn
- expectedChannel := "{namespace},channel"
- expectedEvent := "event"
+ expectedChannelAndEvent := []string{"{namespace},channel", "event"}
m.On("Keys", mKeysExpected).Return(mKeysReturn, nil)
- m.AssertNotCalled(t, "DelPub", expectedChannel, expectedEvent, mDelExpected)
+ m.AssertNotCalled(t, "DelMPub", expectedChannelAndEvent, mDelExpected)
err := i.RemoveAllAndPublish([]string{"channel", "event___anotherEvent"})
assert.NotNil(t, err)
m.AssertExpectations(t)
mDelExpected := mKeysReturn
m.On("Keys", mKeysExpected).Return(mKeysReturn, nil)
m.On("Del", mDelExpected).Return(nil)
- m.AssertNotCalled(t, "DelPub", "", "", mDelExpected)
+ m.AssertNotCalled(t, "DelMPub", "", "", mDelExpected)
err := i.RemoveAllAndPublish([]string{})
assert.Nil(t, err)
m.AssertExpectations(t)
mKeysReturn := []string{"{namespace},key1", "{namespace},key2"}
mDelExpected := mKeysReturn
m.On("Keys", mKeysExpected).Return(mKeysReturn, nil)
- m.AssertNotCalled(t, "DelPub", "", "", mDelExpected)
+ m.AssertNotCalled(t, "DelMPub", "", "", mDelExpected)
err := i.RemoveAllAndPublish([]string{"channel", "event", "channel2"})
assert.NotNil(t, err)
m.AssertExpectations(t)