fix IC and register tests

This commit is contained in:
Sonja Happ 2021-01-28 12:23:00 +01:00
parent 6a23b6d8c3
commit ae4436be6d
3 changed files with 153 additions and 120 deletions

View file

@ -304,6 +304,10 @@ func processMessage(message amqp.Delivery) error {
func createExternalIC(payload ICUpdate, ICUUID string) error { func createExternalIC(payload ICUpdate, ICUUID string) error {
if payload.Properties == nil {
return fmt.Errorf("AMQP: Cannot create new IC, Propertie field missing")
}
var newICReq AddICRequest var newICReq AddICRequest
newICReq.InfrastructureComponent.UUID = ICUUID newICReq.InfrastructureComponent.UUID = ICUUID
if payload.Properties.Name == nil || if payload.Properties.Name == nil ||
@ -317,6 +321,7 @@ func createExternalIC(payload ICUpdate, ICUUID string) error {
newICReq.InfrastructureComponent.Type = *payload.Properties.Type newICReq.InfrastructureComponent.Type = *payload.Properties.Type
// add optional params // add optional params
if payload.Status != nil {
if payload.Status.State != nil { if payload.Status.State != nil {
newICReq.InfrastructureComponent.State = *payload.Status.State newICReq.InfrastructureComponent.State = *payload.Status.State
} else { } else {
@ -328,6 +333,11 @@ func createExternalIC(payload ICUpdate, ICUUID string) error {
return nil return nil
} }
if payload.Status.Uptime != nil {
newICReq.InfrastructureComponent.Uptime = *payload.Status.Uptime
}
}
if payload.Properties.WS_url != nil { if payload.Properties.WS_url != nil {
newICReq.InfrastructureComponent.WebsocketURL = *payload.Properties.WS_url newICReq.InfrastructureComponent.WebsocketURL = *payload.Properties.WS_url
} }
@ -340,9 +350,7 @@ func createExternalIC(payload ICUpdate, ICUUID string) error {
if payload.Properties.Description != nil { if payload.Properties.Description != nil {
newICReq.InfrastructureComponent.Description = *payload.Properties.Description newICReq.InfrastructureComponent.Description = *payload.Properties.Description
} }
if payload.Status.Uptime != nil {
newICReq.InfrastructureComponent.Uptime = *payload.Status.Uptime
}
// TODO add JSON start parameter scheme // TODO add JSON start parameter scheme
// set managed externally to true because this IC is created via AMQP // set managed externally to true because this IC is created via AMQP
@ -380,6 +388,8 @@ func createExternalIC(payload ICUpdate, ICUUID string) error {
func (s *InfrastructureComponent) updateExternalIC(payload ICUpdate) error { func (s *InfrastructureComponent) updateExternalIC(payload ICUpdate) error {
var updatedICReq UpdateICRequest var updatedICReq UpdateICRequest
if payload.Status != nil {
if payload.Status.State != nil { if payload.Status.State != nil {
updatedICReq.InfrastructureComponent.State = *payload.Status.State updatedICReq.InfrastructureComponent.State = *payload.Status.State
@ -399,7 +409,9 @@ func (s *InfrastructureComponent) updateExternalIC(payload ICUpdate) error {
if payload.Status.Uptime != nil { if payload.Status.Uptime != nil {
updatedICReq.InfrastructureComponent.Uptime = *payload.Status.Uptime updatedICReq.InfrastructureComponent.Uptime = *payload.Status.Uptime
} }
}
if payload.Properties != nil {
if payload.Properties.Type != nil { if payload.Properties.Type != nil {
updatedICReq.InfrastructureComponent.Type = *payload.Properties.Type updatedICReq.InfrastructureComponent.Type = *payload.Properties.Type
} }
@ -416,12 +428,12 @@ func (s *InfrastructureComponent) updateExternalIC(payload ICUpdate) error {
updatedICReq.InfrastructureComponent.APIURL = *payload.Properties.API_url updatedICReq.InfrastructureComponent.APIURL = *payload.Properties.API_url
} }
if payload.Properties.Location != nil { if payload.Properties.Location != nil {
//postgres.Jsonb{json.RawMessage(`{"location" : " ` + *payload.Status.Location + `"}`)}
updatedICReq.InfrastructureComponent.Location = *payload.Properties.Location updatedICReq.InfrastructureComponent.Location = *payload.Properties.Location
} }
if payload.Properties.Description != nil { if payload.Properties.Description != nil {
updatedICReq.InfrastructureComponent.Description = *payload.Properties.Description updatedICReq.InfrastructureComponent.Description = *payload.Properties.Description
} }
}
// set raw status update if IC // set raw status update if IC
payloadRaw, err := json.Marshal(payload) payloadRaw, err := json.Marshal(payload)

View file

@ -24,6 +24,8 @@ package infrastructure_component
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/streadway/amqp"
"log"
"os" "os"
"testing" "testing"
"time" "time"
@ -32,7 +34,6 @@ import (
component_configuration "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/component-configuration" component_configuration "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/component-configuration"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario"
"github.com/jinzhu/gorm/dialects/postgres" "github.com/jinzhu/gorm/dialects/postgres"
"github.com/streadway/amqp"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -141,6 +142,27 @@ func TestMain(m *testing.M) {
// that can be associated with a new component configuration // that can be associated with a new component configuration
scenario.RegisterScenarioEndpoints(api.Group("/scenarios")) scenario.RegisterScenarioEndpoints(api.Group("/scenarios"))
// check AMQP connection
err = CheckConnection()
if err.Error() != "connection is nil" {
return
}
// connect AMQP client
// Make sure that AMQP_HOST, AMQP_USER, AMQP_PASS are set
host, err := configuration.GlobalConfig.String("amqp.host")
usr, err := configuration.GlobalConfig.String("amqp.user")
pass, err := configuration.GlobalConfig.String("amqp.pass")
amqpURI := "amqp://" + usr + ":" + pass + "@" + host
// AMQP Connection startup is tested here
// Not repeated in other tests because it is only needed once
err = StartAMQP(amqpURI, api)
if err != nil {
log.Println("unable to connect to AMQP")
return
}
os.Exit(m.Run()) os.Exit(m.Run())
} }
@ -149,22 +171,6 @@ func TestAddICAsAdmin(t *testing.T) {
database.MigrateModels() database.MigrateModels()
assert.NoError(t, helper.AddTestUsers()) assert.NoError(t, helper.AddTestUsers())
// check AMQP connection
err := CheckConnection()
assert.Errorf(t, err, "connection is nil")
// connect AMQP client
// Make sure that AMQP_HOST, AMQP_USER, AMQP_PASS are set
host, err := configuration.GlobalConfig.String("amqp.host")
user, err := configuration.GlobalConfig.String("amqp.user")
pass, err := configuration.GlobalConfig.String("amqp.pass")
amqpURI := "amqp://" + user + ":" + pass + "@" + host
// AMQP Connection startup is tested here
// Not repeated in other tests because it is only needed once
err = StartAMQP(amqpURI, api)
assert.NoError(t, err)
// authenticate as admin // authenticate as admin
token, err := helper.AuthenticateForTest(router, token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials) "/api/authenticate", "POST", helper.AdminCredentials)
@ -228,6 +234,9 @@ func TestAddICAsAdmin(t *testing.T) {
// Compare POST's response with the newExternalIC // Compare POST's response with the newExternalIC
err = helper.CompareResponse(resp, helper.KeyModels{"ic": newIC2}) err = helper.CompareResponse(resp, helper.KeyModels{"ic": newIC2})
assert.NoError(t, err) assert.NoError(t, err)
// wait to have async operation be completed
time.Sleep(waitingTime * time.Second)
} }
func TestAddICAsUser(t *testing.T) { func TestAddICAsUser(t *testing.T) {
@ -243,6 +252,7 @@ func TestAddICAsUser(t *testing.T) {
// test POST ic/ $newIC // test POST ic/ $newIC
// This should fail with unprocessable entity 422 error code // This should fail with unprocessable entity 422 error code
// Normal users are not allowed to add ICs // Normal users are not allowed to add ICs
newIC1.ManagedExternally = newFalse()
code, resp, err := helper.TestEndpoint(router, token, code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1}) "/api/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err) assert.NoError(t, err)
@ -305,14 +315,15 @@ func TestUpdateICAsAdmin(t *testing.T) {
// fake an IC update (create) message // fake an IC update (create) message
var update ICUpdate var update ICUpdate
update.Status = new(ICStatus) update.Status = new(ICStatus)
update.Properties = new(ICProperties)
update.Status.State = new(string) update.Status.State = new(string)
*update.Status.State = "idle" *update.Status.State = "idle"
update.Status.Name = new(string) update.Properties.Name = new(string)
*update.Status.Name = newIC2.Name *update.Properties.Name = newIC2.Name
update.Status.Category = new(string) update.Properties.Category = new(string)
*update.Status.Category = newIC2.Category *update.Properties.Category = newIC2.Category
update.Status.Type = new(string) update.Properties.Type = new(string)
*update.Status.Type = newIC2.Type *update.Properties.Type = newIC2.Type
payload, err := json.Marshal(update) payload, err := json.Marshal(update)
assert.NoError(t, err) assert.NoError(t, err)
@ -435,14 +446,15 @@ func TestDeleteICAsAdmin(t *testing.T) {
// fake an IC update (create) message // fake an IC update (create) message
var update ICUpdate var update ICUpdate
update.Status = new(ICStatus) update.Status = new(ICStatus)
update.Properties = new(ICProperties)
update.Status.State = new(string) update.Status.State = new(string)
*update.Status.State = "idle" *update.Status.State = "idle"
update.Status.Name = new(string) update.Properties.Name = new(string)
*update.Status.Name = newIC2.Name *update.Properties.Name = newIC2.Name
update.Status.Category = new(string) update.Properties.Category = new(string)
*update.Status.Category = newIC2.Category *update.Properties.Category = newIC2.Category
update.Status.Type = new(string) update.Properties.Type = new(string)
*update.Status.Type = newIC2.Type *update.Properties.Type = newIC2.Type
payload, err := json.Marshal(update) payload, err := json.Marshal(update)
assert.NoError(t, err) assert.NoError(t, err)
@ -676,6 +688,7 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
// fake an IC update message // fake an IC update message
var update ICUpdate var update ICUpdate
update.Status = new(ICStatus) update.Status = new(ICStatus)
update.Properties = new(ICProperties)
update.Status.State = new(string) update.Status.State = new(string)
*update.Status.State = "idle" *update.Status.State = "idle"
@ -715,22 +728,22 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
assert.Equal(t, 0, number) assert.Equal(t, 0, number)
// complete the (required) data of an IC // complete the (required) data of an IC
update.Status.Name = new(string) update.Properties.Name = new(string)
*update.Status.Name = newIC1.Name *update.Properties.Name = newIC1.Name
update.Status.Category = new(string) update.Properties.Category = new(string)
*update.Status.Category = newIC1.Category *update.Properties.Category = newIC1.Category
update.Status.Type = new(string) update.Properties.Type = new(string)
*update.Status.Type = newIC1.Type *update.Properties.Type = newIC1.Type
update.Status.Uptime = new(float64) update.Status.Uptime = new(float64)
*update.Status.Uptime = -1.0 *update.Status.Uptime = -1.0
update.Status.WS_url = new(string) update.Properties.WS_url = new(string)
*update.Status.WS_url = newIC1.WebsocketURL *update.Properties.WS_url = newIC1.WebsocketURL
update.Status.API_url = new(string) update.Properties.API_url = new(string)
*update.Status.API_url = newIC1.APIURL *update.Properties.API_url = newIC1.APIURL
update.Status.Description = new(string) update.Properties.Description = new(string)
*update.Status.Description = newIC1.Description *update.Properties.Description = newIC1.Description
update.Status.Location = new(string) update.Properties.Location = new(string)
*update.Status.Location = newIC1.Location *update.Properties.Location = newIC1.Location
payload, err = json.Marshal(update) payload, err = json.Marshal(update)
assert.NoError(t, err) assert.NoError(t, err)
@ -765,7 +778,7 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
assert.Equal(t, 1, number) assert.Equal(t, 1, number)
// modify status update // modify status update
*update.Status.Name = "This is the new name" *update.Properties.Name = "This is the new name"
payload, err = json.Marshal(update) payload, err = json.Marshal(update)
assert.NoError(t, err) assert.NoError(t, err)
@ -809,26 +822,26 @@ func TestDeleteICViaAMQPRecv(t *testing.T) {
// fake an IC update message // fake an IC update message
var update ICUpdate var update ICUpdate
update.Status = new(ICStatus) update.Status = new(ICStatus)
update.Properties = new(ICProperties)
update.Status.State = new(string) update.Status.State = new(string)
*update.Status.State = "idle" *update.Status.State = "idle"
// complete the (required) data of an IC // complete the (required) data of an IC
update.Status.Name = new(string) update.Properties.Name = new(string)
*update.Status.Name = newIC1.Name *update.Properties.Name = newIC1.Name
update.Status.Category = new(string) update.Properties.Category = new(string)
*update.Status.Category = newIC1.Category *update.Properties.Category = newIC1.Category
update.Status.Type = new(string) update.Properties.Type = new(string)
*update.Status.Type = newIC1.Type *update.Properties.Type = newIC1.Type
update.Status.Uptime = new(float64) update.Status.Uptime = new(float64)
*update.Status.Uptime = -1.0 *update.Status.Uptime = -1.0
update.Status.WS_url = new(string) update.Properties.WS_url = new(string)
*update.Status.WS_url = newIC1.WebsocketURL *update.Properties.WS_url = newIC1.WebsocketURL
update.Status.API_url = new(string) update.Properties.API_url = new(string)
*update.Status.API_url = newIC1.APIURL *update.Properties.API_url = newIC1.APIURL
update.Status.Description = new(string) update.Properties.Description = new(string)
*update.Status.Description = newIC1.Description *update.Properties.Description = newIC1.Description
update.Status.Location = new(string) update.Properties.Location = new(string)
*update.Status.Location = newIC1.Location *update.Properties.Location = newIC1.Location
payload, err := json.Marshal(update) payload, err := json.Marshal(update)
assert.NoError(t, err) assert.NoError(t, err)

View file

@ -23,17 +23,18 @@
package routes package routes
import ( import (
infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
"os" "os"
"testing" "testing"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration" "git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
var router *gin.Engine var router *gin.Engine
var api *gin.RouterGroup
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
err := configuration.InitConfig() err := configuration.InitConfig()
@ -49,35 +50,42 @@ func TestMain(m *testing.M) {
router = gin.Default() router = gin.Default()
// connect AMQP client (make sure that AMQP_HOST, AMQP_USER, AMQP_PASS are set via command line parameters) basePath, _ := configuration.GlobalConfig.String("base.path")
api = router.Group(basePath)
os.Exit(m.Run())
}
/*
* The order of test functions is important here
* 1. Start and connect AMQP
* 2. Register endpoints
* 3. Add test data
*/
func TestStartAMQP(t *testing.T) {
// connect AMQP client
// Make sure that AMQP_HOST, AMQP_USER, AMQP_PASS are set
host, err := configuration.GlobalConfig.String("amqp.host") host, err := configuration.GlobalConfig.String("amqp.host")
user, err := configuration.GlobalConfig.String("amqp.user") user, err := configuration.GlobalConfig.String("amqp.user")
pass, err := configuration.GlobalConfig.String("amqp.pass") pass, err := configuration.GlobalConfig.String("amqp.pass")
amqpURI := "amqp://" + user + ":" + pass + "@" + host amqpURI := "amqp://" + user + ":" + pass + "@" + host
err = infrastructure_component.ConnectAMQP(amqpURI) // AMQP Connection startup is tested here
// Not repeated in other tests because it is only needed once
os.Exit(m.Run()) err = infrastructure_component.StartAMQP(amqpURI, api)
assert.NoError(t, err)
} }
func TestRegisterEndpoints(t *testing.T) { func TestRegisterEndpoints(t *testing.T) {
database.DropTables() database.DropTables()
database.MigrateModels() database.MigrateModels()
basePath, err := configuration.GlobalConfig.String("base.path")
assert.NoError(t, err)
api := router.Group(basePath)
RegisterEndpoints(router, api) RegisterEndpoints(router, api)
} }
func TestAddTestData(t *testing.T) { func TestAddTestData(t *testing.T) {
err := configuration.InitConfig()
if err != nil {
panic(t)
}
err = ReadTestDataFromJson("../database/testdata.json") err := ReadTestDataFromJson("../database/testdata.json")
assert.NoError(t, err) assert.NoError(t, err)
resp, err := AddTestData(configuration.GlobalConfig, router) resp, err := AddTestData(configuration.GlobalConfig, router)