update unit tests

This commit is contained in:
David Luhmer 2019-04-05 17:04:36 -03:00
parent aef30d46a4
commit eec524ec6c
6 changed files with 112 additions and 62 deletions

View file

@ -114,7 +114,8 @@ dependencies {
// implementation 'com.google.android.gms:play-services:4.2.42'
//implementation project(':Android-SingleSignOn')
//implementation project(path: ':MaterialShowcaseView:library', configuration: 'default')
implementation 'com.github.nextcloud:Android-SingleSignOn:e781a33e01'
//implementation 'com.github.nextcloud:Android-SingleSignOn:e781a33e01'
implementation 'com.github.nextcloud:Android-SingleSignOn:unit-testing-SNAPSHOT'
implementation 'com.github.David-Development:MaterialShowcaseView:bf6afa225d'
// https://mvnrepository.com/artifact/androidx.legacy/legacy-support-v4

View file

@ -29,8 +29,6 @@
-dontwarn org.mockito.**
-keepnames class * implements java.io.Serializable
-keepclassmembers class * implements java.io.Serializable {
@ -160,4 +158,11 @@
-dontwarn okhttp3.internal.platform.ConscryptPlatform
-keep interface org.conscrypt.Conscrypt { *; }
-keep class org.conscrypt.Conscrypt { *; }
-keep class org.conscrypt.Conscrypt { *; }
# https://stackoverflow.com/a/39777485
# Also, note that this rule should be added to the regular proguard file(the one of listed in proguardFiles) and not the test one(declared as testProguardFile)
# java.lang.NoSuchMethodError: No virtual method getParameter
-keepclasseswithmembers public class com.nextcloud.android.sso.aidl.NextcloudRequest { *; }

View file

@ -59,8 +59,8 @@ public class NewFeedTests {
onView(withId(R.id.btn_addFeed)).perform(click());
try {
API api = mApi.getAPI();
verify(api, timeout(2000)).createFeed(feed, 0L);
//API api = mApi.getAPI();
//verify(api, timeout(2000)).createFeed(feed, 0L);
//onView(withId(R.id.et_feed_url)).check(matches(hasErrorText(nullValue(String.class))));
@ -81,8 +81,8 @@ public class NewFeedTests {
onView(withId(R.id.btn_addFeed)).perform(click());
try {
API api = mApi.getAPI();
verify(api, timeout(2000)).createFeed(feed, 0L);
//API api = mApi.getAPI();
//verify(api, timeout(2000)).createFeed(feed, 0L);
// Check Activity still open
Thread.sleep(1000);
@ -103,8 +103,8 @@ public class NewFeedTests {
onView(withId(R.id.btn_addFeed)).perform(click());
try {
API api = mApi.getAPI();
verify(api, timeout(2000)).createFeed(feed, 0L);
//API api = mApi.getAPI();
//verify(api, timeout(2000)).createFeed(feed, 0L);
// Check Activity still open
Thread.sleep(1000);

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.api.NextcloudAPI;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
@ -16,6 +17,7 @@ import java.util.List;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.database.model.Feed;
import de.luhmer.owncloudnewsreader.helper.GsonConfig;
import de.luhmer.owncloudnewsreader.reader.nextcloud.API;
import de.luhmer.owncloudnewsreader.ssl.MemorizingTrustManager;
import okhttp3.ResponseBody;
@ -79,60 +81,13 @@ public class TestApiModule extends ApiModule {
return sharedPrefs;
}
private final String NEW_FEED_SUCCESS = "http://test.de/new";
private final String NEW_FEED_EXISTING = "http://test.de/existing";
private final String NEW_FEED_FAIL = "http://test.de/fail";
private final String NEW_FEED_EXISTING_ERROR_MESSAGE = "{\"message\":\"Feed konnte nicht hinzugef\\u00fcgt werden: Existiert bereits\"}";
private final String NEW_FEED_FAIL_ERROR_MESSAGE = "{\"message\":\"FeedIo\\\\Adapter\\\\NotFoundException: Client error: `GET http:\\/\\/feeds2.feedburner.com\\/stadt-bremerhaven\\/dqXM222` resulted in a `404 Feed not found error: FeedBurner cannot locate this feed URI.` response:\\n\\u003Chtml\\u003E\\n\\u003Chead\\u003E\\n\\u003Cstyle type=\\\"text\\/css\\\"\\u003E\\na:link, a:visited {\\n color: #000099;\\n text-decoration: underline;\\n}\\n\\na:hover {\\n (truncated...)\\n in \\/apps2\\/news\\/lib\\/Fetcher\\/Client\\/FeedIoClient.php:57\\nStack trace:\\n#0 \\/apps2\\/news\\/vendor\\/debril\\/feed-io\\/src\\/FeedIo\\/Reader.php(116): OCA\\\\News\\\\Fetcher\\\\Client\\\\FeedIoClient-\\u003EgetResponse('http:\\/\\/feeds2.f...', Object(DateTime))\\n#1 \\/apps2\\/news\\/vendor\\/debril\\/feed-io\\/src\\/FeedIo\\/FeedIo.php(286): FeedIo\\\\Reader-\\u003Eread('http:\\/\\/feeds2.f...', Object(FeedIo\\\\Feed), Object(DateTime))\\n#2 \\/apps2\\/news\\/lib\\/Fetcher\\/FeedFetcher.php(77): FeedIo\\\\FeedIo-\\u003Eread('http:\\/\\/feeds2.f...')\\n#3 \\/apps2\\/news\\/lib\\/Fetcher\\/Fetcher.php(68): OCA\\\\News\\\\Fetcher\\\\FeedFetcher-\\u003Efetch('http:\\/\\/feeds2.f...', true, NULL, NULL, NULL)\\n#4 \\/apps2\\/news\\/lib\\/Service\\/FeedService.php(116): OCA\\\\News\\\\Fetcher\\\\Fetcher-\\u003Efetch('http:\\/\\/feeds2.f...', true, NULL, NULL, NULL)\\n#5 \\/apps2\\/news\\/lib\\/Controller\\/FeedApiController.php(96): OCA\\\\News\\\\Service\\\\FeedService-\\u003Ecreate('http:\\/\\/feeds2.f...', 0, 'david')\\n#6 \\/nextcloud\\/lib\\/private\\/AppFramework\\/Http\\/Dispatcher.php(166): OCA\\\\News\\\\Controller\\\\FeedApiController-\\u003Ecreate('http:\\/\\/feeds2.f...', 0)\\n#7 \\/nextcloud\\/lib\\/private\\/AppFramework\\/Http\\/Dispatcher.php(99): OC\\\\AppFramework\\\\Http\\\\Dispatcher-\\u003EexecuteController(Object(OCA\\\\News\\\\Controller\\\\FeedApiController), 'create')\\n#8 \\/nextcloud\\/lib\\/private\\/AppFramework\\/App.php(118): OC\\\\AppFramework\\\\Http\\\\Dispatcher-\\u003Edispatch(Object(OCA\\\\News\\\\Controller\\\\FeedApiController), 'create')\\n#9 \\/nextcloud\\/lib\\/private\\/AppFramework\\/Routing\\/RouteActionHandler.php(47): OC\\\\AppFramework\\\\App::main('OCA\\\\\\\\News\\\\\\\\Contro...', 'create', Object(OC\\\\AppFramework\\\\DependencyInjection\\\\DIContainer), Array)\\n#10 [internal function]: OC\\\\AppFramework\\\\Routing\\\\RouteActionHandler-\\u003E__invoke(Array)\\n#11 \\/nextcloud\\/lib\\/private\\/Route\\/Router.php(297): call_user_func(Object(OC\\\\AppFramework\\\\Routing\\\\RouteActionHandler), Array)\\n#12 \\/nextcloud\\/lib\\/base.php(987): OC\\\\Route\\\\Router-\\u003Ematch('\\/apps\\/news\\/api\\/...')\\n#13 \\/nextcloud\\/index.php(42): OC::handleRequest()\\n#14 {main}\"}";
@Override
ApiProvider provideAPI(MemorizingTrustManager mtm, SharedPreferences sp) {
ApiProvider apiProvider = Mockito.mock(ApiProvider.class);
API api = Mockito.mock(API.class);
when(apiProvider.getAPI()).thenReturn(api);
Call<List<Feed>> mockCallSuccess = mockCreateFeed(NEW_FEED_SUCCESS);
Call<List<Feed>> mockCallExisting = mockCreateFeed(NEW_FEED_EXISTING);
Call<List<Feed>> mockCallFail = mockCreateFeed(NEW_FEED_FAIL);
when(api.createFeed(eq(NEW_FEED_SUCCESS), any())).thenReturn(mockCallSuccess);
when(api.createFeed(eq(NEW_FEED_EXISTING), any())).thenReturn(mockCallExisting);
when(api.createFeed(eq(NEW_FEED_FAIL), any())).thenReturn(mockCallFail);
ApiProvider apiProvider = new TestApiProvider(mtm, sp, application);
return apiProvider;
}
// https://github.com/nextcloud/news/blob/master/docs/externalapi/Legacy.md#create-a-feed
private Call<List<Feed>> mockCreateFeed(String url) {
Call<List<Feed>> mockCall = Mockito.mock(Call.class);
doAnswer((Answer<Void>) invocation -> {
Object[] args = invocation.getArguments();
Callback callback = (Callback) args[0];
final Thread thr = new Thread() {
@Override
public void run() {
switch(url) {
case NEW_FEED_SUCCESS:
callback.onResponse(mockCall, Response.success(""));
break;
case NEW_FEED_EXISTING:
callback.onResponse(mockCall, Response.error(409, ResponseBody.create(null, NEW_FEED_EXISTING_ERROR_MESSAGE)));
break;
case NEW_FEED_FAIL:
callback.onResponse(mockCall,Response.error(422 , ResponseBody.create(null, NEW_FEED_FAIL_ERROR_MESSAGE)));
break;
default:
throw new RuntimeException("URL NOT KNOWN FOR TEST!");
}
}
};
thr.start();
return null;
}).when(mockCall).enqueue(any());
return mockCall;
}
}

View file

@ -0,0 +1,89 @@
package de.luhmer.owncloudnewsreader.di;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Looper;
import android.os.NetworkOnMainThreadException;
import android.util.Log;
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 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;
public class TestApiProvider extends ApiProvider {
private static final String TAG = TestApiProvider.class.getCanonicalName();
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));
mApi = new NextcloudRetrofitApiBuilder(nextcloudAPI, API.mApiEndpoint).create(API.class);
}
class NewsTestNetworkRequest extends NetworkRequest {
NewsTestNetworkRequest(NextcloudAPI.ApiConnectedListener callback) {
super(null, null, callback);
}
@Override
protected InputStream performNetworkRequest(NextcloudRequest request, InputStream requestBodyInputStream) throws Exception {
if(Looper.myLooper() == Looper.getMainLooper()) {
throw new NetworkOnMainThreadException();
}
Log.e(TAG, "Requested URL: " + request.getUrl());
InputStream inputStream = null;
switch (request.getUrl()) {
case "/index.php/apps/news/api/v1-2/feeds":
inputStream = handleCreateFeed(request);
break;
}
return inputStream;
}
private final String NEW_FEED_SUCCESS = "http://test.de/new";
private final String NEW_FEED_EXISTING = "http://test.de/existing";
private final String NEW_FEED_FAIL = "http://test.de/fail";
private final String NEW_FEED_EXISTING_ERROR_MESSAGE = "{\"message\":\"Feed konnte nicht hinzugef\\u00fcgt werden: Existiert bereits\"}";
private final String NEW_FEED_FAIL_ERROR_MESSAGE = "{\"message\":\"FeedIo\\\\Adapter\\\\NotFoundException: Client error: `GET http:\\/\\/feeds2.feedburner.com\\/stadt-bremerhaven\\/dqXM222` resulted in a `404 Feed not found error: FeedBurner cannot locate this feed URI.` response:\\n\\u003Chtml\\u003E\\n\\u003Chead\\u003E\\n\\u003Cstyle type=\\\"text\\/css\\\"\\u003E\\na:link, a:visited {\\n color: #000099;\\n text-decoration: underline;\\n}\\n\\na:hover {\\n (truncated...)\\n in \\/apps2\\/news\\/lib\\/Fetcher\\/Client\\/FeedIoClient.php:57\\nStack trace:\\n#0 \\/apps2\\/news\\/vendor\\/debril\\/feed-io\\/src\\/FeedIo\\/Reader.php(116): OCA\\\\News\\\\Fetcher\\\\Client\\\\FeedIoClient-\\u003EgetResponse('http:\\/\\/feeds2.f...', Object(DateTime))\\n#1 \\/apps2\\/news\\/vendor\\/debril\\/feed-io\\/src\\/FeedIo\\/FeedIo.php(286): FeedIo\\\\Reader-\\u003Eread('http:\\/\\/feeds2.f...', Object(FeedIo\\\\Feed), Object(DateTime))\\n#2 \\/apps2\\/news\\/lib\\/Fetcher\\/FeedFetcher.php(77): FeedIo\\\\FeedIo-\\u003Eread('http:\\/\\/feeds2.f...')\\n#3 \\/apps2\\/news\\/lib\\/Fetcher\\/Fetcher.php(68): OCA\\\\News\\\\Fetcher\\\\FeedFetcher-\\u003Efetch('http:\\/\\/feeds2.f...', true, NULL, NULL, NULL)\\n#4 \\/apps2\\/news\\/lib\\/Service\\/FeedService.php(116): OCA\\\\News\\\\Fetcher\\\\Fetcher-\\u003Efetch('http:\\/\\/feeds2.f...', true, NULL, NULL, NULL)\\n#5 \\/apps2\\/news\\/lib\\/Controller\\/FeedApiController.php(96): OCA\\\\News\\\\Service\\\\FeedService-\\u003Ecreate('http:\\/\\/feeds2.f...', 0, 'david')\\n#6 \\/nextcloud\\/lib\\/private\\/AppFramework\\/Http\\/Dispatcher.php(166): OCA\\\\News\\\\Controller\\\\FeedApiController-\\u003Ecreate('http:\\/\\/feeds2.f...', 0)\\n#7 \\/nextcloud\\/lib\\/private\\/AppFramework\\/Http\\/Dispatcher.php(99): OC\\\\AppFramework\\\\Http\\\\Dispatcher-\\u003EexecuteController(Object(OCA\\\\News\\\\Controller\\\\FeedApiController), 'create')\\n#8 \\/nextcloud\\/lib\\/private\\/AppFramework\\/App.php(118): OC\\\\AppFramework\\\\Http\\\\Dispatcher-\\u003Edispatch(Object(OCA\\\\News\\\\Controller\\\\FeedApiController), 'create')\\n#9 \\/nextcloud\\/lib\\/private\\/AppFramework\\/Routing\\/RouteActionHandler.php(47): OC\\\\AppFramework\\\\App::main('OCA\\\\\\\\News\\\\\\\\Contro...', 'create', Object(OC\\\\AppFramework\\\\DependencyInjection\\\\DIContainer), Array)\\n#10 [internal function]: OC\\\\AppFramework\\\\Routing\\\\RouteActionHandler-\\u003E__invoke(Array)\\n#11 \\/nextcloud\\/lib\\/private\\/Route\\/Router.php(297): call_user_func(Object(OC\\\\AppFramework\\\\Routing\\\\RouteActionHandler), Array)\\n#12 \\/nextcloud\\/lib\\/base.php(987): OC\\\\Route\\\\Router-\\u003Ematch('\\/apps\\/news\\/api\\/...')\\n#13 \\/nextcloud\\/index.php(42): OC::handleRequest()\\n#14 {main}\"}";
// https://github.com/nextcloud/news/blob/master/docs/externalapi/Legacy.md#create-a-feed
private InputStream handleCreateFeed(NextcloudRequest request) throws NextcloudHttpRequestFailedException {
switch (request.getParameter().get("url")) {
case NEW_FEED_SUCCESS:
return stringToInputStream("");
case NEW_FEED_EXISTING:
throw new NextcloudHttpRequestFailedException(409, new Throwable(NEW_FEED_EXISTING_ERROR_MESSAGE));
case NEW_FEED_FAIL:
throw new NextcloudHttpRequestFailedException(422, new Throwable(NEW_FEED_FAIL_ERROR_MESSAGE));
}
return null;
}
private InputStream stringToInputStream(String data) {
return new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));
}
}
}

View file

@ -36,9 +36,9 @@ public class ApiProvider {
private static final String TAG = ApiProvider.class.getCanonicalName();
private final MemorizingTrustManager mMemorizingTrustManager;
private final SharedPreferences mPrefs;
private API mApi;
private Context context;
protected final SharedPreferences mPrefs;
protected Context context;
protected API mApi;
public ApiProvider(MemorizingTrustManager mtm, SharedPreferences sp, Context context) {
@ -90,7 +90,7 @@ public class ApiProvider {
mApi = retrofit.create(API.class);
}
private void initSsoApi(final NextcloudAPI.ApiConnectedListener callback) {
protected void initSsoApi(final NextcloudAPI.ApiConnectedListener callback) {
try {
SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, GsonConfig.GetGson(), callback);