validators_actor_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package validators
  15. import (
  16. "testing"
  17. "yunion.io/x/pkg/util/netutils"
  18. )
  19. type actorJoinedByCase struct {
  20. sep string
  21. trimSpace bool
  22. ignoreEmpty bool
  23. C
  24. }
  25. func (ac *actorJoinedByCase) Validator() *ValidatorByActor {
  26. actor := NewActorJoinedBy(ac.sep,
  27. NewActorIPv4Prefix(),
  28. ).TrimSpace(ac.trimSpace).IgnoreEmpty(ac.ignoreEmpty)
  29. return NewValidatorByActor("s", actor)
  30. }
  31. func TestJoinedByActor(t *testing.T) {
  32. var valsWant []interface{}
  33. for _, n := range []string{"10.0.0.0/8", "192.168.0.0/16"} {
  34. p, _ := netutils.NewIPV4Prefix(n)
  35. valsWant = append(valsWant, &p)
  36. }
  37. cases := []*actorJoinedByCase{
  38. {
  39. C: C{
  40. Name: "missing non-optional",
  41. In: `{}`,
  42. Out: `{}`,
  43. Optional: false,
  44. Err: ERR_MISSING_KEY,
  45. ValueWant: nil,
  46. },
  47. },
  48. {
  49. C: C{
  50. Name: "missing optional",
  51. In: `{}`,
  52. Out: `{}`,
  53. Optional: true,
  54. ValueWant: nil,
  55. },
  56. },
  57. {
  58. sep: ",",
  59. C: C{
  60. Name: "missing with default",
  61. In: `{}`,
  62. Out: `{s: "10.0.0.0/8,192.168.0.0/16"}`,
  63. Default: "10.0.0.0/8,192.168.0.0/16",
  64. ValueWant: valsWant,
  65. },
  66. },
  67. {
  68. sep: ",",
  69. C: C{
  70. Name: "good in",
  71. In: `{"s": "10.0.0.0/8,192.168.0.0/16"}`,
  72. Out: `{"s": "10.0.0.0/8,192.168.0.0/16"}`,
  73. ValueWant: valsWant,
  74. },
  75. },
  76. {
  77. sep: ",",
  78. ignoreEmpty: true,
  79. C: C{
  80. Name: "good in (nothing)",
  81. In: `{"s": ""}`,
  82. Out: `{"s": ""}`,
  83. ValueWant: []interface{}{},
  84. },
  85. },
  86. {
  87. sep: ",",
  88. C: C{
  89. Name: "good in (0.0.0.0/32)",
  90. In: `{"s": ""}`,
  91. Out: `{"s": ""}`,
  92. ValueWant: []interface{}{func() *netutils.IPV4Prefix {
  93. p, _ := netutils.NewIPV4Prefix("0.0.0.0/32")
  94. return &p
  95. }()},
  96. },
  97. },
  98. {
  99. sep: ",",
  100. trimSpace: true,
  101. ignoreEmpty: true,
  102. C: C{
  103. Name: "good in (ignore empty, trim space)",
  104. In: `{"s": ",,, 10.0.0.0/8 , 192.168.0.0/16, ,,"}`,
  105. Out: `{"s": "10.0.0.0/8,192.168.0.0/16"}`,
  106. ValueWant: valsWant,
  107. },
  108. },
  109. {
  110. sep: ",",
  111. ignoreEmpty: false,
  112. C: C{
  113. Name: "bad in (empty)",
  114. In: `{"s": ",,, 10.0.0.0/8 , 192.168.0.0/16, ,,"}`,
  115. Out: `{"s": ",,, 10.0.0.0/8 , 192.168.0.0/16, ,,"}`,
  116. Err: ERR_INVALID_VALUE,
  117. ValueWant: nil,
  118. },
  119. },
  120. {
  121. sep: ",",
  122. trimSpace: false,
  123. C: C{
  124. Name: "bad in (space)",
  125. In: `{"s": ",,, 10.0.0.0/8 , 192.168.0.0/16, ,,"}`,
  126. Out: `{"s": ",,, 10.0.0.0/8 , 192.168.0.0/16, ,,"}`,
  127. Err: ERR_INVALID_VALUE,
  128. ValueWant: nil,
  129. },
  130. },
  131. {
  132. C: C{
  133. Name: "bad in (bad value)",
  134. In: `{"s": "10.0.0.259/32"}`,
  135. Out: `{"s": "10.0.0.259/32"}`,
  136. Err: ERR_INVALID_VALUE,
  137. ValueWant: nil,
  138. },
  139. },
  140. }
  141. for _, c := range cases {
  142. t.Run(c.C.Name, func(t *testing.T) {
  143. v := c.Validator()
  144. if c.Default != nil {
  145. s := c.Default.(string)
  146. v.Default(s)
  147. }
  148. if c.Optional {
  149. v.Optional(true)
  150. }
  151. testS(t, v, &c.C)
  152. })
  153. }
  154. }