json_test.TestJSON_Marshal   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
dl 0
loc 17
rs 9.85
c 0
b 0
f 0
nop 1
1
package json_test
2
3
import (
4
	"encoding/json"
5
	"testing"
6
7
	myJson "github.com/memnix/memnix-rest/pkg/json"
8
)
9
10
// MockJSONHelper is a mock implementation of the Helper interface for testing purposes.
11
type MockJSONHelper struct {
12
	MarshalFunc   func(v interface{}) ([]byte, error)
13
	UnmarshalFunc func(data []byte, v interface{}) error
14
}
15
16
func (m *MockJSONHelper) Marshal(v interface{}) ([]byte, error) {
17
	if m.MarshalFunc != nil {
18
		return m.MarshalFunc(v)
19
	}
20
	return nil, nil
21
}
22
23
func (m *MockJSONHelper) Unmarshal(data []byte, v interface{}) error {
24
	if m.UnmarshalFunc != nil {
25
		return m.UnmarshalFunc(data, v)
26
	}
27
	return nil
28
}
29
30
func TestJSON_Marshal(t *testing.T) {
31
	// Create a JSON instance with a mock JSON helper
32
	mockHelper := &MockJSONHelper{
33
		MarshalFunc: func(_ interface{}) ([]byte, error) {
34
			return []byte(`{"mocked":true}`), nil
35
		},
36
	}
37
	jsonHelper := myJson.NewJSON(mockHelper)
38
39
	// Test JSON.Marshal method
40
	result, err := jsonHelper.Marshal(map[string]interface{}{"key": "value"})
41
	if err != nil {
42
		t.Errorf("JSON.Marshal should not return an error, but got: %v", err)
43
	}
44
	expectedJSON := `{"mocked":true}`
45
	if string(result) != expectedJSON {
46
		t.Errorf("Expected JSON: %s, but got: %s", expectedJSON, string(result))
47
	}
48
}
49
50
func TestJSON_Unmarshal(t *testing.T) {
51
	// Create a JSON instance with a mock JSON helper
52
	mockHelper := &MockJSONHelper{
53
		UnmarshalFunc: func(_ []byte, v interface{}) error {
54
			return json.Unmarshal([]byte(`{"mocked":true}`), v)
55
		},
56
	}
57
	jsonHelper := myJson.NewJSON(mockHelper)
58
59
	// Test JSON.Unmarshal method
60
	var output map[string]interface{}
61
	err := jsonHelper.Unmarshal([]byte(`{"key": "value"}`), &output)
62
	if err != nil {
63
		t.Errorf("JSON.Unmarshal should not return an error, but got: %v", err)
64
	}
65
	expectedOutput := map[string]interface{}{"mocked": true}
66
	if !compareJSON(output, expectedOutput) {
67
		t.Errorf("Expected output: %v, but got: %v", expectedOutput, output)
68
	}
69
}
70
71
func TestNativeJSON_Marshal(t *testing.T) {
72
	nativeJSONHelper := &myJson.NativeJSON{}
73
74
	// Test NativeJSON.Marshal method
75
	result, err := nativeJSONHelper.Marshal(map[string]interface{}{"key": "value"})
76
	if err != nil {
77
		t.Errorf("NativeJSON.Marshal should not return an error, but got: %v", err)
78
	}
79
	expectedJSON := `{"key":"value"}`
80
	if string(result) != expectedJSON {
81
		t.Errorf("Expected JSON: %s, but got: %s", expectedJSON, string(result))
82
	}
83
}
84
85
func TestNativeJSON_Unmarshal(t *testing.T) {
86
	nativeJSONHelper := &myJson.NativeJSON{}
87
88
	// Test NativeJSON.Unmarshal method
89
	var output map[string]interface{}
90
	err := nativeJSONHelper.Unmarshal([]byte(`{"key": "value"}`), &output)
91
	if err != nil {
92
		t.Errorf("NativeJSON.Unmarshal should not return an error, but got: %v", err)
93
	}
94
	expectedOutput := map[string]interface{}{"key": "value"}
95
	if !compareJSON(output, expectedOutput) {
96
		t.Errorf("Expected output: %v, but got: %v", expectedOutput, output)
97
	}
98
}
99
100
// compareJSON compares two JSON objects for equality.
101
func compareJSON(a, b map[string]interface{}) bool {
102
	aJSON, _ := json.Marshal(a)
103
	bJSON, _ := json.Marshal(b)
104
	return string(aJSON) == string(bJSON)
105
}
106