6
6
import com .e2e_tests .pubsub_emulator .PubSubEmulatorInitializer ;
7
7
import com .google .cloud .spring .pubsub .PubSubAdmin ;
8
8
import com .google .cloud .spring .pubsub .core .PubSubTemplate ;
9
+ import org .apache .commons .lang3 .ThreadUtils ;
9
10
import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
10
11
import org .apache .hc .client5 .http .impl .classic .HttpClients ;
11
12
import org .apache .hc .core5 .http .ClassicHttpRequest ;
12
13
import org .apache .hc .core5 .http .ClassicHttpResponse ;
14
+ import org .apache .hc .core5 .http .ParseException ;
13
15
import org .apache .hc .core5 .http .impl .io .DefaultClassicHttpRequestFactory ;
16
+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
14
17
import org .apache .hc .core5 .http .io .entity .StringEntity ;
15
18
import org .junit .jupiter .api .BeforeEach ;
16
19
import org .junit .jupiter .api .Test ;
23
26
24
27
import java .io .IOException ;
25
28
import java .io .UncheckedIOException ;
29
+ import java .time .Duration ;
26
30
27
31
//@AutoConfigureMockMvc
28
32
@ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .NONE , classes = E2ETests .App .class )
@@ -67,37 +71,52 @@ public void preparePubsubEmulator() {
67
71
68
72
@ Test
69
73
public void simpleEndToEndTest () {
70
-
71
74
GenericContainer mainOwnerService = MainOwnerService .startContainer ();
72
75
GenericContainer labelOwnerService = LabelOwnerService .startContainer ();
73
-
74
76
try {
75
-
76
77
int mainOwnerServiceMappedPort = mainOwnerService .getMappedPort (8080 );
77
-
78
78
Long ownerId = 7L ;
79
79
80
- String body = """
81
- {"id":%s,"name":"name1","address":"address2","phone":"phone3","email":"email4"}""" .formatted (ownerId );
82
- String uploadUri = "http://localhost:%s/upload-initial-owner" .formatted (mainOwnerServiceMappedPort );
83
- ClassicHttpRequest uploadRequest = DefaultClassicHttpRequestFactory .INSTANCE .newHttpRequest ("POST" , uploadUri );
84
- uploadRequest .setEntity (new StringEntity (body ));
85
- uploadRequest .setHeader ("Content-Type" , "application/json" );
86
-
80
+ ClassicHttpRequest uploadRequest = createUploadRequest (ownerId , mainOwnerServiceMappedPort );
87
81
ClassicHttpResponse uploadResponse = sendRequest (uploadRequest );
88
- System .out .println (uploadResponse );
82
+ String uploadResponseBody = getBody (uploadResponse );
83
+ System .out .println (uploadResponseBody );
89
84
90
- String fetchUri = "http://localhost:%s/fetch-labeled-owner/%s" .formatted (mainOwnerServiceMappedPort , ownerId );
91
- ClassicHttpRequest fetchRequest = DefaultClassicHttpRequestFactory .INSTANCE .newHttpRequest ("GET" , fetchUri );
85
+ sleep (10 );
92
86
87
+ ClassicHttpRequest fetchRequest = createFetchRequest (ownerId , mainOwnerServiceMappedPort );
93
88
ClassicHttpResponse fetchResponse = sendRequest (fetchRequest );
94
- System .out .println (fetchResponse );
89
+ String fetchResponseBody = getBody (fetchResponse );
90
+ System .out .println (fetchResponseBody );
95
91
} finally {
96
92
mainOwnerService .stop ();
97
93
labelOwnerService .stop ();
98
94
}
99
95
}
100
96
97
+ private ClassicHttpRequest createUploadRequest (Long ownerId , int mainOwnerServiceMappedPort ) {
98
+ String body = """
99
+ {"id":%s,"name":"name1","address":"address2","phone":"phone3","email":"email4"}""" .formatted (ownerId );
100
+ String uploadUri = "http://localhost:%s/upload-initial-owner" .formatted (mainOwnerServiceMappedPort );
101
+ ClassicHttpRequest uploadRequest = DefaultClassicHttpRequestFactory .INSTANCE .newHttpRequest ("POST" , uploadUri );
102
+ uploadRequest .setEntity (new StringEntity (body ));
103
+ uploadRequest .setHeader ("Content-Type" , "application/json" );
104
+ return uploadRequest ;
105
+ }
106
+
107
+ private ClassicHttpRequest createFetchRequest (Long ownerId , int mainOwnerServiceMappedPort ) {
108
+ String fetchUri = "http://localhost:%s/fetch-labeled-owner/%s" .formatted (mainOwnerServiceMappedPort , ownerId );
109
+ return DefaultClassicHttpRequestFactory .INSTANCE .newHttpRequest ("GET" , fetchUri );
110
+ }
111
+
112
+ private void sleep (int seconds ) {
113
+ try {
114
+ ThreadUtils .sleep (Duration .ofSeconds (seconds ));
115
+ } catch (InterruptedException e ) {
116
+ throw new RuntimeException (e );
117
+ }
118
+ }
119
+
101
120
private ClassicHttpResponse sendRequest (ClassicHttpRequest apacheRequest ) {
102
121
try (CloseableHttpClient httpClient = HttpClients .createDefault ()) {
103
122
return httpClient .execute (apacheRequest , r -> r );
@@ -106,5 +125,13 @@ private ClassicHttpResponse sendRequest(ClassicHttpRequest apacheRequest) {
106
125
}
107
126
}
108
127
128
+ private String getBody (ClassicHttpResponse response ) {
129
+ try {
130
+ return EntityUtils .toString (response .getEntity ());
131
+ } catch (IOException | ParseException e ) {
132
+ throw new RuntimeException (e );
133
+ }
134
+ }
135
+
109
136
110
137
}
0 commit comments