chore: 并入 iotlcm-upstream-chan 历史到 upstream-chan/(原提交 1e74497)
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
package upstreamimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/logic/upstreamimpl/dx"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model"
|
||||
)
|
||||
|
||||
func TestMapDxRegionLimitStatusByOrder(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
orderStatus string
|
||||
limitStatus string
|
||||
want string
|
||||
}{
|
||||
{name: "not ordered", orderStatus: "1", limitStatus: "0", want: "2"},
|
||||
{name: "ordered and disconnected", orderStatus: "0", limitStatus: "0", want: "1"},
|
||||
{name: "ordered and connected", orderStatus: "0", limitStatus: "1", want: "0"},
|
||||
{name: "network unknown", orderStatus: "0", limitStatus: "-", want: "2"},
|
||||
{name: "unexpected values", orderStatus: "", limitStatus: "", want: "2"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := mapDxRegionLimitStatusByOrder(test.orderStatus, test.limitStatus); got != test.want {
|
||||
t.Fatalf("mapDxRegionLimitStatusByOrder(%q, %q) = %q, want %q", test.orderStatus, test.limitStatus, got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeDxRegionLimitDetails(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
orderStatus string
|
||||
limitStatus string
|
||||
limitType string
|
||||
limitArea string
|
||||
want []string
|
||||
}{
|
||||
{name: "not ordered hides details", orderStatus: "1", limitStatus: "0", limitType: "4", limitArea: "park", want: []string{"", "", ""}},
|
||||
{name: "ordered preserves documented values", orderStatus: "0", limitStatus: "-", limitType: "4", limitArea: " park ", want: []string{"-", "4", "park"}},
|
||||
{name: "ordered rejects undocumented values", orderStatus: "0", limitStatus: "9", limitType: "0", limitArea: "", want: []string{"", "", ""}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
limitStatus, limitType, limitArea := normalizeDxRegionLimitDetails(test.orderStatus, test.limitStatus, test.limitType, test.limitArea)
|
||||
if got := []string{limitStatus, limitType, limitArea}; !reflect.DeepEqual(got, test.want) {
|
||||
t.Fatalf("normalizeDxRegionLimitDetails() = %#v, want %#v", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuerySimRegionLimitStatusUsesDocumentedParameter(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/openapi/v1/areaLimit/qryAreaLimitInfo" {
|
||||
t.Fatalf("path = %q", r.URL.Path)
|
||||
}
|
||||
if got := r.URL.Query().Get("accNum"); got != "13300000000" {
|
||||
t.Fatalf("accNum = %q", got)
|
||||
}
|
||||
if got := r.URL.Query().Get("access_number"); got != "" {
|
||||
t.Fatalf("access_number = %q, want empty", got)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"code":"0","message":"ok","data":{"orderStatus":1,"limitType":0,"limitArea":"","limitStatus":"0"}}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
sim := &sSimDX{client: dx.NewClient(dx.Config{BaseURL: server.URL, AppKey: "test", AppSecret: "secret"})}
|
||||
output, err := sim.QuerySimRegionLimitStatus(context.Background(), &model.SimRegionLimitStatusInput{SimKeys: []*model.SimKey{{MSISDN: "13300000000"}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(output) != 1 {
|
||||
t.Fatalf("results = %d", len(output))
|
||||
}
|
||||
data := output[0].ResData
|
||||
if data.RegionLimitStatus != "2" || data.OrderStatus != "1" || data.LimitStatus != "" || data.LimitType != "" || data.LimitArea != "" {
|
||||
t.Fatalf("result = %+v", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuerySimCardStatusPreservesTelecomHTTPRespCode(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/openapi/v1/prodinst/queryCardMainStatus" {
|
||||
t.Fatalf("path = %q", r.URL.Path)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(797)
|
||||
_, _ = w.Write([]byte(`{"respCode":"S00004","respMsg":"request limit exceeded"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
sim := &sSimDX{client: dx.NewClient(dx.Config{BaseURL: server.URL, AppKey: "test", AppSecret: "secret"})}
|
||||
output, err := sim.QuerySimCardStatus(context.Background(), &model.SimCardStatusInput{SimKeys: []*model.SimKey{{MSISDN: "13300000000"}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(output) != 1 {
|
||||
t.Fatalf("results = %d", len(output))
|
||||
}
|
||||
result := output[0]
|
||||
if result.Code != consts.ApiStatusError || result.ErrCode != "S00004" {
|
||||
t.Fatalf("result = %+v, want gateway code with S00004", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuerySimCardStatusPreservesTelecomBusinessCode(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"result":"11011","resultMsg":"card does not exist"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
sim := &sSimDX{client: dx.NewClient(dx.Config{BaseURL: server.URL, AppKey: "test", AppSecret: "secret"})}
|
||||
output, err := sim.QuerySimCardStatus(context.Background(), &model.SimCardStatusInput{SimKeys: []*model.SimKey{{MSISDN: "13300000000"}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(output) != 1 {
|
||||
t.Fatalf("results = %d", len(output))
|
||||
}
|
||||
result := output[0]
|
||||
if result.Code != 11011 || result.ErrCode != "11011" {
|
||||
t.Fatalf("result = %+v, want business code 11011", result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user