mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
- create all test data in one file
- revise testing functions (WORK IN PROGRESS)
This commit is contained in:
parent
b200ed5ed7
commit
d79fb53cb2
15 changed files with 530 additions and 676 deletions
|
@ -1,13 +1,10 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/jinzhu/gorm/dialects/postgres"
|
|
||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -99,28 +96,30 @@ func DummyPopulateDB(test_db *gorm.DB) {
|
||||||
|
|
||||||
MigrateModels(test_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"}`)
|
// Users
|
||||||
propertiesB := json.RawMessage(`{"name" : "TestNameB", "category" : "CategoryB", "location" : "where ever you want", "type": "generic"}`)
|
checkErr(test_db.Create(&User0).Error) // Admin
|
||||||
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}}
|
checkErr(test_db.Create(&UserA).Error) // Normal User
|
||||||
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(&UserB).Error) // Normal User
|
||||||
checkErr(test_db.Create(&simr_A).Error)
|
|
||||||
checkErr(test_db.Create(&simr_B).Error)
|
|
||||||
|
|
||||||
outSig_A := Signal{Name: "outSignal_A", Direction: "out", Index: 0, Unit: "V"}
|
// Simulators
|
||||||
outSig_B := Signal{Name: "outSignal_B", Direction: "out", Index: 1, Unit: "V"}
|
checkErr(test_db.Create(&SimulatorA).Error)
|
||||||
inSig_A := Signal{Name: "inSignal_A", Direction: "in", Index: 0, Unit: "A"}
|
checkErr(test_db.Create(&SimulatorB).Error)
|
||||||
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)
|
|
||||||
|
|
||||||
mo_A := SimulationModel{Name: "SimulationModel_A"}
|
// Scenarios
|
||||||
mo_B := SimulationModel{Name: "SimulationModel_B"}
|
checkErr(test_db.Create(&ScenarioA).Error)
|
||||||
checkErr(test_db.Create(&mo_A).Error)
|
checkErr(test_db.Create(&ScenarioB).Error)
|
||||||
checkErr(test_db.Create(&mo_B).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_A := File{Name: "File_A"}
|
||||||
file_B := File{Name: "File_B"}
|
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_C).Error)
|
||||||
checkErr(test_db.Create(&file_D).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_A := Dashboard{Name: "Dashboard_A"}
|
||||||
dab_B := Dashboard{Name: "Dashboard_B"}
|
dab_B := Dashboard{Name: "Dashboard_B"}
|
||||||
checkErr(test_db.Create(&dab_A).Error)
|
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
|
// For `has many` use the models with id=1 and id=2
|
||||||
|
|
||||||
// User HM Scenarios, Scenario HM Users (Many-to-Many)
|
// 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(&ScenarioA).Association("Users").Append(&UserA).Error)
|
||||||
checkErr(test_db.Model(&so_A).Association("Users").Append(&usr_B).Error)
|
checkErr(test_db.Model(&ScenarioA).Association("Users").Append(&UserB).Error)
|
||||||
checkErr(test_db.Model(&so_B).Association("Users").Append(&usr_A).Error)
|
checkErr(test_db.Model(&ScenarioB).Association("Users").Append(&UserA).Error)
|
||||||
checkErr(test_db.Model(&so_B).Association("Users").Append(&usr_B).Error)
|
checkErr(test_db.Model(&ScenarioB).Association("Users").Append(&UserB).Error)
|
||||||
|
|
||||||
// Scenario HM SimulationModels
|
// Scenario HM SimulationModels
|
||||||
checkErr(test_db.Model(&so_A).Association("SimulationModels").Append(&mo_A).Error)
|
checkErr(test_db.Model(&ScenarioA).Association("SimulationModels").Append(&SimulationModelA).Error)
|
||||||
checkErr(test_db.Model(&so_A).Association("SimulationModels").Append(&mo_B).Error)
|
checkErr(test_db.Model(&ScenarioA).Association("SimulationModels").Append(&SimulationModelB).Error)
|
||||||
|
|
||||||
// Scenario HM Dashboards
|
// Scenario HM Dashboards
|
||||||
checkErr(test_db.Model(&so_A).Association("Dashboards").Append(&dab_A).Error)
|
checkErr(test_db.Model(&ScenarioA).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_B).Error)
|
||||||
|
|
||||||
// Dashboard HM Widget
|
// 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_A).Error)
|
||||||
checkErr(test_db.Model(&dab_A).Association("Widgets").Append(&widg_B).Error)
|
checkErr(test_db.Model(&dab_A).Association("Widgets").Append(&widg_B).Error)
|
||||||
|
|
||||||
// SimulationModel HM Signals
|
// SimulationModel HM Signals
|
||||||
checkErr(test_db.Model(&mo_A).Association("InputMapping").Append(&inSig_A).Error)
|
checkErr(test_db.Model(&SimulationModelA).Association("InputMapping").Append(&InSignalA).Error)
|
||||||
checkErr(test_db.Model(&mo_A).Association("InputMapping").Append(&inSig_B).Error)
|
checkErr(test_db.Model(&SimulationModelA).Association("InputMapping").Append(&InSignalB).Error)
|
||||||
checkErr(test_db.Model(&mo_A).Association("OutputMapping").Append(&outSig_A).Error)
|
checkErr(test_db.Model(&SimulationModelA).Association("OutputMapping").Append(&OutSignalA).Error)
|
||||||
checkErr(test_db.Model(&mo_A).Association("OutputMapping").Append(&outSig_B).Error)
|
checkErr(test_db.Model(&SimulationModelA).Association("OutputMapping").Append(&OutSignalB).Error)
|
||||||
|
|
||||||
// SimulationModel HM Files
|
// SimulationModel HM Files
|
||||||
checkErr(test_db.Model(&mo_A).Association("Files").Append(&file_C).Error)
|
checkErr(test_db.Model(&SimulationModelA).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_D).Error)
|
||||||
|
|
||||||
// Simulator HM SimulationModels
|
// Simulator HM SimulationModels
|
||||||
checkErr(test_db.Model(&simr_A).Association("SimulationModels").Append(&mo_A).Error)
|
checkErr(test_db.Model(&SimulatorA).Association("SimulationModels").Append(&SimulationModelA).Error)
|
||||||
checkErr(test_db.Model(&simr_A).Association("SimulationModels").Append(&mo_B).Error)
|
checkErr(test_db.Model(&SimulatorA).Association("SimulationModels").Append(&SimulationModelB).Error)
|
||||||
|
|
||||||
// Widget HM Files
|
// 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_A).Error)
|
||||||
checkErr(test_db.Model(&widg_A).Association("Files").Append(&file_B).Error)
|
checkErr(test_db.Model(&widg_A).Association("Files").Append(&file_B).Error)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erase tables and glose the testdb
|
// Erase tables and glose the testdb
|
||||||
|
|
|
@ -26,8 +26,8 @@ type Scenario struct {
|
||||||
Name string `gorm:"not null"`
|
Name string `gorm:"not null"`
|
||||||
// Running state of scenario
|
// Running state of scenario
|
||||||
Running bool `gorm:"default:false"`
|
Running bool `gorm:"default:false"`
|
||||||
// Start parameters of scenario as JSON string
|
// Start parameters of scenario as JSON
|
||||||
StartParameters string
|
StartParameters postgres.Jsonb
|
||||||
// Users that have access to the scenario
|
// Users that have access to the scenario
|
||||||
Users []*User `gorm:"not null;many2many:user_scenarios"`
|
Users []*User `gorm:"not null;many2many:user_scenarios"`
|
||||||
// SimulationModels that belong to the scenario
|
// SimulationModels that belong to the scenario
|
||||||
|
@ -46,8 +46,8 @@ type SimulationModel struct {
|
||||||
OutputLength int `gorm:"default:1"`
|
OutputLength int `gorm:"default:1"`
|
||||||
// Number of input signals
|
// Number of input signals
|
||||||
InputLength int `gorm:"default:1"`
|
InputLength int `gorm:"default:1"`
|
||||||
// Start parameters of simulation model as JSON string
|
// Start parameters of simulation model as JSON
|
||||||
StartParameters string
|
StartParameters postgres.Jsonb
|
||||||
// ID of Scenario to which simulation model belongs
|
// ID of Scenario to which simulation model belongs
|
||||||
ScenarioID uint
|
ScenarioID uint
|
||||||
// ID of simulator associated with simulation model
|
// ID of simulator associated with simulation model
|
||||||
|
@ -169,3 +169,8 @@ type File struct {
|
||||||
// TODO Add file itself here??
|
// TODO Add file itself here??
|
||||||
FileData []byte `gorm:"column:FileData"`
|
FileData []byte `gorm:"column:FileData"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type credentials struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
|
@ -10,20 +10,20 @@ type UserResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ScenarioResponse struct {
|
type ScenarioResponse struct {
|
||||||
Name string `json:"Name"`
|
Name string `json:"name"`
|
||||||
ID uint `json:"ID"`
|
ID uint `json:"id"`
|
||||||
Running bool `json:"Running"`
|
Running bool `json:"running"`
|
||||||
StartParams string `json:"Starting Parameters"`
|
StartParameters postgres.Jsonb `json:"startParameters"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SimulationModelResponse struct {
|
type SimulationModelResponse struct {
|
||||||
ID uint `json:"ID"`
|
ID uint `json:"id"`
|
||||||
Name string `json:"Name"`
|
Name string `json:"name"`
|
||||||
OutputLength int `json:"OutputLength"`
|
OutputLength int `json:"outputLength"`
|
||||||
InputLength int `json:"InputLength"`
|
InputLength int `json:"inputLength"`
|
||||||
ScenarioID uint `json:"ScenarioID"`
|
ScenarioID uint `json:"scenarioID"`
|
||||||
SimulatorID uint `json:"SimulatorID"`
|
SimulatorID uint `json:"simulatorID"`
|
||||||
StartParams string `json:"StartParams"`
|
StartParameters postgres.Jsonb `json:"startParameters"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SimulatorResponse struct {
|
type SimulatorResponse struct {
|
||||||
|
@ -74,11 +74,11 @@ type FileResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type SignalResponse struct {
|
type SignalResponse struct {
|
||||||
Name string `json:"Name"`
|
Name string `json:"name"`
|
||||||
Unit string `json:"Unit"`
|
Unit string `json:"unit"`
|
||||||
Index uint `json:"Index"`
|
Index uint `json:"index"`
|
||||||
Direction string `json:"Direction"`
|
Direction string `json:"direction"`
|
||||||
SimulationModelID uint `json:"SimulationModelID"`
|
SimulationModelID uint `json:"simulationModelID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response messages
|
// Response messages
|
||||||
|
|
|
@ -74,10 +74,10 @@ type ScenarioSerializer struct {
|
||||||
|
|
||||||
func (self *ScenarioSerializer) Response() ScenarioResponse {
|
func (self *ScenarioSerializer) Response() ScenarioResponse {
|
||||||
response := ScenarioResponse{
|
response := ScenarioResponse{
|
||||||
Name: self.Name,
|
Name: self.Name,
|
||||||
ID: self.ID,
|
ID: self.ID,
|
||||||
Running: self.Running,
|
Running: self.Running,
|
||||||
StartParams: self.StartParameters,
|
StartParameters: self.StartParameters,
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
@ -105,13 +105,13 @@ type SimulationModelSerializer struct {
|
||||||
|
|
||||||
func (self *SimulationModelSerializer) Response() SimulationModelResponse {
|
func (self *SimulationModelSerializer) Response() SimulationModelResponse {
|
||||||
response := SimulationModelResponse{
|
response := SimulationModelResponse{
|
||||||
ID: self.ID,
|
ID: self.ID,
|
||||||
Name: self.Name,
|
Name: self.Name,
|
||||||
OutputLength: self.OutputLength,
|
OutputLength: self.OutputLength,
|
||||||
InputLength: self.InputLength,
|
InputLength: self.InputLength,
|
||||||
ScenarioID: self.ScenarioID,
|
ScenarioID: self.ScenarioID,
|
||||||
SimulatorID: self.SimulatorID,
|
SimulatorID: self.SimulatorID,
|
||||||
StartParams: self.StartParameters,
|
StartParameters: self.StartParameters,
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
340
common/testdata.go
Normal file
340
common/testdata.go
Normal file
|
@ -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,
|
||||||
|
}
|
|
@ -76,7 +76,7 @@ func getScenarios(c *gin.Context) {
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Tags scenarios
|
// @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."
|
// @Success 200 "OK."
|
||||||
// @Failure 401 "Unauthorized Access"
|
// @Failure 401 "Unauthorized Access"
|
||||||
// @Failure 403 "Access forbidden."
|
// @Failure 403 "Access forbidden."
|
||||||
|
@ -98,8 +98,8 @@ func addScenario(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var so Scenario
|
var newScenarioData common.ResponseMsgScenario
|
||||||
err = c.BindJSON(&so)
|
err = c.BindJSON(&newScenarioData)
|
||||||
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{
|
||||||
|
@ -108,14 +108,20 @@ func addScenario(c *gin.Context) {
|
||||||
return
|
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
|
// save new scenario to DB
|
||||||
err = so.save()
|
err = newScenario.save()
|
||||||
if common.ProvideErrorResponse(c, err) {
|
if common.ProvideErrorResponse(c, err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// add user to new scenario
|
// add user to new scenario
|
||||||
err = so.addUser(&(u.User))
|
err = newScenario.addUser(&(u.User))
|
||||||
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.",
|
||||||
|
@ -129,7 +135,7 @@ func addScenario(c *gin.Context) {
|
||||||
// @Tags scenarios
|
// @Tags scenarios
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce 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."
|
// @Success 200 "OK."
|
||||||
// @Failure 401 "Unauthorized Access"
|
// @Failure 401 "Unauthorized Access"
|
||||||
// @Failure 403 "Access forbidden."
|
// @Failure 403 "Access forbidden."
|
||||||
|
@ -144,8 +150,8 @@ func updateScenario(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var modifiedSo Scenario
|
var modifiedScenarioData common.ResponseMsgScenario
|
||||||
err := c.BindJSON(&modifiedSo)
|
err := c.BindJSON(&modifiedScenarioData)
|
||||||
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{
|
||||||
|
@ -154,7 +160,7 @@ func updateScenario(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = so.update(modifiedSo)
|
err = so.update(modifiedScenarioData.Scenario)
|
||||||
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.",
|
||||||
|
|
|
@ -33,7 +33,7 @@ func (s *Scenario) save() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scenario) update(modifiedScenario Scenario) error {
|
func (s *Scenario) update(modifiedScenario common.ScenarioResponse) error {
|
||||||
db := common.GetDB()
|
db := common.GetDB()
|
||||||
err := db.Model(s).Update(modifiedScenario).Error
|
err := db.Model(s).Update(modifiedScenario).Error
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -3,106 +3,30 @@ package scenario
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"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/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
|
// Test /scenarios endpoints
|
||||||
func TestScenarioEndpoints(t *testing.T) {
|
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()
|
db := common.DummyInitDB()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
common.DummyPopulateDB(db)
|
common.DummyPopulateDB(db)
|
||||||
|
@ -118,37 +42,14 @@ func TestScenarioEndpoints(t *testing.T) {
|
||||||
|
|
||||||
RegisterScenarioEndpoints(api.Group("/scenarios"))
|
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)
|
msgUsersjson, _ := json.Marshal(msgUsers)
|
||||||
if err != nil {
|
msgUserAjson, _ := json.Marshal(msgUserA)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
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)
|
common.TestEndpoint(t, router, token, "/api/scenarios", "GET", nil, 200, msgScenariosjson)
|
||||||
|
|
||||||
// test POST scenarios/
|
// 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
|
// test GET scenarios/:ScenarioID
|
||||||
common.TestEndpoint(t, router, token, "/api/scenarios/3", "GET", nil, 200, msgScenariojson)
|
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
|
// test DELETE scenarios/:ScenarioID
|
||||||
common.TestEndpoint(t, router, token, "/api/scenarios/3", "DELETE", nil, 200, msgOKjson)
|
common.TestEndpoint(t, router, token, "/api/scenarios/3", "DELETE", nil, 200, msgOKjson)
|
||||||
common.TestEndpoint(t, router, token, "/api/scenarios", "GET", nil, 200, msgScenariosjson)
|
common.TestEndpoint(t, router, token, "/api/scenarios", "GET", nil, 200, msgScenariosjson)
|
||||||
|
|
|
@ -70,7 +70,7 @@ func getSignals(c *gin.Context) {
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Tags signals
|
// @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."
|
// @Success 200 "OK."
|
||||||
// @Failure 401 "Unauthorized Access"
|
// @Failure 401 "Unauthorized Access"
|
||||||
// @Failure 403 "Access forbidden."
|
// @Failure 403 "Access forbidden."
|
||||||
|
@ -79,8 +79,8 @@ func getSignals(c *gin.Context) {
|
||||||
// @Router /signals [post]
|
// @Router /signals [post]
|
||||||
func addSignal(c *gin.Context) {
|
func addSignal(c *gin.Context) {
|
||||||
|
|
||||||
var newSignal Signal
|
var newSignalData common.ResponseMsgSignal
|
||||||
err := c.BindJSON(&newSignal)
|
err := c.BindJSON(&newSignalData)
|
||||||
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{
|
||||||
|
@ -89,6 +89,13 @@ func addSignal(c *gin.Context) {
|
||||||
return
|
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))
|
ok, _ := simulationmodel.CheckPermissions(c, common.Update, "body", int(newSignal.SimulationModelID))
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
|
@ -121,7 +128,7 @@ func updateSignal(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var modifiedSignal Signal
|
var modifiedSignal common.ResponseMsgSignal
|
||||||
err := c.BindJSON(&modifiedSignal)
|
err := c.BindJSON(&modifiedSignal)
|
||||||
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()
|
||||||
|
@ -131,7 +138,7 @@ func updateSignal(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = sig.update(modifiedSignal)
|
err = sig.update(modifiedSignal.Signal)
|
||||||
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.",
|
||||||
|
|
|
@ -63,7 +63,7 @@ func (s *Signal) addToSimulationModel() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Signal) update(modifiedSignal Signal) error {
|
func (s *Signal) update(modifiedSignal common.SignalResponse) error {
|
||||||
db := common.GetDB()
|
db := common.GetDB()
|
||||||
|
|
||||||
err := db.Model(s).Updates(map[string]interface{}{
|
err := db.Model(s).Updates(map[string]interface{}{
|
||||||
|
|
|
@ -10,107 +10,18 @@ import (
|
||||||
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
|
"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
|
// Test /models endpoints
|
||||||
func TestSignalEndpoints(t *testing.T) {
|
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()
|
db := common.DummyInitDB()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
common.DummyPopulateDB(db)
|
common.DummyPopulateDB(db)
|
||||||
|
@ -126,39 +37,12 @@ func TestSignalEndpoints(t *testing.T) {
|
||||||
|
|
||||||
RegisterSignalEndpoints(api.Group("/signals"))
|
RegisterSignalEndpoints(api.Group("/signals"))
|
||||||
|
|
||||||
credjson, err := json.Marshal(cred)
|
credjson, _ := json.Marshal(common.CredUser)
|
||||||
if err != nil {
|
msgOKjson, _ := json.Marshal(common.MsgOK)
|
||||||
panic(err)
|
msgInSignalsjson, _ := json.Marshal(msgInSignals)
|
||||||
}
|
msgOutSignalsjson, _ := json.Marshal(msgOutSignals)
|
||||||
|
inSignalCjson, _ := json.Marshal(msgInSignalC)
|
||||||
msgOKjson, err := json.Marshal(msgOK)
|
inSignalCupdatedjson, _ := json.Marshal(msgInSignalCupdated)
|
||||||
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)
|
|
||||||
|
|
||||||
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
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)
|
common.TestEndpoint(t, router, token, "/api/signals", "POST", inSignalCjson, 200, msgOKjson)
|
||||||
|
|
||||||
// test GET signals/:signalID
|
// 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
|
// test PUT signals/:signalID
|
||||||
common.TestEndpoint(t, router, token, "/api/signals/5", "PUT", inSignalCupdatedjson, 200, msgOKjson)
|
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
|
// test DELETE signals/:signalID
|
||||||
common.TestEndpoint(t, router, token, "/api/signals/5", "DELETE", nil, 200, msgOKjson)
|
common.TestEndpoint(t, router, token, "/api/signals/5", "DELETE", nil, 200, msgOKjson)
|
||||||
|
|
|
@ -55,7 +55,7 @@ func getSimulationModels(c *gin.Context) {
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Tags models
|
// @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."
|
// @Success 200 "OK."
|
||||||
// @Failure 401 "Unauthorized Access"
|
// @Failure 401 "Unauthorized Access"
|
||||||
// @Failure 403 "Access forbidden."
|
// @Failure 403 "Access forbidden."
|
||||||
|
@ -64,8 +64,8 @@ func getSimulationModels(c *gin.Context) {
|
||||||
// @Router /models [post]
|
// @Router /models [post]
|
||||||
func addSimulationModel(c *gin.Context) {
|
func addSimulationModel(c *gin.Context) {
|
||||||
|
|
||||||
var newModel SimulationModel
|
var newModelData common.ResponseMsgSimulationModel
|
||||||
err := c.BindJSON(&newModel)
|
err := c.BindJSON(&newModelData)
|
||||||
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{
|
||||||
|
@ -74,6 +74,15 @@ func addSimulationModel(c *gin.Context) {
|
||||||
return
|
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))
|
ok, _ := scenario.CheckPermissions(c, common.Create, "body", int(newModel.ScenarioID))
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
|
@ -93,7 +102,7 @@ func addSimulationModel(c *gin.Context) {
|
||||||
// @Tags models
|
// @Tags models
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce 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."
|
// @Success 200 "OK."
|
||||||
// @Failure 401 "Unauthorized Access"
|
// @Failure 401 "Unauthorized Access"
|
||||||
// @Failure 403 "Access forbidden."
|
// @Failure 403 "Access forbidden."
|
||||||
|
@ -108,7 +117,7 @@ func updateSimulationModel(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var modifiedModel SimulationModel
|
var modifiedModel common.ResponseMsgSimulationModel
|
||||||
err := c.BindJSON(&modifiedModel)
|
err := c.BindJSON(&modifiedModel)
|
||||||
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()
|
||||||
|
@ -118,7 +127,7 @@ func updateSimulationModel(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.Update(modifiedModel)
|
err = m.Update(modifiedModel.SimulationModel)
|
||||||
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.",
|
||||||
|
|
|
@ -55,7 +55,7 @@ func (m *SimulationModel) addToScenario() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *SimulationModel) Update(modifiedSimulationModel SimulationModel) error {
|
func (m *SimulationModel) Update(modifiedSimulationModel common.SimulationModelResponse) error {
|
||||||
db := common.GetDB()
|
db := common.GetDB()
|
||||||
|
|
||||||
if m.SimulatorID != modifiedSimulationModel.SimulatorID {
|
if m.SimulatorID != modifiedSimulationModel.SimulatorID {
|
||||||
|
|
|
@ -10,106 +10,16 @@ import (
|
||||||
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
|
"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
|
// Test /models endpoints
|
||||||
func TestSimulationModelEndpoints(t *testing.T) {
|
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()
|
db := common.DummyInitDB()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
common.DummyPopulateDB(db)
|
common.DummyPopulateDB(db)
|
||||||
|
@ -125,40 +35,11 @@ func TestSimulationModelEndpoints(t *testing.T) {
|
||||||
|
|
||||||
RegisterSimulationModelEndpoints(api.Group("/models"))
|
RegisterSimulationModelEndpoints(api.Group("/models"))
|
||||||
|
|
||||||
credjson, err := json.Marshal(cred)
|
credjson, _ := json.Marshal(common.CredUser)
|
||||||
if err != nil {
|
msgOKjson, _ := json.Marshal(common.MsgOK)
|
||||||
panic(err)
|
msgModelsjson, _ := json.Marshal(msgModels)
|
||||||
}
|
msgModeljson, _ := json.Marshal(msgModel)
|
||||||
|
msgModelupdatedjson, _ := json.Marshal(msgModelupdated)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
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)
|
common.TestEndpoint(t, router, token, "/api/models?scenarioID=1", "GET", nil, 200, msgModelsjson)
|
||||||
|
|
||||||
// test POST models
|
// 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
|
// test GET models/:ModelID to check if previous POST worked correctly
|
||||||
common.TestEndpoint(t, router, token, "/api/models/3", "GET", nil, 200, msgModeljson)
|
common.TestEndpoint(t, router, token, "/api/models/3", "GET", nil, 200, msgModeljson)
|
||||||
|
|
||||||
// test PUT models/:ModelID
|
// 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)
|
common.TestEndpoint(t, router, token, "/api/models/3", "GET", nil, 200, msgModelupdatedjson)
|
||||||
|
|
||||||
// test DELETE models/:ModelID
|
// test DELETE models/:ModelID
|
||||||
|
|
|
@ -2,7 +2,6 @@ package simulator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/jinzhu/gorm/dialects/postgres"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -11,147 +10,18 @@ import (
|
||||||
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
|
"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
|
// Test /simulator endpoints
|
||||||
func TestSimulatorEndpoints(t *testing.T) {
|
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()
|
db := common.DummyInitDB()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
common.DummyPopulateDB(db)
|
common.DummyPopulateDB(db)
|
||||||
|
@ -167,37 +37,13 @@ func TestSimulatorEndpoints(t *testing.T) {
|
||||||
|
|
||||||
RegisterSimulatorEndpoints(api.Group("/simulators"))
|
RegisterSimulatorEndpoints(api.Group("/simulators"))
|
||||||
|
|
||||||
credjson, err := json.Marshal(cred)
|
credjson, _ := json.Marshal(common.CredAdmin)
|
||||||
|
msgOKjson, _ := json.Marshal(common.MsgOK)
|
||||||
msgOKjson, err := json.Marshal(msgOK)
|
msgModelsjson, _ := json.Marshal(msgModels)
|
||||||
if err != nil {
|
msgSimulatorsjson, _ := json.Marshal(msgSimulators)
|
||||||
panic(err)
|
msgSimulatorjson, _ := json.Marshal(simulatorC_msg)
|
||||||
}
|
simulatorCjson, _ := json.Marshal(simulatorC_msg)
|
||||||
|
simulatorCupdatedjson, _ := json.Marshal(simulatorCupdated_msg)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue