Search in sources :

Example 1 with OcsResponse

use of it.niedermann.owncloud.notes.shared.model.OcsResponse in project nextcloud-notes by stefan-niedermann.

the class CapabilitiesClientTest method testGetCapabilities.

@Test
public void testGetCapabilities() throws Throwable {
    // noinspection unchecked
    final ParsedResponse<OcsResponse<Capabilities>> responseMock = mock(ParsedResponse.class);
    final OcsResponse<Capabilities> mockOcs = new OcsResponse<>();
    mockOcs.ocs = new OcsResponse.OcsWrapper<>();
    mockOcs.ocs.data = new Capabilities();
    mockOcs.ocs.data.setApiVersion("[1.0]");
    when(responseMock.getResponse()).thenReturn(mockOcs);
    when(responseMock.getHeaders()).thenReturn(Map.of("ETag", "1234"));
    when(ocsAPI.getCapabilities(any())).thenReturn(Observable.just(responseMock));
    final var capabilities = CapabilitiesClient.getCapabilities(ApplicationProvider.getApplicationContext(), ssoAccount, null, apiProvider);
    assertEquals("[1.0]", capabilities.getApiVersion());
    assertEquals("ETag should be read correctly from response but wasn't.", "1234", capabilities.getETag());
    when(ocsAPI.getCapabilities(any())).thenReturn(Observable.error(new RuntimeException()));
    assertThrows(RuntimeException.class, () -> CapabilitiesClient.getCapabilities(ApplicationProvider.getApplicationContext(), ssoAccount, null, apiProvider));
    when(ocsAPI.getCapabilities(any())).thenReturn(Observable.error(new RuntimeException(new NetworkErrorException())));
    assertThrows("Should unwrap exception cause if possible", NetworkErrorException.class, () -> CapabilitiesClient.getCapabilities(ApplicationProvider.getApplicationContext(), ssoAccount, null, apiProvider));
}
Also used : NetworkErrorException(android.accounts.NetworkErrorException) Capabilities(it.niedermann.owncloud.notes.shared.model.Capabilities) OcsResponse(it.niedermann.owncloud.notes.shared.model.OcsResponse) Test(org.junit.Test)

Example 2 with OcsResponse

use of it.niedermann.owncloud.notes.shared.model.OcsResponse in project nextcloud-notes by stefan-niedermann.

the class CapabilitiesClientTest method testGetDisplayName.

@SuppressWarnings("unchecked")
@Test
public void testGetDisplayName() throws IOException {
    final var mockOcs = new OcsResponse<OcsUser>();
    mockOcs.ocs = new OcsResponse.OcsWrapper<>();
    mockOcs.ocs.data = new OcsUser();
    mockOcs.ocs.data.displayName = "Peter";
    final var responseMock = Response.success(mockOcs);
    final var callMock = mock(Call.class);
    when(ocsAPI.getUser(any())).thenReturn(callMock);
    when(callMock.execute()).thenReturn(responseMock);
    assertEquals("Peter", CapabilitiesClient.getDisplayName(ApplicationProvider.getApplicationContext(), ssoAccount, apiProvider));
    when(callMock.execute()).thenThrow(new RuntimeException() {

        @Override
        public void printStackTrace() {
        // Do not spam console, this will be printed.
        }
    });
    assertNull(CapabilitiesClient.getDisplayName(ApplicationProvider.getApplicationContext(), ssoAccount, apiProvider));
}
Also used : OcsUser(it.niedermann.owncloud.notes.shared.model.OcsUser) OcsResponse(it.niedermann.owncloud.notes.shared.model.OcsResponse) Test(org.junit.Test)

Aggregations

OcsResponse (it.niedermann.owncloud.notes.shared.model.OcsResponse)2 Test (org.junit.Test)2 NetworkErrorException (android.accounts.NetworkErrorException)1 Capabilities (it.niedermann.owncloud.notes.shared.model.Capabilities)1 OcsUser (it.niedermann.owncloud.notes.shared.model.OcsUser)1