Skip to content

Commit

Permalink
prepare interface and tests for import-type list
Browse files Browse the repository at this point in the history
  • Loading branch information
IngoRoessner committed Oct 18, 2024
1 parent 98e71be commit b7f4eaf
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
3 changes: 3 additions & 0 deletions lib/api/import_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/api/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions lib/client/import-types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions lib/controller/import_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions lib/model/import_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
172 changes: 172 additions & 0 deletions tests/list_test.go
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit b7f4eaf

Please sign in to comment.