1717
1818package org .apache .ignite .internal .cli .commands .cluster ;
1919
20+ import static com .github .tomakehurst .wiremock .client .WireMock .ok ;
21+ import static com .github .tomakehurst .wiremock .client .WireMock .post ;
22+ import static com .github .tomakehurst .wiremock .client .WireMock .serverError ;
23+ import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
2024import static org .apache .ignite .internal .cli .commands .cliconfig .TestConfigManagerHelper .copyResourceToTempFile ;
2125import static org .junit .jupiter .api .Assertions .assertAll ;
22- import static org .mockserver .matchers .MatchType .ONLY_MATCHING_FIELDS ;
23- import static org .mockserver .model .HttpRequest .request ;
24- import static org .mockserver .model .HttpResponse .response ;
25- import static org .mockserver .model .HttpStatusCode .INTERNAL_SERVER_ERROR_500 ;
26- import static org .mockserver .model .JsonBody .json ;
2726
2827import com .typesafe .config .ConfigFactory ;
2928import com .typesafe .config .ConfigRenderOptions ;
3938import org .junit .jupiter .api .DisplayName ;
4039import org .junit .jupiter .api .Test ;
4140import org .junit .jupiter .api .extension .ExtendWith ;
42- import org .mockserver .model .MediaType ;
4341
4442/** Tests "cluster init" command. */
4543@ DisplayName ("cluster init" )
4644@ ExtendWith (WorkDirectoryExtension .class )
4745class ClusterInitTest extends IgniteCliInterfaceTestBase {
48- private static final Pattern PATTERN = Pattern .compile ("\" " );
46+ private static final Pattern QUOTE_PATTERN = Pattern .compile ("\" " );
47+ private static final Pattern CR_PATTERN = Pattern .compile ("\n " );
4948
5049 @ Override
5150 protected Class <?> getCommandClass () {
@@ -68,7 +67,8 @@ void duplicatedOption() {
6867
6968 @ Test
7069 void wrongConfigFilePath () {
71- clientAndServer .when (request ().withMethod ("POST" ).withPath ("/management/v1/cluster/init" )).respond (response (null ));
70+ stubFor (post ("/management/v1/cluster/init" )
71+ .willReturn (ok ()));
7272
7373 execute (
7474 "--url" , mockUrl ,
@@ -115,14 +115,7 @@ void initSuccess() {
115115 + "\" cmgNodes\" :[\" node2ConsistentId\" ,\" node3ConsistentId\" ],"
116116 + "\" clusterName\" :\" cluster\" }" ;
117117
118- clientAndServer
119- .when (request ()
120- .withMethod ("POST" )
121- .withPath ("/management/v1/cluster/init" )
122- .withBody (json (expectedSentContent , ONLY_MATCHING_FIELDS ))
123- .withContentType (MediaType .APPLICATION_JSON_UTF_8 )
124- )
125- .respond (response (null ));
118+ returnOkForPostWithJson ("/management/v1/cluster/init" , expectedSentContent , true );
126119
127120 execute (
128121 "--url" , mockUrl ,
@@ -141,14 +134,7 @@ void initSuccessNoMsCmg() {
141134 + "\" cmgNodes\" :[],"
142135 + "\" clusterName\" :\" cluster\" }" ;
143136
144- clientAndServer
145- .when (request ()
146- .withMethod ("POST" )
147- .withPath ("/management/v1/cluster/init" )
148- .withBody (json (expectedSentContent , ONLY_MATCHING_FIELDS ))
149- .withContentType (MediaType .APPLICATION_JSON_UTF_8 )
150- )
151- .respond (response (null ));
137+ returnOkForPostWithJson ("/management/v1/cluster/init" , expectedSentContent , true );
152138
153139 execute (
154140 "--url" , mockUrl ,
@@ -167,27 +153,20 @@ void initWithAuthenticationSuccess() throws IOException {
167153 Path clusterConfigurationFile = copyResourceToTempFile ("cluster-configuration-with-enabled-auth.conf" ).toPath ();
168154 String clusterConfiguration = Files .readString (clusterConfigurationFile );
169155
170- String expectedSentContent = "{\n "
171- + " \" metaStorageNodes\" : [\n "
172- + " \" node1ConsistentId\" ,\n "
173- + " \" node2ConsistentId\" \n "
174- + " ],\n "
175- + " \" cmgNodes\" : [\n "
176- + " \" node2ConsistentId\" ,\n "
177- + " \" node3ConsistentId\" \n "
178- + " ],\n "
179- + " \" clusterName\" : \" cluster\" ,\n "
180- + " \" clusterConfiguration\" : \" " + escapedJson (clusterConfiguration ) + "\" \n "
156+ String expectedSentContent = "{"
157+ + " \" metaStorageNodes\" : ["
158+ + " \" node1ConsistentId\" ,"
159+ + " \" node2ConsistentId\" "
160+ + " ],"
161+ + " \" cmgNodes\" : ["
162+ + " \" node2ConsistentId\" ,"
163+ + " \" node3ConsistentId\" "
164+ + " ],"
165+ + " \" clusterName\" : \" cluster\" ,"
166+ + " \" clusterConfiguration\" : \" " + escapedJson (clusterConfiguration ) + "\" "
181167 + "}" ;
182168
183- clientAndServer
184- .when (request ()
185- .withMethod ("POST" )
186- .withPath ("/management/v1/cluster/init" )
187- .withBody (json (expectedSentContent , ONLY_MATCHING_FIELDS ))
188- .withContentType (MediaType .APPLICATION_JSON_UTF_8 )
189- )
190- .respond (response (null ));
169+ returnOkForPostWithJson ("/management/v1/cluster/init" , expectedSentContent , true );
191170
192171 execute (
193172 "--url" , mockUrl ,
@@ -202,15 +181,8 @@ void initWithAuthenticationSuccess() throws IOException {
202181
203182 @ Test
204183 void initError () {
205- clientAndServer
206- .when (request ()
207- .withMethod ("POST" )
208- .withPath ("/management/v1/cluster/init" )
209- )
210- .respond (response ()
211- .withStatusCode (INTERNAL_SERVER_ERROR_500 .code ())
212- .withBody ("{\" status\" :500, \" detail\" :\" Oops\" }" )
213- );
184+ stubFor (post ("/management/v1/cluster/init" )
185+ .willReturn (serverError ().withBody ("{\" status\" :500, \" detail\" :\" Oops\" }" )));
214186
215187 execute (
216188 "--url" , mockUrl ,
@@ -234,14 +206,7 @@ void metastorageNodesAreNotMandatoryForInit() {
234206 + "\" cmgNodes\" :[\" node2ConsistentId\" ,\" node3ConsistentId\" ],"
235207 + "\" clusterName\" :\" cluster\" }" ;
236208
237- clientAndServer
238- .when (request ()
239- .withMethod ("POST" )
240- .withPath ("/management/v1/cluster/init" )
241- .withBody (json (expectedSentContent , ONLY_MATCHING_FIELDS ))
242- .withContentType (MediaType .APPLICATION_JSON_UTF_8 )
243- )
244- .respond (response (null ));
209+ returnOkForPostWithJson ("/management/v1/cluster/init" , expectedSentContent , true );
245210
246211 execute (
247212 "--url" , mockUrl ,
@@ -255,7 +220,7 @@ void metastorageNodesAreNotMandatoryForInit() {
255220 @ Test
256221 @ DisplayName ("--url http://localhost:10300 --metastorage-group node2ConsistentId, node3ConsistentId" )
257222 void cmgNodesAreNotMandatoryForInit () {
258- clientAndServer . when ( request (). withMethod ( "POST" ). withPath ( " /management/v1/cluster/init" )). respond ( response ( null ));
223+ stubFor ( post ( " /management/v1/cluster/init" ). willReturn ( ok () ));
259224
260225 execute (
261226 "--url" , mockUrl ,
@@ -310,25 +275,18 @@ void clusterInitFromMultipleConfigFiles() {
310275 + "ignite.system.idleSafeTimeSyncIntervalMillis: 10,\n "
311276 + "ignite.replication.idleSafeTimePropagationDurationMillis: 100" ;
312277
313- String expectedSentContent = "{\n "
314- + " \" metaStorageNodes\" : [\n "
315- + " \" node1ConsistentId\" \n "
316- + " ],\n "
317- + " \" cmgNodes\" : [\n "
318- + " \" node2ConsistentId\" \n "
319- + " ],\n "
320- + " \" clusterName\" : \" cluster\" ,\n "
321- + " \" clusterConfiguration\" : \" " + escapedJson (expectedClusterConfiguration ) + "\" \n "
278+ String expectedSentContent = "{"
279+ + " \" metaStorageNodes\" : ["
280+ + " \" node1ConsistentId\" "
281+ + " ],"
282+ + " \" cmgNodes\" : ["
283+ + " \" node2ConsistentId\" "
284+ + " ],"
285+ + " \" clusterName\" : \" cluster\" ,"
286+ + " \" clusterConfiguration\" : \" " + escapedJson (expectedClusterConfiguration ) + "\" "
322287 + "}" ;
323288
324- clientAndServer
325- .when (request ()
326- .withMethod ("POST" )
327- .withPath ("/management/v1/cluster/init" )
328- .withBody (json (expectedSentContent , ONLY_MATCHING_FIELDS ))
329- .withContentType (MediaType .APPLICATION_JSON_UTF_8 )
330- )
331- .respond (response (null ));
289+ returnOkForPostWithJson ("/management/v1/cluster/init" , expectedSentContent , true );
332290
333291 execute (
334292 "--url" , mockUrl ,
@@ -345,6 +303,8 @@ private static String escapedJson(String configuration) {
345303 String json = ConfigFactory .parseString (configuration )
346304 .root ().render (ConfigRenderOptions .concise ().setFormatted (true ).setJson (true ));
347305
348- return PATTERN .matcher (json ).replaceAll ("\\ \\ \" " );
306+ String quoted = QUOTE_PATTERN .matcher (json ).replaceAll ("\\ \\ \" " );
307+
308+ return CR_PATTERN .matcher (quoted ).replaceAll ("\\ \\ n" );
349309 }
350310}
0 commit comments