mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
Fixex for simulator POST/ PUT endpoints and simulator testing,
This commit is contained in:
parent
eb277fc92e
commit
1f5e64b9ea
6 changed files with 40 additions and 38 deletions
|
@ -30,7 +30,7 @@ type SimulatorResponse struct {
|
|||
ID uint `json:"id"`
|
||||
UUID string `json:"uuid"`
|
||||
Host string `json:"host"`
|
||||
ModelType string `json:"modelType"`
|
||||
Modeltype string `json:"modelType"`
|
||||
Uptime int `json:"uptime"`
|
||||
State string `json:"state"`
|
||||
StateUpdateAt string `json:"stateUpdateAt"`
|
||||
|
|
|
@ -143,7 +143,7 @@ func (self *SimulatorSerializer) Response() SimulatorResponse {
|
|||
ID: self.ID,
|
||||
UUID: self.UUID,
|
||||
Host: self.Host,
|
||||
ModelType: self.Modeltype,
|
||||
Modeltype: self.Modeltype,
|
||||
Uptime: self.Uptime,
|
||||
State: self.State,
|
||||
StateUpdateAt: self.StateUpdateAt,
|
||||
|
|
|
@ -51,10 +51,11 @@ func TestEndpoint(t *testing.T, router *gin.Engine, token string, url string, me
|
|||
}
|
||||
|
||||
assert.Equal(t, expected_code, w.Code)
|
||||
fmt.Println(w.Body.String())
|
||||
//fmt.Println("Actual:", w.Body.String())
|
||||
//fmt.Println("Expected: ", string(expected_response))
|
||||
opts := jsondiff.DefaultConsoleOptions()
|
||||
diff, _ := jsondiff.Compare(w.Body.Bytes(), expected_response, &opts)
|
||||
assert.Equal(t, diff.String(), "FullMatch")
|
||||
assert.Equal(t, "FullMatch", diff.String())
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -76,8 +76,8 @@ func addSimulator(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
var newSimulator Simulator
|
||||
err = c.BindJSON(&newSimulator)
|
||||
var newSimulatorData common.ResponseMsgSimulator
|
||||
err = c.BindJSON(&newSimulatorData)
|
||||
if err != nil {
|
||||
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
|
@ -86,6 +86,17 @@ func addSimulator(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
var newSimulator Simulator
|
||||
newSimulator.ID = newSimulatorData.Simulator.ID
|
||||
newSimulator.State = newSimulatorData.Simulator.State
|
||||
newSimulator.StateUpdateAt = newSimulatorData.Simulator.StateUpdateAt
|
||||
newSimulator.Modeltype = newSimulatorData.Simulator.Modeltype
|
||||
newSimulator.UUID = newSimulatorData.Simulator.UUID
|
||||
newSimulator.Uptime = newSimulatorData.Simulator.Uptime
|
||||
newSimulator.Host = newSimulatorData.Simulator.Host
|
||||
newSimulator.RawProperties = newSimulatorData.Simulator.RawProperties
|
||||
newSimulator.Properties = newSimulatorData.Simulator.Properties
|
||||
|
||||
err = newSimulator.save()
|
||||
if common.ProvideErrorResponse(c, err) == false {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
|
@ -115,10 +126,11 @@ func updateSimulator(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
var modifiedSimulator Simulator
|
||||
var modifiedSimulator common.ResponseMsgSimulator
|
||||
|
||||
err = c.BindJSON(&modifiedSimulator)
|
||||
if err != nil {
|
||||
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
|
||||
errormsg := "Bad request. Error unmarshalling data to JSON: " + err.Error()
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": errormsg,
|
||||
})
|
||||
|
@ -140,7 +152,7 @@ func updateSimulator(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
err = s.update(modifiedSimulator)
|
||||
err = s.update(modifiedSimulator.Simulator)
|
||||
if common.ProvideErrorResponse(c, err) == false {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "OK.",
|
||||
|
|
|
@ -25,20 +25,10 @@ func (s *Simulator) ByID(id uint) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *Simulator) update(modifiedSimulator Simulator) error {
|
||||
func (s *Simulator) update(modifiedSimulator common.SimulatorResponse) error {
|
||||
|
||||
db := common.GetDB()
|
||||
|
||||
err := db.Model(s).Updates(map[string]interface{}{
|
||||
"UUID": modifiedSimulator.UUID,
|
||||
"Host": modifiedSimulator.Host,
|
||||
"Modeltype": modifiedSimulator.Modeltype,
|
||||
"Uptime": modifiedSimulator.Uptime,
|
||||
"State": modifiedSimulator.State,
|
||||
"StateUpdateAt": modifiedSimulator.StateUpdateAt,
|
||||
"Properties": modifiedSimulator.Properties,
|
||||
"RawProperties": modifiedSimulator.RawProperties,
|
||||
}).Error
|
||||
err := db.Model(s).Updates(modifiedSimulator).Error
|
||||
|
||||
return err
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ var simulatorA = common.SimulatorResponse{
|
|||
ID: 1,
|
||||
UUID: "4854af30-325f-44a5-ad59-b67b2597de68",
|
||||
Host: "Host_A",
|
||||
ModelType: "ModelTypeA",
|
||||
Modeltype: "ModelTypeA",
|
||||
Uptime: 0,
|
||||
State: "running",
|
||||
StateUpdateAt: "placeholder",
|
||||
|
@ -72,7 +72,7 @@ var simulatorB = common.SimulatorResponse{
|
|||
ID: 2,
|
||||
UUID: "7be0322d-354e-431e-84bd-ae4c9633138b",
|
||||
Host: "Host_B",
|
||||
ModelType: "ModelTypeB",
|
||||
Modeltype: "ModelTypeB",
|
||||
Uptime: 0,
|
||||
State: "idle",
|
||||
StateUpdateAt: "placeholder",
|
||||
|
@ -105,10 +105,10 @@ var simulatorCupdated = common.Simulator{
|
|||
}
|
||||
|
||||
var simulatorC_response = common.SimulatorResponse{
|
||||
ID: 3,
|
||||
ID: simulatorC.ID,
|
||||
UUID: simulatorC.UUID,
|
||||
Host: simulatorC.Host,
|
||||
ModelType: simulatorC.Modeltype,
|
||||
Modeltype: simulatorC.Modeltype,
|
||||
Uptime: simulatorC.Uptime,
|
||||
State: simulatorC.State,
|
||||
StateUpdateAt: simulatorC.StateUpdateAt,
|
||||
|
@ -116,11 +116,15 @@ var simulatorC_response = common.SimulatorResponse{
|
|||
RawProperties: simulatorC.RawProperties,
|
||||
}
|
||||
|
||||
var simulatorC_msg = common.ResponseMsgSimulator{
|
||||
Simulator: simulatorC_response,
|
||||
}
|
||||
|
||||
var simulatorCupdated_response = common.SimulatorResponse{
|
||||
ID: simulatorCupdated.ID,
|
||||
UUID: simulatorCupdated.UUID,
|
||||
Host: simulatorCupdated.Host,
|
||||
ModelType: simulatorCupdated.Modeltype,
|
||||
Modeltype: simulatorCupdated.Modeltype,
|
||||
Uptime: simulatorCupdated.Uptime,
|
||||
State: simulatorCupdated.State,
|
||||
StateUpdateAt: simulatorCupdated.StateUpdateAt,
|
||||
|
@ -128,6 +132,10 @@ var simulatorCupdated_response = common.SimulatorResponse{
|
|||
RawProperties: simulatorCupdated.RawProperties,
|
||||
}
|
||||
|
||||
var simulatorCupdated_msg = common.ResponseMsgSimulator{
|
||||
Simulator: simulatorCupdated_response,
|
||||
}
|
||||
|
||||
var mySimulators = []common.SimulatorResponse{
|
||||
simulatorA,
|
||||
simulatorB,
|
||||
|
@ -141,10 +149,6 @@ var msgSimulator = common.ResponseMsgSimulator{
|
|||
Simulator: simulatorC_response,
|
||||
}
|
||||
|
||||
var msgSimulatorUpdated = common.ResponseMsgSimulator{
|
||||
Simulator: simulatorCupdated_response,
|
||||
}
|
||||
|
||||
// Test /simulator endpoints
|
||||
func TestSimulatorEndpoints(t *testing.T) {
|
||||
|
||||
|
@ -185,17 +189,12 @@ func TestSimulatorEndpoints(t *testing.T) {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
msgSimulatorUpdatedjson, err := json.Marshal(msgSimulatorUpdated)
|
||||
simulatorCjson, err := json.Marshal(simulatorC_msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
simulatorCjson, err := json.Marshal(simulatorC)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
simulatorCupdatedjson, err := json.Marshal(simulatorCupdated)
|
||||
simulatorCupdatedjson, err := json.Marshal(simulatorCupdated_msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -213,7 +212,7 @@ func TestSimulatorEndpoints(t *testing.T) {
|
|||
|
||||
// test PUT simulators/:SimulatorID
|
||||
common.TestEndpoint(t, router, token, "/api/simulators/3", "PUT", simulatorCupdatedjson, 200, msgOKjson)
|
||||
common.TestEndpoint(t, router, token, "/api/simulators/3", "GET", nil, 200, msgSimulatorUpdatedjson)
|
||||
common.TestEndpoint(t, router, token, "/api/simulators/3", "GET", nil, 200, simulatorCupdatedjson)
|
||||
|
||||
// test DELETE simulators/:SimulatorID
|
||||
common.TestEndpoint(t, router, token, "/api/simulators/3", "DELETE", nil, 200, msgOKjson)
|
||||
|
|
Loading…
Add table
Reference in a new issue