Skip to content

Commit 5c62f2e

Browse files
committed
test: add tests
1 parent ffbc0bf commit 5c62f2e

File tree

11 files changed

+1634
-0
lines changed

11 files changed

+1634
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.xiaoju.open.sdk.didies.core
2+
3+
import spock.lang.Specification
4+
5+
class ConstantsTest extends Specification {
6+
7+
def "VERSION should be defined"() {
8+
expect:
9+
Constants.VERSION == "1.0.0"
10+
}
11+
12+
def "USER_AGENT should be defined"() {
13+
expect:
14+
Constants.USER_AGENT == "User-Agent"
15+
}
16+
17+
def "DIDI_AGENT should contain version"() {
18+
expect:
19+
Constants.DIDI_AGENT == "didies-open-sdk-java/1.0.0"
20+
Constants.DIDI_AGENT.contains(Constants.VERSION)
21+
}
22+
23+
def "INVALID_ACCESS_TOKEN_CODE should be 401"() {
24+
expect:
25+
Constants.INVALID_ACCESS_TOKEN_CODE == 401
26+
}
27+
28+
def "AUTH_URI should be defined"() {
29+
expect:
30+
Constants.AUTH_URI == "/river/Auth/authorize"
31+
}
32+
33+
def "OBJECT_SUFFIX should be defined"() {
34+
expect:
35+
Constants.OBJECT_SUFFIX == "__obj__"
36+
}
37+
38+
def "BASE_REQ_NAMES should be accessible"() {
39+
expect:
40+
Constants.BASE_REQ_NAMES != null
41+
}
42+
43+
def "BaseReqName should contain all required fields"() {
44+
expect:
45+
Constants.BASE_REQ_NAMES.ACCESS_TOKEN == "access_token"
46+
Constants.BASE_REQ_NAMES.CLIENT_ID == "client_id"
47+
Constants.BASE_REQ_NAMES.TIMESTAMP == "timestamp"
48+
Constants.BASE_REQ_NAMES.SIGN == "sign"
49+
Constants.BASE_REQ_NAMES.ENCRYPT_CONTENT == "encrypt_content"
50+
Constants.BASE_REQ_NAMES.ENT == "ent"
51+
}
52+
53+
def "Constants should not be instantiable"() {
54+
when:
55+
def constructor = Constants.getDeclaredConstructor()
56+
constructor.setAccessible(true)
57+
constructor.newInstance()
58+
59+
then:
60+
thrown(Exception) // Abstract class cannot be instantiated
61+
}
62+
63+
def "BaseReqName should be instantiable"() {
64+
when:
65+
def baseReqName = new Constants.BaseReqName()
66+
67+
then:
68+
baseReqName != null
69+
baseReqName instanceof Constants.BaseReqName
70+
}
71+
72+
def "all constant values should be immutable"() {
73+
expect:
74+
// These are final, so trying to reassign should not be possible
75+
Constants.VERSION.getClass() == String
76+
Constants.INVALID_ACCESS_TOKEN_CODE.getClass() == Integer
77+
}
78+
79+
def "DIDI_AGENT format should be correct"() {
80+
expect:
81+
Constants.DIDI_AGENT.startsWith("didies-open-sdk-java/")
82+
Constants.DIDI_AGENT.endsWith("/" + Constants.VERSION)
83+
}
84+
85+
def "all BaseReqName fields should be strings"() {
86+
expect:
87+
Constants.BASE_REQ_NAMES.ACCESS_TOKEN instanceof String
88+
Constants.BASE_REQ_NAMES.CLIENT_ID instanceof String
89+
Constants.BASE_REQ_NAMES.TIMESTAMP instanceof String
90+
Constants.BASE_REQ_NAMES.SIGN instanceof String
91+
Constants.BASE_REQ_NAMES.ENCRYPT_CONTENT instanceof String
92+
Constants.BASE_REQ_NAMES.ENT instanceof String
93+
}
94+
95+
def "constant values should not be empty"() {
96+
expect:
97+
!Constants.VERSION.isEmpty()
98+
!Constants.USER_AGENT.isEmpty()
99+
!Constants.DIDI_AGENT.isEmpty()
100+
!Constants.AUTH_URI.isEmpty()
101+
!Constants.OBJECT_SUFFIX.isEmpty()
102+
}
103+
104+
def "AUTH_URI should start with slash"() {
105+
expect:
106+
Constants.AUTH_URI.startsWith("/")
107+
}
108+
109+
def "OBJECT_SUFFIX should contain double underscores"() {
110+
expect:
111+
Constants.OBJECT_SUFFIX.contains("__")
112+
}
113+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.xiaoju.open.sdk.didies.core.enums
2+
3+
import spock.lang.Specification
4+
import spock.lang.Unroll
5+
6+
class EncryptTypeEnumTest extends Specification {
7+
8+
def "EncryptTypeEnum should have three values"() {
9+
expect:
10+
EncryptTypeEnum.values().length == 3
11+
}
12+
13+
def "EncryptTypeEnum should contain NORMAL, AES128, and AES256"() {
14+
expect:
15+
EncryptTypeEnum.values() as Set == [EncryptTypeEnum.NORMAL, EncryptTypeEnum.AES128, EncryptTypeEnum.AES256] as Set
16+
}
17+
18+
@Unroll
19+
def "EncryptTypeEnum #enum should have correct type value"() {
20+
expect:
21+
encryptType.type == expectedType
22+
23+
where:
24+
encryptType | expectedType
25+
EncryptTypeEnum.NORMAL | 0
26+
EncryptTypeEnum.AES128 | 1
27+
EncryptTypeEnum.AES256 | 2
28+
}
29+
30+
@Unroll
31+
def "EncryptTypeEnum #enum should have correct description"() {
32+
expect:
33+
encryptType.description == expectedDescription
34+
35+
where:
36+
encryptType | expectedDescription
37+
EncryptTypeEnum.NORMAL | "不加密"
38+
EncryptTypeEnum.AES128 | "aes 128"
39+
EncryptTypeEnum.AES256 | "aes 256"
40+
}
41+
42+
def "NORMAL should have type 0"() {
43+
expect:
44+
EncryptTypeEnum.NORMAL.type == 0
45+
EncryptTypeEnum.NORMAL.description == "不加密"
46+
}
47+
48+
def "AES128 should have type 1"() {
49+
expect:
50+
EncryptTypeEnum.AES128.type == 1
51+
EncryptTypeEnum.AES128.description == "aes 128"
52+
}
53+
54+
def "AES256 should have type 2"() {
55+
expect:
56+
EncryptTypeEnum.AES256.type == 2
57+
EncryptTypeEnum.AES256.description == "aes 256"
58+
}
59+
60+
def "valueOf should return correct enum for valid name"() {
61+
expect:
62+
EncryptTypeEnum.valueOf("NORMAL") == EncryptTypeEnum.NORMAL
63+
EncryptTypeEnum.valueOf("AES128") == EncryptTypeEnum.AES128
64+
EncryptTypeEnum.valueOf("AES256") == EncryptTypeEnum.AES256
65+
}
66+
67+
def "valueOf should throw exception for invalid name"() {
68+
when:
69+
EncryptTypeEnum.valueOf("INVALID")
70+
71+
then:
72+
thrown(IllegalArgumentException)
73+
}
74+
75+
def "enum values should be in correct order"() {
76+
expect:
77+
EncryptTypeEnum.values()[0] == EncryptTypeEnum.NORMAL
78+
EncryptTypeEnum.values()[1] == EncryptTypeEnum.AES128
79+
EncryptTypeEnum.values()[2] == EncryptTypeEnum.AES256
80+
}
81+
82+
def "all enum values should have non-null type and description"() {
83+
expect:
84+
EncryptTypeEnum.values().every { it.type != null }
85+
EncryptTypeEnum.values().every { it.description != null }
86+
}
87+
88+
def "all types should be unique"() {
89+
expect:
90+
EncryptTypeEnum.values().collect { it.type }.size() ==
91+
EncryptTypeEnum.values().collect { it.type }.unique().size()
92+
}
93+
94+
@Unroll
95+
def "getter methods should work for #enum"() {
96+
expect:
97+
encryptType.getType() != null
98+
encryptType.getDescription() != null
99+
100+
where:
101+
encryptType << EncryptTypeEnum.values()
102+
}
103+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.xiaoju.open.sdk.didies.core.enums
2+
3+
import spock.lang.Specification
4+
5+
class LogLevelEnumTest extends Specification {
6+
7+
def "LogLevelEnum should have two values"() {
8+
expect:
9+
LogLevelEnum.values().length == 2
10+
}
11+
12+
def "LogLevelEnum should contain DEBUG and INFO"() {
13+
expect:
14+
LogLevelEnum.values() as Set == [LogLevelEnum.DEBUG, LogLevelEnum.INFO] as Set
15+
}
16+
17+
def "DEBUG should have correct level value"() {
18+
expect:
19+
LogLevelEnum.DEBUG.level == "debug"
20+
}
21+
22+
def "INFO should have correct level value"() {
23+
expect:
24+
LogLevelEnum.INFO.level == "info"
25+
}
26+
27+
def "valueOf should return correct enum for valid name"() {
28+
expect:
29+
LogLevelEnum.valueOf("DEBUG") == LogLevelEnum.DEBUG
30+
LogLevelEnum.valueOf("INFO") == LogLevelEnum.INFO
31+
}
32+
33+
def "valueOf should throw exception for invalid name"() {
34+
when:
35+
LogLevelEnum.valueOf("INVALID")
36+
37+
then:
38+
thrown(IllegalArgumentException)
39+
}
40+
41+
def "enum values should be in correct order"() {
42+
expect:
43+
LogLevelEnum.values()[0] == LogLevelEnum.DEBUG
44+
LogLevelEnum.values()[1] == LogLevelEnum.INFO
45+
}
46+
47+
def "all enum values should have non-null level"() {
48+
expect:
49+
LogLevelEnum.values().every { it.level != null }
50+
}
51+
52+
def "all levels should be unique"() {
53+
expect:
54+
LogLevelEnum.values().collect { it.level }.size() ==
55+
LogLevelEnum.values().collect { it.level }.unique().size()
56+
}
57+
58+
def "getter method should work for all enums"() {
59+
expect:
60+
LogLevelEnum.DEBUG.getLevel() == "debug"
61+
LogLevelEnum.INFO.getLevel() == "info"
62+
}
63+
64+
def "level values should be lowercase"() {
65+
expect:
66+
LogLevelEnum.DEBUG.level == LogLevelEnum.DEBUG.level.toLowerCase()
67+
LogLevelEnum.INFO.level == LogLevelEnum.INFO.level.toLowerCase()
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.xiaoju.open.sdk.didies.core.enums
2+
3+
import spock.lang.Specification
4+
5+
class SignMethodEnumTest extends Specification {
6+
7+
def "SignMethodEnum should have two values"() {
8+
expect:
9+
SignMethodEnum.values().length == 2
10+
}
11+
12+
def "SignMethodEnum should contain MD5 and SHA256"() {
13+
expect:
14+
SignMethodEnum.values() as Set == [SignMethodEnum.MD5, SignMethodEnum.SHA256] as Set
15+
}
16+
17+
def "MD5 should have correct signMethod value"() {
18+
expect:
19+
SignMethodEnum.MD5.signMethod == "MD5"
20+
}
21+
22+
def "SHA256 should have correct signMethod value"() {
23+
expect:
24+
SignMethodEnum.SHA256.signMethod == "SHA256"
25+
}
26+
27+
def "valueOf should return correct enum for valid name"() {
28+
expect:
29+
SignMethodEnum.valueOf("MD5") == SignMethodEnum.MD5
30+
SignMethodEnum.valueOf("SHA256") == SignMethodEnum.SHA256
31+
}
32+
33+
def "valueOf should throw exception for invalid name"() {
34+
when:
35+
SignMethodEnum.valueOf("INVALID")
36+
37+
then:
38+
thrown(IllegalArgumentException)
39+
}
40+
41+
def "enum values should be in correct order"() {
42+
expect:
43+
SignMethodEnum.values()[0] == SignMethodEnum.MD5
44+
SignMethodEnum.values()[1] == SignMethodEnum.SHA256
45+
}
46+
47+
def "all enum values should have non-null signMethod"() {
48+
expect:
49+
SignMethodEnum.values().every { it.signMethod != null }
50+
}
51+
52+
def "all signMethods should be unique"() {
53+
expect:
54+
SignMethodEnum.values().collect { it.signMethod }.size() ==
55+
SignMethodEnum.values().collect { it.signMethod }.unique().size()
56+
}
57+
58+
def "getter method should work for all enums"() {
59+
expect:
60+
SignMethodEnum.MD5.getSignMethod() == "MD5"
61+
SignMethodEnum.SHA256.getSignMethod() == "SHA256"
62+
}
63+
64+
def "signMethod values should match standard algorithm names"() {
65+
expect:
66+
SignMethodEnum.MD5.signMethod.toUpperCase() == "MD5"
67+
SignMethodEnum.SHA256.signMethod.toUpperCase() == "SHA256"
68+
}
69+
}

0 commit comments

Comments
 (0)