chore: 并入 iotlcm-worker-svr 历史到 worker-svr/(原提交 96f0f4e)
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
|
||||
)
|
||||
|
||||
func TestExecuteValidationFailureSkipsUpstream(t *testing.T) {
|
||||
operation, err := OperationByBatchType(consts.BatchTypeSimCardStop)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task := &Task{
|
||||
Batch: Batch{BatchNo: "B-1"},
|
||||
Operation: operation,
|
||||
Details: []*Detail{
|
||||
{ID: 1, ICCID: "iccid-1", Authorized: true, Card: &Card{ID: 11, ChanKey: "channel", BossCardStatus: consts.SimBossCardStatusActivated}},
|
||||
{ID: 2, ICCID: "iccid-2", Authorized: false, Card: &Card{ID: 12, ChanKey: "channel", BossCardStatus: consts.SimBossCardStatusActivated}},
|
||||
},
|
||||
}
|
||||
repo := &fakeRepository{task: task}
|
||||
upstream := &fakeUpstream{responseError: errors.New("must not be called")}
|
||||
|
||||
if err = NewExecutor(repo, upstream).Execute(context.Background(), "B-1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if upstream.calls != 0 {
|
||||
t.Fatalf("upstream calls = %d, want 0", upstream.calls)
|
||||
}
|
||||
if !repo.validationSaved || !repo.finalized {
|
||||
t.Fatal("validation failure was not persisted and finalized")
|
||||
}
|
||||
if task.Details[0].Reason == "" || task.Details[1].Reason == "" {
|
||||
t.Fatal("every detail must receive a validation failure reason")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteMapsMixedResponseAndPreservesOrderNumber(t *testing.T) {
|
||||
operation, err := OperationByBatchType(consts.BatchTypeSimCardStart)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task := &Task{
|
||||
Batch: Batch{BatchNo: "B-2"},
|
||||
Operation: operation,
|
||||
Details: []*Detail{
|
||||
{ID: 1, ICCID: "iccid-1", MSISDN: "msisdn-1", IMSI: "imsi-1", Authorized: true, Card: &Card{ID: 11, ChanKey: "channel", MSISDN: "msisdn-1", IMSI: "imsi-1", BossCardStatus: consts.SimBossCardStatusStopped}},
|
||||
{ID: 2, ICCID: "iccid-2", MSISDN: "msisdn-2", IMSI: "imsi-2", Authorized: true, Card: &Card{ID: 12, ChanKey: "channel", MSISDN: "msisdn-2", IMSI: "imsi-2", BossCardStatus: consts.SimBossCardStatusStopped}},
|
||||
},
|
||||
}
|
||||
repo := &fakeRepository{task: task}
|
||||
upstream := &fakeUpstream{response: []model.SimBaseRes[model.SimCardStatusChangeOutputItem]{
|
||||
{Code: 0, Message: "ok", SimKey: nil, ResData: model.SimCardStatusChangeOutputItem{OrderNum: "order-1"}},
|
||||
{Code: 9001, Message: "denied", SimKey: &model.SimKey{ICCID: "iccid-2"}, ResData: model.SimCardStatusChangeOutputItem{OrderNum: "order-2"}},
|
||||
}}
|
||||
|
||||
if err = NewExecutor(repo, upstream).Execute(context.Background(), "B-2"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if upstream.input.ReqData.OperType != consts.SimCardStatusOperTypeStart {
|
||||
t.Fatalf("operType = %q", upstream.input.ReqData.OperType)
|
||||
}
|
||||
if len(repo.saved) != 2 || !repo.saved[0].Success || repo.saved[0].OrderNum != "order-1" {
|
||||
t.Fatalf("first result = %+v", repo.saved)
|
||||
}
|
||||
if repo.saved[1].Success || repo.saved[1].Code != 9001 || repo.saved[1].Message != "denied" || repo.saved[1].OrderNum != "order-2" {
|
||||
t.Fatalf("second result = %+v", repo.saved[1])
|
||||
}
|
||||
if !repo.finalized {
|
||||
t.Fatal("batch was not finalized")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperationByBatchTypeOperType(t *testing.T) {
|
||||
start, err := OperationByBatchType(consts.BatchTypeSimCardStart)
|
||||
if err != nil || start.OperType != "1" || start.TargetStatus != consts.SimBossCardStatusActivated {
|
||||
t.Fatalf("start operation = %+v, err = %v", start, err)
|
||||
}
|
||||
stop, err := OperationByBatchType(consts.BatchTypeSimCardStop)
|
||||
if err != nil || stop.OperType != "0" || stop.TargetStatus != consts.SimBossCardStatusStopped {
|
||||
t.Fatalf("stop operation = %+v, err = %v", stop, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDetailPopulatesCardFieldsOnStatusFailure(t *testing.T) {
|
||||
t.Skip("TODO: 恢复卡状态限制后启用此测试")
|
||||
operation, err := OperationByBatchType(consts.BatchTypeSimCardStart)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
detail := &Detail{
|
||||
ID: 1,
|
||||
RowNo: 1,
|
||||
ICCID: "iccid-1",
|
||||
Card: &Card{
|
||||
ID: 11,
|
||||
ICCID: "iccid-1",
|
||||
MSISDN: "msisdn-1",
|
||||
IMSI: "imsi-1",
|
||||
ChanKey: "channel",
|
||||
BossCardStatus: consts.SimBossCardStatusActivated,
|
||||
},
|
||||
}
|
||||
validateDetail(operation, detail)
|
||||
if detail.Reason == "" {
|
||||
t.Fatal("expected validation failure reason")
|
||||
}
|
||||
if detail.MSISDN != "msisdn-1" || detail.IMSI != "imsi-1" || detail.ChanKey != "channel" {
|
||||
t.Fatalf("card fields not populated on status failure: %+v", detail)
|
||||
}
|
||||
if detail.StatusBefore != consts.SimBossCardStatusActivated {
|
||||
t.Fatalf("StatusBefore = %d, want %d", detail.StatusBefore, consts.SimBossCardStatusActivated)
|
||||
}
|
||||
snapshot := detail.Snapshot()
|
||||
if snapshot.Get("MSISDN").String() != "msisdn-1" || snapshot.Get("IMSI").String() != "imsi-1" || snapshot.Get("chanKey").String() != "channel" {
|
||||
t.Fatalf("snapshot missing card fields: %s", snapshot.MustToJsonString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDetailDetectsMSISDNMismatch(t *testing.T) {
|
||||
operation, err := OperationByBatchType(consts.BatchTypeSimCardStart)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
detail := &Detail{
|
||||
ID: 1,
|
||||
ICCID: "iccid-1",
|
||||
Authorized: true,
|
||||
InputMSISDN: "msisdn-input",
|
||||
Card: &Card{
|
||||
ID: 11,
|
||||
ICCID: "iccid-1",
|
||||
MSISDN: "msisdn-card",
|
||||
IMSI: "imsi-1",
|
||||
ChanKey: "channel",
|
||||
BossCardStatus: consts.SimBossCardStatusStopped,
|
||||
},
|
||||
}
|
||||
validateDetail(operation, detail)
|
||||
if detail.Reason != "MSISDN与卡档案不一致" {
|
||||
t.Fatalf("unexpected reason: %q", detail.Reason)
|
||||
}
|
||||
if detail.MSISDN != "msisdn-card" || detail.InputMSISDN != "msisdn-input" {
|
||||
t.Fatalf("mismatch not preserved: MSISDN=%q InputMSISDN=%q", detail.MSISDN, detail.InputMSISDN)
|
||||
}
|
||||
snapshot := detail.Snapshot()
|
||||
if snapshot.Get("inputMsisdn").String() != "msisdn-input" || snapshot.Get("MSISDN").String() != "msisdn-card" {
|
||||
t.Fatalf("snapshot mismatch fields wrong: %s", snapshot.MustToJsonString())
|
||||
}
|
||||
}
|
||||
|
||||
type fakeRepository struct {
|
||||
task *Task
|
||||
validationSaved bool
|
||||
finalized bool
|
||||
saved []Outcome
|
||||
}
|
||||
|
||||
func (r *fakeRepository) Load(context.Context, string) (*Task, error) { return r.task, nil }
|
||||
|
||||
func (r *fakeRepository) SaveValidationFailure(_ context.Context, task *Task) error {
|
||||
r.validationSaved = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *fakeRepository) SaveResult(_ context.Context, _ *Task, detail *Detail, outcome Outcome) (Outcome, error) {
|
||||
detail.ApplyOutcome(outcome)
|
||||
r.saved = append(r.saved, outcome)
|
||||
return outcome, nil
|
||||
}
|
||||
|
||||
func (r *fakeRepository) Finalize(context.Context, *Task) error {
|
||||
r.finalized = true
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeUpstream struct {
|
||||
calls int
|
||||
input *model.SimCardStatusChangeInput
|
||||
response []model.SimBaseRes[model.SimCardStatusChangeOutputItem]
|
||||
responseError error
|
||||
}
|
||||
|
||||
func (u *fakeUpstream) ChangeSimStatus(_ context.Context, input *model.SimCardStatusChangeInput) ([]model.SimBaseRes[model.SimCardStatusChangeOutputItem], error) {
|
||||
u.calls++
|
||||
u.input = input
|
||||
return u.response, u.responseError
|
||||
}
|
||||
|
||||
func TestValidateDetailCollectsIndependentDiagnostics(t *testing.T) {
|
||||
detail := &Detail{ICCID: "card", Authorized: true, InputMSISDN: "wrong-phone", InputIMSI: "wrong-imsi", Card: &Card{MSISDN: "phone", IMSI: "imsi"}}
|
||||
validateDetail(Operation{}, detail)
|
||||
want := "ICCID缺少通道标识;MSISDN与卡档案不一致;IMSI与卡档案不一致"
|
||||
if detail.Reason != want {
|
||||
t.Fatalf("独立校验原因丢失: got=%q want=%q", detail.Reason, want)
|
||||
}
|
||||
unauthorized := &Detail{ICCID: "card", InputMSISDN: "wrong", Card: &Card{MSISDN: "phone"}}
|
||||
validateDetail(Operation{}, unauthorized)
|
||||
if unauthorized.Reason != "ICCID不在当前操作员授权范围内" {
|
||||
t.Fatalf("越权卡不应返回其他数据差异: %q", unauthorized.Reason)
|
||||
}
|
||||
existing := &Detail{Reason: "原始输入格式错误", Authorized: true, Card: &Card{}}
|
||||
validateDetail(Operation{}, existing)
|
||||
if existing.Reason != "原始输入格式错误" {
|
||||
t.Fatal("原始原因被覆盖")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user