Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
17
pkg/kafka/mock/admin.go
Normal file
17
pkg/kafka/mock/admin.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
|
||||
)
|
||||
|
||||
// Admin is the mock admin
|
||||
type Admin struct {
|
||||
MockFunc func(ctx context.Context, topics []kafka.TopicSpecification, options ...kafka.CreateTopicsAdminOption) ([]kafka.TopicResult, error)
|
||||
}
|
||||
|
||||
// CreateTopics is the mock admin's version
|
||||
func (a *Admin) CreateTopics(ctx context.Context, topics []kafka.TopicSpecification, options ...kafka.CreateTopicsAdminOption) ([]kafka.TopicResult, error) {
|
||||
return a.MockFunc(ctx, topics)
|
||||
}
|
||||
131
pkg/kafka/mock/kafka_mock.go
Normal file
131
pkg/kafka/mock/kafka_mock.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// KafkaMock mock for Kafka utility
|
||||
type KafkaMock struct {
|
||||
lock sync.Mutex
|
||||
err error
|
||||
produce string
|
||||
ProduceMessages map[string]map[string]interface{}
|
||||
}
|
||||
|
||||
// Produce mocks produce method
|
||||
func (k *KafkaMock) Produce(topic string, key string, payload interface{}, headers map[string][]byte) error {
|
||||
k.lock.Lock()
|
||||
defer k.lock.Unlock()
|
||||
|
||||
if k.err != nil {
|
||||
return k.err
|
||||
}
|
||||
|
||||
k.produce = fmt.Sprintf("%s %v", topic, payload)
|
||||
k.addMessage(topic, key, payload)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KafkaMock) ProduceBinary(topic string, key string, data []byte, header map[string][]byte) error {
|
||||
if k.err != nil {
|
||||
return k.err
|
||||
}
|
||||
|
||||
k.produce = fmt.Sprintf("%s %v", topic, data)
|
||||
k.addMessage(topic, key, data)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KafkaMock) ProduceBinaryToChannel(topic string, key string, payload []byte) error {
|
||||
if k.err != nil {
|
||||
return k.err
|
||||
}
|
||||
|
||||
k.produce = fmt.Sprintf("%s %v", topic, payload)
|
||||
k.addMessage(topic, key, payload)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (k *KafkaMock) ProduceBatch(topic string, key string, payload [][]byte) error {
|
||||
if k.err != nil {
|
||||
return k.err
|
||||
}
|
||||
|
||||
for _, item := range payload {
|
||||
k.addMessage(topic, key, item)
|
||||
}
|
||||
|
||||
return nil
|
||||
}*/
|
||||
|
||||
func (k *KafkaMock) ProduceToChannel(topic string, key string, payload interface{}) error {
|
||||
k.addMessage(topic, key, payload)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KafkaMock) ProduceSignalBatch(topic string, key string, payload interface{}) error {
|
||||
if k.err != nil {
|
||||
return k.err
|
||||
}
|
||||
|
||||
k.produce = fmt.Sprintf("%s %v", topic, payload)
|
||||
k.addMessage(topic, key, payload)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KafkaMock) ReadEvents() {
|
||||
// nothing
|
||||
}
|
||||
|
||||
// Close mocks close method
|
||||
func (k *KafkaMock) Close() {
|
||||
// nothing
|
||||
}
|
||||
|
||||
// SetError to set error for mock Kafka utility
|
||||
func (k *KafkaMock) SetError(err error) {
|
||||
k.err = err
|
||||
}
|
||||
|
||||
func (k *KafkaMock) addMessage(topic string, id string, payload interface{}) {
|
||||
if k.ProduceMessages == nil {
|
||||
k.ProduceMessages = map[string]map[string]interface{}{}
|
||||
}
|
||||
|
||||
if k.ProduceMessages[topic] == nil {
|
||||
k.ProduceMessages[topic] = map[string]interface{}{}
|
||||
}
|
||||
|
||||
k.ProduceMessages[topic][id] = payload
|
||||
}
|
||||
|
||||
func (k *KafkaMock) Reset() {
|
||||
k.ProduceMessages = map[string]map[string]interface{}{}
|
||||
}
|
||||
|
||||
func (k *KafkaMock) Len() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (k *KafkaMock) Flush(timeoutMs int) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetProduce to set error for mock Kafka utility
|
||||
func (k *KafkaMock) GetProduce() string {
|
||||
return k.produce
|
||||
}
|
||||
|
||||
// GetKafkaMock returns mock Kafka utilty
|
||||
func GetKafkaMock(err error) *KafkaMock {
|
||||
return &KafkaMock{
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
50
pkg/kafka/mock/kafka_test_case.go
Normal file
50
pkg/kafka/mock/kafka_test_case.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"fiskerinc.com/modules/testhelper"
|
||||
)
|
||||
|
||||
type KafkaTestCase struct {
|
||||
ExpectedProduceMessages map[string]map[string]interface{}
|
||||
MockError error
|
||||
}
|
||||
|
||||
func (tc *KafkaTestCase) Setup(mock *KafkaMock) {
|
||||
mock.SetError(tc.MockError)
|
||||
}
|
||||
|
||||
func (tc *KafkaTestCase) Validate(t *testing.T, name string, mock *KafkaMock) {
|
||||
tc.validate(t, fmt.Sprintf("%s expected", name), tc.ExpectedProduceMessages, mock.ProduceMessages, true)
|
||||
tc.validate(t, fmt.Sprintf("%s unexpected", name), mock.ProduceMessages, tc.ExpectedProduceMessages, false)
|
||||
}
|
||||
|
||||
func (tc *KafkaTestCase) validate(t *testing.T, name string, control map[string]map[string]interface{}, source map[string]map[string]interface{}, compareValues bool) {
|
||||
for topic, expected := range control {
|
||||
if sent, hasTopic := source[topic]; hasTopic {
|
||||
for id, payload := range expected {
|
||||
if msg, hasID := sent[id]; hasID {
|
||||
if compareValues {
|
||||
tc.compare(t, name, payload, msg)
|
||||
}
|
||||
} else {
|
||||
t.Errorf(testhelper.TestErrorTemplate, fmt.Sprintf("%s id", name), id, nil)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
t.Errorf(testhelper.TestErrorTemplate, fmt.Sprintf("%s topic", name), topic, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (tc *KafkaTestCase) compare(t *testing.T, name string, control interface{}, source interface{}) {
|
||||
data, err := json.Marshal(source)
|
||||
if err != nil {
|
||||
t.Errorf(testhelper.TestErrorTemplate, fmt.Sprintf("%s marshal payload", name), nil, err)
|
||||
} else if control != string(data) {
|
||||
t.Errorf(testhelper.TestErrorTemplate, fmt.Sprintf("%s message", name), control, string(data))
|
||||
}
|
||||
}
|
||||
45
pkg/kafka/mock/noncommital_consumer.go
Normal file
45
pkg/kafka/mock/noncommital_consumer.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
|
||||
)
|
||||
|
||||
// NoncommitalConsumerMock mock for Kafka utility
|
||||
type NoncommitalConsumerMock struct{}
|
||||
|
||||
func (c *NoncommitalConsumerMock) Subscribe(topics []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *NoncommitalConsumerMock) ConsumeToChannel(topics []string, events chan *kafka.Message) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *NoncommitalConsumerMock) ConsumeOrRebalancedCatch(topics []string, events chan *kafka.Message, reb chan struct{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *NoncommitalConsumerMock) Seek(topic string, offset kafka.Offset) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *NoncommitalConsumerMock) Commit(message *kafka.Message) ([]kafka.TopicPartition, error) {
|
||||
return []kafka.TopicPartition{message.TopicPartition}, nil
|
||||
}
|
||||
|
||||
func (c *NoncommitalConsumerMock) LastOffsetConsumed(topic string, partition int32) (kafka.Offset, error) {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func (c *NoncommitalConsumerMock) Stop() {}
|
||||
|
||||
func (c *NoncommitalConsumerMock) Check(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// passthrough function for CommitOffsets()
|
||||
func (c *NoncommitalConsumerMock) CommitOffsets(offsets []kafka.TopicPartition) ([]kafka.TopicPartition, error) {
|
||||
return nil, nil
|
||||
}
|
||||
Reference in New Issue
Block a user