11package io .prometheus .metrics .exporter .pushgateway ;
22
3- import static io .prometheus .metrics .model .snapshots .PrometheusNaming .nameValidationScheme ;
3+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .isValidLabelName ;
44import static org .assertj .core .api .Assertions .*;
55import static org .assertj .core .api .Assertions .assertThat ;
6+ import static org .mockito .Mockito .CALLS_REAL_METHODS ;
7+ import static org .mockito .Mockito .mockStatic ;
68import static org .mockserver .model .HttpRequest .request ;
79import static org .mockserver .model .HttpResponse .response ;
810
1315import java .net .InetAddress ;
1416import java .net .URL ;
1517
18+ import io .prometheus .metrics .model .snapshots .PrometheusNaming ;
1619import io .prometheus .metrics .model .snapshots .ValidationScheme ;
1720import org .junit .jupiter .api .AfterEach ;
1821import org .junit .jupiter .api .BeforeEach ;
1922import org .junit .jupiter .api .Test ;
23+ import org .mockito .MockedStatic ;
2024import org .mockserver .client .MockServerClient ;
2125import org .mockserver .integration .ClientAndServer ;
2226
@@ -146,19 +150,22 @@ public void testPushWithGroupingKey() throws IOException {
146150
147151 @ Test
148152 public void testPushWithEscapedGroupingKey () throws IOException {
149- nameValidationScheme = ValidationScheme .UTF_8_VALIDATION ;
150- mockServerClient
151- .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
152- .respond (response ().withStatusCode (202 ));
153- PushGateway pg =
154- PushGateway .builder ()
155- .address ("localhost:" + mockServerClient .getPort ())
156- .registry (registry )
157- .job ("j" )
158- .groupingKey ("l.1" , "v1" )
159- .build ();
160- pg .push ();
161- nameValidationScheme = ValidationScheme .LEGACY_VALIDATION ;
153+ try (MockedStatic <PrometheusNaming > mock = mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
154+ mock .when (PrometheusNaming ::getValidationScheme )
155+ .thenReturn (ValidationScheme .UTF_8_VALIDATION );
156+
157+ mockServerClient
158+ .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
159+ .respond (response ().withStatusCode (202 ));
160+ PushGateway pg =
161+ PushGateway .builder ()
162+ .address ("localhost:" + mockServerClient .getPort ())
163+ .registry (registry )
164+ .job ("j" )
165+ .groupingKey ("l.1" , "v1" )
166+ .build ();
167+ pg .push ();
168+ }
162169 }
163170
164171 @ Test
@@ -179,20 +186,23 @@ public void testPushWithMultiGroupingKey() throws IOException {
179186
180187 @ Test
181188 public void testPushWithMultiEscapedGroupingKey () throws IOException {
182- nameValidationScheme = ValidationScheme .UTF_8_VALIDATION ;
183- mockServerClient
184- .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1/U__l_2e_2/v2" ))
185- .respond (response ().withStatusCode (202 ));
186- PushGateway pg =
187- PushGateway .builder ()
188- .address ("localhost:" + mockServerClient .getPort ())
189- .registry (registry )
190- .job ("j" )
191- .groupingKey ("l.1" , "v1" )
192- .groupingKey ("l.2" , "v2" )
193- .build ();
194- pg .push ();
195- nameValidationScheme = ValidationScheme .LEGACY_VALIDATION ;
189+ try (MockedStatic <PrometheusNaming > mock = mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
190+ mock .when (PrometheusNaming ::getValidationScheme )
191+ .thenReturn (ValidationScheme .UTF_8_VALIDATION );
192+
193+ mockServerClient
194+ .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1/U__l_2e_2/v2" ))
195+ .respond (response ().withStatusCode (202 ));
196+ PushGateway pg =
197+ PushGateway .builder ()
198+ .address ("localhost:" + mockServerClient .getPort ())
199+ .registry (registry )
200+ .job ("j" )
201+ .groupingKey ("l.1" , "v1" )
202+ .groupingKey ("l.2" , "v2" )
203+ .build ();
204+ pg .push ();
205+ }
196206 }
197207
198208 @ Test
@@ -245,19 +255,22 @@ public void testPushCollectorWithGroupingKey() throws IOException {
245255
246256 @ Test
247257 public void testPushCollectorWithEscapedGroupingKey () throws IOException {
248- nameValidationScheme = ValidationScheme .UTF_8_VALIDATION ;
249- mockServerClient
250- .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
251- .respond (response ().withStatusCode (202 ));
252- PushGateway pg =
253- PushGateway .builder ()
254- .address ("localhost:" + mockServerClient .getPort ())
255- .registry (registry )
256- .job ("j" )
257- .groupingKey ("l.1" , "v1" )
258- .build ();
259- pg .push (gauge );
260- nameValidationScheme = ValidationScheme .LEGACY_VALIDATION ;
258+ try (MockedStatic <PrometheusNaming > mock = mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
259+ mock .when (PrometheusNaming ::getValidationScheme )
260+ .thenReturn (ValidationScheme .UTF_8_VALIDATION );
261+
262+ mockServerClient
263+ .when (request ().withMethod ("PUT" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
264+ .respond (response ().withStatusCode (202 ));
265+ PushGateway pg =
266+ PushGateway .builder ()
267+ .address ("localhost:" + mockServerClient .getPort ())
268+ .registry (registry )
269+ .job ("j" )
270+ .groupingKey ("l.1" , "v1" )
271+ .build ();
272+ pg .push (gauge );
273+ }
261274 }
262275
263276 @ Test
@@ -301,19 +314,22 @@ public void testPushAddWithGroupingKey() throws IOException {
301314
302315 @ Test
303316 public void testPushAddWithEscapedGroupingKey () throws IOException {
304- nameValidationScheme = ValidationScheme .UTF_8_VALIDATION ;
305- mockServerClient
306- .when (request ().withMethod ("POST" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
307- .respond (response ().withStatusCode (202 ));
308- PushGateway pg =
309- PushGateway .builder ()
310- .address ("localhost:" + mockServerClient .getPort ())
311- .registry (registry )
312- .groupingKey ("l.1" , "v1" )
313- .job ("j" )
314- .build ();
315- pg .pushAdd ();
316- nameValidationScheme = ValidationScheme .LEGACY_VALIDATION ;
317+ try (MockedStatic <PrometheusNaming > mock = mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
318+ mock .when (PrometheusNaming ::getValidationScheme )
319+ .thenReturn (ValidationScheme .UTF_8_VALIDATION );
320+
321+ mockServerClient
322+ .when (request ().withMethod ("POST" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
323+ .respond (response ().withStatusCode (202 ));
324+ PushGateway pg =
325+ PushGateway .builder ()
326+ .address ("localhost:" + mockServerClient .getPort ())
327+ .registry (registry )
328+ .groupingKey ("l.1" , "v1" )
329+ .job ("j" )
330+ .build ();
331+ pg .pushAdd ();
332+ }
317333 }
318334
319335 @ Test
@@ -333,19 +349,22 @@ public void testPushAddCollectorWithGroupingKey() throws IOException {
333349
334350 @ Test
335351 public void testPushAddCollectorWithEscapedGroupingKey () throws IOException {
336- nameValidationScheme = ValidationScheme .UTF_8_VALIDATION ;
337- mockServerClient
338- .when (request ().withMethod ("POST" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
339- .respond (response ().withStatusCode (202 ));
340- PushGateway pg =
341- PushGateway .builder ()
342- .address ("localhost:" + mockServerClient .getPort ())
343- .registry (registry )
344- .groupingKey ("l.1" , "v1" )
345- .job ("j" )
346- .build ();
347- pg .pushAdd (gauge );
348- nameValidationScheme = ValidationScheme .LEGACY_VALIDATION ;
352+ try (MockedStatic <PrometheusNaming > mock = mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
353+ mock .when (PrometheusNaming ::getValidationScheme )
354+ .thenReturn (ValidationScheme .UTF_8_VALIDATION );
355+
356+ mockServerClient
357+ .when (request ().withMethod ("POST" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
358+ .respond (response ().withStatusCode (202 ));
359+ PushGateway pg =
360+ PushGateway .builder ()
361+ .address ("localhost:" + mockServerClient .getPort ())
362+ .registry (registry )
363+ .groupingKey ("l.1" , "v1" )
364+ .job ("j" )
365+ .build ();
366+ pg .pushAdd (gauge );
367+ }
349368 }
350369
351370 @ Test
@@ -374,18 +393,21 @@ public void testDeleteWithGroupingKey() throws IOException {
374393
375394 @ Test
376395 public void testDeleteWithEscapedGroupingKey () throws IOException {
377- nameValidationScheme = ValidationScheme .UTF_8_VALIDATION ;
378- mockServerClient
379- .when (request ().withMethod ("DELETE" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
380- .respond (response ().withStatusCode (202 ));
381- PushGateway pg =
382- PushGateway .builder ()
383- .address ("localhost:" + mockServerClient .getPort ())
384- .job ("j" )
385- .groupingKey ("l.1" , "v1" )
386- .build ();
387- pg .delete ();
388- nameValidationScheme = ValidationScheme .LEGACY_VALIDATION ;
396+ try (MockedStatic <PrometheusNaming > mock = mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
397+ mock .when (PrometheusNaming ::getValidationScheme )
398+ .thenReturn (ValidationScheme .UTF_8_VALIDATION );
399+
400+ mockServerClient
401+ .when (request ().withMethod ("DELETE" ).withPath ("/metrics/job/j/U__l_2e_1/v1" ))
402+ .respond (response ().withStatusCode (202 ));
403+ PushGateway pg =
404+ PushGateway .builder ()
405+ .address ("localhost:" + mockServerClient .getPort ())
406+ .job ("j" )
407+ .groupingKey ("l.1" , "v1" )
408+ .build ();
409+ pg .delete ();
410+ }
389411 }
390412
391413 @ Test
@@ -407,20 +429,23 @@ public void testInstanceIpGroupingKey() throws IOException {
407429
408430 @ Test
409431 public void testInstanceIpEscapedGroupingKey () throws IOException {
410- nameValidationScheme = ValidationScheme .UTF_8_VALIDATION ;
411- String ip = InetAddress .getLocalHost ().getHostAddress ();
412- assertThat (ip ).isNotEmpty ();
413- mockServerClient
414- .when (request ().withMethod ("DELETE" ).withPath ("/metrics/job/j/instance/" + ip + "/U__l_2e_1/v1" ))
415- .respond (response ().withStatusCode (202 ));
416- PushGateway pg =
417- PushGateway .builder ()
418- .address ("localhost:" + mockServerClient .getPort ())
419- .job ("j" )
420- .groupingKey ("l.1" , "v1" )
421- .instanceIpGroupingKey ()
422- .build ();
423- pg .delete ();
424- nameValidationScheme = ValidationScheme .LEGACY_VALIDATION ;
432+ try (MockedStatic <PrometheusNaming > mock = mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
433+ mock .when (PrometheusNaming ::getValidationScheme )
434+ .thenReturn (ValidationScheme .UTF_8_VALIDATION );
435+
436+ String ip = InetAddress .getLocalHost ().getHostAddress ();
437+ assertThat (ip ).isNotEmpty ();
438+ mockServerClient
439+ .when (request ().withMethod ("DELETE" ).withPath ("/metrics/job/j/instance/" + ip + "/U__l_2e_1/v1" ))
440+ .respond (response ().withStatusCode (202 ));
441+ PushGateway pg =
442+ PushGateway .builder ()
443+ .address ("localhost:" + mockServerClient .getPort ())
444+ .job ("j" )
445+ .groupingKey ("l.1" , "v1" )
446+ .instanceIpGroupingKey ()
447+ .build ();
448+ pg .delete ();
449+ }
425450 }
426451}
0 commit comments