Skip to content

Commit 6e2e14e

Browse files
authored
chore: additional unit tests (#7)
* chore: added validate command test cases (wip) * fix: test name * chore: additional message tests * chore: add result test cases * chore: add parser tests * chore: fix format
1 parent 1bd880a commit 6e2e14e

File tree

7 files changed

+312
-19
lines changed

7 files changed

+312
-19
lines changed

cmd/validate_test.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"testing"
@@ -17,6 +18,9 @@ import (
1718
"github.com/ChargePi/chargeflow/pkg/ocpp"
1819
)
1920

21+
var validOcppRequest = "[2, \"1234567890\", \"Authorize\", {\"idTag\": \"1234567890\"}]"
22+
var validOcppResponse = "[3, \"1234567890\", {\"idTagInfo\": {\"status\": \"Accepted\"}}]"
23+
2024
func Test_registerAdditionalSchemas(t *testing.T) {
2125
logger := zap.L()
2226
registry = schema_registry.NewInMemorySchemaRegistry(logger)
@@ -80,40 +84,57 @@ func Test_registerAdditionalSchemas(t *testing.T) {
8084
}
8185
}
8286

83-
func Test_registerSchemas(t *testing.T) {
84-
85-
}
86-
8787
func Test_Validate(t *testing.T) {
88+
l, _ := zap.NewProduction()
89+
zap.ReplaceGlobals(l)
90+
8891
tests := []struct {
8992
name string
9093
args []string
9194
flags map[string]string
9295
expectedErr error
9396
}{
9497
{
95-
name: "Defaults",
98+
name: "OCPP 1.6 Request",
99+
args: []string{validOcppRequest},
100+
flags: map[string]string{},
101+
},
102+
{
103+
name: "OCPP 1.6 Response",
104+
args: []string{validOcppResponse},
105+
flags: map[string]string{
106+
"response-type": "Authorize",
107+
},
96108
},
97109
{
98-
name: "Invalid OCPP message",
110+
name: "Invalid OCPP message",
111+
args: []string{"{\"invalid\": \"message\"}"},
112+
flags: map[string]string{},
99113
},
100114
{
101115
name: "Invalid OCPP version",
116+
args: []string{validOcppRequest},
117+
flags: map[string]string{
118+
"ocpp-version": "invalid_version",
119+
},
120+
},
121+
{
122+
name: "Provided response without response-type",
123+
args: []string{validOcppResponse},
124+
flags: map[string]string{},
102125
},
103126
}
104127

105128
for _, test := range tests {
106129
t.Run(test.name, func(t *testing.T) {
107-
r := require.New(t)
108-
109130
// Setup: Set command arguments and flags
110-
validate.SetArgs(test.args)
111-
131+
args := test.args
112132
for flag, value := range test.flags {
113-
err := validate.Flags().Set(flag, value)
114-
r.NoError(err)
133+
args = append(args, fmt.Sprintf("--%s=%s", flag, value))
115134
}
116135

136+
validate.SetArgs(args)
137+
117138
// Execute the command
118139
err := validate.Execute()
119140
if test.expectedErr != nil {

pkg/ocpp/message.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package ocpp
22

3-
import (
4-
"fmt"
5-
)
6-
73
// MessageType identifies the type of message exchanged between two OCPP endpoints.
84
type MessageType int
95

@@ -124,7 +120,7 @@ func FormatErrorType(version Version) ErrorCode {
124120
case V20:
125121
return FormatViolationV2
126122
default:
127-
panic(fmt.Sprintf("invalid dialect"))
123+
panic("invalid dialect")
128124
}
129125
}
130126

@@ -135,7 +131,7 @@ func OccurrenceConstraintErrorType(version Version) ErrorCode {
135131
case V20:
136132
return OccurrenceConstraintViolationV2
137133
default:
138-
panic(fmt.Sprintf("invalid dialect"))
134+
panic("invalid dialect")
139135
}
140136
}
141137

pkg/ocpp/message_test.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package ocpp
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFormatErrorType(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
version Version
13+
expected ErrorCode
14+
}{
15+
{
16+
name: "OCPP 1.6",
17+
version: V16,
18+
expected: FormatViolationV16,
19+
},
20+
{
21+
name: "OCPP 2.0",
22+
version: V20,
23+
expected: FormatViolationV2,
24+
},
25+
{
26+
name: "Invalid Version",
27+
version: "",
28+
},
29+
}
30+
31+
for _, tt := range tests {
32+
t.Run(tt.name, func(t *testing.T) {
33+
if tt.name == "Invalid Version" {
34+
assert.Panics(t, func() {
35+
_ = FormatErrorType(tt.version)
36+
})
37+
} else {
38+
result := FormatErrorType(tt.version)
39+
assert.Equal(t, result, tt.expected)
40+
}
41+
})
42+
}
43+
}
44+
45+
func TestOccurrenceConstraintErrorType(t *testing.T) {
46+
tests := []struct {
47+
name string
48+
version Version
49+
expected ErrorCode
50+
}{
51+
{
52+
name: "OCPP 1.6",
53+
version: V16,
54+
expected: OccurrenceConstraintViolationV16,
55+
},
56+
{
57+
name: "OCPP 2.0",
58+
version: V20,
59+
expected: OccurrenceConstraintViolationV2,
60+
},
61+
{
62+
name: "Invalid Version",
63+
},
64+
}
65+
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
if tt.name == "Invalid Version" {
69+
assert.Panics(t, func() {
70+
_ = OccurrenceConstraintErrorType(tt.version)
71+
})
72+
} else {
73+
result := OccurrenceConstraintErrorType(tt.version)
74+
assert.Equal(t, result, tt.expected)
75+
}
76+
})
77+
}
78+
}
79+
80+
func TestIsErrorCodeValid(t *testing.T) {
81+
tests := []struct {
82+
name string
83+
code ErrorCode
84+
expected bool
85+
}{
86+
{
87+
name: "Not implemented",
88+
code: NotImplemented,
89+
expected: true,
90+
},
91+
{
92+
name: "Not supported",
93+
code: NotSupported,
94+
expected: true,
95+
},
96+
{
97+
name: "Internal error",
98+
code: InternalError,
99+
expected: true,
100+
},
101+
{
102+
name: "Format violation",
103+
code: FormatViolationV16,
104+
expected: true,
105+
}, {
106+
name: "Format violation",
107+
code: FormatViolationV2,
108+
expected: true,
109+
},
110+
{
111+
name: "Security error",
112+
code: SecurityError,
113+
expected: true,
114+
},
115+
{
116+
name: "Property constraint violation",
117+
code: PropertyConstraintViolation,
118+
expected: true,
119+
},
120+
{
121+
name: "Occurrence constraint violation",
122+
code: OccurrenceConstraintViolationV16,
123+
expected: true,
124+
},
125+
{
126+
name: "Occurrence constraint violation",
127+
code: OccurrenceConstraintViolationV2,
128+
expected: true,
129+
},
130+
{
131+
name: "Type constraint violation",
132+
code: TypeConstraintViolation,
133+
expected: true,
134+
},
135+
{
136+
name: "Generic error",
137+
code: GenericError,
138+
expected: true,
139+
},
140+
{
141+
name: "Unknown error",
142+
code: "UnknownError",
143+
expected: false,
144+
},
145+
}
146+
147+
for _, tt := range tests {
148+
t.Run(tt.name, func(t *testing.T) {
149+
result := IsErrorCodeValid(tt.code)
150+
assert.Equal(t, result, tt.expected)
151+
})
152+
}
153+
}

pkg/parser/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package parser
33
import (
44
"encoding/json"
55
"fmt"
6+
67
"github.com/spf13/viper"
78

89
"github.com/pkg/errors"

pkg/parser/parser_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package parser
22

33
import (
4+
"github.com/ChargePi/chargeflow/pkg/ocpp"
5+
"github.com/pkg/errors"
6+
"go.uber.org/zap"
47
"testing"
58

69
"github.com/stretchr/testify/suite"
@@ -11,7 +14,83 @@ type parserTestSuite struct {
1114
}
1215

1316
func (s *parserTestSuite) TestParseMessage() {
17+
logger, _ := zap.NewDevelopment()
18+
tests := []struct {
19+
name string
20+
data string
21+
expectedMessage ocpp.Message
22+
expectedResult *Result
23+
expectedError error
24+
}{
25+
{
26+
name: "Valid Request",
27+
data: `[2,"1234", "BootNotification", {"chargePointVendor": "TestVendor", "chargePointModel": "TestModel"}]`,
28+
expectedMessage: &ocpp.Call{
29+
MessageTypeId: ocpp.CALL,
30+
UniqueId: "1234",
31+
Payload: map[string]interface{}{"chargePointVendor": "TestVendor", "chargePointModel": "TestModel"},
32+
Action: "BootNotification",
33+
},
34+
expectedResult: NewResult(),
35+
expectedError: nil,
36+
},
37+
{
38+
name: "Valid Response",
39+
data: `[3,"1234", {"status": "Accepted"}]`,
40+
expectedMessage: &ocpp.CallResult{
41+
MessageTypeId: ocpp.CALL_RESULT,
42+
UniqueId: "1234",
43+
Payload: map[string]interface{}{"status": "Accepted"},
44+
Action: "",
45+
},
46+
expectedResult: NewResult(),
47+
expectedError: nil,
48+
},
49+
{
50+
name: "Valid Error",
51+
data: `[4,"1234", "GenericError", "An error occurred"]`,
52+
expectedMessage: &ocpp.CallError{
53+
MessageTypeId: ocpp.CALL_ERROR,
54+
UniqueId: "1234",
55+
ErrorCode: "GenericError",
56+
ErrorDescription: "An error occurred",
57+
},
58+
expectedResult: NewResult(),
59+
expectedError: nil,
60+
},
61+
{
62+
name: "Invalid Message",
63+
data: `[5,"1234", "InvalidMessage"]`,
64+
expectedMessage: nil,
65+
expectedResult: &Result{
66+
errors: []string{"Unknown message type: 5"},
67+
},
68+
expectedError: errors.New("Unknown message type: 5"),
69+
},
70+
{
71+
name: "Invalid JSON",
72+
data: `{"invalid": "json"}`,
73+
expectedMessage: nil,
74+
expectedResult: &Result{
75+
errors: []string{"cannot parse message"},
76+
},
77+
expectedError: errors.New("cannot parse message"),
78+
},
79+
}
1480

81+
for _, tt := range tests {
82+
s.Run(tt.name, func() {
83+
parser := NewParser(logger)
84+
message, result, err := parser.ParseMessage(tt.data)
85+
if tt.expectedError != nil {
86+
s.ErrorContains(err, tt.expectedError.Error())
87+
} else {
88+
s.NoError(err)
89+
}
90+
s.Equal(tt.expectedMessage, message)
91+
s.Equal(tt.expectedResult, result)
92+
})
93+
}
1594
}
1695

1796
func TestParser(t *testing.T) {

0 commit comments

Comments
 (0)