From 7452aaf75c372383b6f1e08db5df34941fbddfe9 Mon Sep 17 00:00:00 2001 From: Nils Ove Tendenes Date: Tue, 26 Mar 2024 12:23:07 +0100 Subject: [PATCH] feat: add POST-endpoint to filter on IDs --- config/router/router.go | 6 ++ handlers/concept_handlers.go | 21 ++++- handlers/data_service_handlers.go | 21 ++++- handlers/dataset_handlers.go | 21 ++++- handlers/event_handlers.go | 21 ++++- handlers/information_model_handlers.go | 21 ++++- handlers/service_handlers.go | 21 ++++- model/filters.go | 5 ++ openapi.yaml | 105 +++++++++++++++++++++++++ service/concept_service.go | 5 +- service/data_service_service.go | 8 +- service/dataset_service.go | 8 +- service/event_service.go | 8 +- service/information_model_service.go | 8 +- service/service_service.go | 8 +- test/concepts_test.go | 47 +++++++++++ test/data_service_test.go | 47 +++++++++++ test/datasets_test.go | 47 +++++++++++ test/events_test.go | 47 +++++++++++ test/information_model_test.go | 47 +++++++++++ test/services_test.go | 47 +++++++++++ 21 files changed, 552 insertions(+), 17 deletions(-) create mode 100644 model/filters.go diff --git a/config/router/router.go b/config/router/router.go index 322e2a3..629a01c 100644 --- a/config/router/router.go +++ b/config/router/router.go @@ -13,21 +13,27 @@ func InitializeRoutes(e *gin.Engine) { e.GET(env.PathValues.Ready, handlers.ReadyHandler()) e.GET(env.PathValues.Concepts, handlers.GetConcepts()) + e.POST(env.PathValues.Concepts, handlers.FilterConcepts()) e.GET(env.PathValues.Concept, handlers.GetConcept()) e.GET(env.PathValues.DataServices, handlers.GetDataServices()) + e.POST(env.PathValues.DataServices, handlers.FilterDataServices()) e.GET(env.PathValues.DataService, handlers.GetDataService()) e.GET(env.PathValues.Datasets, handlers.GetDatasets()) + e.POST(env.PathValues.Datasets, handlers.FilterDatasets()) e.GET(env.PathValues.Dataset, handlers.GetDataset()) e.GET(env.PathValues.Events, handlers.GetEvents()) + e.POST(env.PathValues.Events, handlers.FilterEvents()) e.GET(env.PathValues.Event, handlers.GetEvent()) e.GET(env.PathValues.InformationModels, handlers.GetInformationModels()) + e.POST(env.PathValues.InformationModels, handlers.FilterInformationModels()) e.GET(env.PathValues.InformationModel, handlers.GetInformationModel()) e.GET(env.PathValues.Services, handlers.GetServices()) + e.POST(env.PathValues.Services, handlers.FilterServices()) e.GET(env.PathValues.Service, handlers.GetService()) } diff --git a/handlers/concept_handlers.go b/handlers/concept_handlers.go index 1613e89..7b150e5 100644 --- a/handlers/concept_handlers.go +++ b/handlers/concept_handlers.go @@ -1,6 +1,7 @@ package handlers import ( + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/service" @@ -10,7 +11,7 @@ import ( func GetConcepts() func(c *gin.Context) { conceptService := service.InitConceptService() return func(c *gin.Context) { - concepts, status := conceptService.GetConcepts(c.Request.Context()) + concepts, status := conceptService.GetConcepts(c.Request.Context(), nil) if status == http.StatusOK { c.JSON(status, concepts) } else { @@ -19,6 +20,24 @@ func GetConcepts() func(c *gin.Context) { } } +func FilterConcepts() func(c *gin.Context) { + conceptService := service.InitConceptService() + return func(c *gin.Context) { + var filters model.Filters + err := c.BindJSON(&filters) + if err != nil { + c.Status(http.StatusBadRequest) + } else { + concepts, status := conceptService.GetConcepts(c.Request.Context(), &filters) + if status == http.StatusOK { + c.JSON(status, concepts) + } else { + c.Status(status) + } + } + } +} + func GetConcept() func(c *gin.Context) { conceptService := service.InitConceptService() return func(c *gin.Context) { diff --git a/handlers/data_service_handlers.go b/handlers/data_service_handlers.go index 37821ab..0e0fa8f 100644 --- a/handlers/data_service_handlers.go +++ b/handlers/data_service_handlers.go @@ -1,6 +1,7 @@ package handlers import ( + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "net/http" "github.com/gin-gonic/gin" @@ -11,7 +12,7 @@ import ( func GetDataServices() func(c *gin.Context) { dataServiceService := service.InitDataServiceService() return func(c *gin.Context) { - dataServices, status := dataServiceService.GetDataServices(c.Request.Context()) + dataServices, status := dataServiceService.GetDataServices(c.Request.Context(), nil) if status == http.StatusOK { c.JSON(status, dataServices) } else { @@ -20,6 +21,24 @@ func GetDataServices() func(c *gin.Context) { } } +func FilterDataServices() func(c *gin.Context) { + dataServiceService := service.InitDataServiceService() + return func(c *gin.Context) { + var filters model.Filters + err := c.BindJSON(&filters) + if err != nil { + c.Status(http.StatusBadRequest) + } else { + dataServices, status := dataServiceService.GetDataServices(c.Request.Context(), &filters) + if status == http.StatusOK { + c.JSON(status, dataServices) + } else { + c.Status(status) + } + } + } +} + func GetDataService() func(c *gin.Context) { dataServiceService := service.InitDataServiceService() return func(c *gin.Context) { diff --git a/handlers/dataset_handlers.go b/handlers/dataset_handlers.go index 8398f44..d26f8f0 100644 --- a/handlers/dataset_handlers.go +++ b/handlers/dataset_handlers.go @@ -1,6 +1,7 @@ package handlers import ( + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/service" @@ -10,7 +11,7 @@ import ( func GetDatasets() func(c *gin.Context) { datasetService := service.InitDatasetService() return func(c *gin.Context) { - datasets, status := datasetService.GetDatasets(c.Request.Context()) + datasets, status := datasetService.GetDatasets(c.Request.Context(), nil) if status == http.StatusOK { c.JSON(status, datasets) } else { @@ -19,6 +20,24 @@ func GetDatasets() func(c *gin.Context) { } } +func FilterDatasets() func(c *gin.Context) { + datasetService := service.InitDatasetService() + return func(c *gin.Context) { + var filters model.Filters + err := c.BindJSON(&filters) + if err != nil { + c.Status(http.StatusBadRequest) + } else { + datasets, status := datasetService.GetDatasets(c.Request.Context(), &filters) + if status == http.StatusOK { + c.JSON(status, datasets) + } else { + c.Status(status) + } + } + } +} + func GetDataset() func(c *gin.Context) { datasetService := service.InitDatasetService() return func(c *gin.Context) { diff --git a/handlers/event_handlers.go b/handlers/event_handlers.go index 84b8be2..ee0af39 100644 --- a/handlers/event_handlers.go +++ b/handlers/event_handlers.go @@ -1,6 +1,7 @@ package handlers import ( + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/service" @@ -10,7 +11,7 @@ import ( func GetEvents() func(c *gin.Context) { eventService := service.InitEventService() return func(c *gin.Context) { - events, status := eventService.GetEvents(c.Request.Context()) + events, status := eventService.GetEvents(c.Request.Context(), nil) if status == http.StatusOK { c.JSON(status, events) } else { @@ -19,6 +20,24 @@ func GetEvents() func(c *gin.Context) { } } +func FilterEvents() func(c *gin.Context) { + eventService := service.InitEventService() + return func(c *gin.Context) { + var filters model.Filters + err := c.BindJSON(&filters) + if err != nil { + c.Status(http.StatusBadRequest) + } else { + events, status := eventService.GetEvents(c.Request.Context(), &filters) + if status == http.StatusOK { + c.JSON(status, events) + } else { + c.Status(status) + } + } + } +} + func GetEvent() func(c *gin.Context) { eventService := service.InitEventService() return func(c *gin.Context) { diff --git a/handlers/information_model_handlers.go b/handlers/information_model_handlers.go index d6cabbf..3a8e6b0 100644 --- a/handlers/information_model_handlers.go +++ b/handlers/information_model_handlers.go @@ -1,6 +1,7 @@ package handlers import ( + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/service" @@ -10,7 +11,7 @@ import ( func GetInformationModels() func(c *gin.Context) { informationModelService := service.InitInformationModelService() return func(c *gin.Context) { - informationModels, status := informationModelService.GetInformationModels(c.Request.Context()) + informationModels, status := informationModelService.GetInformationModels(c.Request.Context(), nil) if status == http.StatusOK { c.JSON(status, informationModels) } else { @@ -19,6 +20,24 @@ func GetInformationModels() func(c *gin.Context) { } } +func FilterInformationModels() func(c *gin.Context) { + informationModelService := service.InitInformationModelService() + return func(c *gin.Context) { + var filters model.Filters + err := c.BindJSON(&filters) + if err != nil { + c.Status(http.StatusBadRequest) + } else { + informationModels, status := informationModelService.GetInformationModels(c.Request.Context(), &filters) + if status == http.StatusOK { + c.JSON(status, informationModels) + } else { + c.Status(status) + } + } + } +} + func GetInformationModel() func(c *gin.Context) { informationModelService := service.InitInformationModelService() return func(c *gin.Context) { diff --git a/handlers/service_handlers.go b/handlers/service_handlers.go index 7818d84..08ec8e9 100644 --- a/handlers/service_handlers.go +++ b/handlers/service_handlers.go @@ -1,6 +1,7 @@ package handlers import ( + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/service" @@ -10,7 +11,7 @@ import ( func GetServices() func(c *gin.Context) { serviceService := service.InitServiceService() return func(c *gin.Context) { - services, status := serviceService.GetServices(c.Request.Context()) + services, status := serviceService.GetServices(c.Request.Context(), nil) if status == http.StatusOK { c.JSON(status, services) } else { @@ -19,6 +20,24 @@ func GetServices() func(c *gin.Context) { } } +func FilterServices() func(c *gin.Context) { + serviceService := service.InitServiceService() + return func(c *gin.Context) { + var filters model.Filters + err := c.BindJSON(&filters) + if err != nil { + c.Status(http.StatusBadRequest) + } else { + services, status := serviceService.GetServices(c.Request.Context(), &filters) + if status == http.StatusOK { + c.JSON(status, services) + } else { + c.Status(status) + } + } + } +} + func GetService() func(c *gin.Context) { serviceService := service.InitServiceService() return func(c *gin.Context) { diff --git a/model/filters.go b/model/filters.go new file mode 100644 index 0000000..acc81b1 --- /dev/null +++ b/model/filters.go @@ -0,0 +1,5 @@ +package model + +type Filters struct { + IDs []string `json:"ids"` +} diff --git a/openapi.yaml b/openapi.yaml index 1bd0251..4a00d23 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -29,6 +29,22 @@ paths: type: array items: $ref: '#/components/schemas/Resource' + post: + description: Returns concepts filtered on IDs from body + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Filters" + responses: + '200': + description: OK + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Resource' /concepts/{id}: get: description: Returns single concept @@ -59,6 +75,22 @@ paths: type: array items: $ref: '#/components/schemas/Resource' + post: + description: Returns data services filtered on IDs from body + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Filters" + responses: + '200': + description: OK + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Resource' /data-services/{id}: get: description: Returns single data service @@ -88,6 +120,22 @@ paths: type: array items: $ref: '#/components/schemas/Resource' + post: + description: Returns datasets filtered on IDs from body + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Filters" + responses: + '200': + description: OK + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Resource' /datasets/{id}: get: description: Returns single dataset @@ -117,6 +165,22 @@ paths: type: array items: $ref: '#/components/schemas/Resource' + post: + description: Returns events filtered on IDs from body + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Filters" + responses: + '200': + description: OK + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Resource' /events/{id}: get: description: Returns single event @@ -146,6 +210,22 @@ paths: type: array items: $ref: '#/components/schemas/Resource' + post: + description: Returns information models filtered on IDs from body + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Filters" + responses: + '200': + description: OK + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Resource' /information-models/{id}: get: description: Returns single information model @@ -175,6 +255,22 @@ paths: type: array items: $ref: '#/components/schemas/Resource' + post: + description: Returns services filtered on IDs from body + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Filters" + responses: + '200': + description: OK + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Resource' /services/{id}: get: description: Returns single service @@ -196,6 +292,15 @@ paths: components: schemas: + Filters: + type: object + properties: + ids: + type: array + description: Resource ids + items: + type: string + Resource: type: object properties: diff --git a/service/concept_service.go b/service/concept_service.go index b255807..6a324d6 100644 --- a/service/concept_service.go +++ b/service/concept_service.go @@ -25,8 +25,11 @@ func InitConceptService() *ConceptService { return &service } -func (service ConceptService) GetConcepts(ctx context.Context) ([]map[string]interface{}, int) { +func (service ConceptService) GetConcepts(ctx context.Context, filters *model.Filters) ([]map[string]interface{}, int) { query := bson.D{} + if filters != nil { + query = bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: filters.IDs}}}} + } concepts, err := service.ConceptRepository.GetResources(ctx, query) if err != nil { logrus.Error("Get concepts failed ") diff --git a/service/data_service_service.go b/service/data_service_service.go index d1ba0a1..589af61 100644 --- a/service/data_service_service.go +++ b/service/data_service_service.go @@ -5,10 +5,11 @@ import ( "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/utils/mappers" + "net/http" + "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" - "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/config/logger" "github.com/Informasjonsforvaltning/fdk-resource-service/repository" @@ -25,8 +26,11 @@ func InitDataServiceService() *DataServiceService { return &service } -func (service DataServiceService) GetDataServices(ctx context.Context) ([]map[string]interface{}, int) { +func (service DataServiceService) GetDataServices(ctx context.Context, filters *model.Filters) ([]map[string]interface{}, int) { query := bson.D{} + if filters != nil { + query = bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: filters.IDs}}}} + } dataServices, err := service.DataServiceRepository.GetResources(ctx, query) if err != nil { logrus.Error("Get data services failed ") diff --git a/service/dataset_service.go b/service/dataset_service.go index 46ba4cd..19dd8b8 100644 --- a/service/dataset_service.go +++ b/service/dataset_service.go @@ -4,10 +4,11 @@ import ( "context" "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/model" + "net/http" + "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" - "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/config/logger" "github.com/Informasjonsforvaltning/fdk-resource-service/repository" @@ -25,8 +26,11 @@ func InitDatasetService() *DatasetService { return &service } -func (service DatasetService) GetDatasets(ctx context.Context) ([]map[string]interface{}, int) { +func (service DatasetService) GetDatasets(ctx context.Context, filters *model.Filters) ([]map[string]interface{}, int) { query := bson.D{} + if filters != nil { + query = bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: filters.IDs}}}} + } datasets, err := service.DatasetRepository.GetResources(ctx, query) if err != nil { logrus.Error("Get datasets failed ") diff --git a/service/event_service.go b/service/event_service.go index eade044..7b6bd72 100644 --- a/service/event_service.go +++ b/service/event_service.go @@ -5,10 +5,11 @@ import ( "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/utils/mappers" + "net/http" + "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" - "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/config/logger" "github.com/Informasjonsforvaltning/fdk-resource-service/repository" @@ -25,8 +26,11 @@ func InitEventService() *EventService { return &service } -func (service EventService) GetEvents(ctx context.Context) ([]map[string]interface{}, int) { +func (service EventService) GetEvents(ctx context.Context, filters *model.Filters) ([]map[string]interface{}, int) { query := bson.D{} + if filters != nil { + query = bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: filters.IDs}}}} + } events, err := service.EventRepository.GetResources(ctx, query) if err != nil { logrus.Error("Get events failed ") diff --git a/service/information_model_service.go b/service/information_model_service.go index 2972018..810934a 100644 --- a/service/information_model_service.go +++ b/service/information_model_service.go @@ -5,10 +5,11 @@ import ( "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/utils/mappers" + "net/http" + "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" - "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/config/logger" "github.com/Informasjonsforvaltning/fdk-resource-service/repository" @@ -25,8 +26,11 @@ func InitInformationModelService() *InformationModelService { return &service } -func (service InformationModelService) GetInformationModels(ctx context.Context) ([]map[string]interface{}, int) { +func (service InformationModelService) GetInformationModels(ctx context.Context, filters *model.Filters) ([]map[string]interface{}, int) { query := bson.D{} + if filters != nil { + query = bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: filters.IDs}}}} + } informationModels, err := service.InformationModelRepository.GetResources(ctx, query) if err != nil { logrus.Error("Get information models failed ") diff --git a/service/service_service.go b/service/service_service.go index ecb8b2a..8d4a6a7 100644 --- a/service/service_service.go +++ b/service/service_service.go @@ -5,10 +5,11 @@ import ( "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/utils/mappers" + "net/http" + "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" - "net/http" "github.com/Informasjonsforvaltning/fdk-resource-service/config/logger" "github.com/Informasjonsforvaltning/fdk-resource-service/repository" @@ -25,8 +26,11 @@ func InitServiceService() *ServiceService { return &serviceService } -func (s ServiceService) GetServices(ctx context.Context) ([]map[string]interface{}, int) { +func (s ServiceService) GetServices(ctx context.Context, filters *model.Filters) ([]map[string]interface{}, int) { query := bson.D{} + if filters != nil { + query = bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: filters.IDs}}}} + } services, err := s.ServiceRepository.GetResources(ctx, query) if err != nil { logrus.Error("Get services failed ") diff --git a/test/concepts_test.go b/test/concepts_test.go index 1e1e3c3..3be1cd2 100644 --- a/test/concepts_test.go +++ b/test/concepts_test.go @@ -1,9 +1,11 @@ package test import ( + "bytes" "context" "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/config/router" + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/service" "github.com/stretchr/testify/assert" "net/http" @@ -75,6 +77,51 @@ func TestGetConcepts(t *testing.T) { assert.True(t, slices.Contains(ids, "222")) } +func TestFilterConceptsIncludeOne(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/concepts", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestConcept + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 1) + + var ids []string + for _, concept := range actualResponse { + ids = append(ids, concept.ID) + } + assert.True(t, slices.Contains(ids, "111")) +} + +func TestFilterConceptsIncludeTwo(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111", "222"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/concepts", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestConcept + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 2) + + var ids []string + for _, concept := range actualResponse { + ids = append(ids, concept.ID) + } + assert.True(t, slices.Contains(ids, "111")) + assert.True(t, slices.Contains(ids, "222")) +} + func TestCreateConcept(t *testing.T) { conceptService := service.InitConceptService() diff --git a/test/data_service_test.go b/test/data_service_test.go index dbe1025..7189a22 100644 --- a/test/data_service_test.go +++ b/test/data_service_test.go @@ -1,9 +1,11 @@ package test import ( + "bytes" "context" "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/config/router" + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/service" "github.com/stretchr/testify/assert" "net/http" @@ -75,6 +77,51 @@ func TestGetDataServices(t *testing.T) { assert.True(t, slices.Contains(ids, "222")) } +func TestFilterDataServicesIncludeOne(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/data-services", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestDataService + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 1) + + var ids []string + for _, dataService := range actualResponse { + ids = append(ids, dataService.ID) + } + assert.True(t, slices.Contains(ids, "111")) +} + +func TestFilterDataServicesIncludeTwo(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111", "222"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/data-services", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestDataService + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 2) + + var ids []string + for _, dataService := range actualResponse { + ids = append(ids, dataService.ID) + } + assert.True(t, slices.Contains(ids, "111")) + assert.True(t, slices.Contains(ids, "222")) +} + func TestCreateDataService(t *testing.T) { dataServiceService := service.InitDataServiceService() diff --git a/test/datasets_test.go b/test/datasets_test.go index dc93ea2..e0c15fd 100644 --- a/test/datasets_test.go +++ b/test/datasets_test.go @@ -1,9 +1,11 @@ package test import ( + "bytes" "context" "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/config/router" + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/service" "github.com/stretchr/testify/assert" "net/http" @@ -75,6 +77,51 @@ func TestGetDatasets(t *testing.T) { assert.True(t, slices.Contains(ids, "222")) } +func TestFilterDatasetsIncludeOne(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/datasets", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestDataset + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 1) + + var ids []string + for _, dataset := range actualResponse { + ids = append(ids, dataset.ID) + } + assert.True(t, slices.Contains(ids, "111")) +} + +func TestFilterDatasetsIncludeTwo(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111", "222"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/datasets", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestDataset + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 2) + + var ids []string + for _, dataset := range actualResponse { + ids = append(ids, dataset.ID) + } + assert.True(t, slices.Contains(ids, "111")) + assert.True(t, slices.Contains(ids, "222")) +} + func TestCreateResource(t *testing.T) { datasetService := service.InitDatasetService() diff --git a/test/events_test.go b/test/events_test.go index e8fd52f..3d9d196 100644 --- a/test/events_test.go +++ b/test/events_test.go @@ -1,9 +1,11 @@ package test import ( + "bytes" "context" "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/config/router" + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/service" "github.com/stretchr/testify/assert" "net/http" @@ -75,6 +77,51 @@ func TestGetEvents(t *testing.T) { assert.True(t, slices.Contains(ids, "222")) } +func TestFilterEventsIncludeOne(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/events", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestEvent + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 1) + + var ids []string + for _, event := range actualResponse { + ids = append(ids, event.ID) + } + assert.True(t, slices.Contains(ids, "111")) +} + +func TestFilterEventsIncludeTwo(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111", "222"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/events", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestEvent + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 2) + + var ids []string + for _, event := range actualResponse { + ids = append(ids, event.ID) + } + assert.True(t, slices.Contains(ids, "111")) + assert.True(t, slices.Contains(ids, "222")) +} + func TestCreateEvent(t *testing.T) { eventService := service.InitEventService() diff --git a/test/information_model_test.go b/test/information_model_test.go index de6eeec..f1e26c8 100644 --- a/test/information_model_test.go +++ b/test/information_model_test.go @@ -1,9 +1,11 @@ package test import ( + "bytes" "context" "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/config/router" + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/service" "github.com/stretchr/testify/assert" "net/http" @@ -75,6 +77,51 @@ func TestGetInformationModels(t *testing.T) { assert.True(t, slices.Contains(ids, "222")) } +func TestFilterInformationModelsIncludeOne(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/information-models", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestInformationModel + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 1) + + var ids []string + for _, informationModel := range actualResponse { + ids = append(ids, informationModel.ID) + } + assert.True(t, slices.Contains(ids, "111")) +} + +func TestFilterInformationModelsIncludeTwo(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111", "222"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/information-models", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestInformationModel + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 2) + + var ids []string + for _, informationModel := range actualResponse { + ids = append(ids, informationModel.ID) + } + assert.True(t, slices.Contains(ids, "111")) + assert.True(t, slices.Contains(ids, "222")) +} + func TestCreateInformationModel(t *testing.T) { informationModelService := service.InitInformationModelService() diff --git a/test/services_test.go b/test/services_test.go index 28ec5c2..725e284 100644 --- a/test/services_test.go +++ b/test/services_test.go @@ -1,9 +1,11 @@ package test import ( + "bytes" "context" "encoding/json" "github.com/Informasjonsforvaltning/fdk-resource-service/config/router" + "github.com/Informasjonsforvaltning/fdk-resource-service/model" "github.com/Informasjonsforvaltning/fdk-resource-service/service" "github.com/stretchr/testify/assert" "net/http" @@ -75,6 +77,51 @@ func TestGetServices(t *testing.T) { assert.True(t, slices.Contains(ids, "222")) } +func TestFilterServicesIncludeOne(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/services", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestService + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 1) + + var ids []string + for _, service := range actualResponse { + ids = append(ids, service.ID) + } + assert.True(t, slices.Contains(ids, "111")) +} + +func TestFilterServicesIncludeTwo(t *testing.T) { + app := router.SetupRouter() + body, _ := json.Marshal(model.Filters{IDs: []string{"111", "222"}}) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("POST", "/services", bytes.NewBuffer(body)) + app.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + var actualResponse []TestService + err := json.Unmarshal(w.Body.Bytes(), &actualResponse) + + assert.Nil(t, err) + assert.Equal(t, len(actualResponse), 2) + + var ids []string + for _, service := range actualResponse { + ids = append(ids, service.ID) + } + assert.True(t, slices.Contains(ids, "111")) + assert.True(t, slices.Contains(ids, "222")) +} + func TestCreateService(t *testing.T) { serviceService := service.InitServiceService()