/
amqp_it_test.go
138 lines (125 loc) · 4.18 KB
/
amqp_it_test.go
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
//go:build integration
// +build integration
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package ers
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
amqp "github.com/rabbitmq/amqp091-go"
)
func TestAMQPER(t *testing.T) {
cfg, err := config.NewCGRConfigFromJSONStringWithDefaults(`{
"ers": { // EventReaderService
"enabled": true, // starts the EventReader service: <true|false>
"sessions_conns":["*localhost"],
"readers": [
{
"id": "amqp", // identifier of the EventReader profile
"type": "*amqp_json_map", // reader type <*file_csv>
"run_delay": "-1", // sleep interval in seconds between consecutive runs, -1 to use automation via inotify or 0 to disable running all together
"concurrent_requests": 1024, // maximum simultaneous requests/files to process, 0 for unlimited
"source_path": "amqp://guest:guest@localhost:5672/",// read data from this path
"opts": {
"amqpQueueID": "cdrs3",
"amqpConsumerTag": "test-key",
"amqpExchange": "test-exchange",
"amqpExchangeType": "direct",
"amqpRoutingKey": "test-key",
},
"processed_path": "", // move processed data here
"tenant": "cgrates.org", // tenant used by import
"filters": [], // limit parsing based on the filters
"flags": [], // flags to influence the event processing
"fields":[ // import fields template, tag will match internally CDR field, in case of .csv value will be represented by index of the field value
{"tag": "CGRID", "type": "*composed", "value": "~*req.CGRID", "path": "*cgreq.CGRID"},
],
},
],
},
}`)
if err != nil {
t.Fatal(err)
}
if err := cfg.CheckConfigSanity(); err != nil {
t.Fatal(err)
}
utils.Logger, _ = utils.Newlogger(utils.MetaSysLog, cfg.GeneralCfg().NodeID)
utils.Logger.SetLogLevel(7)
rdrEvents = make(chan *erEvent, 1)
rdrErr = make(chan error, 1)
rdrExit = make(chan struct{}, 1)
if rdr, err = NewAMQPER(cfg, 1, rdrEvents, make(chan *erEvent, 1),
rdrErr, new(engine.FilterS), rdrExit); err != nil {
t.Fatal(err)
}
connection, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
t.Fatal(err)
}
defer connection.Close()
channel, err := connection.Channel()
if err != nil {
t.Fatal(err)
}
rdr.Serve()
randomCGRID := utils.UUIDSha1Prefix()
if err = channel.PublishWithContext(
context.TODO(),
"test-exchange", // publish to an exchange
"test-key", // routing to 0 or more queues
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: utils.ContentJSON,
Body: []byte(fmt.Sprintf(`{"CGRID": "%s"}`, randomCGRID)),
DeliveryMode: amqp.Persistent, // 1=non-persistent, 2=persistent
},
); err != nil {
t.Fatal(err)
}
select {
case err = <-rdrErr:
t.Error(err)
case ev := <-rdrEvents:
if ev.rdrCfg.ID != "amqp" {
t.Errorf("Expected 'amqp' received `%s`", ev.rdrCfg.ID)
}
expected := &utils.CGREvent{
Tenant: "cgrates.org",
ID: ev.cgrEvent.ID,
Time: ev.cgrEvent.Time,
Event: map[string]any{
"CGRID": randomCGRID,
},
APIOpts: map[string]any{},
}
if !reflect.DeepEqual(ev.cgrEvent, expected) {
t.Errorf("Expected %s ,received %s", utils.ToJSON(expected), utils.ToJSON(ev.cgrEvent))
}
case <-time.After(10 * time.Second):
t.Fatal("Timeout")
}
if _, err := channel.QueueDelete("cdrs3", false, false, false); err != nil {
t.Fatal(err)
}
close(rdrExit)
}