Skip to content

Commit 2acc798

Browse files
committed
Added tests for new logging mechanism which uses MaskingLoggingFilter (#310).
1 parent 05f056b commit 2acc798

File tree

1 file changed

+102
-19
lines changed

1 file changed

+102
-19
lines changed

src/test/java/org/gitlab4j/api/TestRequestResponseLogging.java

Lines changed: 102 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@
2727
import static org.junit.Assert.assertTrue;
2828
import static org.junit.Assume.assumeTrue;
2929

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;
3038
import java.util.logging.Level;
39+
import java.util.logging.Logger;
40+
import java.util.logging.SimpleFormatter;
41+
import java.util.logging.StreamHandler;
3142

32-
import org.junit.Before;
3343
import org.junit.BeforeClass;
34-
import org.junit.Rule;
44+
import org.junit.ClassRule;
3545
import org.junit.Test;
3646
import org.junit.contrib.java.lang.system.SystemErrRule;
47+
import org.junit.rules.TemporaryFolder;
3748

3849
/**
3950
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
@@ -45,8 +56,12 @@
4556
*/
4657
public class TestRequestResponseLogging {
4758

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+
5065

5166
// The following needs to be set to your test repository
5267
private static final String TEST_HOST_URL;
@@ -56,11 +71,17 @@ public class TestRequestResponseLogging {
5671
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
5772
}
5873

59-
private static GitLabApi gitLabApi;
74+
private static GitLabApi gitLabApiWithEntityLogging;
75+
private static GitLabApi gitLabApiNoEntityLogging;
76+
private static GitLabApi gitLabApiNoMaskingLogging;
6077
private static GitLabApi gitLabApiWithoutLogging;
6178

79+
private static Logger logger;
80+
private static StreamHandler loggingHandler;
81+
private static File tempLoggingFile;
82+
6283
@BeforeClass
63-
public static void setup() throws GitLabApiException {
84+
public static void setup() throws Exception {
6485

6586
String problems = "";
6687

@@ -74,34 +95,96 @@ public static void setup() throws GitLabApiException {
7495

7596
if (problems.isEmpty()) {
7697

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+
79115
gitLabApiWithoutLogging = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN);
80116

81117
} else {
82118
System.err.print(problems);
83119
}
84120
}
85121

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..."));
90135
}
91136

92137
@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..."));
98165
}
99166

100167
@Test
101168
public void shouldNotLogRequests() throws GitLabApiException {
169+
170+
assumeTrue(gitLabApiWithoutLogging != null);
102171
systemErrorRule.clearLog();
103-
gitLabApiWithoutLogging.getRunnersApi().getAllRunners();
172+
gitLabApiWithoutLogging.getProjectApi().getProjects(1, 1);
104173
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();
106189
}
107190
}

0 commit comments

Comments
 (0)