32
32
import com .google .api .client .testing .http .MockLowLevelHttpRequest ;
33
33
import com .google .api .client .testing .http .MockLowLevelHttpResponse ;
34
34
import com .google .api .services .compute .Compute ;
35
- import com .google .api .services .compute .model .Autoscaler ;
36
- import com .google .api .services .compute .model .AutoscalerAggregatedList ;
37
- import com .google .api .services .compute .model .AutoscalerList ;
38
- import com .google .api .services .compute .model .AutoscalersScopedList ;
39
- import com .google .api .services .compute .model .Instance ;
40
- import com .google .api .services .compute .model .InstanceAggregatedList ;
41
- import com .google .api .services .compute .model .InstanceGroupManager ;
42
- import com .google .api .services .compute .model .InstanceGroupManagerList ;
43
- import com .google .api .services .compute .model .InstanceList ;
44
- import com .google .api .services .compute .model .InstanceTemplate ;
45
- import com .google .api .services .compute .model .InstanceTemplateList ;
46
- import com .google .api .services .compute .model .InstancesScopedList ;
47
- import com .google .api .services .compute .model .RegionInstanceGroupManagerList ;
35
+ import com .google .api .services .compute .model .*;
48
36
import com .google .common .collect .ImmutableList ;
49
37
import com .google .common .collect .ImmutableListMultimap ;
50
38
import com .google .common .collect .ImmutableMap ;
@@ -72,8 +60,10 @@ final class StubComputeFactory {
72
60
73
61
private static final JsonFactory JSON_FACTORY = JacksonFactory .getDefaultInstance ();
74
62
63
+ private static final String COMPUTE_PATH_PREFIX = "/compute/[-.a-zA-Z0-9]+" ;
64
+
75
65
private static final String COMPUTE_PROJECT_PATH_PREFIX =
76
- "/compute/[-.a-zA-Z0-9]+ /projects/[-.a-zA-Z0-9]+" ;
66
+ COMPUTE_PATH_PREFIX + " /projects/[-.a-zA-Z0-9]+" ;
77
67
78
68
private static final Pattern BATCH_COMPUTE_PATTERN =
79
69
Pattern .compile ("/batch/compute/[-.a-zA-Z0-9]+" );
@@ -114,11 +104,15 @@ final class StubComputeFactory {
114
104
Pattern .compile (COMPUTE_PROJECT_PATH_PREFIX + "/regions/([-a-z0-9]+)/autoscalers" );
115
105
private static final Pattern AGGREGATED_AUTOSCALERS_PATTERN =
116
106
Pattern .compile (COMPUTE_PROJECT_PATH_PREFIX + "/aggregated/autoscalers" );
107
+ private static final Pattern GET_PROJECT_PATTERN =
108
+ Pattern .compile (COMPUTE_PATH_PREFIX + "/projects/([-.a-zA-Z0-9]+)" );
117
109
118
110
private List <InstanceGroupManager > instanceGroupManagers = new ArrayList <>();
119
111
private List <InstanceTemplate > instanceTemplates = new ArrayList <>();
120
112
private List <Instance > instances = new ArrayList <>();
121
113
private List <Autoscaler > autoscalers = new ArrayList <>();
114
+ private List <Project > projects = new ArrayList <>();
115
+ private Exception projectException ;
122
116
123
117
StubComputeFactory setInstanceGroupManagers (InstanceGroupManager ... instanceGroupManagers ) {
124
118
this .instanceGroupManagers = ImmutableList .copyOf (instanceGroupManagers );
@@ -140,6 +134,16 @@ StubComputeFactory setAutoscalers(Autoscaler... autoscalers) {
140
134
return this ;
141
135
}
142
136
137
+ StubComputeFactory setProjects (Project ... projects ) {
138
+ this .projects = ImmutableList .copyOf (projects );
139
+ return this ;
140
+ }
141
+
142
+ StubComputeFactory setProjectException (Exception projectException ) {
143
+ this .projectException = projectException ;
144
+ return this ;
145
+ }
146
+
143
147
Compute create () {
144
148
HttpTransport httpTransport =
145
149
new StubHttpTransport ()
@@ -166,7 +170,8 @@ Compute create() {
166
170
.addGetResponse (
167
171
LIST_REGIONAL_AUTOSCALERS_PATTERN ,
168
172
new PathBasedJsonResponseGenerator (this ::regionalAutoscalerList ))
169
- .addGetResponse (AGGREGATED_AUTOSCALERS_PATTERN , this ::autoscalerAggregatedList );
173
+ .addGetResponse (AGGREGATED_AUTOSCALERS_PATTERN , this ::autoscalerAggregatedList )
174
+ .addGetResponse (GET_PROJECT_PATTERN , this ::project );
170
175
return new Compute (
171
176
httpTransport , JacksonFactory .getDefaultInstance (), /* httpRequestInitializer= */ null );
172
177
}
@@ -322,6 +327,21 @@ private MockLowLevelHttpResponse autoscalerAggregatedList(LowLevelHttpRequest re
322
327
return jsonResponse (new AutoscalerAggregatedList ().setItems (autoscalers ));
323
328
}
324
329
330
+ private MockLowLevelHttpResponse project (MockLowLevelHttpRequest request ) {
331
+ if (projectException != null ) {
332
+ return errorResponse (500 , projectException );
333
+ }
334
+
335
+ Matcher matcher = GET_PROJECT_PATTERN .matcher (getPath (request ));
336
+ checkState (matcher .matches ());
337
+ String name = matcher .group (1 );
338
+ return projects .stream ()
339
+ .filter (project -> name .equals (project .getName ()))
340
+ .findFirst ()
341
+ .map (StubComputeFactory ::jsonResponse )
342
+ .orElse (errorResponse (404 ));
343
+ }
344
+
325
345
private static <T > ImmutableListMultimap <String , T > aggregate (
326
346
Collection <T > items , Function <T , String > zoneFunction , Function <T , String > regionFunction ) {
327
347
return items .stream ()
@@ -345,9 +365,18 @@ private static <T> String getAggregateKey(
345
365
}
346
366
347
367
private static MockLowLevelHttpResponse errorResponse (int statusCode ) {
368
+ return errorResponse (statusCode , null );
369
+ }
370
+
371
+ private static MockLowLevelHttpResponse errorResponse (int statusCode , Exception exception ) {
348
372
GoogleJsonErrorContainer errorContainer = new GoogleJsonErrorContainer ();
349
373
GoogleJsonError error = new GoogleJsonError ();
350
374
error .setCode (statusCode );
375
+
376
+ if (exception != null ) {
377
+ error .setMessage (exception .getMessage ());
378
+ }
379
+
351
380
errorContainer .setError (error );
352
381
return jsonResponse (statusCode , errorContainer );
353
382
}
0 commit comments