-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathServiceRegistrationTest.java
More file actions
162 lines (137 loc) · 5.09 KB
/
ServiceRegistrationTest.java
File metadata and controls
162 lines (137 loc) · 5.09 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package io.scalecube.services.gateway.rest;
import static io.scalecube.services.api.ServiceMessage.HEADER_REQUEST_METHOD;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import io.scalecube.services.Microservices;
import io.scalecube.services.Microservices.Context;
import io.scalecube.services.annotations.RestMethod;
import io.scalecube.services.annotations.Service;
import io.scalecube.services.annotations.ServiceMethod;
import io.scalecube.services.api.ServiceMessage;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import reactor.core.publisher.Mono;
public class ServiceRegistrationTest {
@ParameterizedTest
@ValueSource(
classes = {
EchoService.class,
GoodRestService.class,
CreateRestService.class,
UpdateRestService.class
})
void registerDuplicateService(Class<?> serviceInterface) {
try (final var microservices =
Microservices.start(
new Context().services(mock(serviceInterface), mock(serviceInterface)))) {
fail("Expected exception");
} catch (Exception e) {
assertInstanceOf(IllegalStateException.class, e, e::getMessage);
assertThat(e.getMessage(), Matchers.startsWith("MethodInvoker already exists"));
}
}
@Test
void registerInvalidRestService() {
try (final var microservices =
Microservices.start(new Context().services(mock(BadRestService.class)))) {
fail("Expected exception");
} catch (Exception e) {
assertInstanceOf(IllegalStateException.class, e, e::getMessage);
assertThat(e.getMessage(), Matchers.startsWith("MethodInvoker already exists"));
}
}
@Test
void registerSingleValidRestService() {
try (final var microservices =
Microservices.start(new Context().services(mock(GoodRestService.class)))) {
final var serviceRegistry = microservices.serviceRegistry();
final var foo = System.nanoTime();
final var methodInvokerWithoutRestMethod =
serviceRegistry.lookupInvoker(
ServiceMessage.builder().qualifier("v1/service/echo/" + foo).build());
assertNull(methodInvokerWithoutRestMethod);
final var methodInvokerByGet =
serviceRegistry.lookupInvoker(
ServiceMessage.builder()
.header(HEADER_REQUEST_METHOD, "GET")
.qualifier("v1/service/echo/" + foo)
.build());
assertNotNull(methodInvokerByGet);
final var methodInvokerByPost =
serviceRegistry.lookupInvoker(
ServiceMessage.builder()
.header(HEADER_REQUEST_METHOD, "POST")
.qualifier("v1/service/echo/" + foo)
.build());
assertNotNull(methodInvokerByPost);
}
}
@Test
void registerMultipleValidRestServices() {
try (final var microservices =
Microservices.start(
new Context().services(mock(CreateRestService.class), mock(UpdateRestService.class)))) {
final var serviceRegistry = microservices.serviceRegistry();
final var foo = System.nanoTime();
final var methodInvokerWithoutRestMethod =
serviceRegistry.lookupInvoker(
ServiceMessage.builder().qualifier("v1/service/account/" + foo).build());
assertNull(methodInvokerWithoutRestMethod);
final var methodInvokerByPost =
serviceRegistry.lookupInvoker(
ServiceMessage.builder()
.header(HEADER_REQUEST_METHOD, "POST")
.qualifier("v1/service/account/" + foo)
.build());
assertNotNull(methodInvokerByPost);
final var methodInvokerByPut =
serviceRegistry.lookupInvoker(
ServiceMessage.builder()
.header(HEADER_REQUEST_METHOD, "PUT")
.qualifier("v1/service/account/" + foo)
.build());
assertNotNull(methodInvokerByPut);
}
}
@Service("v1/service")
interface EchoService {
@ServiceMethod("get/:foo")
Mono<SomeResponse> echo();
}
@Service("v1/service")
interface BadRestService {
@RestMethod("GET")
@ServiceMethod("get/:foo")
Mono<SomeResponse> echo();
@RestMethod("GET")
@ServiceMethod("get/:foo")
Mono<SomeResponse> ping();
}
@Service("v1/service")
interface GoodRestService {
@RestMethod("GET")
@ServiceMethod("echo/:foo")
Mono<SomeResponse> echo();
@RestMethod("POST")
@ServiceMethod("echo/:foo")
Mono<SomeResponse> ping();
}
@Service("v1/service")
interface CreateRestService {
@RestMethod("POST")
@ServiceMethod("account/:foo")
Mono<SomeResponse> account();
}
@Service("v1/service")
interface UpdateRestService {
@RestMethod("PUT")
@ServiceMethod("account/:foo")
Mono<SomeResponse> account();
}
}