Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Extract data classes from fhir with system and code #9

Merged
merged 3 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ package api

import (
"bytes"
"github.com/golang/mock/gomock"
"github.com/nuts-foundation/nuts-fhir-validation/pkg"
"github.com/nuts-foundation/nuts-go-core/mock"
"fmt"
"io/ioutil"
"net/http"
"testing"

"github.com/golang/mock/gomock"
"github.com/nuts-foundation/nuts-fhir-validation/pkg"
core "github.com/nuts-foundation/nuts-go-core"
"github.com/nuts-foundation/nuts-go-core/mock"
)

func TestDefaultValidationBackend_Validate(t *testing.T) {
Expand Down Expand Up @@ -96,7 +99,7 @@ func validationResult() ValidationResponse {
Consent: &SimplifiedConsent{
Actors: []Identifier{"urn:oid:2.16.840.1.113883.2.4.6.1:00000007"},
Custodian: Identifier("urn:oid:2.16.840.1.113883.2.4.6.1:00000000"),
Resources: []string{"Observation"},
Resources: []string{"http://hl7.org/fhir/resource-types#Observation", fmt.Sprintf("urn:oid:%s:MEDICAL", core.NutsConsentClassesOID)},
Subject: Identifier("urn:oid:2.16.840.1.113883.2.4.6.3:999999990"),
},
Outcome: "valid",
Expand Down
4 changes: 4 additions & 0 deletions examples/observation_consent.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@
{
"system": "http://hl7.org/fhir/resource-types",
"code": "Observation"
},
{
"system": "urn:oid:1.3.6.1.4.1.54851.1",
"code": "MEDICAL"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/magiconair/properties v1.8.1 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/nuts-foundation/nuts-go-core v0.0.0-20191028170600-66675906a53a
github.com/nuts-foundation/nuts-go-core v0.0.0-20191218133145-27ebcf628fab
github.com/pelletier/go-toml v1.5.0 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ github.com/nuts-foundation/nuts-go-core v0.0.0-20191014142450-dd0fd3a25ffb h1:Sd
github.com/nuts-foundation/nuts-go-core v0.0.0-20191014142450-dd0fd3a25ffb/go.mod h1:tPPkfE9y+T+jTc8klZ0B8FXtc23p9NIpg5BjI+FIiEs=
github.com/nuts-foundation/nuts-go-core v0.0.0-20191028170600-66675906a53a h1:0r1YHKAMSiP6yFy322lNclOU53LVfdkuRFsdQBJ/NU0=
github.com/nuts-foundation/nuts-go-core v0.0.0-20191028170600-66675906a53a/go.mod h1:+IeGzu5J8qlF4Jcol+mY+MRcLDIfJ8OQnQV4wDp887o=
github.com/nuts-foundation/nuts-go-core v0.0.0-20191218133145-27ebcf628fab h1:Dxjdaw+HToUVmh2H1/pg+BFNzNWm72bqVlA3LqcCiGs=
github.com/nuts-foundation/nuts-go-core v0.0.0-20191218133145-27ebcf628fab/go.mod h1:+IeGzu5J8qlF4Jcol+mY+MRcLDIfJ8OQnQV4wDp887o=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down
24 changes: 19 additions & 5 deletions pkg/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/thedevsaddam/gojsonq.v2"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -61,20 +62,33 @@ func ValidatorInstance() *Validator {
return instance
}

// ResourcesFrom extracts the consent resources from some fhir json
func ResourcesFrom(jsonq *gojsonq.JSONQ) []string {
var resources []string
// DataClassesFrom extracts the consent provision classes from some fhir json, replaces ResourcesFrom
// It combines the system and code field to a single string using the correct divider (: or #) based on the type of system
func DataClassesFrom(jsonq *gojsonq.JSONQ) []string {
var dataClasses []string
listOfClasses := jsonq.Copy().From("provision.provision").Pluck("class").([]interface{})

// lists of lists
for _, classList := range listOfClasses {
cls := classList.([]interface{})
for _, cl := range cls {
clMap := cl.(map[string]interface{})
resources = append(resources, fmt.Sprintf("%s", clMap["code"]))
system := clMap["system"].(string)
divider := "#"

if strings.Index(system, "urn:oid") != -1 {
divider = ":"
}

dataClasses = append(dataClasses, fmt.Sprintf("%s%s%s", clMap["system"], divider, clMap["code"]))
}
}
return resources
return dataClasses
}

// ResourcesFrom extracts the consent resources from some fhir json, deprecated, replaced by DataClassesFrom
func ResourcesFrom(jsonq *gojsonq.JSONQ) []string {
return DataClassesFrom(jsonq)
}

// ActorsFrom extracts the consent actors from some fhir json
Expand Down
23 changes: 20 additions & 3 deletions pkg/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
package pkg

import (
"github.com/stretchr/testify/assert"
"gopkg.in/thedevsaddam/gojsonq.v2"
"fmt"
"io/ioutil"
"testing"
"time"

core "github.com/nuts-foundation/nuts-go-core"
"github.com/stretchr/testify/assert"
"gopkg.in/thedevsaddam/gojsonq.v2"
)

func TestDefaultValidationBackend_ValidateAgainstSchema(t *testing.T) {
Expand Down Expand Up @@ -88,6 +91,20 @@ func validationBackend() Validator {
return client
}

func TestResourcesFrom(t *testing.T) {
bytes, _ := ioutil.ReadFile("../examples/observation_consent.json")
jsonq := gojsonq.New().JSONString(string(bytes))
dataClasses := ResourcesFrom(jsonq)

t.Run("with namespace", func(t *testing.T) {
assert.Equal(t, "http://hl7.org/fhir/resource-types#Observation", dataClasses[0])
})

t.Run("with urn", func(t *testing.T) {
assert.Equal(t, fmt.Sprintf("urn:oid:%s:MEDICAL", core.NutsConsentClassesOID), dataClasses[1])
})
}

func TestPeriodFrom(t *testing.T) {
//"start": "2016-06-23T17:02:33+10:00",
//"end": "2016-06-23T17:32:33+10:00"
Expand All @@ -107,4 +124,4 @@ func TestVersionFrom(t *testing.T) {
jsonq := gojsonq.New().JSONString(string(bytes))

assert.Equal(t, "1", VersionFrom(jsonq))
}
}