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"`
|
ID uint `json:"id"`
|
||||||
UUID string `json:"uuid"`
|
UUID string `json:"uuid"`
|
||||||
Host string `json:"host"`
|
Host string `json:"host"`
|
||||||
ModelType string `json:"modelType"`
|
Modeltype string `json:"modelType"`
|
||||||
Uptime int `json:"uptime"`
|
Uptime int `json:"uptime"`
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
StateUpdateAt string `json:"stateUpdateAt"`
|
StateUpdateAt string `json:"stateUpdateAt"`
|
||||||
|
|
|
@ -143,7 +143,7 @@ func (self *SimulatorSerializer) Response() SimulatorResponse {
|
||||||
ID: self.ID,
|
ID: self.ID,
|
||||||
UUID: self.UUID,
|
UUID: self.UUID,
|
||||||
Host: self.Host,
|
Host: self.Host,
|
||||||
ModelType: self.Modeltype,
|
Modeltype: self.Modeltype,
|
||||||
Uptime: self.Uptime,
|
Uptime: self.Uptime,
|
||||||
State: self.State,
|
State: self.State,
|
||||||
StateUpdateAt: self.StateUpdateAt,
|
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)
|
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()
|
opts := jsondiff.DefaultConsoleOptions()
|
||||||
diff, _ := jsondiff.Compare(w.Body.Bytes(), expected_response, &opts)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var newSimulator Simulator
|
var newSimulatorData common.ResponseMsgSimulator
|
||||||
err = c.BindJSON(&newSimulator)
|
err = c.BindJSON(&newSimulatorData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
|
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
@ -86,6 +86,17 @@ func addSimulator(c *gin.Context) {
|
||||||
return
|
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()
|
err = newSimulator.save()
|
||||||
if common.ProvideErrorResponse(c, err) == false {
|
if common.ProvideErrorResponse(c, err) == false {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
@ -115,10 +126,11 @@ func updateSimulator(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var modifiedSimulator Simulator
|
var modifiedSimulator common.ResponseMsgSimulator
|
||||||
|
|
||||||
err = c.BindJSON(&modifiedSimulator)
|
err = c.BindJSON(&modifiedSimulator)
|
||||||
if err != nil {
|
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{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
"error": errormsg,
|
"error": errormsg,
|
||||||
})
|
})
|
||||||
|
@ -140,7 +152,7 @@ func updateSimulator(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.update(modifiedSimulator)
|
err = s.update(modifiedSimulator.Simulator)
|
||||||
if common.ProvideErrorResponse(c, err) == false {
|
if common.ProvideErrorResponse(c, err) == false {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "OK.",
|
"message": "OK.",
|
||||||
|
|
|
@ -25,20 +25,10 @@ func (s *Simulator) ByID(id uint) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Simulator) update(modifiedSimulator Simulator) error {
|
func (s *Simulator) update(modifiedSimulator common.SimulatorResponse) error {
|
||||||
|
|
||||||
db := common.GetDB()
|
db := common.GetDB()
|
||||||
|
err := db.Model(s).Updates(modifiedSimulator).Error
|
||||||
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
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ var simulatorA = common.SimulatorResponse{
|
||||||
ID: 1,
|
ID: 1,
|
||||||
UUID: "4854af30-325f-44a5-ad59-b67b2597de68",
|
UUID: "4854af30-325f-44a5-ad59-b67b2597de68",
|
||||||
Host: "Host_A",
|
Host: "Host_A",
|
||||||
ModelType: "ModelTypeA",
|
Modeltype: "ModelTypeA",
|
||||||
Uptime: 0,
|
Uptime: 0,
|
||||||
State: "running",
|
State: "running",
|
||||||
StateUpdateAt: "placeholder",
|
StateUpdateAt: "placeholder",
|
||||||
|
@ -72,7 +72,7 @@ var simulatorB = common.SimulatorResponse{
|
||||||
ID: 2,
|
ID: 2,
|
||||||
UUID: "7be0322d-354e-431e-84bd-ae4c9633138b",
|
UUID: "7be0322d-354e-431e-84bd-ae4c9633138b",
|
||||||
Host: "Host_B",
|
Host: "Host_B",
|
||||||
ModelType: "ModelTypeB",
|
Modeltype: "ModelTypeB",
|
||||||
Uptime: 0,
|
Uptime: 0,
|
||||||
State: "idle",
|
State: "idle",
|
||||||
StateUpdateAt: "placeholder",
|
StateUpdateAt: "placeholder",
|
||||||
|
@ -105,10 +105,10 @@ var simulatorCupdated = common.Simulator{
|
||||||
}
|
}
|
||||||
|
|
||||||
var simulatorC_response = common.SimulatorResponse{
|
var simulatorC_response = common.SimulatorResponse{
|
||||||
ID: 3,
|
ID: simulatorC.ID,
|
||||||
UUID: simulatorC.UUID,
|
UUID: simulatorC.UUID,
|
||||||
Host: simulatorC.Host,
|
Host: simulatorC.Host,
|
||||||
ModelType: simulatorC.Modeltype,
|
Modeltype: simulatorC.Modeltype,
|
||||||
Uptime: simulatorC.Uptime,
|
Uptime: simulatorC.Uptime,
|
||||||
State: simulatorC.State,
|
State: simulatorC.State,
|
||||||
StateUpdateAt: simulatorC.StateUpdateAt,
|
StateUpdateAt: simulatorC.StateUpdateAt,
|
||||||
|
@ -116,11 +116,15 @@ var simulatorC_response = common.SimulatorResponse{
|
||||||
RawProperties: simulatorC.RawProperties,
|
RawProperties: simulatorC.RawProperties,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var simulatorC_msg = common.ResponseMsgSimulator{
|
||||||
|
Simulator: simulatorC_response,
|
||||||
|
}
|
||||||
|
|
||||||
var simulatorCupdated_response = common.SimulatorResponse{
|
var simulatorCupdated_response = common.SimulatorResponse{
|
||||||
ID: simulatorCupdated.ID,
|
ID: simulatorCupdated.ID,
|
||||||
UUID: simulatorCupdated.UUID,
|
UUID: simulatorCupdated.UUID,
|
||||||
Host: simulatorCupdated.Host,
|
Host: simulatorCupdated.Host,
|
||||||
ModelType: simulatorCupdated.Modeltype,
|
Modeltype: simulatorCupdated.Modeltype,
|
||||||
Uptime: simulatorCupdated.Uptime,
|
Uptime: simulatorCupdated.Uptime,
|
||||||
State: simulatorCupdated.State,
|
State: simulatorCupdated.State,
|
||||||
StateUpdateAt: simulatorCupdated.StateUpdateAt,
|
StateUpdateAt: simulatorCupdated.StateUpdateAt,
|
||||||
|
@ -128,6 +132,10 @@ var simulatorCupdated_response = common.SimulatorResponse{
|
||||||
RawProperties: simulatorCupdated.RawProperties,
|
RawProperties: simulatorCupdated.RawProperties,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var simulatorCupdated_msg = common.ResponseMsgSimulator{
|
||||||
|
Simulator: simulatorCupdated_response,
|
||||||
|
}
|
||||||
|
|
||||||
var mySimulators = []common.SimulatorResponse{
|
var mySimulators = []common.SimulatorResponse{
|
||||||
simulatorA,
|
simulatorA,
|
||||||
simulatorB,
|
simulatorB,
|
||||||
|
@ -141,10 +149,6 @@ var msgSimulator = common.ResponseMsgSimulator{
|
||||||
Simulator: simulatorC_response,
|
Simulator: simulatorC_response,
|
||||||
}
|
}
|
||||||
|
|
||||||
var msgSimulatorUpdated = common.ResponseMsgSimulator{
|
|
||||||
Simulator: simulatorCupdated_response,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test /simulator endpoints
|
// Test /simulator endpoints
|
||||||
func TestSimulatorEndpoints(t *testing.T) {
|
func TestSimulatorEndpoints(t *testing.T) {
|
||||||
|
|
||||||
|
@ -185,17 +189,12 @@ func TestSimulatorEndpoints(t *testing.T) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
msgSimulatorUpdatedjson, err := json.Marshal(msgSimulatorUpdated)
|
simulatorCjson, err := json.Marshal(simulatorC_msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
simulatorCjson, err := json.Marshal(simulatorC)
|
simulatorCupdatedjson, err := json.Marshal(simulatorCupdated_msg)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
simulatorCupdatedjson, err := json.Marshal(simulatorCupdated)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -213,7 +212,7 @@ func TestSimulatorEndpoints(t *testing.T) {
|
||||||
|
|
||||||
// test PUT simulators/:SimulatorID
|
// test PUT simulators/:SimulatorID
|
||||||
common.TestEndpoint(t, router, token, "/api/simulators/3", "PUT", simulatorCupdatedjson, 200, msgOKjson)
|
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
|
// test DELETE simulators/:SimulatorID
|
||||||
common.TestEndpoint(t, router, token, "/api/simulators/3", "DELETE", nil, 200, msgOKjson)
|
common.TestEndpoint(t, router, token, "/api/simulators/3", "DELETE", nil, 200, msgOKjson)
|
||||||
|
|
Loading…
Add table
Reference in a new issue