package upstream import ( "context" "encoding/json" "os" "strings" "testing" "time" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model" ) func TestSimCardStatusChangeProtocol(t *testing.T) { input := model.SimCardStatusChangeInput{ SimKeys: []*model.SimKey{{ChanKey: "channel-a", ICCID: "89860001"}}, ReqData: model.SimCardStatusChangeInputItem{OperType: "1"}, } body, err := json.Marshal(input) if err != nil { t.Fatal(err) } const want = `{"simKeys":[{"chanKey":"channel-a","iccid":"89860001","imsi":"","msisdn":""}],"reqData":{"operType":"1"}}` if got := string(body); got != want { t.Fatalf("request body = %s, want %s", got, want) } } func TestCurrentUsageConsumerProtocol(t *testing.T) { input := model.SimDataUsageInput{ SimKeys: []*model.SimKey{{ChanKey: "channel-a", ICCID: "89860001", IMSI: "460001", MSISDN: "13800000000"}}, } body, err := json.Marshal(input) if err != nil { t.Fatal(err) } const want = `{"simKeys":[{"chanKey":"channel-a","iccid":"89860001","imsi":"460001","msisdn":"13800000000"}],"reqData":{}}` if got := string(body); got != want { t.Fatalf("request body = %s, want %s", got, want) } client := NewWithCurrentUsageClient("iotlcmupstreamchan-http-svc:8110") if client.currentUsageClientName != "iotlcmupstreamchan-http-svc:8110" { t.Fatalf("current usage client = %q", client.currentUsageClientName) } if client.currentUsageTimeout != 0 { t.Fatalf("default current usage timeout = %s, want unset", client.currentUsageTimeout) } configured := NewWithCurrentUsageClient("iotlcmupstreamchan-http-svc:8110", 90*time.Second) if configured.currentUsageTimeout != 90*time.Second { t.Fatalf("configured current usage timeout = %s, want 90s", configured.currentUsageTimeout) } } func TestInboundBasicInfoClientCanUseDedicatedEndpoint(t *testing.T) { client := NewWithInboundBasicInfoClient("iotlcm.example.com:8110") clientName, err := client.inboundBasicInfoAddress(context.Background()) if err != nil { t.Fatal(err) } if clientName != "iotlcm.example.com:8110" { t.Fatalf("inbound basic info client = %q, want dedicated chan endpoint", clientName) } } func TestInboundBasicInfoClientCanUseEnvEndpoint(t *testing.T) { const endpoint = "iotlcm.example.com:8110" t.Setenv(inboundBasicInfoClientNameEnv, endpoint) client := New() clientName, err := client.inboundBasicInfoAddress(context.Background()) if err != nil { t.Fatal(err) } if clientName != endpoint { t.Fatalf("inbound basic info client = %q, want env endpoint", clientName) } } func TestConfiguredInboundBasicInfoClientUsesDedicatedEndpoint(t *testing.T) { if value := os.Getenv(inboundBasicInfoClientNameEnv); value != "" { t.Setenv(inboundBasicInfoClientNameEnv, "") } client := New() clientName, err := client.inboundBasicInfoAddress(context.Background()) if err != nil { t.Fatal(err) } if !strings.Contains(clientName, ":8110") { t.Fatalf("inbound basic info client = %q, want dedicated chan endpoint", clientName) } } func TestSimCardApnChangeProtocol(t *testing.T) { input := model.SimCardApnChangeInput{ SimKeys: []*model.SimKey{{ChanKey: "channel-a", ICCID: "89860001"}}, ReqData: model.SimCardApnChangeInputItem{OperType: "0", ApnName: "CMIOT"}, } body, err := json.Marshal(input) if err != nil { t.Fatal(err) } const want = `{"simKeys":[{"chanKey":"channel-a","iccid":"89860001","imsi":"","msisdn":""}],"reqData":{"operType":"0","apnName":"CMIOT"}}` if got := string(body); got != want { t.Fatalf("request body = %s, want %s", got, want) } } func TestDecodeSimCardApnChangeResponseKeepsFailedItems(t *testing.T) { body := []byte(`{"code":0,"message":"ok","data":[{"code":0,"message":"success","simKey":{"iccid":"89860001"},"resData":{"orderNum":"order-1"}},{"code":4001,"message":"denied","simKey":{"iccid":"89860002"},"resData":{"orderNum":"order-2"}}]}`) items, err := decodeResponse[model.SimCardApnChangeOutputItem](body) if err != nil { t.Fatal(err) } if len(items) != 2 { t.Fatalf("item count = %d, want 2", len(items)) } if items[1].Code != 4001 || items[1].ResData.OrderNum != "order-2" { t.Fatalf("failed item was not preserved: %+v", items[1]) } } func TestDecodeSimCardStatusChangeResponseKeepsFailedItems(t *testing.T) { body := []byte(`{"code":0,"message":"ok","data":[{"code":0,"message":"success","simKey":{"iccid":"89860001"},"resData":{"orderNum":"order-1"}},{"code":4001,"message":"denied","simKey":{"iccid":"89860002"},"resData":{"orderNum":"order-2"}}]}`) items, err := decodeResponse[model.SimCardStatusChangeOutputItem](body) if err != nil { t.Fatal(err) } if len(items) != 2 { t.Fatalf("item count = %d, want 2", len(items)) } if items[1].Code != 4001 || items[1].ResData.OrderNum != "order-2" { t.Fatalf("failed item was not preserved: %+v", items[1]) } }