27
27
import static org .junit .Assert .assertTrue ;
28
28
import static org .junit .Assume .assumeTrue ;
29
29
30
+ import java .io .File ;
31
+ import java .io .IOException ;
32
+ import java .io .PrintWriter ;
33
+ import java .nio .charset .StandardCharsets ;
34
+ import java .nio .file .Files ;
35
+ import java .nio .file .Paths ;
36
+ import java .util .ArrayList ;
37
+ import java .util .logging .FileHandler ;
30
38
import java .util .logging .Level ;
39
+ import java .util .logging .Logger ;
40
+ import java .util .logging .SimpleFormatter ;
41
+ import java .util .logging .StreamHandler ;
31
42
32
- import org .junit .Before ;
33
43
import org .junit .BeforeClass ;
34
- import org .junit .Rule ;
44
+ import org .junit .ClassRule ;
35
45
import org .junit .Test ;
36
46
import org .junit .contrib .java .lang .system .SystemErrRule ;
47
+ import org .junit .rules .TemporaryFolder ;
37
48
38
49
/**
39
50
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
45
56
*/
46
57
public class TestRequestResponseLogging {
47
58
48
- @ Rule
49
- public final SystemErrRule systemErrorRule = new SystemErrRule ().enableLog ();
59
+ @ ClassRule
60
+ public final static SystemErrRule systemErrorRule = new SystemErrRule ().enableLog ();
61
+
62
+ @ ClassRule
63
+ public final static TemporaryFolder tempFolder = new TemporaryFolder ();
64
+
50
65
51
66
// The following needs to be set to your test repository
52
67
private static final String TEST_HOST_URL ;
@@ -56,11 +71,17 @@ public class TestRequestResponseLogging {
56
71
TEST_PRIVATE_TOKEN = TestUtils .getProperty ("TEST_PRIVATE_TOKEN" );
57
72
}
58
73
59
- private static GitLabApi gitLabApi ;
74
+ private static GitLabApi gitLabApiWithEntityLogging ;
75
+ private static GitLabApi gitLabApiNoEntityLogging ;
76
+ private static GitLabApi gitLabApiNoMaskingLogging ;
60
77
private static GitLabApi gitLabApiWithoutLogging ;
61
78
79
+ private static Logger logger ;
80
+ private static StreamHandler loggingHandler ;
81
+ private static File tempLoggingFile ;
82
+
62
83
@ BeforeClass
63
- public static void setup () throws GitLabApiException {
84
+ public static void setup () throws Exception {
64
85
65
86
String problems = "" ;
66
87
@@ -74,34 +95,96 @@ public static void setup() throws GitLabApiException {
74
95
75
96
if (problems .isEmpty ()) {
76
97
77
- gitLabApi = new GitLabApi (TEST_HOST_URL , TEST_PRIVATE_TOKEN );
78
- gitLabApi .enableRequestResponseLogging (Level .INFO );
98
+ tempLoggingFile = tempFolder .newFile ("test-loging.log" );
99
+
100
+ loggingHandler = new FileHandler (tempLoggingFile .getAbsolutePath ());
101
+ loggingHandler .setFormatter (new SimpleFormatter ());
102
+ logger = Logger .getLogger (TestRequestResponseLogging .class .getName ());
103
+ logger .setUseParentHandlers (false );
104
+ logger .addHandler (loggingHandler );
105
+ loggingHandler .setLevel (Level .ALL );
106
+ logger .setLevel (Level .ALL );
107
+
108
+ gitLabApiWithEntityLogging = new GitLabApi (TEST_HOST_URL , TEST_PRIVATE_TOKEN );
109
+ gitLabApiWithEntityLogging .enableRequestResponseLogging (logger , Level .INFO , 100 );
110
+ gitLabApiNoEntityLogging = new GitLabApi (TEST_HOST_URL , TEST_PRIVATE_TOKEN );
111
+ gitLabApiNoEntityLogging .enableRequestResponseLogging (logger , Level .INFO );
112
+ gitLabApiNoMaskingLogging = new GitLabApi (TEST_HOST_URL , TEST_PRIVATE_TOKEN );
113
+ gitLabApiNoMaskingLogging .enableRequestResponseLogging (logger , Level .INFO , 100 , new ArrayList <String >());
114
+
79
115
gitLabApiWithoutLogging = new GitLabApi (TEST_HOST_URL , TEST_PRIVATE_TOKEN );
80
116
81
117
} else {
82
118
System .err .print (problems );
83
119
}
84
120
}
85
121
86
- @ Before
87
- public void beforeMethod () {
88
- assumeTrue (gitLabApi != null );
89
- assumeTrue (gitLabApiWithoutLogging != null );
122
+ @ Test
123
+ public void shouldLogRequestsWithEntities () throws Exception {
124
+
125
+ assumeTrue (gitLabApiWithEntityLogging != null );
126
+ clearLogFile ();
127
+ gitLabApiWithEntityLogging .getProjectApi ().getProjects (1 , 1 );
128
+ String log = readLogFile ();
129
+ System .out .println (log );
130
+
131
+ assertTrue ("Request/response log information was missing." , log .contains ("PRIVATE-TOKEN:" ));
132
+ assertTrue ("Request/response PRIVATE-TOKEN value was incorrectly present." , log .contains ("PRIVATE-TOKEN: ********" ));
133
+ assertTrue ("Request/response log information was missing." , log .contains ("/api/v4/projects" ));
134
+ assertTrue ("Request/response entity was missing." , log .contains ("...more..." ));
90
135
}
91
136
92
137
@ Test
93
- public void shouldLogRequests () throws GitLabApiException {
94
- systemErrorRule .clearLog ();
95
- gitLabApi .getRunnersApi ().getAllRunners ();
96
- String log = systemErrorRule .getLog ();
97
- assertTrue ("Request/response log information was missing." , log .contains ("/api/v4/runners" ));
138
+ public void shouldLogRequestsWithoutEntities () throws Exception {
139
+
140
+ assumeTrue (gitLabApiNoEntityLogging != null );
141
+ clearLogFile ();
142
+ gitLabApiNoEntityLogging .getProjectApi ().getProjects (1 , 1 );
143
+ String log = readLogFile ();
144
+ System .out .println (log );
145
+
146
+ assertTrue ("Request/response log information was missing." , log .contains ("PRIVATE-TOKEN:" ));
147
+ assertTrue ("Request/response PRIVATE-TOKEN value was incorrectly present." , log .contains ("PRIVATE-TOKEN: ********" ));
148
+ assertTrue ("Request/response log information was missing." , log .contains ("/api/v4/projects" ));
149
+ assertFalse ("Request/response entity was incorrectly present." , log .contains ("...more..." ));
150
+ }
151
+
152
+ @ Test
153
+ public void shouldLogPrivateToken () throws Exception {
154
+
155
+ assumeTrue (gitLabApiNoMaskingLogging != null );
156
+ clearLogFile ();
157
+ gitLabApiNoMaskingLogging .getProjectApi ().getProjects (1 , 1 );
158
+ String log = readLogFile ();
159
+ System .out .println (log );
160
+
161
+ assertTrue ("Request/response log information was missing." , log .contains ("PRIVATE-TOKEN:" ));
162
+ assertFalse ("Request/response PRIVATE-TOKEN value was missing." , log .contains ("PRIVATE-TOKEN: ********" ));
163
+ assertTrue ("Request/response log information was missing." , log .contains ("/api/v4/projects" ));
164
+ assertTrue ("Request/response entity was incorrectly present." , log .contains ("...more..." ));
98
165
}
99
166
100
167
@ Test
101
168
public void shouldNotLogRequests () throws GitLabApiException {
169
+
170
+ assumeTrue (gitLabApiWithoutLogging != null );
102
171
systemErrorRule .clearLog ();
103
- gitLabApiWithoutLogging .getRunnersApi ().getAllRunners ( );
172
+ gitLabApiWithoutLogging .getProjectApi ().getProjects ( 1 , 1 );
104
173
String log = systemErrorRule .getLog ();
105
- assertFalse ("Request/response log information was incorrectly present." , log .contains ("/api/v4/runners" ));
174
+
175
+ assertFalse ("Request/response log information was incorrectly present." , log .contains ("PRIVATE-TOKEN:" ));
176
+ assertFalse ("Request/response log information was incorrectly present." , log .contains ("/api/v4/projects" ));
177
+ assertFalse ("Request/response entity was incorrectly present." , log .contains ("...more..." ));
178
+ }
179
+
180
+ private static String readLogFile () throws IOException {
181
+ StringBuilder contentBuilder = new StringBuilder ();
182
+ Files .lines (Paths .get (tempLoggingFile .getAbsolutePath ()), StandardCharsets .UTF_8 )
183
+ .forEach (s -> contentBuilder .append (s ).append ("\n " ));
184
+ return contentBuilder .toString ();
185
+ }
186
+
187
+ private static void clearLogFile () throws IOException {
188
+ new PrintWriter (tempLoggingFile .getAbsolutePath ()).close ();
106
189
}
107
190
}
0 commit comments