Resolved PR Review comments

This commit is contained in:
MohitKatare-protean
2025-05-13 12:30:34 +05:30
parent 2e0b5834d7
commit e5cccc30f8
4 changed files with 344 additions and 77 deletions

View File

@@ -2,22 +2,18 @@ package main
import (
"context"
"fmt"
"github.com/beckn/beckn-onix/pkg/log"
"github.com/beckn/beckn-onix/pkg/plugin/definition"
"github.com/beckn/beckn-onix/pkg/plugin/implementation/publisher"
"github.com/rabbitmq/amqp091-go"
)
// publisherProvider implements the PublisherProvider interface.
// It is responsible for creating a new Publisher instance.
type publisherProvider struct{}
// New creates a new Publisher instance based on the provided configuration map.
// It also returns a cleanup function to close resources and any potential errors encountered.
// New creates a new Publisher instance based on the provided configuration.
func (p *publisherProvider) New(ctx context.Context, config map[string]string) (definition.Publisher, func() error, error) {
// Step 1: Map config
cfg := &publisher.Config{
Addr: config["addr"],
Exchange: config["exchange"],
@@ -27,69 +23,14 @@ func (p *publisherProvider) New(ctx context.Context, config map[string]string) (
}
log.Debugf(ctx, "Publisher config mapped: %+v", cfg)
// Step 2: Validate
if err := publisher.Validate(cfg); err != nil {
log.Errorf(ctx, err, "Publisher config validation failed")
pub, cleanup, err := publisher.New(cfg)
if err != nil {
log.Errorf(ctx, err, "Failed to create publisher instance")
return nil, nil, err
}
log.Infof(ctx, "Publisher config validated successfully")
// Step 3:URL
connURL, err := publisher.GetConnURL(cfg)
if err != nil {
log.Errorf(ctx, err, "Failed to build RabbitMQ connection URL")
return nil, nil, fmt.Errorf("failed to build connection URL: %w", err)
}
log.Debugf(ctx, "RabbitMQ connection URL built: %s", connURL)
// Step 4: Connect
conn, err := amqp091.Dial(connURL)
if err != nil {
log.Errorf(ctx, err, "Failed to connect to RabbitMQ")
return nil, nil, fmt.Errorf("%w: %v", publisher.ErrConnectionFailed, err)
}
log.Infof(ctx, "Connected to RabbitMQ")
ch, err := conn.Channel()
if err != nil {
conn.Close()
log.Errorf(ctx, err, "Failed to open RabbitMQ channel")
return nil, nil, fmt.Errorf("%w: %v", publisher.ErrChannelFailed, err)
}
log.Infof(ctx, "RabbitMQ channel opened successfully")
// Step 5: Declare Exchange
err = ch.ExchangeDeclare(
cfg.Exchange,
"topic",
cfg.Durable,
false,
false,
false,
nil,
)
if err != nil {
ch.Close()
conn.Close()
log.Errorf(ctx, err, "Failed to declare exchange: %s", cfg.Exchange)
return nil, nil, fmt.Errorf("%w: %v", publisher.ErrExchangeDeclare, err)
}
log.Infof(ctx, "RabbitMQ exchange declared successfully: %s", cfg.Exchange)
// Step 6: Create publisher instance
publisher := &publisher.Publisher{
Conn: conn,
Channel: ch,
Config: cfg,
}
cleanup := func() error {
log.Infof(ctx, "Cleaning up RabbitMQ resources")
_ = ch.Close()
return conn.Close()
}
log.Infof(ctx, "Publisher instance created successfully")
return publisher, cleanup, nil
return pub, cleanup, nil
}
// Provider is the instance of publisherProvider that implements the PublisherProvider interface.

View File

@@ -0,0 +1,106 @@
package main
import (
"context"
"errors"
"strings"
"testing"
"github.com/beckn/beckn-onix/pkg/plugin/implementation/publisher"
"github.com/rabbitmq/amqp091-go"
)
type mockChannel struct{}
func (m *mockChannel) PublishWithContext(ctx context.Context, exchange, key string, mandatory, immediate bool, msg amqp091.Publishing) error {
return nil
}
func (m *mockChannel) ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp091.Table) error {
return nil
}
func (m *mockChannel) Close() error {
return nil
}
func TestPublisherProvider_New_Success(t *testing.T) {
// Save original dialFunc and channelFunc
originalDialFunc := publisher.DialFunc
originalChannelFunc := publisher.ChannelFunc
defer func() {
publisher.DialFunc = originalDialFunc
publisher.ChannelFunc = originalChannelFunc
}()
// Override mocks
publisher.DialFunc = func(url string) (*amqp091.Connection, error) {
return nil, nil
}
publisher.ChannelFunc = func(conn *amqp091.Connection) (publisher.Channel, error) {
return &mockChannel{}, nil
}
t.Setenv("RABBITMQ_USERNAME", "guest")
t.Setenv("RABBITMQ_PASSWORD", "guest")
config := map[string]string{
"addr": "localhost",
"exchange": "test-exchange",
"routing_key": "test.key",
"durable": "true",
"use_tls": "false",
}
ctx := context.Background()
pub, cleanup, err := Provider.New(ctx, config)
if err != nil {
t.Fatalf("Provider.New returned error: %v", err)
}
if pub == nil {
t.Fatal("Expected non-nil publisher")
}
if cleanup == nil {
t.Fatal("Expected non-nil cleanup function")
}
if err := cleanup(); err != nil {
t.Errorf("Cleanup returned error: %v", err)
}
}
func TestPublisherProvider_New_Failure(t *testing.T) {
// Save and restore dialFunc
originalDialFunc := publisher.DialFunc
defer func() { publisher.DialFunc = originalDialFunc }()
// Simulate dial failure
publisher.DialFunc = func(url string) (*amqp091.Connection, error) {
return nil, errors.New("dial failed")
}
t.Setenv("RABBITMQ_USERNAME", "guest")
t.Setenv("RABBITMQ_PASSWORD", "guest")
config := map[string]string{
"addr": "localhost",
"exchange": "test-exchange",
"routing_key": "test.key",
"durable": "true",
}
ctx := context.Background()
pub, cleanup, err := Provider.New(ctx, config)
if err == nil {
t.Fatal("Expected error from Provider.New but got nil")
}
if !strings.Contains(err.Error(), "dial failed") {
t.Errorf("Expected 'dial failed' error, got: %v", err)
}
if pub != nil {
t.Errorf("Expected nil publisher, got: %v", pub)
}
if cleanup != nil {
t.Error("Expected nil cleanup, got non-nil")
}
}