Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ServerCommunicationErrorHandlerTest.java « api « remote « deck « nextcloud « niedermann « it « java « test « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b9c7288a078f2ec7824d0ae32f5d7b03286cd51e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package it.niedermann.nextcloud.deck.remote.api;

import static org.junit.Assert.assertEquals;

import com.nextcloud.android.sso.exceptions.UnknownErrorException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import it.niedermann.nextcloud.deck.exceptions.OfflineException;

@RunWith(RobolectricTestRunner.class)
public class ServerCommunicationErrorHandlerTest {

    @Test
    public void shouldMap_UnknownErrorExceptions_To_OfflineExceptions_ForConnectionTimeout() {
        final var result = ServerCommunicationErrorHandler.translateError(new UnknownErrorException("failed to connect to myhome.example.com/2222:A:B:C:D (port 443) from /2020:A:B:C:D (port 42803) after 60000ms: isConnected failed: ECONNREFUSED (Connection refused)"));
        assertEquals(OfflineException.class, result.getClass());
        assertEquals(OfflineException.Reason.CONNECTION_REFUSED, ((OfflineException) result).getReason());
    }

    @Test
    public void shouldMap_UnknownErrorExceptions_To_OfflineExceptions_ForHostUnreachable() {
        final var result = ServerCommunicationErrorHandler.translateError(new UnknownErrorException("Unable to resolve host \"nextcloud-prod.fritz.box\": No address associated with hostname"));
        assertEquals(OfflineException.class, result.getClass());
        assertEquals(OfflineException.Reason.CONNECTION_REFUSED, ((OfflineException) result).getReason());
    }

    @Test
    public void shouldMap_ClassNotFoundExceptions_To_OfflineExceptions_ForConnectionRefused() {
        final var result = ServerCommunicationErrorHandler.translateError(new ClassNotFoundException("java.lang.ClassNotFoundException: org.apache.commons.httpclient.ConnectTimeoutException"));
        assertEquals(OfflineException.class, result.getClass());
        assertEquals(OfflineException.Reason.CONNECTION_TIMEOUT, ((OfflineException) result).getReason());
    }

    @Test
    public void shouldSkip_UnknownErrorExceptions_ForOtherCases() {
        final var result = ServerCommunicationErrorHandler.translateError(new UnknownErrorException("Foo Bar"));
        assertEquals(UnknownErrorException.class, result.getClass());
        assertEquals("Foo Bar", result.getMessage());
    }

    @Test
    public void shouldSkip_ClassNotFoundExceptions_ForOtherCases() {
        final var result = ServerCommunicationErrorHandler.translateError(new ClassNotFoundException("Foo Bar"));
        assertEquals(ClassNotFoundException.class, result.getClass());
        assertEquals("Foo Bar", result.getMessage());
    }

    @Test
    public void shouldSkip_KnownExceptions_WithNullMessages() {
        assertEquals(ClassNotFoundException.class, ServerCommunicationErrorHandler.translateError(new ClassNotFoundException()).getClass());
        assertEquals(UnknownErrorException.class, ServerCommunicationErrorHandler.translateError(new UnknownErrorException()).getClass());
    }
}