From b7f4eaf9a5177c63d4c2f7795671a1cd34581614 Mon Sep 17 00:00:00 2001 From: Ingo Date: Fri, 18 Oct 2024 09:56:59 +0200 Subject: [PATCH] prepare interface and tests for import-type list --- .github/workflows/tests.yml | 30 ++++++ lib/api/import_types.go | 3 + lib/api/interfaces.go | 1 + lib/client/client.go | 4 + lib/client/import-types.go | 5 + lib/controller/import_types.go | 5 + lib/model/import_type.go | 13 +++ tests/list_test.go | 172 +++++++++++++++++++++++++++++++++ 8 files changed, 233 insertions(+) create mode 100644 .github/workflows/tests.yml create mode 100644 tests/list_test.go diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..de5a0d1 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,30 @@ +name: Tests +on: + push: + branches: + - master + - main + - dev +jobs: + test: + runs-on: ubuntu-latest + timeout-minutes: 240 + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '1.23' + + - name: Build + run: go build -v ./... + + - name: Test + timeout-minutes: 240 + uses: nick-fields/retry@v2 + with: + max_attempts: 3 + retry_on: error + timeout_minutes: 240 + command: go test -p 1 -short -timeout 99999s ./... \ No newline at end of file diff --git a/lib/api/import_types.go b/lib/api/import_types.go index 2f6743e..c54fb01 100644 --- a/lib/api/import_types.go +++ b/lib/api/import_types.go @@ -63,6 +63,9 @@ func ImportTypesEndpoints(config config.Config, control Controller, router *http if sort == "" { sort = "name" } + //TODO: + // fill model.ImportTypeListOptions with + // use ListImportTypesV2(token jwt.Token, options model.ImportTypeListOptions) (result []model.ImportType, err error, errCode int) result, err, errCode := control.ListImportTypes(token, limitInt, offsetInt, sort) if err != nil { http.Error(writer, err.Error(), errCode) diff --git a/lib/api/interfaces.go b/lib/api/interfaces.go index cad275d..cff8c04 100644 --- a/lib/api/interfaces.go +++ b/lib/api/interfaces.go @@ -24,6 +24,7 @@ import ( type Controller interface { ReadImportType(id string, token jwt.Token) (result model.ImportType, err error, errCode int) ListImportTypes(token jwt.Token, limit int64, offset int64, sort string) (result []model.ImportType, err error, errCode int) + ListImportTypesV2(token jwt.Token, options model.ImportTypeListOptions) (result []model.ImportType, err error, errCode int) CreateImportType(importType model.ImportType, token jwt.Token) (result model.ImportType, err error, code int) SetImportType(importType model.ImportType, token jwt.Token) (err error, code int) DeleteImportType(id string, token jwt.Token) (err error, errCode int) diff --git a/lib/client/client.go b/lib/client/client.go index 01b0606..ca4411c 100644 --- a/lib/client/client.go +++ b/lib/client/client.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "github.com/SENERGY-Platform/import-repository/lib/api" + "github.com/SENERGY-Platform/import-repository/lib/model" "io" "net/http" ) @@ -51,3 +52,6 @@ func do[T any](req *http.Request) (result T, err error, code int) { } return } + +type ImportTypeListOptions = model.ImportTypeListOptions +type ImportTypeFilterCriteria = model.ImportTypeFilterCriteria diff --git a/lib/client/import-types.go b/lib/client/import-types.go index 45e8582..fcf3d33 100644 --- a/lib/client/import-types.go +++ b/lib/client/import-types.go @@ -44,6 +44,11 @@ func (c Client) ListImportTypes(token jwt.Token, limit int64, offset int64, sort return do[[]model.ImportType](req) } +func (c Client) ListImportTypesV2(token jwt.Token, options model.ImportTypeListOptions) (result []model.ImportType, err error, errCode int) { + //TODO implement me + panic("implement me") +} + func (c Client) CreateImportType(importType model.ImportType, token jwt.Token) (result model.ImportType, err error, code int) { b, err := json.Marshal(importType) if err != nil { diff --git a/lib/controller/import_types.go b/lib/controller/import_types.go index 3a481a9..b742924 100644 --- a/lib/controller/import_types.go +++ b/lib/controller/import_types.go @@ -87,6 +87,11 @@ func (this *Controller) ListImportTypes(token jwt.Token, limit int64, offset int return result, nil, http.StatusOK } +func (this *Controller) ListImportTypesV2(token jwt.Token, options model.ImportTypeListOptions) (result []model.ImportType, err error, errCode int) { + //TODO: implement + panic("implement me") +} + func (this *Controller) SetImportType(importType model.ImportType, token jwt.Token) (err error, errCode int) { err, code := this.CheckAccessToImportType(token, importType.Id, permV2Model.Write) if err != nil { diff --git a/lib/model/import_type.go b/lib/model/import_type.go index 976c933..3512bdb 100644 --- a/lib/model/import_type.go +++ b/lib/model/import_type.go @@ -107,3 +107,16 @@ type ImportConfig struct { DefaultValue interface{} `json:"default_value"` DefaultValueString *string `json:"-"` } + +type ImportTypeListOptions struct { + Search string + Limit int64 //default 100 + Offset int64 //default 0 + SortBy string //default name.asc + Criteria []ImportTypeFilterCriteria //filter; ignored if nil +} + +type ImportTypeFilterCriteria struct { + FunctionId string `json:"function_id"` + AspectIds []string `json:"aspect_ids"` +} diff --git a/tests/list_test.go b/tests/list_test.go new file mode 100644 index 0000000..137cb4d --- /dev/null +++ b/tests/list_test.go @@ -0,0 +1,172 @@ +/* + * Copyright 2024 InfAI (CC SES) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tests + +import ( + "context" + "github.com/SENERGY-Platform/import-repository/lib/client" + "github.com/SENERGY-Platform/import-repository/lib/model" + "reflect" + "sync" + "testing" +) + +func TestList(t *testing.T) { + wg := &sync.WaitGroup{} + defer wg.Wait() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + _, conf, err := createTestEnv(ctx, wg) + if err != nil { + t.Error(err) + return + } + + getColorFunction := "urn:infai:ses:measuring-function:getColorFunction" + getHumidityFunction := "urn:infai:ses:measuring-function:getHumidityFunction" + + testCharacteristic := "urn:infai:ses:characteristic:test" + + deviceAspect := "urn:infai:ses:aspect:deviceAspect" + airAspect := "urn:infai:ses:aspect:airAspect" + + it1, err := createImportType(conf, model.ImportType{ + Name: "it1", + Output: model.ContentVariable{ + Name: "output", + SubContentVariables: []model.ContentVariable{ + { + Name: "value", + SubContentVariables: []model.ContentVariable{ + { + Name: "value", + CharacteristicId: testCharacteristic, + AspectId: deviceAspect, + FunctionId: getColorFunction, + }, + { + Name: "foo", + AspectId: airAspect, + FunctionId: getHumidityFunction, + }, + }, + }, + }, + }, + }) + if err != nil { + t.Error(err) + return + } + + it2, err := createImportType(conf, model.ImportType{ + Name: "it2", + Output: model.ContentVariable{ + Name: "output", + SubContentVariables: []model.ContentVariable{ + { + Name: "value", + CharacteristicId: testCharacteristic, + AspectId: deviceAspect, + FunctionId: getColorFunction, + }, + }, + }, + }) + if err != nil { + t.Error(err) + return + } + + it3, err := createImportType(conf, model.ImportType{ + Name: "it3", + Output: model.ContentVariable{ + Name: "output", + SubContentVariables: []model.ContentVariable{ + { + Name: "foo", + AspectId: airAspect, + FunctionId: getHumidityFunction, + }, + }, + }, + }) + if err != nil { + t.Error(err) + return + } + + itNone, err := createImportType(conf, model.ImportType{ + Name: "none", + Output: model.ContentVariable{ + Name: "output", + }, + }) + if err != nil { + t.Error(err) + return + } + + c := client.NewClient("http://localhost:" + conf.ServerPort) + + t.Run("default", testImportTypesList(c, client.ImportTypeListOptions{}, []model.ImportType{it1, it2, it3, itNone})) + + t.Run("sort name.asc", testImportTypesList(c, client.ImportTypeListOptions{SortBy: "name.asc"}, []model.ImportType{it1, it2, it3, itNone})) + t.Run("sort name.desc", testImportTypesList(c, client.ImportTypeListOptions{SortBy: "name.desc"}, []model.ImportType{itNone, it3, it2, it1})) + + t.Run("limit offset", testImportTypesList(c, client.ImportTypeListOptions{Limit: 2, Offset: 1}, []model.ImportType{it2, it3})) + + t.Run("search it", testImportTypesList(c, client.ImportTypeListOptions{Search: "it"}, []model.ImportType{it1, it2, it3})) + + t.Run("search none", testImportTypesList(c, client.ImportTypeListOptions{Search: "none"}, []model.ImportType{itNone})) + + t.Run("criteria humidity device", testImportTypesList(c, client.ImportTypeListOptions{Criteria: []model.ImportTypeFilterCriteria{ + {FunctionId: getHumidityFunction, AspectIds: []string{deviceAspect}}, + }}, []model.ImportType{})) + + t.Run("criteria color device", testImportTypesList(c, client.ImportTypeListOptions{Criteria: []model.ImportTypeFilterCriteria{ + {FunctionId: getColorFunction, AspectIds: []string{deviceAspect}}, + }}, []model.ImportType{it1, it2})) + + t.Run("criteria humidity air", testImportTypesList(c, client.ImportTypeListOptions{Criteria: []model.ImportTypeFilterCriteria{ + {FunctionId: getHumidityFunction, AspectIds: []string{airAspect}}, + }}, []model.ImportType{it1, it3})) + + t.Run("criteria humidity air,unknown", testImportTypesList(c, client.ImportTypeListOptions{Criteria: []model.ImportTypeFilterCriteria{ + {FunctionId: getHumidityFunction, AspectIds: []string{airAspect, "unknown"}}, + }}, []model.ImportType{it1, it3})) + + t.Run("multiple criteria", testImportTypesList(c, client.ImportTypeListOptions{Criteria: []model.ImportTypeFilterCriteria{ + {FunctionId: getHumidityFunction, AspectIds: []string{airAspect, "unknown"}}, + {FunctionId: getColorFunction, AspectIds: []string{deviceAspect}}, + }}, []model.ImportType{it1})) +} + +func testImportTypesList(c client.Interface, options client.ImportTypeListOptions, expected []model.ImportType) func(t *testing.T) { + return func(t *testing.T) { + result, err, _ := c.ListImportTypesV2(userjwt, options) + if err != nil { + t.Error(err) + return + } + if !reflect.DeepEqual(result, expected) { + t.Errorf("\n%#v\n%#v\n", result, expected) + return + } + } +}