Skip to content

Commit

Permalink
Merge pull request #22 from elimity-com/feature/21-timestamp-support
Browse files Browse the repository at this point in the history
Add optional timestamp to DomainGraph struct
  • Loading branch information
rikbw authored Jun 18, 2020
2 parents b7d804c + 66f099d commit be553a7
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 83 deletions.
9 changes: 8 additions & 1 deletion domaingraph.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package insights

import "net/http"
import (
"net/http"
"time"
)

// AttributeAssignment represents the assignment of an attribute of a given type with a compatible value, which may be
// managed at an Elimity Insights server.
Expand All @@ -27,9 +30,11 @@ func (c Client) ReloadDomainGraph(domainGraph DomainGraph) error {
}

// DomainGraph represents a graph of domain data that may be managed at an Elimity Insights server.
// The Timestamp field is optional.
type DomainGraph struct {
Entities []Entity
Relationships []Relationship
Timestamp *time.Time
}

func (g DomainGraph) model() domainGraphModel {
Expand All @@ -48,6 +53,7 @@ func (g DomainGraph) model() domainGraphModel {
return domainGraphModel{
Entities: entityModels,
Relationships: relationshipModels,
Timestamp: g.Timestamp,
}
}

Expand Down Expand Up @@ -109,6 +115,7 @@ type attributeAssignmentModel struct {
type domainGraphModel struct {
Entities []entityModel `json:"entities"`
Relationships []relationshipModel `json:"relationships"`
Timestamp *time.Time `json:"historyTimestamp,omitempty"`
}

type entityModel struct {
Expand Down
190 changes: 108 additions & 82 deletions domaingraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"

Expand All @@ -12,92 +13,66 @@ import (
)

func TestClientReloadDomainGraph(t *testing.T) {
var fun http.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/domain-graph/reload" {
t.Fatalf(`got path %q, want "/domain-graph/reload"`, request.URL.Path)
}

actualBodyBytes, err := ioutil.ReadAll(request.Body)
if err != nil {
t.Fatalf("failed reading request body: %v", err)
}

var actualBody interface{}
if err := json.Unmarshal(actualBodyBytes, &actualBody); err != nil {
t.Fatalf("failed unmarshalling body: %v", err)
}

expectedBodyString := `{
"entities": [
{
"active": true,
"attributeAssignments": [
{
"attributeTypeName": "foo",
"value": {
"type": "boolean",
"value": "true"
}
},
{
"attributeTypeName": "bar",
"value": {
"type": "date",
"value": "2006-01-02"
}
expectedBodyString := `{
"entities": [
{
"active": true,
"attributeAssignments": [
{
"attributeTypeName": "foo",
"value": {
"type": "boolean",
"value": "true"
}
],
"id": "foo",
"name": "bar",
"type": "baz"
},
{
"active": false,
"attributeAssignments": [
{
"attributeTypeName": "baz",
"value": {
"type": "time",
"value": "15:04:05Z"
}
},
{
"attributeTypeName": "bar",
"value": {
"type": "date",
"value": "2006-01-02"
}
],
"id": "bar",
"name": "baz",
"type": "foo"
}
],
"relationships": [
{
"attributeAssignments": [
{
"attributeTypeName": "asd",
"value": {
"type": "string",
"value": "asd"
}
}
],
"id": "foo",
"name": "bar",
"type": "baz"
},
{
"active": false,
"attributeAssignments": [
{
"attributeTypeName": "baz",
"value": {
"type": "time",
"value": "15:04:05Z"
}
],
"fromId": "foo",
"fromType": "baz",
"toId": "bar",
"toType": "foo"
}
]
}`
expectedBodyBytes := []byte(expectedBodyString)

var expectedBody interface{}
if err := json.Unmarshal(expectedBodyBytes, &expectedBody); err != nil {
panic(err)
}

if diff := cmp.Diff(expectedBody, actualBody); diff != "" {
t.Fatalf("body mismatch (-got, +want):\n%s", diff)
}
}
}
],
"id": "bar",
"name": "baz",
"type": "foo"
}
],
"relationships": [
{
"attributeAssignments": [
{
"attributeTypeName": "asd",
"value": {
"type": "string",
"value": "asd"
}
}
],
"fromId": "foo",
"fromType": "baz",
"toId": "bar",
"toType": "foo"
}
]
}`

client, server := setup(t, fun)
client, server := domainGraphTestClientServer(t, expectedBodyString)
defer server.Close()

fooValue := insights.NewBooleanValue(true)
Expand Down Expand Up @@ -158,3 +133,54 @@ func TestClientReloadDomainGraph(t *testing.T) {
t.Fatalf("failed reloading domain graph: %v", err)
}
}

func TestClientReloadDomainGraphTimestamp(t *testing.T) {
expectedBodyString := `{
"entities": [],
"relationships": [],
"historyTimestamp": "2020-06-15T16:26:10Z"
}`

client, server := domainGraphTestClientServer(t, expectedBodyString)
defer server.Close()

timestamp := time.Date(2020, time.June, 15, 16, 26, 10, 0, time.UTC)
domainGraph := insights.DomainGraph{
Timestamp: &timestamp,
}

if err := client.ReloadDomainGraph(domainGraph); err != nil {
t.Fatalf("failed reloading domain graph: %v", err)
}
}

func domainGraphTestClientServer(t *testing.T, expectedBodyString string) (insights.Client, *httptest.Server) {
var handlerFunc http.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/domain-graph/reload" {
t.Fatalf(`got path %q, want "/domain-graph/reload"`, request.URL.Path)
}

actualBodyBytes, err := ioutil.ReadAll(request.Body)
if err != nil {
t.Fatalf("failed reading request body: %v", err)
}

var actualBody interface{}
if err := json.Unmarshal(actualBodyBytes, &actualBody); err != nil {
t.Fatalf("failed unmarshalling body: %v", err)
}

expectedBodyBytes := []byte(expectedBodyString)

var expectedBody interface{}
if err := json.Unmarshal(expectedBodyBytes, &expectedBody); err != nil {
panic(err)
}

if diff := cmp.Diff(expectedBody, actualBody); diff != "" {
t.Fatalf("body mismatch (-got, +want):\n%s", diff)
}
}

return setup(t, handlerFunc)
}

0 comments on commit be553a7

Please sign in to comment.