mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-24 00:28:44 +05:00
Add coverage tests for json_codec.go
This commit is contained in:
committed by
Dan Christian Bogos
parent
53e277a57d
commit
7d97e8a99d
@@ -24,6 +24,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/rpc"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -145,23 +146,26 @@ func TestJSONCodecReadRequestHeader2(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestJSONReadRequestBody(t *testing.T) {
|
||||
tests := []struct{
|
||||
name string
|
||||
arg any
|
||||
exp error
|
||||
params bool
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arg any
|
||||
exp string
|
||||
params bool
|
||||
isApier bool
|
||||
}{
|
||||
{
|
||||
name: "nil argument",
|
||||
arg: nil,
|
||||
exp: nil,
|
||||
params: false,
|
||||
name: "nil argument",
|
||||
arg: nil,
|
||||
params: false,
|
||||
isApier: false,
|
||||
},
|
||||
{
|
||||
name: "check error missing param",
|
||||
arg: "test",
|
||||
exp: errMissingParams,
|
||||
params: true,
|
||||
name: "check error missing param",
|
||||
arg: "test",
|
||||
exp: "jsonrpc: request body missing params",
|
||||
params: true,
|
||||
isApier: false,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -173,21 +177,256 @@ func TestJSONReadRequestBody(t *testing.T) {
|
||||
s := NewCustomJSONServerCodec(conn)
|
||||
sj := s.(*jsonServerCodec)
|
||||
|
||||
if tt.params {
|
||||
sj.req.Params = nil
|
||||
if !tt.params {
|
||||
jm, _ := json.RawMessage.MarshalJSON([]byte(`{
|
||||
"method": "ApierV.Method",
|
||||
"params": [{
|
||||
"TPid": "test"
|
||||
}],
|
||||
"id": 0
|
||||
}`))
|
||||
js := json.RawMessage(jm)
|
||||
sj.req.Params = &js
|
||||
}
|
||||
if tt.isApier {
|
||||
sj.req.isApier = true
|
||||
}
|
||||
|
||||
err := s.ReadRequestBody(tt.arg)
|
||||
rcv := s.ReadRequestBody(tt.arg)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != tt.exp.Error() {
|
||||
t.Errorf("recived %s, expected %s", err, tt.exp)
|
||||
if rcv != nil {
|
||||
if rcv.Error() != tt.exp {
|
||||
t.Errorf("recived %s, expected %s", rcv, tt.exp)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONReadRequestBody2(t *testing.T) {
|
||||
conn := &bufferClose{
|
||||
Buffer: &bytes.Buffer{},
|
||||
}
|
||||
s := NewCustomJSONServerCodec(conn)
|
||||
sj := s.(*jsonServerCodec)
|
||||
|
||||
b, _ := json.Marshal([1]any{&CGREvent{Tenant: "cgrates.org"}})
|
||||
js := json.RawMessage(b)
|
||||
sj.req.Params = &js
|
||||
|
||||
sj.req.isApier = true
|
||||
|
||||
args := &MethodParameters{
|
||||
Method: "test",
|
||||
Parameters: [1]any{&CGREvent{Tenant: "cgrates.net"}},
|
||||
}
|
||||
rcv := s.ReadRequestBody(args)
|
||||
|
||||
if rcv != nil {
|
||||
t.Errorf("recived %v, expected %v", rcv, nil)
|
||||
}
|
||||
|
||||
a := args.Parameters
|
||||
if a.(map[string]any)[Tenant] != "cgrates.org" {
|
||||
t.Errorf("expected %s, recived %s", args.Parameters.(*CGREvent).Tenant, "cgrates.org")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONReadRequestBody3(t *testing.T) {
|
||||
conn := &bufferClose{
|
||||
Buffer: &bytes.Buffer{},
|
||||
}
|
||||
s := NewCustomJSONServerCodec(conn)
|
||||
sj := s.(*jsonServerCodec)
|
||||
|
||||
b, _ := json.Marshal([1]any{&CGREvent{Tenant: "cgrates.org"}})
|
||||
js := json.RawMessage(b)
|
||||
sj.req.Params = &js
|
||||
|
||||
sj.req.isApier = false
|
||||
|
||||
args := &MethodParameters{
|
||||
Method: "test",
|
||||
Parameters: [1]any{&CGREvent{Tenant: "cgrates.net"}},
|
||||
}
|
||||
rcv := s.ReadRequestBody(args)
|
||||
|
||||
if rcv != nil {
|
||||
t.Errorf("recived %v, expected %v", rcv, nil)
|
||||
}
|
||||
|
||||
a := args.Parameters.([1]any)
|
||||
if a[0].(*CGREvent).Tenant != "cgrates.net" {
|
||||
t.Errorf("expected %s, recived %s", a[0].(*CGREvent).Tenant, "cgrate.net")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
- NewCustomJSONServerCodec
|
||||
- defer Close
|
||||
- ReadRequestHeader
|
||||
- check whether c.req was populated correctly
|
||||
- check whether c.seq was incremented
|
||||
- check whether c.req.Id was added to c.pending array
|
||||
- ReadRequestBody
|
||||
- check if x.Parameters was properly modified
|
||||
- WriteResponse
|
||||
- check if the rpc.Response is populated correctly
|
||||
*/
|
||||
func TestJSONCodecOK(t *testing.T) {
|
||||
//Create server
|
||||
conn := &bufferClose{
|
||||
Buffer: &bytes.Buffer{},
|
||||
}
|
||||
s := NewCustomJSONServerCodec(conn)
|
||||
c := s.(*jsonServerCodec)
|
||||
|
||||
|
||||
defer s.Close()
|
||||
|
||||
//ReadRequesHeader
|
||||
rr := rpc.Request{
|
||||
ServiceMethod: "Service.Method",
|
||||
Seq: 1,
|
||||
}
|
||||
|
||||
conn.Write([]byte(`{
|
||||
"method": "ApierV.Method",
|
||||
"params": [{
|
||||
"TPid": "test"
|
||||
}],
|
||||
"id": 0
|
||||
}`))
|
||||
|
||||
err := s.ReadRequestHeader(&rr)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if c.req.Method != "ApierV.Method" {
|
||||
t.Errorf("received %v, expected %v", c.req.Method, "ApierV.Method")
|
||||
}
|
||||
|
||||
exp := json.RawMessage([]byte{48})
|
||||
rcv := *c.pending[1]
|
||||
if !reflect.DeepEqual(rcv, exp) {
|
||||
t.Errorf("received %v, expected %v", rcv, exp)
|
||||
}
|
||||
|
||||
if c.seq != 1 {
|
||||
t.Errorf("received %v, expected %v", c.seq, 1)
|
||||
}
|
||||
|
||||
//ReadRequestBody
|
||||
b, _ := json.Marshal([1]any{&CGREvent{Tenant: "cgrates.org"}})
|
||||
js := json.RawMessage(b)
|
||||
c.req.Params = &js
|
||||
|
||||
c.req.isApier = true
|
||||
|
||||
args := &MethodParameters{
|
||||
Method: "test",
|
||||
Parameters: [1]any{&CGREvent{Tenant: "cgrates.net"}},
|
||||
}
|
||||
err = s.ReadRequestBody(args)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("received %v, expected %v", err, nil)
|
||||
}
|
||||
|
||||
a := args.Parameters
|
||||
if a.(map[string]any)[Tenant] != "cgrates.org" {
|
||||
t.Errorf("expected %s, received %s", args.Parameters.(*CGREvent).Tenant, "cgrates.org")
|
||||
}
|
||||
|
||||
//WriteResponse
|
||||
res := rpc.Response{
|
||||
Seq: 1,
|
||||
}
|
||||
err = c.WriteResponse(&res, "test")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
mp := make(map[string]any)
|
||||
err = c.dec.Decode(&mp)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if mp["error"] != nil ||
|
||||
mp["result"] != "test" ||
|
||||
mp["id"] != 0. {
|
||||
t.Error("unexpected reply", mp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONCodecWriteResponse(t *testing.T) {
|
||||
conn := &bufferClose{
|
||||
Buffer: &bytes.Buffer{},
|
||||
}
|
||||
s := NewCustomJSONServerCodec(conn)
|
||||
c := s.(*jsonServerCodec)
|
||||
|
||||
defer s.Close()
|
||||
|
||||
res := rpc.Response{
|
||||
Seq: 1,
|
||||
}
|
||||
err := c.WriteResponse(&res, "test")
|
||||
if err.Error() != "invalid sequence number in response" {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONCodecWriteResponse2(t *testing.T) {
|
||||
conn := &bufferClose{
|
||||
Buffer: &bytes.Buffer{},
|
||||
}
|
||||
s := NewCustomJSONServerCodec(conn)
|
||||
c := s.(*jsonServerCodec)
|
||||
|
||||
conn.Write([]byte(`{
|
||||
"method": "ApierV.Method",
|
||||
"params": [{
|
||||
"TPid": "test"
|
||||
}]
|
||||
}`))
|
||||
|
||||
defer s.Close()
|
||||
|
||||
rr := rpc.Request{
|
||||
ServiceMethod: "Service.Method",
|
||||
Seq: 1,
|
||||
}
|
||||
err := s.ReadRequestHeader(&rr)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
res := rpc.Response{
|
||||
Seq: 1,
|
||||
Error: "error",
|
||||
}
|
||||
err = c.WriteResponse(&res, "test")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
mp := make(map[string]any)
|
||||
err = c.dec.Decode(&mp)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if mp["error"] != "error" ||
|
||||
mp["result"] != nil ||
|
||||
mp["id"] != nil {
|
||||
t.Error("unexpected reply", mp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONCodecClose(t *testing.T) {
|
||||
conn := &bufferClose{
|
||||
Buffer: &bytes.Buffer{},
|
||||
|
||||
Reference in New Issue
Block a user