1+ // Copyright 2025 Google LLC
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // https://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ using Microsoft . AspNetCore . Builder ;
16+ using Microsoft . AspNetCore . Hosting ;
17+ using Microsoft . Extensions . DependencyInjection ;
18+ using Microsoft . Extensions . Hosting ;
19+
20+ namespace Google . Cloud . SpannerLib . MockServer ;
21+
22+ /// <summary>
23+ /// Helper class for starting an in-memory mock Spanner server that is used for testing.
24+ /// </summary>
25+ public class MockServerStartup ( MockSpannerService mockSpannerService , MockDatabaseAdminService mockDatabaseAdminService )
26+ {
27+ /// <summary>
28+ /// The in-mem Spanner service.
29+ /// </summary>
30+ private MockSpannerService MockSpannerService { get ; } = mockSpannerService ;
31+
32+ /// <summary>
33+ /// The in-mem Spanner database admin service for executing DDL operations.
34+ /// </summary>
35+ private MockDatabaseAdminService MockDatabaseAdminService { get ; } = mockDatabaseAdminService ;
36+
37+ /// <summary>
38+ /// Configures the services that will be available on this gRPC server.
39+ /// This method is called by reflection when the tests are started.
40+ /// </summary>
41+ /// <param name="services">The services collection where the services should be added</param>
42+ public void ConfigureServices ( IServiceCollection services )
43+ {
44+ services . AddGrpc ( ) ;
45+ services . AddSingleton ( MockSpannerService ) ;
46+ services . AddSingleton ( MockDatabaseAdminService ) ;
47+ }
48+
49+ /// <summary>
50+ /// Configures the gRPC server. This method is called by reflection when the tests are started.
51+ /// </summary>
52+ /// <param name="app">The builder for the application that will be hosting the service</param>
53+ /// <param name="env">The webhost environment that is hosting the service</param>
54+ public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
55+ {
56+ if ( env . IsDevelopment ( ) )
57+ {
58+ app . UseDeveloperExceptionPage ( ) ;
59+ }
60+
61+ app . UseRouting ( ) ;
62+ app . UseEndpoints ( endpoints =>
63+ {
64+ endpoints . MapGrpcService < MockSpannerService > ( ) ;
65+ endpoints . MapGrpcService < MockDatabaseAdminService > ( ) ;
66+ } ) ;
67+ }
68+ }
0 commit comments