Skip to content

Commit 8ffe32c

Browse files
add tests
1 parent da497e8 commit 8ffe32c

File tree

13 files changed

+928
-3
lines changed

13 files changed

+928
-3
lines changed

.github/workflows/test.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: CI Tests
2+
3+
on:
4+
push:
5+
branches: [ master, main ]
6+
pull_request:
7+
branches: [ master, main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
java-version: [8, 11, 17]
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Set up JDK ${{ matrix.java-version }}
22+
uses: actions/setup-java@v4
23+
with:
24+
java-version: ${{ matrix.java-version }}
25+
distribution: 'temurin'
26+
27+
- name: Cache Maven dependencies
28+
uses: actions/cache@v4
29+
with:
30+
path: ~/.m2
31+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
32+
restore-keys: ${{ runner.os }}-m2
33+
34+
- name: Run tests
35+
run: mvn clean test
36+
37+
- name: Generate test report
38+
uses: dorny/test-reporter@v1
39+
if: success() || failure()
40+
with:
41+
name: Maven Tests (Java ${{ matrix.java-version }})
42+
path: target/surefire-reports/*.xml
43+
reporter: java-junit
44+
45+
- name: Upload test results
46+
uses: actions/upload-artifact@v4
47+
if: always()
48+
with:
49+
name: test-results-java-${{ matrix.java-version }}
50+
path: target/surefire-reports/
51+
52+
build:
53+
runs-on: ubuntu-latest
54+
needs: test
55+
56+
steps:
57+
- name: Checkout code
58+
uses: actions/checkout@v4
59+
60+
- name: Set up JDK 8
61+
uses: actions/setup-java@v4
62+
with:
63+
java-version: 8
64+
distribution: 'temurin'
65+
66+
- name: Cache Maven dependencies
67+
uses: actions/cache@v4
68+
with:
69+
path: ~/.m2
70+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
71+
restore-keys: ${{ runner.os }}-m2
72+
73+
- name: Build project
74+
run: mvn clean package
75+
76+
- name: Upload build artifacts
77+
uses: actions/upload-artifact@v4
78+
with:
79+
name: tunnel-jar
80+
path: target/TestingBotTunnel-*.jar

pom.xml

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@
7676
</execution>
7777
</executions>
7878
</plugin>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-surefire-plugin</artifactId>
82+
<version>3.0.0-M9</version>
83+
<configuration>
84+
<includes>
85+
<include>**/*Test.java</include>
86+
</includes>
87+
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</argLine>
88+
<forkCount>1</forkCount>
89+
<reuseForks>false</reuseForks>
90+
</configuration>
91+
</plugin>
7992
<plugin>
8093
<groupId>org.apache.maven.plugins</groupId>
8194
<artifactId>maven-shade-plugin</artifactId>
@@ -130,9 +143,39 @@
130143
</build>
131144
<dependencies>
132145
<dependency>
133-
<groupId>junit</groupId>
134-
<artifactId>junit</artifactId>
135-
<version>4.13.1</version>
146+
<groupId>org.junit.jupiter</groupId>
147+
<artifactId>junit-jupiter</artifactId>
148+
<version>5.9.2</version>
149+
<scope>test</scope>
150+
</dependency>
151+
<dependency>
152+
<groupId>org.mockito</groupId>
153+
<artifactId>mockito-core</artifactId>
154+
<version>5.1.1</version>
155+
<scope>test</scope>
156+
</dependency>
157+
<dependency>
158+
<groupId>org.mockito</groupId>
159+
<artifactId>mockito-junit-jupiter</artifactId>
160+
<version>5.1.1</version>
161+
<scope>test</scope>
162+
</dependency>
163+
<dependency>
164+
<groupId>org.mockito</groupId>
165+
<artifactId>mockito-inline</artifactId>
166+
<version>5.1.1</version>
167+
<scope>test</scope>
168+
</dependency>
169+
<dependency>
170+
<groupId>org.assertj</groupId>
171+
<artifactId>assertj-core</artifactId>
172+
<version>3.24.2</version>
173+
<scope>test</scope>
174+
</dependency>
175+
<dependency>
176+
<groupId>org.wiremock</groupId>
177+
<artifactId>wiremock</artifactId>
178+
<version>3.3.1</version>
136179
<scope>test</scope>
137180
</dependency>
138181
<dependency>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.testingbot.tunnel;
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer;
4+
import net.sf.json.JSONObject;
5+
import org.junit.jupiter.api.AfterEach;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
10+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
13+
14+
class ApiTest {
15+
16+
private WireMockServer wireMockServer;
17+
private App app;
18+
private Api api;
19+
20+
@BeforeEach
21+
void setUp() {
22+
wireMockServer = new WireMockServer(options().port(8089));
23+
wireMockServer.start();
24+
25+
app = new App();
26+
app.setClientKey("test_key");
27+
app.setClientSecret("test_secret");
28+
app.setTunnelIdentifier("test_tunnel");
29+
30+
api = new Api(app);
31+
}
32+
33+
@AfterEach
34+
void tearDown() {
35+
if (wireMockServer != null) {
36+
wireMockServer.stop();
37+
}
38+
}
39+
40+
@Test
41+
void createTunnel_withValidCredentials_shouldHandleNetworkCall() {
42+
try {
43+
api.createTunnel();
44+
} catch (Exception e) {
45+
assertThat(e.getMessage()).contains("Could not start tunnel");
46+
}
47+
}
48+
49+
@Test
50+
void pollTunnel_withValidTunnelId_shouldHandleRequest() {
51+
// Given
52+
String tunnelId = "123";
53+
54+
// Then
55+
assertThatThrownBy(() -> api.pollTunnel(tunnelId))
56+
.isInstanceOf(Exception.class)
57+
.hasMessageContaining("Could not get tunnel info");
58+
}
59+
60+
@Test
61+
void setTunnelID_shouldUpdateTunnelId() {
62+
// Given
63+
int expectedTunnelId = 456;
64+
65+
// When
66+
api.setTunnelID(expectedTunnelId);
67+
68+
// Then
69+
assertThat(api).isNotNull();
70+
}
71+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.testingbot.tunnel;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
import static org.assertj.core.api.Assertions.assertThatCode;
8+
9+
class AppTest {
10+
11+
private App app;
12+
13+
@BeforeEach
14+
void setUp() {
15+
app = new App();
16+
}
17+
18+
@Test
19+
void defaultValues_shouldBeSetCorrectly() {
20+
// Given & When & Then
21+
assertThat(app.getSeleniumPort()).isEqualTo(4445);
22+
assertThat(app.getJettyPort()).isEqualTo(0);
23+
assertThat(app.getTunnelID()).isEqualTo(0);
24+
assertThat(app.getHubPort()).isEqualTo(80);
25+
assertThat(app.getMetricsPort()).isEqualTo(8003);
26+
assertThat(app.isBypassingSquid()).isFalse();
27+
assertThat(app.isNoBump()).isFalse();
28+
assertThat(app.isDebugMode()).isFalse();
29+
}
30+
31+
@Test
32+
void setClientKey_shouldUpdateClientKey() {
33+
// Given
34+
String expectedKey = "test_client_key";
35+
36+
// When
37+
app.setClientKey(expectedKey);
38+
39+
// Then
40+
assertThat(app.getClientKey()).isEqualTo(expectedKey);
41+
}
42+
43+
@Test
44+
void setClientSecret_shouldUpdateClientSecret() {
45+
// Given
46+
String expectedSecret = "test_client_secret";
47+
48+
// When
49+
app.setClientSecret(expectedSecret);
50+
51+
// Then
52+
assertThat(app.getClientSecret()).isEqualTo(expectedSecret);
53+
}
54+
55+
56+
@Test
57+
void setJettyPort_shouldUpdateJettyPort() {
58+
// Given
59+
int expectedPort = 9090;
60+
61+
// When
62+
app.setJettyPort(expectedPort);
63+
64+
// Then
65+
assertThat(app.getJettyPort()).isEqualTo(expectedPort);
66+
}
67+
68+
@Test
69+
void setTunnelIdentifier_shouldUpdateTunnelIdentifier() {
70+
// Given
71+
String expectedIdentifier = "my_test_tunnel";
72+
73+
// When
74+
app.setTunnelIdentifier(expectedIdentifier);
75+
76+
// Then
77+
assertThat(app.getTunnelIdentifier()).isEqualTo(expectedIdentifier);
78+
}
79+
80+
@Test
81+
void setDebugMode_shouldUpdateDebugMode() {
82+
// Given & When
83+
app.setDebugMode(true);
84+
85+
// Then
86+
assertThat(app.isDebugMode()).isTrue();
87+
}
88+
89+
@Test
90+
void setFreeJettyPort_shouldFindAvailablePort() {
91+
// Given
92+
app.setJettyPort(0);
93+
94+
// When
95+
app.setFreeJettyPort();
96+
97+
// Then
98+
assertThat(app.getJettyPort()).isGreaterThan(0);
99+
}
100+
101+
@Test
102+
void addCustomHeader_shouldStoreHeader() {
103+
// Given
104+
String headerName = "X-Test-Header";
105+
String headerValue = "test-value";
106+
107+
// When
108+
app.addCustomHeader(headerName, headerValue);
109+
110+
// Then
111+
assertThat(app.getCustomHeaders()).containsEntry(headerName, headerValue);
112+
}
113+
114+
@Test
115+
void setProxy_shouldUpdateProxySettings() {
116+
// Given
117+
String proxyConfig = "proxy.example.com:8080";
118+
119+
// When
120+
app.setProxy(proxyConfig);
121+
122+
// Then
123+
assertThat(app.getProxy()).isEqualTo(proxyConfig);
124+
}
125+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.testingbot.tunnel;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.net.ServerSocket;
7+
8+
import static org.assertj.core.api.Assertions.assertThatCode;
9+
10+
class DoctorTest {
11+
12+
private App app;
13+
14+
@BeforeEach
15+
void setUp() {
16+
app = new App();
17+
app.setClientKey("test_key");
18+
app.setClientSecret("test_secret");
19+
app.setJettyPort(findFreePort());
20+
}
21+
22+
@Test
23+
void constructor_shouldInitializeAndPerformChecks() {
24+
assertThatCode(() -> {
25+
Doctor doctor = new Doctor(app);
26+
}).doesNotThrowAnyException();
27+
}
28+
29+
private int findFreePort() {
30+
try (ServerSocket socket = new ServerSocket(0)) {
31+
return socket.getLocalPort();
32+
} catch (Exception e) {
33+
return 8087; // fallback to default
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)