2
2
3
3
import com .github .tomakehurst .wiremock .WireMockServer ;
4
4
import com .github .tomakehurst .wiremock .standalone .CommandLineOptions ;
5
- import com .google .protobuf .AbstractMessage ;
6
- import com .google .protobuf .InvalidProtocolBufferException ;
7
5
import com .google .protobuf .Message ;
8
6
import com .google .protobuf .MessageOrBuilder ;
9
- import com .google .protobuf .util .JsonFormat ;
10
7
import io .adven .grpc .wiremock .configurer .WiremockConfigurer ;
11
8
import org .slf4j .Logger ;
12
9
import org .slf4j .LoggerFactory ;
16
13
import java .io .IOException ;
17
14
import java .io .InputStream ;
18
15
import java .io .UncheckedIOException ;
19
- import java .lang .reflect .InvocationTargetException ;
20
16
import java .net .URI ;
21
17
import java .net .http .HttpClient ;
22
18
import java .net .http .HttpRequest ;
@@ -33,11 +29,13 @@ public class HttpMock {
33
29
private static final Logger LOG = LoggerFactory .getLogger (HttpMock .class );
34
30
private static final String PREFIX = "wiremock_" ;
35
31
private final WiremockConfigurer configurer ;
32
+ private final ProtoJsonConverter converter ;
36
33
private WireMockServer server ;
37
34
private final HttpClient httpClient = HttpClient .newHttpClient ();
38
35
39
- public HttpMock (WiremockConfigurer configurer ) {
36
+ public HttpMock (WiremockConfigurer configurer , ProtoJsonConverter converter ) {
40
37
this .configurer = configurer ;
38
+ this .converter = converter ;
41
39
}
42
40
43
41
public void start () {
@@ -90,7 +88,8 @@ public Response request(String path, Object message, Map<String, String> headers
90
88
.headers (headers .entrySet ().stream ().flatMap (e -> Stream .of (e .getKey (), e .getValue ())).toArray (String []::new ))
91
89
.build (),
92
90
HttpResponse .BodyHandlers .ofInputStream ()
93
- )
91
+ ),
92
+ converter
94
93
);
95
94
}
96
95
@@ -100,14 +99,16 @@ public Response request(String path, Object message) throws IOException, Interru
100
99
101
100
public static final class Response {
102
101
private final HttpResponse <InputStream > httpResponse ;
102
+ private final ProtoJsonConverter converter ;
103
103
104
- public Response (HttpResponse <InputStream > httpResponse ) {
104
+ public Response (HttpResponse <InputStream > httpResponse , ProtoJsonConverter converter ) {
105
105
this .httpResponse = httpResponse ;
106
+ this .converter = converter ;
106
107
}
107
108
108
109
public Message getMessage (Class <?> aClass ) {
109
110
if (httpResponse .statusCode () == 200 ) {
110
- return ProtoJsonUtil .fromJson (getBody (), aClass );
111
+ return converter .fromJson (getBody (), aClass );
111
112
}
112
113
throw new BadHttpResponseException (httpResponse .statusCode (), getBody ());
113
114
}
@@ -130,26 +131,6 @@ private boolean isGzip() {
130
131
}
131
132
132
133
private HttpRequest .BodyPublisher asJson (Object arg ) throws IOException {
133
- return HttpRequest .BodyPublishers .ofString (ProtoJsonUtil .toJson ((MessageOrBuilder ) arg ));
134
- }
135
-
136
- private static final class ProtoJsonUtil {
137
- static String toJson (MessageOrBuilder messageOrBuilder ) throws IOException {
138
- return JsonFormat .printer ().print (messageOrBuilder );
139
- }
140
-
141
- @ SuppressWarnings ({"unchecked" , "rawtypes" })
142
- static <T extends Message > T fromJson (String json , Class <?> clazz ) {
143
- try {
144
- LOG .info ("Converting to {} json:\n {}" , clazz , json );
145
- AbstractMessage .Builder builder = (AbstractMessage .Builder ) clazz .getMethod ("newBuilder" ).invoke (null );
146
- JsonFormat .parser ().merge (json , builder );
147
- T result = (T ) builder .build ();
148
- LOG .info ("Grpc response:\n {}" , result );
149
- return result ;
150
- } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InvalidProtocolBufferException e ) {
151
- throw new IllegalArgumentException ("Failed to convert " + json + " to " + clazz , e );
152
- }
153
- }
134
+ return HttpRequest .BodyPublishers .ofString (converter .toJson ((MessageOrBuilder ) arg ));
154
135
}
155
136
}
0 commit comments