Skip to content

Commit 411e06c

Browse files
committed
Improve test coverage
1 parent e24f3ea commit 411e06c

3 files changed

Lines changed: 58 additions & 8 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package me.proxer.library.api;
2+
3+
import okhttp3.HttpUrl;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
/**
10+
* @author Ruben Gees
11+
*/
12+
public class HttpUrlAdapterTest {
13+
14+
private HttpUrlAdapter adapter;
15+
16+
@Before
17+
public void setUp() throws Exception {
18+
adapter = new HttpUrlAdapter();
19+
}
20+
21+
@Test
22+
public void testNormal() throws Exception {
23+
assertThat(adapter.fromJson("https://www.example.com")).isEqualTo(HttpUrl.parse("https://www.example.com"));
24+
}
25+
26+
@Test
27+
public void testSchemaLess() throws Exception {
28+
assertThat(adapter.fromJson("//www.example.com")).isEqualTo(HttpUrl.parse("http://www.example.com"));
29+
}
30+
31+
@Test
32+
public void testInvalid() throws Exception {
33+
assertThat(adapter.fromJson("example.com")).isNull();
34+
}
35+
}

library/src/test/java/me/proxer/library/api/ProxerCallTest.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package me.proxer.library.api;
22

33
import me.proxer.library.ProxerTest;
4+
import me.proxer.library.api.ProxerException.ErrorType;
5+
import me.proxer.library.api.ProxerException.ServerErrorType;
46
import me.proxer.library.entitiy.notifications.NewsArticle;
57
import okhttp3.mockwebserver.MockResponse;
68
import okhttp3.mockwebserver.SocketPolicy;
@@ -25,7 +27,7 @@ public void testTimeoutError() throws IOException {
2527
.setSocketPolicy(SocketPolicy.NO_RESPONSE));
2628

2729
assertThatExceptionOfType(ProxerException.class).isThrownBy(() -> api.notifications().news().build().execute())
28-
.has(new Condition<Throwable>(e -> ((ProxerException) e).getErrorType() == ProxerException.ErrorType.TIMEOUT,
30+
.has(new Condition<Throwable>(e -> ((ProxerException) e).getErrorType() == ErrorType.TIMEOUT,
2931
"TIMEOUT ErrorType"));
3032
}
3133

@@ -34,7 +36,7 @@ public void testIOError() throws IOException {
3436
server.enqueue(new MockResponse().setBody(fromResource("news.json")).setResponseCode(404));
3537

3638
assertThatExceptionOfType(ProxerException.class).isThrownBy(() -> api.notifications().news().build().execute())
37-
.has(new Condition<Throwable>(e -> ((ProxerException) e).getErrorType() == ProxerException.ErrorType.IO,
39+
.has(new Condition<Throwable>(e -> ((ProxerException) e).getErrorType() == ErrorType.IO,
3840
"IO ErrorType"));
3941
}
4042

@@ -43,7 +45,7 @@ public void testInvalidEncodingError() throws IOException {
4345
server.enqueue(new MockResponse().setBody(fromResource("news.json").replace(":", "invalid")));
4446

4547
assertThatExceptionOfType(ProxerException.class).isThrownBy(() -> api.notifications().news().build().execute())
46-
.has(new Condition<Throwable>(e -> ((ProxerException) e).getErrorType() == ProxerException.ErrorType.IO,
48+
.has(new Condition<Throwable>(e -> ((ProxerException) e).getErrorType() == ErrorType.IO,
4749
"IO ErrorType"));
4850
}
4951

@@ -52,10 +54,23 @@ public void testInvalidDataError() throws IOException {
5254
server.enqueue(new MockResponse().setBody(fromResource("news.json").replace("256", "invalid")));
5355

5456
assertThatExceptionOfType(ProxerException.class).isThrownBy(() -> api.notifications().news().build().execute())
55-
.has(new Condition<Throwable>(e -> ((ProxerException) e).getErrorType() == ProxerException.ErrorType.PARSING,
57+
.has(new Condition<Throwable>(e -> ((ProxerException) e).getErrorType() == ErrorType.PARSING,
5658
"PARSING ErrorType"));
5759
}
5860

61+
@Test
62+
public void testServerError() throws Exception {
63+
server.enqueue(new MockResponse().setBody(fromResource("conferences_error.json")));
64+
65+
assertThatExceptionOfType(ProxerException.class).isThrownBy(() -> api.messenger().conferences().build().execute())
66+
.has(new Condition<Throwable>(e -> ((ProxerException) e).getErrorType() == ErrorType.SERVER,
67+
"SERVER ErrorType"))
68+
.has(new Condition<Throwable>(e -> ((ProxerException) e).getServerErrorType() == ServerErrorType.MESSAGES_LOGIN_REQUIRED,
69+
"MESSAGES_LOGIN_REQUIRED ServerErrorType"))
70+
.has(new Condition<Throwable>(e -> e.getMessage().equals("Du bist nicht eingeloggt."),
71+
"Message is equal"));
72+
}
73+
5974
@Test(timeout = 1000L)
6075
public void testEnqueue() throws IOException, InterruptedException {
6176
final CountDownLatch lock = new CountDownLatch(1);
@@ -80,7 +95,7 @@ public void testEnqueueError() throws IOException, InterruptedException {
8095
// Failed. The lock will never be counted down and timeout.
8196
},
8297
exception -> {
83-
if (exception.getErrorType() == ProxerException.ErrorType.IO) {
98+
if (exception.getErrorType() == ErrorType.IO) {
8499
lock.countDown();
85100
}
86101

@@ -112,7 +127,7 @@ public void testCancel() throws IOException, InterruptedException {
112127
call.enqueue(result -> {
113128
// Failed. The lock will never be counted down and timeout.
114129
}, exception -> {
115-
if (exception.getErrorType() == ProxerException.ErrorType.IO) {
130+
if (exception.getErrorType() == ErrorType.IO) {
116131
lock.countDown();
117132
}
118133

library/src/test/java/me/proxer/library/util/ProxerUrlsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ public void testNewsWeb() {
119119

120120
@Test
121121
public void testNewsWebWithDevice() {
122-
assertThat(ProxerUrls.newsWeb("4", "5", Device.MOBILE).toString())
123-
.isEqualTo("https://proxer.me/forum/4/5?device=mobile");
122+
assertThat(ProxerUrls.newsWeb("4", "5", Device.UNSPECIFIED).toString())
123+
.isEqualTo("https://proxer.me/forum/4/5?device=");
124124
}
125125

126126
@Test

0 commit comments

Comments
 (0)