1
+ /*******************************************************************************
2
+ * Copyright (c) 2025 IBM Corporation and others.
3
+ * All rights reserved. This program and the accompanying materials
4
+ * are made available under the terms of the Eclipse Public License 2.0
5
+ * which accompanies this distribution, and is available at
6
+ * http://www.eclipse.org/legal/epl-2.0/
7
+ *
8
+ * SPDX-License-Identifier: EPL-2.0
9
+ *******************************************************************************/
10
+ package io .openliberty .microprofile .telemetry .internal_fat ;
11
+
12
+ import java .io .BufferedReader ;
13
+ import java .io .IOException ;
14
+ import java .io .InputStreamReader ;
15
+ import java .net .HttpURLConnection ;
16
+ import java .net .URI ;
17
+ import java .net .URL ;
18
+ import java .util .HashSet ;
19
+ import java .util .Set ;
20
+
21
+ import javax .ws .rs .HttpMethod ;
22
+
23
+ import org .junit .AfterClass ;
24
+ import org .junit .BeforeClass ;
25
+ import org .junit .ClassRule ;
26
+ import org .junit .Test ;
27
+ import org .junit .runner .RunWith ;
28
+
29
+ import com .ibm .websphere .simplicity .log .Log ;
30
+
31
+ import componenttest .annotation .Server ;
32
+ import componenttest .custom .junit .runner .FATRunner ;
33
+ import componenttest .rules .repeater .RepeatTests ;
34
+ import componenttest .topology .impl .LibertyServer ;
35
+ import componenttest .topology .utils .FATServletClient ;
36
+ import io .openliberty .microprofile .telemetry .internal_fat .shared .TelemetryActions ;
37
+ import junit .framework .Assert ;
38
+
39
+ /**
40
+ * Test to verify that only a singular CWMOT5100I is emitted for each unique internal WAB/application
41
+ * i.e. health, metrics, rest handler ,etc.
42
+ *
43
+ * This issue only affects mpTelemetry-2.0 when used with HTTP Metrics (supported only in mpTelemetry-2.0)
44
+ */
45
+ @ RunWith (FATRunner .class )
46
+ public class TelemetrySdkDisabledTrueWarningTest extends FATServletClient {
47
+
48
+ private static final String CLASS_NAME = TelemetrySdkDisabledTrueWarningTest .class .getName ();
49
+
50
+ private static final String CWMOT5100I = "CWMOT5100I" ;
51
+
52
+ public static final String SERVER_NAME = "Telemetry10sdkDisbledTrueWarning" ;
53
+
54
+ @ Server (SERVER_NAME )
55
+ public static LibertyServer server ;
56
+
57
+ @ ClassRule
58
+ public static RepeatTests r = TelemetryActions .telemetry20Repeats (SERVER_NAME );
59
+
60
+ @ BeforeClass
61
+ public static void setUp () throws Exception {
62
+ server .startServer ();
63
+ }
64
+
65
+ @ AfterClass
66
+ public static void tearDown () throws Exception {
67
+ server .stopServer ();
68
+ }
69
+
70
+ @ Test
71
+ public void uniqeCWMOT5100ITest () throws Exception {
72
+ Assert .assertTrue (server .isStarted ());
73
+
74
+ //Startup check.
75
+ validateCWMOT5100Iwarning ();
76
+
77
+ //hit metrics
78
+ requestHttpServlet ("/metrics" );
79
+ //hit health
80
+ requestHttpServlet ("/health" );
81
+
82
+ //runtime check after hitting endpoints
83
+ validateCWMOT5100Iwarning ();
84
+
85
+ }
86
+
87
+ private void validateCWMOT5100Iwarning () throws Exception {
88
+ Set <String > warningsIssued = new HashSet <String >();
89
+ for (String warning : server .findStringsInLogs (CWMOT5100I )) {
90
+
91
+ //returns false if string already exists in set
92
+ if (!warningsIssued .add (warning .split (CWMOT5100I )[1 ])) {
93
+ Assert .fail (String .format ("Detected duplciate %s warning: %s" , CWMOT5100I , warning ));
94
+ }
95
+ }
96
+ }
97
+
98
+ protected String requestHttpServlet (String servletPath ) {
99
+ HttpURLConnection con = null ;
100
+ try {
101
+ String path = "http://" + server .getHostname () + ":"
102
+ + server .getHttpDefaultPort () + servletPath ;
103
+
104
+ URI theURI = new URI (path );
105
+
106
+ URL theURL = theURI .toURL ();
107
+ con = (HttpURLConnection ) theURL .openConnection ();
108
+ con .setDoInput (true );
109
+ con .setDoOutput (true );
110
+ con .setUseCaches (false );
111
+ con .setRequestMethod (HttpMethod .GET );
112
+ String sep = System .getProperty ("line.separator" );
113
+ String line = null ;
114
+ StringBuilder lines = new StringBuilder ();
115
+ BufferedReader br = new BufferedReader (new InputStreamReader (con .getInputStream ()));
116
+
117
+ while ((line = br .readLine ()) != null && line .length () > 0 ) {
118
+ lines .append (line ).append (sep );
119
+ }
120
+
121
+ Assert .assertEquals ("Did not recieve 200 response code while connecting to " + path , 200 , con .getResponseCode ());
122
+
123
+ return lines .toString ();
124
+ } catch (IOException e ) {
125
+ Log .info (this .getClass (), "requestHttpServlet" , "Encountered IO exception " + e );
126
+ return null ;
127
+ } catch (Exception e ) {
128
+ Log .info (this .getClass (), "requestHttpServlet" , "Encountered an exception " + e );
129
+ return null ;
130
+ } finally {
131
+ if (con != null )
132
+ con .disconnect ();
133
+ }
134
+
135
+ }
136
+ }
0 commit comments