1package publicapi
2
3import (
4	"encoding/json"
5	"fmt"
6	"testing"
7	"time"
8
9	"github.com/stretchr/testify/require"
10)
11
12func TestSnapshot_UnmarshalJSON(t *testing.T) {
13	var (
14		testCreatedAt, _ = time.Parse(iso8601Format, "2020-08-12T11:12:36Z")
15		testID           = testRandomID(t)
16		testInstanceID   = testRandomID(t)
17		testName         = "test_ROOT-846459_20200706132534"
18		testExportMD5Sum = "c9887de796993c2519b463bcd9509e08"
19		testExportURL    = fmt.Sprintf("https://sos-ch-gva-2.exo.io/test/%s/%s", testRandomID(t), testID)
20		testState        = SnapshotStateExported
21
22		expected = Snapshot{
23			CreatedAt: &testCreatedAt,
24			Export: &struct {
25				Md5sum       *string `json:"md5sum,omitempty"`
26				PresignedUrl *string `json:"presigned-url,omitempty"` // nolint:revive
27			}{
28				Md5sum:       &testExportMD5Sum,
29				PresignedUrl: &testExportURL,
30			},
31			Id:       &testID,
32			Instance: &Instance{Id: &testInstanceID},
33			Name:     &testName,
34			State:    &testState,
35		}
36
37		actual Snapshot
38
39		jsonSnapshot = `{
40  "created-at": "` + testCreatedAt.Format(iso8601Format) + `",
41  "export": {"md5sum": "` + testExportMD5Sum + `", "presigned-url": "` + testExportURL + `"},
42  "id": "` + testID + `",
43  "instance": {"id": "` + testInstanceID + `"},
44  "name": "` + testName + `",
45  "state": "` + string(testState) + `"
46}`
47	)
48
49	require.NoError(t, json.Unmarshal([]byte(jsonSnapshot), &actual))
50	require.Equal(t, expected, actual)
51}
52
53func TestSnapshot_MarshalJSON(t *testing.T) {
54	var (
55		testCreatedAt, _ = time.Parse(iso8601Format, "2020-08-12T11:12:36Z")
56		testID           = testRandomID(t)
57		testInstanceID   = testRandomID(t)
58		testName         = "test_ROOT-846459_20200706132534"
59		testExportMD5Sum = "c9887de796993c2519b463bcd9509e08"
60		testExportURL    = fmt.Sprintf("https://sos-ch-gva-2.exo.io/test/%s/%s", testRandomID(t), testID)
61		testState        = SnapshotStateExported
62
63		snapshot = Snapshot{
64			CreatedAt: &testCreatedAt,
65			Export: &struct {
66				Md5sum       *string `json:"md5sum,omitempty"`
67				PresignedUrl *string `json:"presigned-url,omitempty"` // nolint:revive
68			}{
69				Md5sum:       &testExportMD5Sum,
70				PresignedUrl: &testExportURL,
71			},
72			Id:       &testID,
73			Instance: &Instance{Id: &testInstanceID},
74			Name:     &testName,
75			State:    &testState,
76		}
77
78		expected = []byte(`{` +
79			`"created-at":"` + testCreatedAt.Format(iso8601Format) + `",` +
80			`"export":{"md5sum":"` + testExportMD5Sum + `","presigned-url":"` + testExportURL + `"},` +
81			`"id":"` + testID + `",` +
82			`"instance":{"id":"` + testInstanceID + `"},` +
83			`"name":"` + testName + `",` +
84			`"state":"` + string(testState) + `"` +
85			`}`)
86	)
87
88	actual, err := json.Marshal(snapshot)
89	require.NoError(t, err)
90	require.Equal(t, expected, actual)
91}
92