@@ -2,8 +2,8 @@ package common.validation
2
2
3
3
import java .net .URL
4
4
5
- import cats .data .NonEmptyList
6
5
import cats .data .Validated .{Invalid , Valid }
6
+ import cats .data .{NonEmptyList , ValidatedNel }
7
7
import cats .syntax .validated ._
8
8
import com .typesafe .config .ConfigFactory
9
9
import common .exception .AggregatedMessageException
@@ -49,6 +49,21 @@ class ValidationSpec extends FlatSpec with Matchers with Mockito {
49
49
url.getPath should be(" /world" )
50
50
}
51
51
52
+ it should " convert a valid to a successful future" in {
53
+ val nel : ValidatedNel [String , String ] = " ok" .valid
54
+ val actual = nel.toFuture(_ => throw new RuntimeException (" Should not be used" ))
55
+ actual.value should be(Option (Success (" ok" )))
56
+ }
57
+
58
+ it should " convert a invalidNel to an unsuccessful future" in {
59
+ val nel : ValidatedNel [String , String ] = " failed" .invalidNel
60
+ val actual = nel.toFuture(nel => new RuntimeException (nel.toList.mkString(" processed " , " ," , " and caught" )))
61
+ actual.value match {
62
+ case Some (Failure (exception : RuntimeException )) => exception.getMessage should be(" processed failed and caught" )
63
+ case other => fail(s " expected message: 'processed failed and caught' but got ' $other' " )
64
+ }
65
+ }
66
+
52
67
it should " succeed to validate a valid value" in {
53
68
val result = validate(" hello" )
54
69
result should be(" hello" .valid)
@@ -85,4 +100,20 @@ class ValidationSpec extends FlatSpec with Matchers with Mockito {
85
100
exception.errorMessages should contain theSameElementsAs List (" :(" )
86
101
}
87
102
103
+ it should " convert a Some to an ErrorOr" in {
104
+ Option (" ok" ).toErrorOr(" not used" ) should be(" ok" .valid)
105
+ }
106
+
107
+ it should " convert a None to an ErrorOr" in {
108
+ None .toErrorOr(" error message" ) should be(" error message" .invalidNel)
109
+ }
110
+
111
+ it should " convert a Some to an Checked" in {
112
+ Option (" ok" ).toChecked(" not used" ) should be(Right (" ok" ))
113
+ }
114
+
115
+ it should " convert a None to an Checked" in {
116
+ None .toChecked(" error message" ) should be(Left (NonEmptyList .one(" error message" )))
117
+ }
118
+
88
119
}
0 commit comments