add checks that requests are made

This commit is contained in:
David Luhmer 2019-04-05 17:27:56 -03:00
parent eec524ec6c
commit d2abb52e01
2 changed files with 42 additions and 12 deletions

View file

@ -1,10 +1,13 @@
package com.nextcloud.android.sso.api;
import com.nextcloud.android.sso.aidl.NextcloudRequest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.runners.MockitoJUnitRunner;
import javax.inject.Inject;
@ -15,8 +18,8 @@ import de.luhmer.owncloudnewsreader.NewFeedActivity;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.TestApplication;
import de.luhmer.owncloudnewsreader.di.ApiProvider;
import de.luhmer.owncloudnewsreader.di.TestApiProvider;
import de.luhmer.owncloudnewsreader.di.TestComponent;
import de.luhmer.owncloudnewsreader.reader.nextcloud.API;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
@ -29,6 +32,8 @@ import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
@ -47,9 +52,14 @@ public class NewFeedTests {
public void setUp() {
TestComponent ac = (TestComponent) ((TestApplication)(activityRule.getActivity().getApplication())).getAppComponent();
ac.inject(this);
// Reset Spy object
mApi.initApi(null);
//reset(((TestApiProvider)mApi).networkRequestSpy);
}
@Test
public void addNewFeed_New_sameActivity() {
String feed = "http://test.de/new";
@ -59,8 +69,7 @@ public class NewFeedTests {
onView(withId(R.id.btn_addFeed)).perform(click());
try {
//API api = mApi.getAPI();
//verify(api, timeout(2000)).createFeed(feed, 0L);
verifyRequest(feed);
//onView(withId(R.id.et_feed_url)).check(matches(hasErrorText(nullValue(String.class))));
@ -81,8 +90,7 @@ public class NewFeedTests {
onView(withId(R.id.btn_addFeed)).perform(click());
try {
//API api = mApi.getAPI();
//verify(api, timeout(2000)).createFeed(feed, 0L);
verifyRequest(feed);
// Check Activity still open
Thread.sleep(1000);
@ -103,8 +111,7 @@ public class NewFeedTests {
onView(withId(R.id.btn_addFeed)).perform(click());
try {
//API api = mApi.getAPI();
//verify(api, timeout(2000)).createFeed(feed, 0L);
verifyRequest(feed);
// Check Activity still open
Thread.sleep(1000);
@ -115,4 +122,14 @@ public class NewFeedTests {
fail(e.getMessage());
}
}
// Verify that the API was actually called
private void verifyRequest(String feed) throws Exception {
NetworkRequest nr = ((TestApiProvider)mApi).networkRequestSpy;
ArgumentCaptor<NextcloudRequest> argument = ArgumentCaptor.forClass(NextcloudRequest.class);
verify(nr, timeout(2000)).performNetworkRequest(argument.capture(), any());
assertEquals("/index.php/apps/news/api/v1-2/feeds", argument.getValue().getUrl());
assertEquals(feed, argument.getValue().getParameter().get("url"));
assertEquals("0", argument.getValue().getParameter().get("folderId"));
}
}

View file

@ -10,31 +10,44 @@ import com.nextcloud.android.sso.aidl.NextcloudRequest;
import com.nextcloud.android.sso.api.NetworkRequest;
import com.nextcloud.android.sso.api.NextcloudAPI;
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import org.mockito.Mockito;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.function.BiConsumer;
import de.luhmer.owncloudnewsreader.helper.GsonConfig;
import de.luhmer.owncloudnewsreader.reader.nextcloud.API;
import de.luhmer.owncloudnewsreader.ssl.MemorizingTrustManager;
import retrofit2.NextcloudRetrofitApiBuilder;
import static org.mockito.ArgumentMatchers.any;
public class TestApiProvider extends ApiProvider {
private static final String TAG = TestApiProvider.class.getCanonicalName();
public NewsTestNetworkRequest networkRequestSpy;
TestApiProvider(MemorizingTrustManager mtm, SharedPreferences sp, Context context) {
super(mtm, sp, context);
}
@Override
protected void initSsoApi(final NextcloudAPI.ApiConnectedListener callback) {
NextcloudAPI nextcloudAPI = new NextcloudAPI(GsonConfig.GetGson(), new NewsTestNetworkRequest(callback));
NewsTestNetworkRequest networkRequest = new NewsTestNetworkRequest(callback);
networkRequestSpy = Mockito.spy(networkRequest);
// By spying on the method "performNetworkRequest" we can later check if requests were build correctly
try {
Mockito.doCallRealMethod().when(networkRequestSpy).performNetworkRequest(any(), any());
} catch (Exception e) {
e.printStackTrace();
}
NextcloudAPI nextcloudAPI = new NextcloudAPI(GsonConfig.GetGson(), networkRequestSpy);
mApi = new NextcloudRetrofitApiBuilder(nextcloudAPI, API.mApiEndpoint).create(API.class);
}
@ -46,7 +59,7 @@ public class TestApiProvider extends ApiProvider {
}
@Override
protected InputStream performNetworkRequest(NextcloudRequest request, InputStream requestBodyInputStream) throws Exception {
public InputStream performNetworkRequest(NextcloudRequest request, InputStream requestBodyInputStream) throws Exception {
if(Looper.myLooper() == Looper.getMainLooper()) {
throw new NetworkOnMainThreadException();
}