This repository was archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathHandledExceptionTest.java
More file actions
82 lines (71 loc) · 3.41 KB
/
HandledExceptionTest.java
File metadata and controls
82 lines (71 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package me.alidg.errors;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.http.HttpStatus;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Arrays.asList;
import static java.util.Collections.*;
import static me.alidg.Params.p;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
/**
* Unit tests for {@link HandledException} class.
*
* @author Ali Dehghani
*/
public class HandledExceptionTest {
@ParameterizedTest
@MethodSource("provideParamsForPrimary")
public void primaryConstructor_ShouldEnforceItsPreconditions(Set<String> errorCodes,
HttpStatus status,
Class<? extends Throwable> expected,
String message) {
assertThatThrownBy(() -> new HandledException(errorCodes, status, singletonMap("error", emptyList())))
.isInstanceOf(expected)
.hasMessage(message);
}
@ParameterizedTest
@MethodSource("provideParamsForSecondary")
public void secondConstructor_ShouldEnforceItsPreconditions(String errorCode,
HttpStatus status,
Class<? extends Throwable> expected,
String message) {
assertThatThrownBy(() -> new HandledException(errorCode, status, singletonMap("error", emptyList())))
.isInstanceOf(expected)
.hasMessage(message);
}
@ParameterizedTest
@MethodSource("provideMaps")
public void constructors_ShouldSetNullArgumentsAsEmptyMaps(Map<String, List<Argument>> provided,
Map<?, ?> expected) {
assertThat(new HandledException(singleton("error"), BAD_REQUEST, provided).getArguments())
.isEqualTo(expected);
assertThat(new HandledException("error", BAD_REQUEST, provided).getArguments())
.isEqualTo(expected);
}
private static Object[] provideParamsForPrimary() {
return p(
p(null, null, NullPointerException.class, "Error codes is required"),
p(new HashSet<>(asList("", "", null)), null, NullPointerException.class, "Status code is required"),
p(singleton(null), BAD_REQUEST, NullPointerException.class, "The single error code can't be null"),
p(emptySet(), BAD_REQUEST, IllegalArgumentException.class, "At least one error code should be provided")
);
}
private static Object[] provideParamsForSecondary() {
return p(
p(null, null, NullPointerException.class, "Status code is required"),
p("error", null, NullPointerException.class, "Status code is required"),
p(null, BAD_REQUEST, NullPointerException.class, "The single error code can't be null")
);
}
private static Object[] provideMaps() {
return p(
p(null, emptyMap()),
p(singletonMap("key", emptyList()), singletonMap("key", emptyList()))
);
}
}