Skip to content

Commit 4eb856c

Browse files
jgoldhammergmessner
authored andcommitted
Initial commit (#228).
1 parent 4a32495 commit 4eb856c

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Greg Messner <greg@messners.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package org.gitlab4j.api;
25+
26+
import static org.junit.Assert.assertFalse;
27+
import static org.junit.Assert.assertTrue;
28+
import static org.junit.Assume.assumeTrue;
29+
30+
import java.util.logging.Level;
31+
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.Rule;
35+
import org.junit.Test;
36+
import org.junit.contrib.java.lang.system.SystemErrRule;
37+
38+
/**
39+
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
40+
* <p>
41+
* TEST_HOST_URL
42+
* TEST_PRIVATE_TOKEN
43+
* <p>
44+
* If any of the above are NULL, all tests in this class will be skipped.
45+
*/
46+
public class TestRequestResponseLogging {
47+
48+
@Rule
49+
public final SystemErrRule systemErrorRule = new SystemErrRule().enableLog();
50+
51+
// The following needs to be set to your test repository
52+
private static final String TEST_HOST_URL;
53+
private static final String TEST_PRIVATE_TOKEN;
54+
static {
55+
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
56+
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
57+
}
58+
59+
private static GitLabApi gitLabApi;
60+
private static GitLabApi gitLabApiWithoutLogging;
61+
62+
@BeforeClass
63+
public static void setup() throws GitLabApiException {
64+
65+
String problems = "";
66+
67+
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
68+
problems += "TEST_HOST_URL cannot be empty\n";
69+
}
70+
71+
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
72+
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
73+
}
74+
75+
if (problems.isEmpty()) {
76+
77+
gitLabApi = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN);
78+
gitLabApi.enableRequestResponseLogging(Level.INFO);
79+
gitLabApiWithoutLogging = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN);
80+
81+
} else {
82+
System.err.print(problems);
83+
}
84+
}
85+
86+
@Before
87+
public void beforeMethod() {
88+
assumeTrue(gitLabApi != null);
89+
assumeTrue(gitLabApiWithoutLogging != null);
90+
}
91+
92+
@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"));
98+
}
99+
100+
@Test
101+
public void shouldNotLogRequests() throws GitLabApiException {
102+
systemErrorRule.clearLog();
103+
gitLabApiWithoutLogging.getRunnersApi().getAllRunners();
104+
String log = systemErrorRule.getLog();
105+
assertFalse("Request/response log information was incorrectly present.", log.contains("/api/v4/runners"));
106+
}
107+
}

0 commit comments

Comments
 (0)