-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDispatchHandlerTest.java
More file actions
53 lines (44 loc) · 1.59 KB
/
Copy pathDispatchHandlerTest.java
File metadata and controls
53 lines (44 loc) · 1.59 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
package com.retailsvc.http.internal;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import com.retailsvc.http.MissingOperationHandlerException;
import com.retailsvc.http.Request;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
class DispatchHandlerTest {
private static void withOperationId(
String operationId, ScopedValue.CallableOp<Void, Exception> body) throws Exception {
RequestContext ctx = new RequestContext(new byte[0], null, operationId, Map.of());
ScopedValue.where(Request.CONTEXT, ctx).call(body);
}
@Test
void invokesRegisteredHandler() throws Exception {
HttpHandler handler = mock(HttpHandler.class);
HttpExchange ex = mock(HttpExchange.class);
withOperationId(
"get-x",
() -> {
new DispatchHandler(Map.of("get-x", handler)).handle(ex);
return null;
});
// bound op-id is "get-x"; DispatchHandler should look up the registered HttpHandler.
Mockito.verify(handler).handle(Mockito.any());
}
@Test
void throwsWhenHandlerMissing() {
DispatchHandler d = new DispatchHandler(Map.of());
HttpExchange ex = mock(HttpExchange.class);
assertThatThrownBy(
() ->
withOperationId(
"ghost",
() -> {
d.handle(ex);
return null;
}))
.isInstanceOf(MissingOperationHandlerException.class);
}
}