■ ■ ■ ■ ■ ■
plugin/src/test/java/com/google/tsunami/plugin/PluginServiceClientTest.java
| skipped 15 lines |
16 | 16 | | package com.google.tsunami.plugin; |
17 | 17 | | |
18 | 18 | | import static com.google.common.truth.Truth.assertThat; |
| 19 | + | import static java.util.concurrent.TimeUnit.SECONDS; |
19 | 20 | | |
| 21 | + | import com.google.common.collect.Lists; |
20 | 22 | | import com.google.common.util.concurrent.ListenableFuture; |
21 | | - | import com.google.common.util.concurrent.ListeningScheduledExecutorService; |
22 | | - | import com.google.inject.Guice; |
23 | | - | import com.google.inject.Key; |
24 | | - | import com.google.tsunami.common.concurrent.ScheduledThreadPoolModule; |
25 | 23 | | import com.google.tsunami.common.data.NetworkEndpointUtils; |
26 | 24 | | import com.google.tsunami.proto.DetectionReport; |
27 | 25 | | import com.google.tsunami.proto.DetectionReportList; |
| 26 | + | import com.google.tsunami.proto.ListPluginsRequest; |
| 27 | + | import com.google.tsunami.proto.ListPluginsResponse; |
28 | 28 | | import com.google.tsunami.proto.MatchedPlugin; |
29 | 29 | | import com.google.tsunami.proto.NetworkEndpoint; |
30 | 30 | | import com.google.tsunami.proto.NetworkService; |
| skipped 4 lines |
35 | 35 | | import com.google.tsunami.proto.RunResponse; |
36 | 36 | | import com.google.tsunami.proto.TargetInfo; |
37 | 37 | | import com.google.tsunami.proto.TransportProtocol; |
| 38 | + | import io.grpc.Deadline; |
38 | 39 | | import io.grpc.inprocess.InProcessChannelBuilder; |
39 | 40 | | import io.grpc.inprocess.InProcessServerBuilder; |
40 | 41 | | import io.grpc.stub.StreamObserver; |
41 | 42 | | import io.grpc.testing.GrpcCleanupRule; |
42 | 43 | | import io.grpc.util.MutableHandlerRegistry; |
43 | | - | import java.lang.annotation.Retention; |
44 | | - | import java.lang.annotation.RetentionPolicy; |
45 | | - | import java.time.Duration; |
46 | 44 | | import java.util.ArrayList; |
47 | 45 | | import java.util.List; |
48 | | - | import javax.inject.Qualifier; |
49 | 46 | | import org.junit.Before; |
50 | 47 | | import org.junit.Rule; |
51 | 48 | | import org.junit.Test; |
| skipped 4 lines |
56 | 53 | | @RunWith(JUnit4.class) |
57 | 54 | | public final class PluginServiceClientTest { |
58 | 55 | | |
59 | | - | // TODO(b/236740807): Create a wrapper for results and errors. |
60 | | - | |
61 | | - | // Useful test thread pool used for testing grpc handlers |
62 | | - | @Qualifier |
63 | | - | @Retention(RetentionPolicy.RUNTIME) |
64 | | - | @interface TestThreadPool {} |
65 | | - | |
66 | 56 | | @Rule public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); |
67 | 57 | | |
68 | | - | private static final int THREAD_POOLS = 1; |
69 | | - | private static final String THREAD_POOL_NAME = "test"; |
70 | | - | |
71 | 58 | | private static final String PLUGIN_NAME = "test plugin"; |
72 | 59 | | private static final String PLUGIN_VERSION = "0.0.1"; |
73 | 60 | | private static final String PLUGIN_DESCRIPTION = "test description"; |
74 | 61 | | private static final String PLUGIN_AUTHOR = "tester"; |
75 | 62 | | |
76 | | - | private static final Duration DURATION_DEFAULT = Duration.ofSeconds(1); |
| 63 | + | private static final Deadline DEADLINE_DEFAULT = Deadline.after(1, SECONDS); |
77 | 64 | | |
78 | 65 | | private PluginServiceClient pluginService; |
79 | 66 | | private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry(); |
| skipped 10 lines |
90 | 77 | | |
91 | 78 | | pluginService = |
92 | 79 | | new PluginServiceClient( |
93 | | - | InProcessChannelBuilder.forName(serverName).directExecutor().build(), |
94 | | - | Guice.createInjector( |
95 | | - | new ScheduledThreadPoolModule.Builder() |
96 | | - | .setName(THREAD_POOL_NAME) |
97 | | - | .setSize(THREAD_POOLS) |
98 | | - | .setAnnotation(TestThreadPool.class) |
99 | | - | .build()) |
100 | | - | .getInstance( |
101 | | - | Key.get(ListeningScheduledExecutorService.class, TestThreadPool.class))); |
| 80 | + | InProcessChannelBuilder.forName(serverName).directExecutor().build()); |
102 | 81 | | } |
103 | 82 | | |
104 | 83 | | @Test |
| skipped 14 lines |
119 | 98 | | }; |
120 | 99 | | serviceRegistry.addService(runImpl); |
121 | 100 | | |
122 | | - | ListenableFuture<RunResponse> run = pluginService.runWithDeadline(runRequest, DURATION_DEFAULT); |
| 101 | + | ListenableFuture<RunResponse> run = pluginService.runWithDeadline(runRequest, DEADLINE_DEFAULT); |
123 | 102 | | RunResponse runResponse = run.get(); |
124 | 103 | | |
125 | 104 | | assertThat(run.isDone()).isTrue(); |
| skipped 20 lines |
146 | 125 | | }; |
147 | 126 | | serviceRegistry.addService(runImpl); |
148 | 127 | | |
149 | | - | ListenableFuture<RunResponse> run = pluginService.runWithDeadline(runRequest, DURATION_DEFAULT); |
| 128 | + | ListenableFuture<RunResponse> run = pluginService.runWithDeadline(runRequest, DEADLINE_DEFAULT); |
150 | 129 | | RunResponse runResponse = run.get(); |
151 | 130 | | |
152 | 131 | | assertThat(run.isDone()).isTrue(); |
| skipped 55 lines |
208 | 187 | | }; |
209 | 188 | | serviceRegistry.addService(runImpl); |
210 | 189 | | |
211 | | - | ListenableFuture<RunResponse> run = pluginService.runWithDeadline(runRequest, DURATION_DEFAULT); |
| 190 | + | ListenableFuture<RunResponse> run = pluginService.runWithDeadline(runRequest, DEADLINE_DEFAULT); |
212 | 191 | | RunResponse runResponse = run.get(); |
213 | 192 | | |
214 | 193 | | assertThat(run.isDone()).isTrue(); |
| skipped 1 lines |
216 | 195 | | assertRunResponseContainsAllRunRequestParameters(runResponse, runRequest); |
217 | 196 | | } |
218 | 197 | | |
| 198 | + | @Test |
| 199 | + | public void listPlugins_returnsMultiplePlugins() throws Exception { |
| 200 | + | ListPluginsRequest request = ListPluginsRequest.getDefaultInstance(); |
| 201 | + | |
| 202 | + | List<PluginDefinition> plugins = Lists.newArrayList(); |
| 203 | + | for (int i = 0; i < 5; i++) { |
| 204 | + | plugins.add(createSinglePluginDefinitionWithName(String.format(PLUGIN_NAME + "%d", i))); |
| 205 | + | } |
| 206 | + | |
| 207 | + | PluginServiceImplBase listPluginsImpl = |
| 208 | + | new PluginServiceImplBase() { |
| 209 | + | @Override |
| 210 | + | public void listPlugins( |
| 211 | + | ListPluginsRequest request, StreamObserver<ListPluginsResponse> responseObserver) { |
| 212 | + | responseObserver.onNext( |
| 213 | + | ListPluginsResponse.newBuilder().addAllPlugins(plugins).build()); |
| 214 | + | responseObserver.onCompleted(); |
| 215 | + | } |
| 216 | + | }; |
| 217 | + | serviceRegistry.addService(listPluginsImpl); |
| 218 | + | |
| 219 | + | ListenableFuture<ListPluginsResponse> listPlugins = |
| 220 | + | pluginService.listPluginsWithDeadline(request, DEADLINE_DEFAULT); |
| 221 | + | |
| 222 | + | assertThat(listPlugins.isDone()).isTrue(); |
| 223 | + | assertThat(listPlugins.get().getPluginsList()).containsExactlyElementsIn(plugins); |
| 224 | + | } |
| 225 | + | |
219 | 226 | | private void assertRunResponseContainsAllRunRequestParameters( |
220 | 227 | | RunResponse response, RunRequest request) throws Exception { |
221 | 228 | | for (MatchedPlugin plugin : request.getPluginsList()) { |
| skipped 34 lines |
256 | 263 | | .addPlugins(MatchedPlugin.newBuilder().addServices(httpService).setPlugin(singlePlugin)) |
257 | 264 | | .build(); |
258 | 265 | | } |
259 | | - | // TODO(b/236740807): Add test case for errors related to RPC calls once wrapper CL is done. |
260 | 266 | | } |
261 | 267 | | |