/
scenario-interface.go
178 lines (141 loc) · 6.85 KB
/
scenario-interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package service
import (
"errors"
serverDomain "github.com/aaronchen2k/deeptest/cmd/server/v1/domain"
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
"github.com/aaronchen2k/deeptest/internal/pkg/domain"
"github.com/aaronchen2k/deeptest/internal/server/modules/model"
"github.com/aaronchen2k/deeptest/internal/server/modules/repo"
"github.com/jinzhu/copier"
)
type ScenarioInterfaceService struct {
EndpointInterfaceRepo *repo.EndpointInterfaceRepo `inject:""`
DebugInterfaceRepo *repo.DebugInterfaceRepo `inject:""`
ScenarioInterfaceRepo *repo.ScenarioInterfaceRepo `inject:""`
EndpointRepo *repo.EndpointRepo `inject:""`
ServeRepo *repo.ServeRepo `inject:""`
ScenarioProcessorRepo *repo.ScenarioProcessorRepo `inject:""`
ServeServerRepo *repo.ServeServerRepo `inject:""`
DiagnoseInterfaceRepo *repo.DiagnoseInterfaceRepo `inject:""`
EndpointCaseRepo *repo.EndpointCaseRepo `inject:""`
DebugInvokeRepo *repo.DebugInvokeRepo `inject:""`
ScenarioNodeService *ScenarioNodeService `inject:""`
DebugSceneService *DebugSceneService `inject:""`
DebugInterfaceService *DebugInterfaceService `inject:""`
SceneService *SceneService `inject:""`
EnvironmentService *EnvironmentService `inject:""`
DatapoolService *DatapoolService `inject:""`
EndpointCaseService *EndpointCaseService `inject:""`
}
func (s *ScenarioInterfaceService) GetDebugDataFromScenarioInterface(tenantId consts.TenantId, scenarioInterfaceId uint) (req domain.DebugData, err error) {
scenarioInterfacePo, _ := s.ScenarioInterfaceRepo.GetDetail(tenantId, scenarioInterfaceId)
if err != nil {
return
}
endpointInterface, _ := s.EndpointInterfaceRepo.Get(tenantId, scenarioInterfacePo.EndpointInterfaceId)
s.SetProps(tenantId, endpointInterface, &scenarioInterfacePo, &req)
return
}
func (s *ScenarioInterfaceService) SetProps(tenantId consts.TenantId,
endpointInterface model.EndpointInterface, scenarioInterfacePo *model.DebugInterface, debugData *domain.DebugData) {
endpoint, err := s.EndpointRepo.GetAll(tenantId, endpointInterface.EndpointId, "v0.1.0")
serve, err := s.ServeRepo.Get(tenantId, endpoint.ServeId)
if err != nil {
return
}
securities, err := s.ServeRepo.ListSecurity(tenantId, serve.ID)
if err != nil {
return
}
serve.Securities = securities
debugData.EndpointInterfaceId = endpointInterface.ID
copier.CopyWithOption(&debugData, scenarioInterfacePo, copier.Option{DeepCopy: true})
debugData.EndpointInterfaceId = endpointInterface.ID // reset
if debugData.Headers == nil {
debugData.Headers = &[]domain.Header{}
}
*debugData.Headers = append(*debugData.Headers, domain.Header{Name: "", Value: ""})
if debugData.QueryParams == nil {
debugData.QueryParams = &[]domain.Param{}
}
*debugData.QueryParams = append(*debugData.QueryParams, domain.Param{Name: "", Value: "", ParamIn: consts.ParamInQuery})
if debugData.PathParams == nil {
debugData.PathParams = &[]domain.Param{}
}
*debugData.PathParams = append(*debugData.PathParams, domain.Param{Name: "", Value: "", ParamIn: consts.ParamInPath})
if debugData.BodyFormData == nil {
debugData.BodyFormData = &[]domain.BodyFormDataItem{}
}
*debugData.BodyFormData = append(*debugData.BodyFormData, domain.BodyFormDataItem{
Name: "", Value: "", Type: consts.FormDataTypeText})
if debugData.BodyFormUrlencoded == nil {
debugData.BodyFormUrlencoded = &[]domain.BodyFormUrlEncodedItem{}
}
*debugData.BodyFormUrlencoded = append(*debugData.BodyFormUrlencoded, domain.BodyFormUrlEncodedItem{
Name: "", Value: "",
})
return
}
//func (s *ScenarioInterfaceService) GetScenarioInterface(endpointInterfaceId uint) (ret domain.DebugData, err error) {
// scenarioInterfaceId, _ := s.ScenarioInterfaceRepo.HasScenarioInterfaceRecord(endpointInterfaceId)
//
// if scenarioInterfaceId > 0 {
// ret, err = s.GetDebugDataFromScenarioInterface(scenarioInterfaceId)
// } else {
// ret, err = s.DebugInterfaceService.GetDebugDataFromEndpointInterface(endpointInterfaceId)
// if err != nil || ret.EndpointInterfaceId == 0 {
// return domain.DebugData{}, err
// }
// _, err = s.SaveDebugData(ret)
// }
//
// return
//}
func (s *ScenarioInterfaceService) SaveDebugData(tenantId consts.TenantId, req domain.DebugData) (debug model.DebugInterface, err error) {
s.DebugInterfaceService.CopyValueFromRequest(tenantId, &debug, req)
//endpointInterface, _ := s.EndpointInterfaceRepo.Get(req.EndpointInterfaceId)
//debug.EndpointId = endpointInterface.EndpointId
if req.DebugInterfaceId > 0 {
debug.ID = req.DebugInterfaceId
}
err = s.ScenarioInterfaceRepo.SaveDebugData(tenantId, &debug)
//更新执行器method
s.ScenarioProcessorRepo.UpdateMethod(tenantId, debug.ScenarioProcessorId, debug.Method)
return
}
func (s *ScenarioInterfaceService) ResetDebugData(tenantId consts.TenantId, scenarioProcessorId int, createBy uint) (
newProcessor model.Processor, err error) {
scenarioProcessor, _ := s.ScenarioProcessorRepo.Get(tenantId, uint(scenarioProcessorId))
parentProcessor, _ := s.ScenarioProcessorRepo.Get(tenantId, scenarioProcessor.ParentId)
debugInterface, _ := s.DebugInterfaceRepo.Get(tenantId, scenarioProcessor.EntityId)
if debugInterface.DiagnoseInterfaceId > 0 {
diagnoseInterface, _ := s.DiagnoseInterfaceRepo.Get(tenantId, debugInterface.DiagnoseInterfaceId)
if diagnoseInterface.Deleted {
err = errors.New("interface is deleted")
return
}
diagnoseInterfaceTo := s.DiagnoseInterfaceRepo.ToTo(tenantId, &diagnoseInterface)
newProcessor, err = s.ScenarioNodeService.createDirOrInterfaceFromDiagnose(tenantId, diagnoseInterfaceTo, parentProcessor, scenarioProcessor.Ordr)
} else if debugInterface.CaseInterfaceId > 0 {
endpointCase, _ := s.EndpointCaseRepo.Get(tenantId, debugInterface.CaseInterfaceId)
if endpointCase.Deleted {
err = errors.New("interface is deleted")
return
}
interfaceCase := serverDomain.InterfaceCase{}
copier.CopyWithOption(&interfaceCase, &endpointCase, copier.Option{IgnoreEmpty: true, DeepCopy: true})
endpointCaseTo := s.EndpointCaseService.EndpointCaseToTo(tenantId, &interfaceCase)
newProcessor, err = s.ScenarioNodeService.createDirOrInterfaceFromCase(tenantId, endpointCaseTo, parentProcessor, scenarioProcessor.Ordr)
} else if debugInterface.EndpointInterfaceId > 0 {
serveId := uint(0)
newProcessor, err = s.ScenarioNodeService.createInterfaceFromDefine(tenantId, debugInterface.EndpointInterfaceId, &serveId, createBy, parentProcessor, scenarioProcessor.Name, scenarioProcessor.Ordr)
}
if err != nil {
return
}
s.DebugInvokeRepo.ChangeProcessorOwner(tenantId, scenarioProcessor.ID, newProcessor.ID, newProcessor.EntityId, newProcessor.EndpointInterfaceId)
// must put below, since creation will use its DebugInterface
s.DebugInterfaceRepo.Delete(tenantId, scenarioProcessor.EntityId)
s.ScenarioProcessorRepo.Delete(tenantId, scenarioProcessor.ID)
return
}