diff --git a/common/database.go b/common/database.go index b412623..e3c4456 100644 --- a/common/database.go +++ b/common/database.go @@ -1,13 +1,10 @@ package common import ( - "encoding/json" "flag" "fmt" "github.com/jinzhu/gorm" - "github.com/jinzhu/gorm/dialects/postgres" _ "github.com/jinzhu/gorm/dialects/postgres" - "golang.org/x/crypto/bcrypt" "log" ) @@ -99,28 +96,30 @@ func DummyPopulateDB(test_db *gorm.DB) { MigrateModels(test_db) - // Create two entries of each model + // Create entries of each model (data defined in testdata.go) - propertiesA := json.RawMessage(`{"name" : "TestNameA", "category" : "CategoryA", "location" : "anywhere on earth", "type": "dummy"}`) - propertiesB := json.RawMessage(`{"name" : "TestNameB", "category" : "CategoryB", "location" : "where ever you want", "type": "generic"}`) - simr_A := Simulator{UUID: "4854af30-325f-44a5-ad59-b67b2597de68", Host: "Host_A", State: "running", Modeltype: "ModelTypeA", StateUpdateAt: "placeholder", Properties: postgres.Jsonb{propertiesA}, RawProperties: postgres.Jsonb{propertiesA}} - simr_B := Simulator{UUID: "7be0322d-354e-431e-84bd-ae4c9633138b", Host: "Host_B", State: "idle", Modeltype: "ModelTypeB", StateUpdateAt: "placeholder", Properties: postgres.Jsonb{propertiesB}, RawProperties: postgres.Jsonb{propertiesB}} - checkErr(test_db.Create(&simr_A).Error) - checkErr(test_db.Create(&simr_B).Error) + // Users + checkErr(test_db.Create(&User0).Error) // Admin + checkErr(test_db.Create(&UserA).Error) // Normal User + checkErr(test_db.Create(&UserB).Error) // Normal User - outSig_A := Signal{Name: "outSignal_A", Direction: "out", Index: 0, Unit: "V"} - outSig_B := Signal{Name: "outSignal_B", Direction: "out", Index: 1, Unit: "V"} - inSig_A := Signal{Name: "inSignal_A", Direction: "in", Index: 0, Unit: "A"} - inSig_B := Signal{Name: "inSignal_B", Direction: "in", Index: 1, Unit: "A"} - checkErr(test_db.Create(&outSig_A).Error) - checkErr(test_db.Create(&outSig_B).Error) - checkErr(test_db.Create(&inSig_A).Error) - checkErr(test_db.Create(&inSig_B).Error) + // Simulators + checkErr(test_db.Create(&SimulatorA).Error) + checkErr(test_db.Create(&SimulatorB).Error) - mo_A := SimulationModel{Name: "SimulationModel_A"} - mo_B := SimulationModel{Name: "SimulationModel_B"} - checkErr(test_db.Create(&mo_A).Error) - checkErr(test_db.Create(&mo_B).Error) + // Scenarios + checkErr(test_db.Create(&ScenarioA).Error) + checkErr(test_db.Create(&ScenarioB).Error) + + // Signals + checkErr(test_db.Create(&OutSignalA).Error) + checkErr(test_db.Create(&OutSignalB).Error) + checkErr(test_db.Create(&InSignalA).Error) + checkErr(test_db.Create(&InSignalB).Error) + + // Simulation Models + checkErr(test_db.Create(&SimulationModelA).Error) + checkErr(test_db.Create(&SimulationModelB).Error) file_A := File{Name: "File_A"} file_B := File{Name: "File_B"} @@ -131,33 +130,6 @@ func DummyPopulateDB(test_db *gorm.DB) { checkErr(test_db.Create(&file_C).Error) checkErr(test_db.Create(&file_D).Error) - so_A := Scenario{Name: "Scenario_A"} - so_B := Scenario{Name: "Scenario_B"} - checkErr(test_db.Create(&so_A).Error) - checkErr(test_db.Create(&so_B).Error) - - // Hash passwords with bcrypt algorithm - var bcryptCost = 10 - - pw_0, err := - bcrypt.GenerateFromPassword([]byte("xyz789"), bcryptCost) - checkErr(err) - - pw_A, err := - bcrypt.GenerateFromPassword([]byte("abc123"), bcryptCost) - checkErr(err) - - pw_B, err := - bcrypt.GenerateFromPassword([]byte("bcd234"), bcryptCost) - checkErr(err) - - usr_0 := User{Username: "User_0", Password: string(pw_0), Role: "Admin"} - usr_A := User{Username: "User_A", Password: string(pw_A), Role: "User"} - usr_B := User{Username: "User_B", Password: string(pw_B), Role: "User"} - checkErr(test_db.Create(&usr_0).Error) - checkErr(test_db.Create(&usr_A).Error) - checkErr(test_db.Create(&usr_B).Error) - dab_A := Dashboard{Name: "Dashboard_A"} dab_B := Dashboard{Name: "Dashboard_B"} checkErr(test_db.Create(&dab_A).Error) @@ -173,41 +145,40 @@ func DummyPopulateDB(test_db *gorm.DB) { // For `has many` use the models with id=1 and id=2 // User HM Scenarios, Scenario HM Users (Many-to-Many) - checkErr(test_db.Model(&so_A).Association("Users").Append(&usr_A).Error) - checkErr(test_db.Model(&so_A).Association("Users").Append(&usr_B).Error) - checkErr(test_db.Model(&so_B).Association("Users").Append(&usr_A).Error) - checkErr(test_db.Model(&so_B).Association("Users").Append(&usr_B).Error) + checkErr(test_db.Model(&ScenarioA).Association("Users").Append(&UserA).Error) + checkErr(test_db.Model(&ScenarioA).Association("Users").Append(&UserB).Error) + checkErr(test_db.Model(&ScenarioB).Association("Users").Append(&UserA).Error) + checkErr(test_db.Model(&ScenarioB).Association("Users").Append(&UserB).Error) // Scenario HM SimulationModels - checkErr(test_db.Model(&so_A).Association("SimulationModels").Append(&mo_A).Error) - checkErr(test_db.Model(&so_A).Association("SimulationModels").Append(&mo_B).Error) + checkErr(test_db.Model(&ScenarioA).Association("SimulationModels").Append(&SimulationModelA).Error) + checkErr(test_db.Model(&ScenarioA).Association("SimulationModels").Append(&SimulationModelB).Error) // Scenario HM Dashboards - checkErr(test_db.Model(&so_A).Association("Dashboards").Append(&dab_A).Error) - checkErr(test_db.Model(&so_A).Association("Dashboards").Append(&dab_B).Error) + checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&dab_A).Error) + checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&dab_B).Error) // Dashboard HM Widget checkErr(test_db.Model(&dab_A).Association("Widgets").Append(&widg_A).Error) checkErr(test_db.Model(&dab_A).Association("Widgets").Append(&widg_B).Error) // SimulationModel HM Signals - checkErr(test_db.Model(&mo_A).Association("InputMapping").Append(&inSig_A).Error) - checkErr(test_db.Model(&mo_A).Association("InputMapping").Append(&inSig_B).Error) - checkErr(test_db.Model(&mo_A).Association("OutputMapping").Append(&outSig_A).Error) - checkErr(test_db.Model(&mo_A).Association("OutputMapping").Append(&outSig_B).Error) + checkErr(test_db.Model(&SimulationModelA).Association("InputMapping").Append(&InSignalA).Error) + checkErr(test_db.Model(&SimulationModelA).Association("InputMapping").Append(&InSignalB).Error) + checkErr(test_db.Model(&SimulationModelA).Association("OutputMapping").Append(&OutSignalA).Error) + checkErr(test_db.Model(&SimulationModelA).Association("OutputMapping").Append(&OutSignalB).Error) // SimulationModel HM Files - checkErr(test_db.Model(&mo_A).Association("Files").Append(&file_C).Error) - checkErr(test_db.Model(&mo_A).Association("Files").Append(&file_D).Error) + checkErr(test_db.Model(&SimulationModelA).Association("Files").Append(&file_C).Error) + checkErr(test_db.Model(&SimulationModelA).Association("Files").Append(&file_D).Error) // Simulator HM SimulationModels - checkErr(test_db.Model(&simr_A).Association("SimulationModels").Append(&mo_A).Error) - checkErr(test_db.Model(&simr_A).Association("SimulationModels").Append(&mo_B).Error) + checkErr(test_db.Model(&SimulatorA).Association("SimulationModels").Append(&SimulationModelA).Error) + checkErr(test_db.Model(&SimulatorA).Association("SimulationModels").Append(&SimulationModelB).Error) // Widget HM Files checkErr(test_db.Model(&widg_A).Association("Files").Append(&file_A).Error) checkErr(test_db.Model(&widg_A).Association("Files").Append(&file_B).Error) - } // Erase tables and glose the testdb diff --git a/common/models.go b/common/models.go index aa43f09..aa3c618 100644 --- a/common/models.go +++ b/common/models.go @@ -26,8 +26,8 @@ type Scenario struct { Name string `gorm:"not null"` // Running state of scenario Running bool `gorm:"default:false"` - // Start parameters of scenario as JSON string - StartParameters string + // Start parameters of scenario as JSON + StartParameters postgres.Jsonb // Users that have access to the scenario Users []*User `gorm:"not null;many2many:user_scenarios"` // SimulationModels that belong to the scenario @@ -46,8 +46,8 @@ type SimulationModel struct { OutputLength int `gorm:"default:1"` // Number of input signals InputLength int `gorm:"default:1"` - // Start parameters of simulation model as JSON string - StartParameters string + // Start parameters of simulation model as JSON + StartParameters postgres.Jsonb // ID of Scenario to which simulation model belongs ScenarioID uint // ID of simulator associated with simulation model @@ -169,3 +169,8 @@ type File struct { // TODO Add file itself here?? FileData []byte `gorm:"column:FileData"` } + +type credentials struct { + Username string `json:"username"` + Password string `json:"password"` +} diff --git a/common/responses.go b/common/responses.go index b61c542..42cc0ea 100644 --- a/common/responses.go +++ b/common/responses.go @@ -10,20 +10,20 @@ type UserResponse struct { } type ScenarioResponse struct { - Name string `json:"Name"` - ID uint `json:"ID"` - Running bool `json:"Running"` - StartParams string `json:"Starting Parameters"` + Name string `json:"name"` + ID uint `json:"id"` + Running bool `json:"running"` + StartParameters postgres.Jsonb `json:"startParameters"` } type SimulationModelResponse struct { - ID uint `json:"ID"` - Name string `json:"Name"` - OutputLength int `json:"OutputLength"` - InputLength int `json:"InputLength"` - ScenarioID uint `json:"ScenarioID"` - SimulatorID uint `json:"SimulatorID"` - StartParams string `json:"StartParams"` + ID uint `json:"id"` + Name string `json:"name"` + OutputLength int `json:"outputLength"` + InputLength int `json:"inputLength"` + ScenarioID uint `json:"scenarioID"` + SimulatorID uint `json:"simulatorID"` + StartParameters postgres.Jsonb `json:"startParameters"` } type SimulatorResponse struct { @@ -74,11 +74,11 @@ type FileResponse struct { } type SignalResponse struct { - Name string `json:"Name"` - Unit string `json:"Unit"` - Index uint `json:"Index"` - Direction string `json:"Direction"` - SimulationModelID uint `json:"SimulationModelID"` + Name string `json:"name"` + Unit string `json:"unit"` + Index uint `json:"index"` + Direction string `json:"direction"` + SimulationModelID uint `json:"simulationModelID"` } // Response messages diff --git a/common/serializers.go b/common/serializers.go index 24ee87f..e923cfe 100644 --- a/common/serializers.go +++ b/common/serializers.go @@ -74,10 +74,10 @@ type ScenarioSerializer struct { func (self *ScenarioSerializer) Response() ScenarioResponse { response := ScenarioResponse{ - Name: self.Name, - ID: self.ID, - Running: self.Running, - StartParams: self.StartParameters, + Name: self.Name, + ID: self.ID, + Running: self.Running, + StartParameters: self.StartParameters, } return response } @@ -105,13 +105,13 @@ type SimulationModelSerializer struct { func (self *SimulationModelSerializer) Response() SimulationModelResponse { response := SimulationModelResponse{ - ID: self.ID, - Name: self.Name, - OutputLength: self.OutputLength, - InputLength: self.InputLength, - ScenarioID: self.ScenarioID, - SimulatorID: self.SimulatorID, - StartParams: self.StartParameters, + ID: self.ID, + Name: self.Name, + OutputLength: self.OutputLength, + InputLength: self.InputLength, + ScenarioID: self.ScenarioID, + SimulatorID: self.SimulatorID, + StartParameters: self.StartParameters, } return response } diff --git a/common/testdata.go b/common/testdata.go new file mode 100644 index 0000000..4c4edc8 --- /dev/null +++ b/common/testdata.go @@ -0,0 +1,340 @@ +package common + +import ( + "encoding/json" + "github.com/jinzhu/gorm/dialects/postgres" + "golang.org/x/crypto/bcrypt" + "time" +) + +// Generic + +var MsgOK = ResponseMsg{ + Message: "OK.", +} + +// Users + +// Hash passwords with bcrypt algorithm +var bcryptCost = 10 +var pw0, _ = bcrypt.GenerateFromPassword([]byte("xyz789"), bcryptCost) +var pwA, _ = bcrypt.GenerateFromPassword([]byte("abc123"), bcryptCost) +var pwB, _ = bcrypt.GenerateFromPassword([]byte("bcd234"), bcryptCost) +var User0 = User{ID: 1, Username: "User_0", Password: string(pw0), Role: "Admin", Mail: "User_0@example.com"} +var User0_response = UserResponse{Username: User0.Username, Role: User0.Role, ID: User0.ID, Mail: User0.Mail} +var UserA = User{ID: 2, Username: "User_A", Password: string(pwA), Role: "User", Mail: "User_A@example.com"} +var UserA_response = UserResponse{Username: UserA.Username, Role: UserA.Role, ID: UserA.ID, Mail: UserA.Mail} +var UserB = User{ID: 3, Username: "User_B", Password: string(pwB), Role: "User", Mail: "User_B@example.com"} +var UserB_response = UserResponse{Username: UserB.Username, Role: UserB.Role, ID: UserB.ID, Mail: UserB.Mail} + +// Credentials + +var CredAdmin = credentials{ + Username: User0.Username, + Password: "xyz789", +} + +var CredUser = credentials{ + Username: UserA.Username, + Password: "abc123", +} + +// Simulators + +var propertiesA = json.RawMessage(`{"name" : "TestNameA", "category" : "CategoryA", "location" : "anywhere on earth", "type": "dummy"}`) +var propertiesB = json.RawMessage(`{"name" : "TestNameB", "category" : "CategoryB", "location" : "where ever you want", "type": "generic"}`) +var propertiesC = json.RawMessage(`{"name" : "TestNameC", "category" : "CategoryC", "location" : "my desk", "type": "blubb"}`) +var propertiesCupdated = json.RawMessage(`{"name" : "TestNameCUpdate", "category" : "CategoryC", "location" : "my desk", "type": "blubb"}`) + +var SimulatorA = Simulator{ + ID: 1, + UUID: "4854af30-325f-44a5-ad59-b67b2597de68", + Host: "Host_A", + Modeltype: "ModelTypeA", + Uptime: 0, + State: "running", + StateUpdateAt: time.Now().String(), + Properties: postgres.Jsonb{propertiesA}, + RawProperties: postgres.Jsonb{propertiesA}, +} + +var SimulatorA_response = SimulatorResponse{ + ID: SimulatorA.ID, + UUID: SimulatorA.UUID, + Host: SimulatorA.Host, + Modeltype: SimulatorA.Modeltype, + Uptime: SimulatorA.Uptime, + State: SimulatorA.State, + StateUpdateAt: SimulatorA.StateUpdateAt, + Properties: SimulatorA.Properties, + RawProperties: SimulatorA.RawProperties, +} + +var SimulatorB = Simulator{ + ID: 2, + UUID: "7be0322d-354e-431e-84bd-ae4c9633138b", + Host: "Host_B", + Modeltype: "ModelTypeB", + Uptime: 0, + State: "idle", + StateUpdateAt: time.Now().String(), + Properties: postgres.Jsonb{propertiesB}, + RawProperties: postgres.Jsonb{propertiesB}, +} + +var SimulatorB_response = SimulatorResponse{ + ID: SimulatorB.ID, + UUID: SimulatorB.UUID, + Host: SimulatorB.Host, + Modeltype: SimulatorB.Modeltype, + Uptime: SimulatorB.Uptime, + State: SimulatorB.State, + StateUpdateAt: SimulatorB.StateUpdateAt, + Properties: SimulatorB.Properties, + RawProperties: SimulatorB.RawProperties, +} + +var SimulatorC = Simulator{ + ID: 3, + UUID: "6d9776bf-b693-45e8-97b6-4c13d151043f", + Host: "Host_C", + Modeltype: "ModelTypeC", + Uptime: 0, + State: "idle", + StateUpdateAt: time.Now().String(), + Properties: postgres.Jsonb{propertiesC}, + RawProperties: postgres.Jsonb{propertiesC}, +} + +var SimulatorC_response = SimulatorResponse{ + ID: SimulatorC.ID, + UUID: SimulatorC.UUID, + Host: SimulatorC.Host, + Modeltype: SimulatorC.Modeltype, + Uptime: SimulatorC.Uptime, + State: SimulatorC.State, + StateUpdateAt: SimulatorC.StateUpdateAt, + Properties: SimulatorC.Properties, + RawProperties: SimulatorC.RawProperties, +} + +var SimulatorCUpdated = Simulator{ + ID: SimulatorC.ID, + UUID: SimulatorC.UUID, + Host: "Host_Cupdated", + Modeltype: "ModelTypeCUpdated", + Uptime: SimulatorC.Uptime, + State: "running", + StateUpdateAt: time.Now().String(), + Properties: postgres.Jsonb{propertiesCupdated}, + RawProperties: postgres.Jsonb{propertiesCupdated}, +} + +var SimulatorCUpdated_response = SimulatorResponse{ + ID: SimulatorCUpdated.ID, + UUID: SimulatorCUpdated.UUID, + Host: SimulatorCUpdated.Host, + Modeltype: SimulatorCUpdated.Modeltype, + Uptime: SimulatorCUpdated.Uptime, + State: SimulatorCUpdated.State, + StateUpdateAt: SimulatorCUpdated.StateUpdateAt, + Properties: SimulatorCUpdated.Properties, + RawProperties: SimulatorCUpdated.RawProperties, +} + +// Scenarios + +var startParametersA = json.RawMessage(`{"parameter1" : "testValue1A", "parameter2" : "testValue2A", "parameter3" : 42}`) +var startParametersB = json.RawMessage(`{"parameter1" : "testValue1B", "parameter2" : "testValue2B", "parameter3" : 43}`) +var startParametersC = json.RawMessage(`{"parameter1" : "testValue1C", "parameter2" : "testValue2C", "parameter3" : 44}`) + +var ScenarioA = Scenario{ID: 1, Name: "Scenario_A", Running: true, StartParameters: postgres.Jsonb{startParametersA}} +var ScenarioA_response = ScenarioResponse{ID: ScenarioA.ID, Name: ScenarioA.Name, Running: ScenarioA.Running, StartParameters: ScenarioA.StartParameters} +var ScenarioB = Scenario{ID: 2, Name: "Scenario_B", Running: false, StartParameters: postgres.Jsonb{startParametersB}} +var ScenarioB_response = ScenarioResponse{ID: ScenarioB.ID, Name: ScenarioB.Name, Running: ScenarioB.Running, StartParameters: ScenarioB.StartParameters} +var ScenarioC = Scenario{ID: 3, Name: "Scenario_C", Running: false, StartParameters: postgres.Jsonb{startParametersC}} +var ScenarioC_response = ScenarioResponse{ID: ScenarioC.ID, Name: ScenarioC.Name, Running: ScenarioC.Running, StartParameters: ScenarioC.StartParameters} +var ScenarioCUpdated = Scenario{ID: ScenarioC.ID, Name: "Scenario_Cupdated", Running: true, StartParameters: postgres.Jsonb{startParametersC}} +var ScenarioCUpdated_response = ScenarioResponse{ID: ScenarioCUpdated.ID, Name: ScenarioCUpdated.Name, Running: ScenarioCUpdated.Running, StartParameters: ScenarioCUpdated.StartParameters} + +// Simulation Models + +var SimulationModelA = SimulationModel{ + ID: 1, + Name: "SimulationModel_A", + OutputLength: 1, + InputLength: 1, + ScenarioID: 1, + SimulatorID: 1, + StartParameters: postgres.Jsonb{startParametersA}, +} + +var SimulationModelA_response = SimulationModelResponse{ + ID: SimulationModelA.ID, + Name: SimulationModelA.Name, + InputLength: SimulationModelA.InputLength, + OutputLength: SimulationModelA.OutputLength, + ScenarioID: SimulationModelA.ScenarioID, + SimulatorID: SimulationModelA.SimulatorID, + StartParameters: SimulationModelA.StartParameters, +} + +var SimulationModelB = SimulationModel{ + ID: 2, + Name: "SimulationModel_B", + OutputLength: 1, + InputLength: 1, + ScenarioID: 1, + SimulatorID: 1, + StartParameters: postgres.Jsonb{startParametersB}, +} + +var SimulationModelB_response = SimulationModelResponse{ + ID: SimulationModelB.ID, + Name: SimulationModelB.Name, + InputLength: SimulationModelB.InputLength, + OutputLength: SimulationModelB.OutputLength, + ScenarioID: SimulationModelB.ScenarioID, + SimulatorID: SimulationModelB.SimulatorID, + StartParameters: SimulationModelB.StartParameters, +} + +var SimulationModelC = SimulationModel{ + ID: 3, + Name: "SimulationModel_C", + OutputLength: 1, + InputLength: 1, + ScenarioID: 1, + SimulatorID: 1, + StartParameters: postgres.Jsonb{startParametersC}, +} + +var SimulationModelC_response = SimulationModelResponse{ + ID: SimulationModelC.ID, + Name: SimulationModelC.Name, + InputLength: SimulationModelC.InputLength, + OutputLength: SimulationModelC.OutputLength, + ScenarioID: SimulationModelC.ScenarioID, + SimulatorID: SimulationModelC.SimulatorID, + StartParameters: SimulationModelC.StartParameters, +} + +var SimulationModelCUpdated = SimulationModel{ + ID: SimulationModelC.ID, + Name: "SimulationModel_CUpdated", + OutputLength: SimulationModelC.OutputLength, + InputLength: SimulationModelC.InputLength, + ScenarioID: SimulationModelC.ScenarioID, + SimulatorID: 2, + StartParameters: SimulationModelC.StartParameters, + InputMapping: SimulationModelC.InputMapping, + OutputMapping: SimulationModelC.OutputMapping, +} + +var SimulationModelCUpdated_response = SimulationModelResponse{ + ID: SimulationModelCUpdated.ID, + Name: SimulationModelCUpdated.Name, + InputLength: SimulationModelCUpdated.InputLength, + OutputLength: SimulationModelCUpdated.OutputLength, + ScenarioID: SimulationModelCUpdated.ScenarioID, + SimulatorID: SimulationModelCUpdated.SimulatorID, + StartParameters: SimulationModelCUpdated.StartParameters, +} + +// Signals + +var OutSignalA = Signal{ + Name: "outSignal_A", + Direction: "out", + Index: 0, + Unit: "V", + SimulationModelID: 1, +} + +var OutSignalA_response = SignalResponse{ + Name: OutSignalA.Name, + Direction: OutSignalA.Direction, + Index: OutSignalA.Index, + Unit: OutSignalA.Unit, + SimulationModelID: OutSignalA.SimulationModelID, +} + +var OutSignalB = Signal{ + Name: "outSignal_B", + Direction: "out", + Index: 1, + Unit: "V", + SimulationModelID: 1, +} + +var OutSignalB_response = SignalResponse{ + Name: OutSignalB.Name, + Direction: OutSignalB.Direction, + Index: OutSignalB.Index, + Unit: OutSignalB.Unit, + SimulationModelID: OutSignalB.SimulationModelID, +} + +var InSignalA = Signal{ + Name: "inSignal_A", + Direction: "in", + Index: 0, + Unit: "A", + SimulationModelID: 1, +} + +var InSignalA_response = SignalResponse{ + Name: InSignalA.Name, + Direction: InSignalA.Direction, + Index: InSignalA.Index, + Unit: InSignalA.Unit, + SimulationModelID: InSignalA.SimulationModelID, +} + +var InSignalB = Signal{ + Name: "inSignal_B", + Direction: "in", + Index: 1, + Unit: "A", + SimulationModelID: 1, +} + +var InSignalB_response = SignalResponse{ + Name: InSignalB.Name, + Direction: InSignalB.Direction, + Index: InSignalB.Index, + Unit: InSignalB.Unit, + SimulationModelID: InSignalB.SimulationModelID, +} + +var InSignalC = Signal{ + Name: "inSignal_C", + Direction: "in", + Index: 2, + Unit: "A", + SimulationModelID: 1, +} + +var InSignalC_response = SignalResponse{ + Name: InSignalC.Name, + Direction: InSignalC.Direction, + Index: InSignalC.Index, + Unit: InSignalC.Unit, + SimulationModelID: InSignalC.SimulationModelID, +} + +var InSignalCUpdated = Signal{ + Name: "inSignalupdated_C", + Direction: InSignalC.Direction, + Index: InSignalC.Index, + Unit: "Ohm", + SimulationModelID: InSignalC.SimulationModelID, +} + +var InSignalCUpdated_response = SignalResponse{ + Name: InSignalCUpdated.Name, + Direction: InSignalCUpdated.Direction, + Index: InSignalCUpdated.Index, + Unit: InSignalCUpdated.Unit, + SimulationModelID: InSignalCUpdated.SimulationModelID, +} diff --git a/routes/scenario/scenario_endpoints.go b/routes/scenario/scenario_endpoints.go index b1dab79..4be99fc 100644 --- a/routes/scenario/scenario_endpoints.go +++ b/routes/scenario/scenario_endpoints.go @@ -76,7 +76,7 @@ func getScenarios(c *gin.Context) { // @Accept json // @Produce json // @Tags scenarios -// @Param inputScenario body common.ScenarioResponse true "Scenario to be added" +// @Param inputScenario body common.ResponseMsgScenario true "Scenario to be added" // @Success 200 "OK." // @Failure 401 "Unauthorized Access" // @Failure 403 "Access forbidden." @@ -98,8 +98,8 @@ func addScenario(c *gin.Context) { return } - var so Scenario - err = c.BindJSON(&so) + var newScenarioData common.ResponseMsgScenario + err = c.BindJSON(&newScenarioData) if err != nil { errormsg := "Bad request. Error binding form data to JSON: " + err.Error() c.JSON(http.StatusBadRequest, gin.H{ @@ -108,14 +108,20 @@ func addScenario(c *gin.Context) { return } + var newScenario Scenario + newScenario.ID = newScenarioData.Scenario.ID + newScenario.StartParameters = newScenarioData.Scenario.StartParameters + newScenario.Running = newScenarioData.Scenario.Running + newScenario.Name = newScenarioData.Scenario.Name + // save new scenario to DB - err = so.save() + err = newScenario.save() if common.ProvideErrorResponse(c, err) { return } // add user to new scenario - err = so.addUser(&(u.User)) + err = newScenario.addUser(&(u.User)) if common.ProvideErrorResponse(c, err) == false { c.JSON(http.StatusOK, gin.H{ "message": "OK.", @@ -129,7 +135,7 @@ func addScenario(c *gin.Context) { // @Tags scenarios // @Accept json // @Produce json -// @Param inputScenario body common.ScenarioResponse true "Scenario to be updated" +// @Param inputScenario body common.ResponseMsgScenario true "Scenario to be updated" // @Success 200 "OK." // @Failure 401 "Unauthorized Access" // @Failure 403 "Access forbidden." @@ -144,8 +150,8 @@ func updateScenario(c *gin.Context) { return } - var modifiedSo Scenario - err := c.BindJSON(&modifiedSo) + var modifiedScenarioData common.ResponseMsgScenario + err := c.BindJSON(&modifiedScenarioData) if err != nil { errormsg := "Bad request. Error binding form data to JSON: " + err.Error() c.JSON(http.StatusBadRequest, gin.H{ @@ -154,7 +160,7 @@ func updateScenario(c *gin.Context) { return } - err = so.update(modifiedSo) + err = so.update(modifiedScenarioData.Scenario) if common.ProvideErrorResponse(c, err) == false { c.JSON(http.StatusOK, gin.H{ "message": "OK.", diff --git a/routes/scenario/scenario_methods.go b/routes/scenario/scenario_methods.go index 745cb7e..650c691 100644 --- a/routes/scenario/scenario_methods.go +++ b/routes/scenario/scenario_methods.go @@ -33,7 +33,7 @@ func (s *Scenario) save() error { return err } -func (s *Scenario) update(modifiedScenario Scenario) error { +func (s *Scenario) update(modifiedScenario common.ScenarioResponse) error { db := common.GetDB() err := db.Model(s).Update(modifiedScenario).Error return err diff --git a/routes/scenario/scenario_test.go b/routes/scenario/scenario_test.go index 98b85eb..36259d3 100644 --- a/routes/scenario/scenario_test.go +++ b/routes/scenario/scenario_test.go @@ -3,106 +3,30 @@ package scenario import ( "encoding/json" "fmt" + "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" - "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user" - - "github.com/gin-gonic/gin" - "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user" ) -var token string - -type credentials struct { - Username string `json:"username"` - Password string `json:"password"` -} - -var cred = credentials{ - Username: "User_A", - Password: "abc123", -} - -var msgOK = common.ResponseMsg{ - Message: "OK.", -} - -var user_A = common.UserResponse{ - Username: "User_A", - Role: "User", - Mail: "", - ID: 2, -} - -var user_B = common.UserResponse{ - Username: "User_B", - Role: "User", - Mail: "", - ID: 3, -} - -var myUsers = []common.UserResponse{ - user_A, - user_B, -} - -var myUserA = []common.UserResponse{ - user_A, -} - -var msgUsers = common.ResponseMsgUsers{ - Users: myUsers, -} - -var msgUserA = common.ResponseMsgUsers{ - Users: myUserA, -} - -var scenarioA = common.ScenarioResponse{ - Name: "Scenario_A", - ID: 1, - Running: false, -} - -var scenarioB = common.ScenarioResponse{ - Name: "Scenario_B", - ID: 2, - Running: false, -} - -var scenarioC = common.Scenario{ - Name: "Scenario_C", - Running: false, - StartParameters: "test", -} - -var scenarioC_response = common.ScenarioResponse{ - ID: 3, - Name: scenarioC.Name, - Running: scenarioC.Running, - StartParams: scenarioC.StartParameters, -} - -var myScenarios = []common.ScenarioResponse{ - scenarioA, - scenarioB, -} - -var msgScenarios = common.ResponseMsgScenarios{ - Scenarios: myScenarios, -} - -var msgScenario = common.ResponseMsgScenario{ - Scenario: scenarioC_response, -} - // Test /scenarios endpoints func TestScenarioEndpoints(t *testing.T) { + var token string + + var myUsers = []common.UserResponse{common.UserA_response, common.UserB_response} + var myUserA = []common.UserResponse{common.UserA_response} + var msgUsers = common.ResponseMsgUsers{Users: myUsers} + var msgUserA = common.ResponseMsgUsers{Users: myUserA} + var myScenarios = []common.ScenarioResponse{common.ScenarioA_response, common.ScenarioB_response} + var msgScenarios = common.ResponseMsgScenarios{Scenarios: myScenarios} + var msgScenario = common.ResponseMsgScenario{Scenario: common.ScenarioC_response} + var msgScenarioUpdated = common.ResponseMsgScenario{Scenario: common.ScenarioCUpdated_response} + db := common.DummyInitDB() defer db.Close() common.DummyPopulateDB(db) @@ -118,37 +42,14 @@ func TestScenarioEndpoints(t *testing.T) { RegisterScenarioEndpoints(api.Group("/scenarios")) - credjson, err := json.Marshal(cred) + credjson, _ := json.Marshal(common.CredUser) + msgOKjson, _ := json.Marshal(common.MsgOK) + msgScenariosjson, _ := json.Marshal(msgScenarios) + msgScenariojson, _ := json.Marshal(msgScenario) + msgScenarioUpdatedjson, _ := json.Marshal(msgScenarioUpdated) - msgOKjson, err := json.Marshal(msgOK) - if err != nil { - panic(err) - } - - msgUsersjson, err := json.Marshal(msgUsers) - if err != nil { - panic(err) - } - - msgUserAjson, err := json.Marshal(msgUserA) - if err != nil { - panic(err) - } - - msgScenariosjson, err := json.Marshal(msgScenarios) - if err != nil { - panic(err) - } - - msgScenariojson, err := json.Marshal(msgScenario) - if err != nil { - panic(err) - } - - scenarioCjson, err := json.Marshal(scenarioC) - if err != nil { - panic(err) - } + msgUsersjson, _ := json.Marshal(msgUsers) + msgUserAjson, _ := json.Marshal(msgUserA) token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200) @@ -156,11 +57,15 @@ func TestScenarioEndpoints(t *testing.T) { common.TestEndpoint(t, router, token, "/api/scenarios", "GET", nil, 200, msgScenariosjson) // test POST scenarios/ - common.TestEndpoint(t, router, token, "/api/scenarios", "POST", scenarioCjson, 200, msgOKjson) + common.TestEndpoint(t, router, token, "/api/scenarios", "POST", msgScenariojson, 200, msgOKjson) // test GET scenarios/:ScenarioID common.TestEndpoint(t, router, token, "/api/scenarios/3", "GET", nil, 200, msgScenariojson) + // test PUT scenarios/:ScenarioID + common.TestEndpoint(t, router, token, "/api/scenarios/3", "PUT", msgScenarioUpdatedjson, 200, msgOKjson) + common.TestEndpoint(t, router, token, "/api/scenarios/3", "GET", nil, 200, msgScenarioUpdatedjson) + // test DELETE scenarios/:ScenarioID common.TestEndpoint(t, router, token, "/api/scenarios/3", "DELETE", nil, 200, msgOKjson) common.TestEndpoint(t, router, token, "/api/scenarios", "GET", nil, 200, msgScenariosjson) diff --git a/routes/signal/signal_endpoints.go b/routes/signal/signal_endpoints.go index 29df23a..e077baa 100644 --- a/routes/signal/signal_endpoints.go +++ b/routes/signal/signal_endpoints.go @@ -70,7 +70,7 @@ func getSignals(c *gin.Context) { // @Accept json // @Produce json // @Tags signals -// @Param inputSignal body common.Signal true "A signal to be added to the model incl. direction and model ID to which signal shall be added" +// @Param inputSignal body common.ResponseMsgSignal true "A signal to be added to the model incl. direction and model ID to which signal shall be added" // @Success 200 "OK." // @Failure 401 "Unauthorized Access" // @Failure 403 "Access forbidden." @@ -79,8 +79,8 @@ func getSignals(c *gin.Context) { // @Router /signals [post] func addSignal(c *gin.Context) { - var newSignal Signal - err := c.BindJSON(&newSignal) + var newSignalData common.ResponseMsgSignal + err := c.BindJSON(&newSignalData) if err != nil { errormsg := "Bad request. Error binding form data to JSON: " + err.Error() c.JSON(http.StatusBadRequest, gin.H{ @@ -89,6 +89,13 @@ func addSignal(c *gin.Context) { return } + var newSignal Signal + newSignal.Index = newSignalData.Signal.Index + newSignal.SimulationModelID = newSignalData.Signal.SimulationModelID + newSignal.Direction = newSignalData.Signal.Direction + newSignal.Unit = newSignalData.Signal.Unit + newSignal.Name = newSignalData.Signal.Name + ok, _ := simulationmodel.CheckPermissions(c, common.Update, "body", int(newSignal.SimulationModelID)) if !ok { return @@ -121,7 +128,7 @@ func updateSignal(c *gin.Context) { return } - var modifiedSignal Signal + var modifiedSignal common.ResponseMsgSignal err := c.BindJSON(&modifiedSignal) if err != nil { errormsg := "Bad request. Error binding form data to JSON: " + err.Error() @@ -131,7 +138,7 @@ func updateSignal(c *gin.Context) { return } - err = sig.update(modifiedSignal) + err = sig.update(modifiedSignal.Signal) if common.ProvideErrorResponse(c, err) == false { c.JSON(http.StatusOK, gin.H{ "message": "OK.", diff --git a/routes/signal/signal_methods.go b/routes/signal/signal_methods.go index ffab8ff..980e96d 100644 --- a/routes/signal/signal_methods.go +++ b/routes/signal/signal_methods.go @@ -63,7 +63,7 @@ func (s *Signal) addToSimulationModel() error { return err } -func (s *Signal) update(modifiedSignal Signal) error { +func (s *Signal) update(modifiedSignal common.SignalResponse) error { db := common.GetDB() err := db.Model(s).Updates(map[string]interface{}{ diff --git a/routes/signal/signal_test.go b/routes/signal/signal_test.go index aaa5c40..6ed0c7f 100644 --- a/routes/signal/signal_test.go +++ b/routes/signal/signal_test.go @@ -10,107 +10,18 @@ import ( "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user" ) -var token string - -type credentials struct { - Username string `json:"username"` - Password string `json:"password"` -} - -var cred = credentials{ - Username: "User_A", - Password: "abc123", -} - -var msgOK = common.ResponseMsg{ - Message: "OK.", -} - -var inSignalA = common.SignalResponse{ - Name: "inSignal_A", - Direction: "in", - Index: 0, - Unit: "A", - SimulationModelID: 1, -} - -var inSignalB = common.SignalResponse{ - Name: "inSignal_B", - Direction: "in", - Index: 1, - Unit: "A", - SimulationModelID: 1, -} - -var inSignalC = common.SignalResponse{ - Name: "inSignal_C", - Direction: "in", - Index: 2, - Unit: "A", - SimulationModelID: 1, -} - -var inSignalCupdated = common.Signal{ - Name: "inSignalupdated_C", - Direction: "in", - Index: 2, - Unit: "Ohm", - SimulationModelID: 1, -} - -var inSignalCupdatedResp = common.SignalResponse{ - Name: inSignalCupdated.Name, - Direction: inSignalCupdated.Direction, - Index: inSignalCupdated.Index, - Unit: inSignalCupdated.Unit, - SimulationModelID: inSignalCupdated.SimulationModelID, -} - -var outSignalA = common.SignalResponse{ - Name: "outSignal_A", - Direction: "out", - Index: 0, - Unit: "V", - SimulationModelID: 1, -} - -var outSignalB = common.SignalResponse{ - Name: "outSignal_B", - Direction: "out", - Index: 1, - Unit: "V", - SimulationModelID: 1, -} - -var myInSignals = []common.SignalResponse{ - inSignalA, - inSignalB, -} - -var myOutSignals = []common.SignalResponse{ - outSignalA, - outSignalB, -} - -var msgInSignals = common.ResponseMsgSignals{ - Signals: myInSignals, -} - -var msgInSignalCupdated = common.ResponseMsgSignal{ - Signal: inSignalCupdatedResp, -} - -var msgOutSignals = common.ResponseMsgSignals{ - Signals: myOutSignals, -} - -var msgInSignalC = common.ResponseMsgSignal{ - Signal: inSignalC, -} - // Test /models endpoints func TestSignalEndpoints(t *testing.T) { + var token string + + var myInSignals = []common.SignalResponse{common.InSignalA_response, common.InSignalB_response} + var myOutSignals = []common.SignalResponse{common.OutSignalA_response, common.OutSignalB_response} + var msgInSignals = common.ResponseMsgSignals{Signals: myInSignals} + var msgInSignalC = common.ResponseMsgSignal{Signal: common.InSignalC_response} + var msgInSignalCupdated = common.ResponseMsgSignal{Signal: common.InSignalCUpdated_response} + var msgOutSignals = common.ResponseMsgSignals{Signals: myOutSignals} + db := common.DummyInitDB() defer db.Close() common.DummyPopulateDB(db) @@ -126,39 +37,12 @@ func TestSignalEndpoints(t *testing.T) { RegisterSignalEndpoints(api.Group("/signals")) - credjson, err := json.Marshal(cred) - if err != nil { - panic(err) - } - - msgOKjson, err := json.Marshal(msgOK) - if err != nil { - panic(err) - } - - msgInSignalsjson, err := json.Marshal(msgInSignals) - if err != nil { - panic(err) - } - - msgOutSignalsjson, err := json.Marshal(msgOutSignals) - if err != nil { - panic(err) - } - - inSignalCjson, err := json.Marshal(inSignalC) - if err != nil { - panic(err) - } - - msgInSignalCjson, err := json.Marshal(msgInSignalC) - if err != nil { - panic(err) - } - - msgInSignalCupdatedjson, err := json.Marshal(msgInSignalCupdated) - - inSignalCupdatedjson, err := json.Marshal(inSignalCupdated) + credjson, _ := json.Marshal(common.CredUser) + msgOKjson, _ := json.Marshal(common.MsgOK) + msgInSignalsjson, _ := json.Marshal(msgInSignals) + msgOutSignalsjson, _ := json.Marshal(msgOutSignals) + inSignalCjson, _ := json.Marshal(msgInSignalC) + inSignalCupdatedjson, _ := json.Marshal(msgInSignalCupdated) token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200) @@ -170,11 +54,11 @@ func TestSignalEndpoints(t *testing.T) { common.TestEndpoint(t, router, token, "/api/signals", "POST", inSignalCjson, 200, msgOKjson) // test GET signals/:signalID - common.TestEndpoint(t, router, token, "/api/signals/5", "GET", nil, 200, msgInSignalCjson) + common.TestEndpoint(t, router, token, "/api/signals/5", "GET", nil, 200, inSignalCjson) // test PUT signals/:signalID common.TestEndpoint(t, router, token, "/api/signals/5", "PUT", inSignalCupdatedjson, 200, msgOKjson) - common.TestEndpoint(t, router, token, "/api/signals/5", "GET", nil, 200, msgInSignalCupdatedjson) + common.TestEndpoint(t, router, token, "/api/signals/5", "GET", nil, 200, inSignalCupdatedjson) // test DELETE signals/:signalID common.TestEndpoint(t, router, token, "/api/signals/5", "DELETE", nil, 200, msgOKjson) diff --git a/routes/simulationmodel/simulationmodel_endpoints.go b/routes/simulationmodel/simulationmodel_endpoints.go index ad08d4a..2bb5565 100644 --- a/routes/simulationmodel/simulationmodel_endpoints.go +++ b/routes/simulationmodel/simulationmodel_endpoints.go @@ -55,7 +55,7 @@ func getSimulationModels(c *gin.Context) { // @Accept json // @Produce json // @Tags models -// @Param inputSimulationModel body common.SimulationModelResponse true "Simulation model to be added incl. IDs of scenario and simulator" +// @Param inputSimulationModel body common.ResponseMsgSimulationModel true "Simulation model to be added incl. IDs of scenario and simulator" // @Success 200 "OK." // @Failure 401 "Unauthorized Access" // @Failure 403 "Access forbidden." @@ -64,8 +64,8 @@ func getSimulationModels(c *gin.Context) { // @Router /models [post] func addSimulationModel(c *gin.Context) { - var newModel SimulationModel - err := c.BindJSON(&newModel) + var newModelData common.ResponseMsgSimulationModel + err := c.BindJSON(&newModelData) if err != nil { errormsg := "Bad request. Error binding form data to JSON: " + err.Error() c.JSON(http.StatusBadRequest, gin.H{ @@ -74,6 +74,15 @@ func addSimulationModel(c *gin.Context) { return } + var newModel SimulationModel + newModel.ID = newModelData.SimulationModel.ID + newModel.Name = newModelData.SimulationModel.Name + newModel.SimulatorID = newModelData.SimulationModel.SimulatorID + newModel.ScenarioID = newModelData.SimulationModel.ScenarioID + newModel.StartParameters = newModelData.SimulationModel.StartParameters + newModel.OutputLength = 0 + newModel.InputLength = 0 + ok, _ := scenario.CheckPermissions(c, common.Create, "body", int(newModel.ScenarioID)) if !ok { return @@ -93,7 +102,7 @@ func addSimulationModel(c *gin.Context) { // @Tags models // @Accept json // @Produce json -// @Param inputSimulationModel body common.SimulationModelResponse true "Simulation model to be updated" +// @Param inputSimulationModel body common.ResponseMsgSimulationModel true "Simulation model to be updated" // @Success 200 "OK." // @Failure 401 "Unauthorized Access" // @Failure 403 "Access forbidden." @@ -108,7 +117,7 @@ func updateSimulationModel(c *gin.Context) { return } - var modifiedModel SimulationModel + var modifiedModel common.ResponseMsgSimulationModel err := c.BindJSON(&modifiedModel) if err != nil { errormsg := "Bad request. Error binding form data to JSON: " + err.Error() @@ -118,7 +127,7 @@ func updateSimulationModel(c *gin.Context) { return } - err = m.Update(modifiedModel) + err = m.Update(modifiedModel.SimulationModel) if common.ProvideErrorResponse(c, err) == false { c.JSON(http.StatusOK, gin.H{ "message": "OK.", diff --git a/routes/simulationmodel/simulationmodel_methods.go b/routes/simulationmodel/simulationmodel_methods.go index 9564a0c..295376b 100644 --- a/routes/simulationmodel/simulationmodel_methods.go +++ b/routes/simulationmodel/simulationmodel_methods.go @@ -55,7 +55,7 @@ func (m *SimulationModel) addToScenario() error { return err } -func (m *SimulationModel) Update(modifiedSimulationModel SimulationModel) error { +func (m *SimulationModel) Update(modifiedSimulationModel common.SimulationModelResponse) error { db := common.GetDB() if m.SimulatorID != modifiedSimulationModel.SimulatorID { diff --git a/routes/simulationmodel/simulationmodel_test.go b/routes/simulationmodel/simulationmodel_test.go index bede18f..9ca0797 100644 --- a/routes/simulationmodel/simulationmodel_test.go +++ b/routes/simulationmodel/simulationmodel_test.go @@ -10,106 +10,16 @@ import ( "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user" ) -var token string - -type credentials struct { - Username string `json:"username"` - Password string `json:"password"` -} - -var cred = credentials{ - Username: "User_A", - Password: "abc123", -} - -var msgOK = common.ResponseMsg{ - Message: "OK.", -} - -var modelA = common.SimulationModelResponse{ - ID: 1, - Name: "SimulationModel_A", - OutputLength: 1, - InputLength: 1, - ScenarioID: 1, - SimulatorID: 1, - StartParams: "", -} - -var modelB = common.SimulationModelResponse{ - ID: 2, - Name: "SimulationModel_B", - OutputLength: 1, - InputLength: 1, - ScenarioID: 1, - SimulatorID: 1, - StartParams: "", -} - -var modelC = common.SimulationModel{ - ID: 3, - Name: "SimulationModel_C", - OutputLength: 1, - InputLength: 1, - ScenarioID: 1, - SimulatorID: 1, - StartParameters: "test", - InputMapping: nil, - OutputMapping: nil, -} - -var modelCupdated = common.SimulationModel{ - ID: modelC.ID, - Name: "SimulationModel_CUpdated", - OutputLength: modelC.OutputLength, - InputLength: modelC.InputLength, - ScenarioID: modelC.ScenarioID, - SimulatorID: 2, - StartParameters: modelC.StartParameters, - InputMapping: modelC.InputMapping, - OutputMapping: modelC.OutputMapping, -} - -var modelC_response = common.SimulationModelResponse{ - ID: modelC.ID, - Name: modelC.Name, - InputLength: modelC.InputLength, - OutputLength: modelC.OutputLength, - ScenarioID: modelC.ScenarioID, - SimulatorID: modelC.SimulatorID, - StartParams: modelC.StartParameters, -} - -var modelC_responseUpdated = common.SimulationModelResponse{ - ID: modelC.ID, - Name: modelCupdated.Name, - InputLength: modelC.InputLength, - OutputLength: modelC.OutputLength, - ScenarioID: modelC.ScenarioID, - SimulatorID: modelCupdated.SimulatorID, - StartParams: modelC.StartParameters, -} - -var myModels = []common.SimulationModelResponse{ - modelA, - modelB, -} - -var msgModels = common.ResponseMsgSimulationModels{ - SimulationModels: myModels, -} - -var msgModel = common.ResponseMsgSimulationModel{ - SimulationModel: modelC_response, -} - -var msgModelupdated = common.ResponseMsgSimulationModel{ - SimulationModel: modelC_responseUpdated, -} - // Test /models endpoints func TestSimulationModelEndpoints(t *testing.T) { + var token string + + var myModels = []common.SimulationModelResponse{common.SimulationModelA_response, common.SimulationModelB_response} + var msgModels = common.ResponseMsgSimulationModels{SimulationModels: myModels} + var msgModel = common.ResponseMsgSimulationModel{SimulationModel: common.SimulationModelC_response} + var msgModelupdated = common.ResponseMsgSimulationModel{SimulationModel: common.SimulationModelCUpdated_response} + db := common.DummyInitDB() defer db.Close() common.DummyPopulateDB(db) @@ -125,40 +35,11 @@ func TestSimulationModelEndpoints(t *testing.T) { RegisterSimulationModelEndpoints(api.Group("/models")) - credjson, err := json.Marshal(cred) - if err != nil { - panic(err) - } - - msgOKjson, err := json.Marshal(msgOK) - if err != nil { - panic(err) - } - - msgModelsjson, err := json.Marshal(msgModels) - if err != nil { - panic(err) - } - - msgModeljson, err := json.Marshal(msgModel) - if err != nil { - panic(err) - } - - msgModelupdatedjson, err := json.Marshal(msgModelupdated) - if err != nil { - panic(err) - } - - modelCjson, err := json.Marshal(modelC) - if err != nil { - panic(err) - } - - modelCupdatedjson, err := json.Marshal(modelCupdated) - if err != nil { - panic(err) - } + credjson, _ := json.Marshal(common.CredUser) + msgOKjson, _ := json.Marshal(common.MsgOK) + msgModelsjson, _ := json.Marshal(msgModels) + msgModeljson, _ := json.Marshal(msgModel) + msgModelupdatedjson, _ := json.Marshal(msgModelupdated) token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200) @@ -166,13 +47,13 @@ func TestSimulationModelEndpoints(t *testing.T) { common.TestEndpoint(t, router, token, "/api/models?scenarioID=1", "GET", nil, 200, msgModelsjson) // test POST models - common.TestEndpoint(t, router, token, "/api/models", "POST", modelCjson, 200, msgOKjson) + common.TestEndpoint(t, router, token, "/api/models", "POST", msgModeljson, 200, msgOKjson) // test GET models/:ModelID to check if previous POST worked correctly common.TestEndpoint(t, router, token, "/api/models/3", "GET", nil, 200, msgModeljson) // test PUT models/:ModelID - common.TestEndpoint(t, router, token, "/api/models/3", "PUT", modelCupdatedjson, 200, msgOKjson) + common.TestEndpoint(t, router, token, "/api/models/3", "PUT", msgModelupdatedjson, 200, msgOKjson) common.TestEndpoint(t, router, token, "/api/models/3", "GET", nil, 200, msgModelupdatedjson) // test DELETE models/:ModelID diff --git a/routes/simulator/simulator_test.go b/routes/simulator/simulator_test.go index 6333eb7..b47e4a9 100644 --- a/routes/simulator/simulator_test.go +++ b/routes/simulator/simulator_test.go @@ -2,7 +2,6 @@ package simulator import ( "encoding/json" - "github.com/jinzhu/gorm/dialects/postgres" "testing" "github.com/gin-gonic/gin" @@ -11,147 +10,18 @@ import ( "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user" ) -var token string - -type credentials struct { - Username string `json:"username"` - Password string `json:"password"` -} - -var cred = credentials{ - Username: "User_0", - Password: "xyz789", -} - -var msgOK = common.ResponseMsg{ - Message: "OK.", -} - -var model_A = common.SimulationModelResponse{ - ID: 1, - Name: "SimulationModel_A", - OutputLength: 1, - InputLength: 1, - ScenarioID: 1, - SimulatorID: 1, - StartParams: "", -} - -var model_B = common.SimulationModelResponse{ - ID: 2, - Name: "SimulationModel_B", - OutputLength: 1, - InputLength: 1, - ScenarioID: 1, - SimulatorID: 1, - StartParams: "", -} - -var myModels = []common.SimulationModelResponse{ - model_A, - model_B, -} - -var msgModels = common.ResponseMsgSimulationModels{ - SimulationModels: myModels, -} - -var simulatorA = common.SimulatorResponse{ - ID: 1, - UUID: "4854af30-325f-44a5-ad59-b67b2597de68", - Host: "Host_A", - Modeltype: "ModelTypeA", - Uptime: 0, - State: "running", - StateUpdateAt: "placeholder", - Properties: postgres.Jsonb{json.RawMessage(`{"name" : "TestNameA", "category" : "CategoryA", "location" : "anywhere on earth", "type": "dummy"}`)}, - RawProperties: postgres.Jsonb{json.RawMessage(`{"name" : "TestNameA", "category" : "CategoryA", "location" : "anywhere on earth", "type": "dummy"}`)}, -} - -var simulatorB = common.SimulatorResponse{ - ID: 2, - UUID: "7be0322d-354e-431e-84bd-ae4c9633138b", - Host: "Host_B", - Modeltype: "ModelTypeB", - Uptime: 0, - State: "idle", - StateUpdateAt: "placeholder", - Properties: postgres.Jsonb{json.RawMessage(`{"name" : "TestNameB", "category" : "CategoryB", "location" : "where ever you want", "type": "generic"}`)}, - RawProperties: postgres.Jsonb{json.RawMessage(`{"name" : "TestNameB", "category" : "CategoryB", "location" : "where ever you want", "type": "generic"}`)}, -} - -var simulatorC = common.Simulator{ - ID: 3, - UUID: "6d9776bf-b693-45e8-97b6-4c13d151043f", - Host: "Host_C", - Modeltype: "ModelTypeC", - Uptime: 0, - State: "idle", - StateUpdateAt: "placeholder", - Properties: postgres.Jsonb{json.RawMessage(`{"name" : "TestNameC", "category" : "CategoryC", "location" : "my desk", "type": "blubb"}`)}, - RawProperties: postgres.Jsonb{json.RawMessage(`{"name" : "TestNameC", "category" : "CategoryC", "location" : "my desk", "type": "blubb"}`)}, -} - -var simulatorCupdated = common.Simulator{ - ID: 3, - UUID: "6d9776bf-b693-45e8-97b6-4c13d151043f", - Host: "Host_Cupdated", - Modeltype: "ModelTypeCUpdated", - Uptime: 0, - State: "running", - StateUpdateAt: "placeholder", - Properties: postgres.Jsonb{json.RawMessage(`{"name" : "TestNameCUpdate", "category" : "CategoryC", "location" : "my desk", "type": "blubb"}`)}, - RawProperties: postgres.Jsonb{json.RawMessage(`{"name" : "TestNameCUpdate", "category" : "CategoryC", "location" : "my desk", "type": "blubb"}`)}, -} - -var simulatorC_response = common.SimulatorResponse{ - ID: simulatorC.ID, - UUID: simulatorC.UUID, - Host: simulatorC.Host, - Modeltype: simulatorC.Modeltype, - Uptime: simulatorC.Uptime, - State: simulatorC.State, - StateUpdateAt: simulatorC.StateUpdateAt, - Properties: simulatorC.Properties, - 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, - Uptime: simulatorCupdated.Uptime, - State: simulatorCupdated.State, - StateUpdateAt: simulatorCupdated.StateUpdateAt, - Properties: simulatorCupdated.Properties, - RawProperties: simulatorCupdated.RawProperties, -} - -var simulatorCupdated_msg = common.ResponseMsgSimulator{ - Simulator: simulatorCupdated_response, -} - -var mySimulators = []common.SimulatorResponse{ - simulatorA, - simulatorB, -} - -var msgSimulators = common.ResponseMsgSimulators{ - Simulators: mySimulators, -} - -var msgSimulator = common.ResponseMsgSimulator{ - Simulator: simulatorC_response, -} - // Test /simulator endpoints func TestSimulatorEndpoints(t *testing.T) { + var token string + + var myModels = []common.SimulationModelResponse{common.SimulationModelA_response, common.SimulationModelB_response} + var msgModels = common.ResponseMsgSimulationModels{SimulationModels: myModels} + var simulatorC_msg = common.ResponseMsgSimulator{Simulator: common.SimulatorC_response} + var simulatorCupdated_msg = common.ResponseMsgSimulator{Simulator: common.SimulatorCUpdated_response} + var mySimulators = []common.SimulatorResponse{common.SimulatorA_response, common.SimulatorB_response} + var msgSimulators = common.ResponseMsgSimulators{Simulators: mySimulators} + db := common.DummyInitDB() defer db.Close() common.DummyPopulateDB(db) @@ -167,37 +37,13 @@ func TestSimulatorEndpoints(t *testing.T) { RegisterSimulatorEndpoints(api.Group("/simulators")) - credjson, err := json.Marshal(cred) - - msgOKjson, err := json.Marshal(msgOK) - if err != nil { - panic(err) - } - - msgModelsjson, err := json.Marshal(msgModels) - if err != nil { - panic(err) - } - - msgSimulatorsjson, err := json.Marshal(msgSimulators) - if err != nil { - panic(err) - } - - msgSimulatorjson, err := json.Marshal(msgSimulator) - if err != nil { - panic(err) - } - - simulatorCjson, err := json.Marshal(simulatorC_msg) - if err != nil { - panic(err) - } - - simulatorCupdatedjson, err := json.Marshal(simulatorCupdated_msg) - if err != nil { - panic(err) - } + credjson, _ := json.Marshal(common.CredAdmin) + msgOKjson, _ := json.Marshal(common.MsgOK) + msgModelsjson, _ := json.Marshal(msgModels) + msgSimulatorsjson, _ := json.Marshal(msgSimulators) + msgSimulatorjson, _ := json.Marshal(simulatorC_msg) + simulatorCjson, _ := json.Marshal(simulatorC_msg) + simulatorCupdatedjson, _ := json.Marshal(simulatorCupdated_msg) token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)