/*
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 Affero 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>
*/
package config

import (
	"reflect"
	"testing"

	"github.com/cgrates/cgrates/utils"
)

func TestListenCfgloadFromJsonCfg(t *testing.T) {
	jsonCfg := &ListenJsonCfg{
		Rpc_json:     utils.StringPointer("127.0.0.1:2012"),
		Rpc_gob:      utils.StringPointer("127.0.0.1:2013"),
		Http:         utils.StringPointer("127.0.0.1:2080"),
		Rpc_json_tls: utils.StringPointer("127.0.0.1:2022"),
		Rpc_gob_tls:  utils.StringPointer("127.0.0.1:2023"),
		Http_tls:     utils.StringPointer("127.0.0.1:2280"),
	}
	expected := &ListenCfg{
		RPCJSONListen:    "127.0.0.1:2012",
		RPCGOBListen:     "127.0.0.1:2013",
		HTTPListen:       "127.0.0.1:2080",
		RPCJSONTLSListen: "127.0.0.1:2022",
		RPCGOBTLSListen:  "127.0.0.1:2023",
		HTTPTLSListen:    "127.0.0.1:2280",
	}
	jsnCfg := NewDefaultCGRConfig()
	if err := jsnCfg.listenCfg.loadFromJSONCfg(jsonCfg); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expected, jsnCfg.listenCfg) {
		t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expected), utils.ToJSON(jsnCfg.listenCfg))
	}
	jsonCfg = nil
	if err := jsnCfg.listenCfg.loadFromJSONCfg(jsonCfg); err != nil {
		t.Error(err)
	}
}

func TestListenCfgAsMapInterface(t *testing.T) {
	cfgJSONStr := `{
        "listen": {},
}`
	eMap := map[string]any{
		utils.RPCJSONListenCfg:    "127.0.0.1:2012",
		utils.RPCGOBListenCfg:     "127.0.0.1:2013",
		utils.HTTPListenCfg:       "127.0.0.1:2080",
		utils.RPCJSONTLSListenCfg: "127.0.0.1:2022",
		utils.RPCGOBTLSListenCfg:  "127.0.0.1:2023",
		utils.HTTPTLSListenCfg:    "127.0.0.1:2280",
	}
	if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgJSONStr); err != nil {
		t.Error(err)
	} else if rcv := cgrCfg.listenCfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) {
		t.Errorf("Expected %+v, received %+v", eMap, rcv)
	}
}

func TestListenCfgAsMapInterface1(t *testing.T) {
	cfgJSONStr := `{
	"listen": {
		"rpc_json": "127.0.0.1:2010",			
        "rpc_gob": "127.0.0.1:2018",			
        "rpc_json_tls" : "127.0.0.1:2025",		
        "rpc_gob_tls": "127.0.0.1:2001",		
        "http_tls": "127.0.0.1:2288",			
	}
}`
	eMap := map[string]any{
		utils.RPCJSONListenCfg:    "127.0.0.1:2010",
		utils.RPCGOBListenCfg:     "127.0.0.1:2018",
		utils.HTTPListenCfg:       "127.0.0.1:2080",
		utils.RPCJSONTLSListenCfg: "127.0.0.1:2025",
		utils.RPCGOBTLSListenCfg:  "127.0.0.1:2001",
		utils.HTTPTLSListenCfg:    "127.0.0.1:2288",
	}
	if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgJSONStr); err != nil {
		t.Error(err)
	} else if rcv := cgrCfg.listenCfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) {
		t.Errorf("Expected %+v, received %+v", eMap, rcv)
	}
}

func TestListenCfgClone(t *testing.T) {
	ban := &ListenCfg{
		RPCJSONListen:    "127.0.0.1:2012",
		RPCGOBListen:     "127.0.0.1:2013",
		HTTPListen:       "127.0.0.1:2080",
		RPCJSONTLSListen: "127.0.0.1:2022",
		RPCGOBTLSListen:  "127.0.0.1:2023",
		HTTPTLSListen:    "127.0.0.1:2280",
	}
	rcv := ban.Clone()
	if !reflect.DeepEqual(ban, rcv) {
		t.Errorf("Expected: %+v\nReceived: %+v", utils.ToJSON(ban), utils.ToJSON(rcv))
	}
	if rcv.RPCJSONListen = ""; ban.RPCJSONListen != "127.0.0.1:2012" {
		t.Errorf("Expected clone to not modify the cloned")
	}
}

func TestDiffListenJsonCfg(t *testing.T) {
	var d *ListenJsonCfg

	v1 := &ListenCfg{
		RPCJSONListen:    "localhost:8080",
		RPCGOBListen:     "localhost:8081",
		HTTPListen:       "localhost:8082",
		RPCJSONTLSListen: "localhost:8083",
		RPCGOBTLSListen:  "localhost:8084",
		HTTPTLSListen:    "localhost:8085",
	}

	v2 := &ListenCfg{
		RPCJSONListen:    "localhost:7080",
		RPCGOBListen:     "localhost:7081",
		HTTPListen:       "localhost:7082",
		RPCJSONTLSListen: "localhost:7083",
		RPCGOBTLSListen:  "localhost:7084",
		HTTPTLSListen:    "localhost:7085",
	}

	expected := &ListenJsonCfg{
		Rpc_json:     utils.StringPointer("localhost:7080"),
		Rpc_gob:      utils.StringPointer("localhost:7081"),
		Http:         utils.StringPointer("localhost:7082"),
		Rpc_json_tls: utils.StringPointer("localhost:7083"),
		Rpc_gob_tls:  utils.StringPointer("localhost:7084"),
		Http_tls:     utils.StringPointer("localhost:7085"),
	}

	rcv := diffListenJsonCfg(d, v1, v2)
	if !reflect.DeepEqual(rcv, expected) {
		t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv))
	}

	v1 = v2
	expected = &ListenJsonCfg{}

	rcv = diffListenJsonCfg(d, v1, v2)
	if !reflect.DeepEqual(rcv, expected) {
		t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv))
	}
}

func TestListenCfgCloneSection(t *testing.T) {
	lstnCfg := &ListenCfg{
		RPCJSONListen:    "localhost:8080",
		RPCGOBListen:     "localhost:8081",
		HTTPListen:       "localhost:8082",
		RPCJSONTLSListen: "localhost:8083",
		RPCGOBTLSListen:  "localhost:8084",
		HTTPTLSListen:    "localhost:8085",
	}

	exp := &ListenCfg{
		RPCJSONListen:    "localhost:8080",
		RPCGOBListen:     "localhost:8081",
		HTTPListen:       "localhost:8082",
		RPCJSONTLSListen: "localhost:8083",
		RPCGOBTLSListen:  "localhost:8084",
		HTTPTLSListen:    "localhost:8085",
	}

	rcv := lstnCfg.CloneSection()
	if !reflect.DeepEqual(rcv, exp) {
		t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(exp), utils.ToJSON(rcv))
	}
}
