mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Migrated OkHttpJsonApiClient#getMedia and getPictureOfTheDay to MediaClient (#3056)
* With media client APIs migrated to retrofit (#2998) * With media client APIs migrated to retrofit * Add test cases and java docs * Fix test * Fix build * Fix build and other minor issues * Fix tests * Categories related client API's migrated to retrofit (#3053) * Commit 1 * searchCategories migrated to retrofit * SearchCategoriesFragment migrated to new API * Removed unused code * Created tests * implemented searching by prefix fixed SearchCategoryFragment behaviour where the same categories would be added to the list instead of new ones. * added tests * Migrated searchTitles to searchCategories, function behaviour seems identical * With media client APIs migrated to retrofit (#2998) * With media client APIs migrated to retrofit * Add test cases and java docs * Fix test * Fix build * Fix build and other minor issues * Fix tests * Categories related client API's migrated to retrofit (#3053) * Commit 1 * searchCategories migrated to retrofit * SearchCategoriesFragment migrated to new API * Removed unused code * Created tests * implemented searching by prefix fixed SearchCategoryFragment behaviour where the same categories would be added to the list instead of new ones. * added tests * Migrated searchTitles to searchCategories, function behaviour seems identical * OkHttpJsonApi#getMediaList migrated to retrofit (#3054) * Migrated OkHttpJsonApi#getMediaList partially to MediaClient#getCategoryImages * Migrated rest of OkHttpJsonApi#getMediaList functionality to MediaClient#getCategoryImages * Removed unused code and tests * Fixed small bug * Added tests * Removed unused CategoryImageController * getSubCategoryList and getParentCategoryList migrated to retrofit (#3055) * Migrated getSubCategoryList to retrofit * Migrated getParentCategoryList to retrofit * Removed obsolete functions * Added tests * Fixed small bugs * Migrated OkHttpJsonApiClient#getMedia and getPictureOfTheDay to MediaClient * Removed obsolete functions and added tests * Fixed merge errors * With media client APIs migrated to retrofit (#2998) * With media client APIs migrated to retrofit * Add test cases and java docs * Fix test * Fix build * Fix build and other minor issues * Fix tests * Categories related client API's migrated to retrofit (#3053) * Commit 1 * searchCategories migrated to retrofit * SearchCategoriesFragment migrated to new API * Removed unused code * Created tests * implemented searching by prefix fixed SearchCategoryFragment behaviour where the same categories would be added to the list instead of new ones. * added tests * Migrated searchTitles to searchCategories, function behaviour seems identical * OkHttpJsonApi#getMediaList migrated to retrofit (#3054) * Migrated OkHttpJsonApi#getMediaList partially to MediaClient#getCategoryImages * Migrated rest of OkHttpJsonApi#getMediaList functionality to MediaClient#getCategoryImages * Removed unused code and tests * Fixed small bug * Added tests * Removed unused CategoryImageController * getSubCategoryList and getParentCategoryList migrated to retrofit (#3055) * Migrated getSubCategoryList to retrofit * Migrated getParentCategoryList to retrofit * Removed obsolete functions * Added tests * Fixed small bugs * Consume login client from data client library (#2894) Fix actions for review client Use data client library for notifications With delete helper migrated to data client With wikidata edits With notifications and modifications migrated to data client With upload migrated to retrofit Delete unused code Reuse thank interface from the library
This commit is contained in:
parent
d24ca9d4fe
commit
8b36ee52d1
15 changed files with 181 additions and 159 deletions
|
|
@ -29,6 +29,7 @@ import fr.free.nrw.commons.utils.MediaDataExtractorUtil;
|
||||||
|
|
||||||
public class Media implements Parcelable {
|
public class Media implements Parcelable {
|
||||||
|
|
||||||
|
public static final Media EMPTY = new Media("");
|
||||||
public static Creator<Media> CREATOR = new Creator<Media>() {
|
public static Creator<Media> CREATOR = new Creator<Media>() {
|
||||||
@Override
|
@Override
|
||||||
public Media createFromParcel(Parcel parcel) {
|
public Media createFromParcel(Parcel parcel) {
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ public class MediaDataExtractor {
|
||||||
* @return return data rich Media object
|
* @return return data rich Media object
|
||||||
*/
|
*/
|
||||||
public Single<Media> getMediaFromFileName(String filename) {
|
public Single<Media> getMediaFromFileName(String filename) {
|
||||||
return okHttpJsonApiClient.getMedia(filename, false);
|
return mediaClient.getMedia(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import fr.free.nrw.commons.kvstore.JsonKvStore;
|
||||||
import fr.free.nrw.commons.mwapi.MediaWikiApi;
|
import fr.free.nrw.commons.mwapi.MediaWikiApi;
|
||||||
import io.reactivex.Completable;
|
import io.reactivex.Completable;
|
||||||
import io.reactivex.Observable;
|
import io.reactivex.Observable;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manage the current logged in user session.
|
* Manage the current logged in user session.
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import javax.inject.Singleton;
|
||||||
|
|
||||||
import fr.free.nrw.commons.Media;
|
import fr.free.nrw.commons.Media;
|
||||||
import fr.free.nrw.commons.bookmarks.Bookmark;
|
import fr.free.nrw.commons.bookmarks.Bookmark;
|
||||||
|
import fr.free.nrw.commons.media.MediaClient;
|
||||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
||||||
import io.reactivex.Observable;
|
import io.reactivex.Observable;
|
||||||
import io.reactivex.ObservableSource;
|
import io.reactivex.ObservableSource;
|
||||||
|
|
@ -19,15 +20,14 @@ import io.reactivex.functions.Function;
|
||||||
@Singleton
|
@Singleton
|
||||||
public class BookmarkPicturesController {
|
public class BookmarkPicturesController {
|
||||||
|
|
||||||
private final OkHttpJsonApiClient okHttpJsonApiClient;
|
private final MediaClient mediaClient;
|
||||||
private final BookmarkPicturesDao bookmarkDao;
|
private final BookmarkPicturesDao bookmarkDao;
|
||||||
|
|
||||||
private List<Bookmark> currentBookmarks;
|
private List<Bookmark> currentBookmarks;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public BookmarkPicturesController(OkHttpJsonApiClient okHttpJsonApiClient,
|
public BookmarkPicturesController(MediaClient mediaClient, BookmarkPicturesDao bookmarkDao) {
|
||||||
BookmarkPicturesDao bookmarkDao) {
|
this.mediaClient = mediaClient;
|
||||||
this.okHttpJsonApiClient = okHttpJsonApiClient;
|
|
||||||
this.bookmarkDao = bookmarkDao;
|
this.bookmarkDao = bookmarkDao;
|
||||||
currentBookmarks = new ArrayList<>();
|
currentBookmarks = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
@ -47,7 +47,7 @@ public class BookmarkPicturesController {
|
||||||
|
|
||||||
private Observable<Media> getMediaFromBookmark(Bookmark bookmark) {
|
private Observable<Media> getMediaFromBookmark(Bookmark bookmark) {
|
||||||
Media dummyMedia = new Media("");
|
Media dummyMedia = new Media("");
|
||||||
return okHttpJsonApiClient.getMedia(bookmark.getMediaName(), false)
|
return mediaClient.getMedia(bookmark.getMediaName())
|
||||||
.map(media -> media == null ? dummyMedia : media)
|
.map(media -> media == null ? dummyMedia : media)
|
||||||
.onErrorReturn(throwable -> dummyMedia)
|
.onErrorReturn(throwable -> dummyMedia)
|
||||||
.toObservable();
|
.toObservable();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,14 @@
|
||||||
package fr.free.nrw.commons.media;
|
package fr.free.nrw.commons.media;
|
||||||
|
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.wikipedia.dataclient.mwapi.MwQueryPage;
|
||||||
|
import org.wikipedia.dataclient.mwapi.MwQueryResponse;
|
||||||
|
import org.wikipedia.dataclient.mwapi.MwQueryResult;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import org.wikipedia.dataclient.mwapi.MwQueryResponse;
|
import org.wikipedia.dataclient.mwapi.MwQueryResponse;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -13,6 +21,13 @@ import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import fr.free.nrw.commons.Media;
|
import fr.free.nrw.commons.Media;
|
||||||
|
import fr.free.nrw.commons.utils.CommonsDateUtil;
|
||||||
|
import io.reactivex.Observable;
|
||||||
|
import io.reactivex.Single;
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import timber.log.Timber;
|
||||||
import io.reactivex.Observable;
|
import io.reactivex.Observable;
|
||||||
import io.reactivex.Single;
|
import io.reactivex.Single;
|
||||||
|
|
||||||
|
|
@ -103,5 +118,47 @@ public class MediaClient {
|
||||||
.map(Media::from)
|
.map(Media::from)
|
||||||
.collect(ArrayList<Media>::new, List::add);
|
.collect(ArrayList<Media>::new, List::add);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches Media object from the imageInfo API
|
||||||
|
*
|
||||||
|
* @param titles the tiles to be searched for. Can be filename or template name
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Single<Media> getMedia(String titles) {
|
||||||
|
return mediaInterface.getMedia(titles)
|
||||||
|
.flatMap(mwQueryResponse -> {
|
||||||
|
if (null == mwQueryResponse
|
||||||
|
|| null == mwQueryResponse.query()
|
||||||
|
|| null == mwQueryResponse.query().firstPage()) {
|
||||||
|
return Observable.empty();
|
||||||
|
}
|
||||||
|
return Observable.just(mwQueryResponse.query().firstPage());
|
||||||
|
})
|
||||||
|
.map(Media::from)
|
||||||
|
.single(Media.EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The method returns the picture of the day
|
||||||
|
*
|
||||||
|
* @return Media object corresponding to the picture of the day
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
public Single<Media> getPictureOfTheDay() {
|
||||||
|
String date = CommonsDateUtil.getIso8601DateFormatShort().format(new Date());
|
||||||
|
Timber.d("Current date is %s", date);
|
||||||
|
String template = "Template:Potd/" + date;
|
||||||
|
return mediaInterface.getMediaWithGenerator(template)
|
||||||
|
.flatMap(mwQueryResponse -> {
|
||||||
|
if (null == mwQueryResponse
|
||||||
|
|| null == mwQueryResponse.query()
|
||||||
|
|| null == mwQueryResponse.query().firstPage()) {
|
||||||
|
return Observable.empty();
|
||||||
|
}
|
||||||
|
return Observable.just(mwQueryResponse.query().firstPage());
|
||||||
|
})
|
||||||
|
.map(Media::from)
|
||||||
|
.single(Media.EMPTY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,6 @@ public interface MediaInterface {
|
||||||
@GET("w/api.php?action=query&format=json&formatversion=2&list=allimages")
|
@GET("w/api.php?action=query&format=json&formatversion=2&list=allimages")
|
||||||
Observable<MwQueryResponse> checkFileExistsUsingSha(@Query("aisha1") String aisha1);
|
Observable<MwQueryResponse> checkFileExistsUsingSha(@Query("aisha1") String aisha1);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method retrieves a list of Media objects filtered using image generator query
|
* This method retrieves a list of Media objects filtered using image generator query
|
||||||
*
|
*
|
||||||
|
|
@ -63,4 +62,28 @@ public interface MediaInterface {
|
||||||
"|Artist|LicenseShortName|LicenseUrl")
|
"|Artist|LicenseShortName|LicenseUrl")
|
||||||
Observable<MwQueryResponse> getMediaListFromSearch(@Query("gsrsearch") String keyword, @Query("gsrlimit") int itemLimit, @QueryMap Map<String, String> continuation);
|
Observable<MwQueryResponse> getMediaListFromSearch(@Query("gsrsearch") String keyword, @Query("gsrlimit") int itemLimit, @QueryMap Map<String, String> continuation);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches Media object from the imageInfo API
|
||||||
|
*
|
||||||
|
* @param title the tiles to be searched for. Can be filename or template name
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GET("w/api.php?action=query&format=json&formatversion=2" +
|
||||||
|
"&prop=imageinfo&iiprop=url|extmetadata&iiurlwidth=640" +
|
||||||
|
"&iiextmetadatafilter=DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal" +
|
||||||
|
"|Artist|LicenseShortName|LicenseUrl")
|
||||||
|
Observable<MwQueryResponse> getMedia(@Query("titles") String title);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches Media object from the imageInfo API
|
||||||
|
* Passes an image generator parameter
|
||||||
|
*
|
||||||
|
* @param title the tiles to be searched for. Can be filename or template name
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GET("w/api.php?action=query&format=json&formatversion=2&generator=images" +
|
||||||
|
"&prop=imageinfo&iiprop=url|extmetadata&iiurlwidth=640" +
|
||||||
|
"&iiextmetadatafilter=DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal" +
|
||||||
|
"|Artist|LicenseShortName|LicenseUrl")
|
||||||
|
Observable<MwQueryResponse> getMediaWithGenerator(@Query("titles") String title);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import android.text.TextUtils;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
import org.apache.http.conn.ClientConnectionManager;
|
import org.apache.http.conn.ClientConnectionManager;
|
||||||
import org.apache.http.conn.scheme.PlainSocketFactory;
|
import org.apache.http.conn.scheme.PlainSocketFactory;
|
||||||
|
|
@ -24,6 +25,10 @@ import java.util.Date;
|
||||||
|
|
||||||
import fr.free.nrw.commons.BuildConfig;
|
import fr.free.nrw.commons.BuildConfig;
|
||||||
import fr.free.nrw.commons.CommonsApplication;
|
import fr.free.nrw.commons.CommonsApplication;
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.BuildConfig;
|
||||||
|
import fr.free.nrw.commons.CommonsApplication;
|
||||||
|
|
||||||
import io.reactivex.Single;
|
import io.reactivex.Single;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -228,58 +228,6 @@ public class OkHttpJsonApiClient {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The method returns the picture of the day
|
|
||||||
*
|
|
||||||
* @return Media object corresponding to the picture of the day
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public Single<Media> getPictureOfTheDay() {
|
|
||||||
String date = CommonsDateUtil.getIso8601DateFormatShort().format(new Date());
|
|
||||||
Timber.d("Current date is %s", date);
|
|
||||||
String template = "Template:Potd/" + date;
|
|
||||||
return getMedia(template, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches Media object from the imageInfo API
|
|
||||||
*
|
|
||||||
* @param titles the tiles to be searched for. Can be filename or template name
|
|
||||||
* @param useGenerator specifies if a image generator parameter needs to be passed or not
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Single<Media> getMedia(String titles, boolean useGenerator) {
|
|
||||||
HttpUrl.Builder urlBuilder = HttpUrl
|
|
||||||
.parse(commonsBaseUrl)
|
|
||||||
.newBuilder()
|
|
||||||
.addQueryParameter("action", "query")
|
|
||||||
.addQueryParameter("format", "json")
|
|
||||||
.addQueryParameter("formatversion", "2")
|
|
||||||
.addQueryParameter("titles", titles);
|
|
||||||
|
|
||||||
if (useGenerator) {
|
|
||||||
urlBuilder.addQueryParameter("generator", "images");
|
|
||||||
}
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(appendMediaProperties(urlBuilder).build())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
return Single.fromCallable(() -> {
|
|
||||||
Response response = okHttpClient.newCall(request).execute();
|
|
||||||
if (response.body() != null && response.isSuccessful()) {
|
|
||||||
String json = response.body().string();
|
|
||||||
MwQueryResponse mwQueryPage = gson.fromJson(json, MwQueryResponse.class);
|
|
||||||
if (mwQueryPage.success() && mwQueryPage.query().firstPage() != null) {
|
|
||||||
return Media.from(mwQueryPage.query().firstPage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whenever imageInfo is fetched, these common properties can be specified for the API call
|
* Whenever imageInfo is fetched, these common properties can be specified for the API call
|
||||||
* https://www.mediawiki.org/wiki/API:Imageinfo
|
* https://www.mediawiki.org/wiki/API:Imageinfo
|
||||||
|
|
|
||||||
|
|
@ -25,26 +25,21 @@ public class ReviewHelper {
|
||||||
|
|
||||||
private static final String[] imageExtensions = new String[]{".jpg", ".jpeg", ".png"};
|
private static final String[] imageExtensions = new String[]{".jpg", ".jpeg", ".png"};
|
||||||
|
|
||||||
private final OkHttpJsonApiClient okHttpJsonApiClient;
|
|
||||||
private final MediaWikiApi mediaWikiApi;
|
|
||||||
private final MediaClient mediaClient;
|
private final MediaClient mediaClient;
|
||||||
private final ReviewInterface reviewInterface;
|
private final ReviewInterface reviewInterface;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ReviewHelper(OkHttpJsonApiClient okHttpJsonApiClient,
|
public ReviewHelper(MediaClient mediaClient, ReviewInterface reviewInterface) {
|
||||||
MediaWikiApi mediaWikiApi,
|
|
||||||
MediaClient mediaClient, ReviewInterface reviewInterface) {
|
|
||||||
this.okHttpJsonApiClient = okHttpJsonApiClient;
|
|
||||||
this.mediaWikiApi = mediaWikiApi;
|
|
||||||
this.mediaClient = mediaClient;
|
this.mediaClient = mediaClient;
|
||||||
this.reviewInterface = reviewInterface;
|
this.reviewInterface = reviewInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches recent changes from MediaWiki API
|
* Fetches recent changes from MediaWiki AP
|
||||||
* Calls the API to get 10 changes in the last 1 hour
|
* Calls the API to get 10 changes in the last 1 hour
|
||||||
* Earlier we were getting changes for the last 30 days but as the API returns just 10 results
|
* Earlier we were getting changes for the last 30 days but as the API returns just 10 results
|
||||||
* its best to fetch for just last 1 hour.
|
* its best to fetch for just last 1 hour.
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private Observable<RecentChange> getRecentChanges() {
|
private Observable<RecentChange> getRecentChanges() {
|
||||||
|
|
@ -84,6 +79,7 @@ public class ReviewHelper {
|
||||||
/**
|
/**
|
||||||
* Returns a proper Media object if the file is not already nominated for deletion
|
* Returns a proper Media object if the file is not already nominated for deletion
|
||||||
* Else it returns an empty Media object
|
* Else it returns an empty Media object
|
||||||
|
*
|
||||||
* @param recentChange
|
* @param recentChange
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|
@ -94,13 +90,14 @@ public class ReviewHelper {
|
||||||
if (isDeleted) {
|
if (isDeleted) {
|
||||||
return Single.just(new Media(""));
|
return Single.just(new Media(""));
|
||||||
}
|
}
|
||||||
return okHttpJsonApiClient.getMedia(recentChange.getTitle(), false);
|
return mediaClient.getMedia(recentChange.getTitle());
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the first revision of the file from filename
|
* Gets the first revision of the file from filename
|
||||||
|
*
|
||||||
* @param filename
|
* @param filename
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|
@ -113,6 +110,7 @@ public class ReviewHelper {
|
||||||
* Checks if the change is reviewable or not.
|
* Checks if the change is reviewable or not.
|
||||||
* - checks the type and revisionId of the change
|
* - checks the type and revisionId of the change
|
||||||
* - checks supported image extensions
|
* - checks supported image extensions
|
||||||
|
*
|
||||||
* @param recentChange
|
* @param recentChange
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ import androidx.annotation.Nullable;
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.contributions.MainActivity;
|
import fr.free.nrw.commons.contributions.MainActivity;
|
||||||
import fr.free.nrw.commons.di.ApplicationlessInjection;
|
import fr.free.nrw.commons.di.ApplicationlessInjection;
|
||||||
|
import fr.free.nrw.commons.media.MediaClient;
|
||||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||||
import io.reactivex.disposables.CompositeDisposable;
|
import io.reactivex.disposables.CompositeDisposable;
|
||||||
|
|
@ -42,7 +43,7 @@ public class PicOfDayAppWidget extends AppWidgetProvider {
|
||||||
|
|
||||||
private CompositeDisposable compositeDisposable = new CompositeDisposable();
|
private CompositeDisposable compositeDisposable = new CompositeDisposable();
|
||||||
|
|
||||||
@Inject OkHttpJsonApiClient okHttpJsonApiClient;
|
@Inject MediaClient mediaClient;
|
||||||
|
|
||||||
void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
|
void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
|
||||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.pic_of_day_app_widget);
|
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.pic_of_day_app_widget);
|
||||||
|
|
@ -67,7 +68,7 @@ public class PicOfDayAppWidget extends AppWidgetProvider {
|
||||||
RemoteViews views,
|
RemoteViews views,
|
||||||
AppWidgetManager appWidgetManager,
|
AppWidgetManager appWidgetManager,
|
||||||
int appWidgetId) {
|
int appWidgetId) {
|
||||||
compositeDisposable.add(okHttpJsonApiClient.getPictureOfTheDay()
|
compositeDisposable.add(mediaClient.getPictureOfTheDay()
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe(
|
.subscribe(
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,13 @@ class MediaDataExtractorTest {
|
||||||
internal var mwApi: MediaWikiApi? = null
|
internal var mwApi: MediaWikiApi? = null
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
internal var okHttpJsonApiClient: OkHttpJsonApiClient? = null
|
internal var mediaClient: MediaClient? = null
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
internal var mediaClient: MediaClient? = null
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
internal var mediaClient: MediaClient? = null
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
internal var mediaClient: MediaClient? = null
|
internal var mediaClient: MediaClient? = null
|
||||||
|
|
@ -46,7 +52,7 @@ class MediaDataExtractorTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun fetchMediaDetails() {
|
fun fetchMediaDetails() {
|
||||||
`when`(okHttpJsonApiClient?.getMedia(ArgumentMatchers.anyString(), ArgumentMatchers.anyBoolean()))
|
`when`(mediaClient?.getMedia(ArgumentMatchers.anyString()))
|
||||||
.thenReturn(Single.just(mock(Media::class.java)))
|
.thenReturn(Single.just(mock(Media::class.java)))
|
||||||
|
|
||||||
`when`(mediaClient?.checkPageExistsUsingTitle(ArgumentMatchers.anyString()))
|
`when`(mediaClient?.checkPageExistsUsingTitle(ArgumentMatchers.anyString()))
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import java.util.List;
|
||||||
|
|
||||||
import fr.free.nrw.commons.Media;
|
import fr.free.nrw.commons.Media;
|
||||||
import fr.free.nrw.commons.bookmarks.Bookmark;
|
import fr.free.nrw.commons.bookmarks.Bookmark;
|
||||||
|
import fr.free.nrw.commons.media.MediaClient;
|
||||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
||||||
import io.reactivex.Single;
|
import io.reactivex.Single;
|
||||||
|
|
||||||
|
|
@ -29,7 +30,7 @@ import static org.mockito.Mockito.when;
|
||||||
public class BookmarkPicturesControllerTest {
|
public class BookmarkPicturesControllerTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
OkHttpJsonApiClient okHttpJsonApiClient;
|
MediaClient mediaClient;
|
||||||
@Mock
|
@Mock
|
||||||
BookmarkPicturesDao bookmarkDao;
|
BookmarkPicturesDao bookmarkDao;
|
||||||
|
|
||||||
|
|
@ -46,7 +47,7 @@ public class BookmarkPicturesControllerTest {
|
||||||
Media mockMedia = getMockMedia();
|
Media mockMedia = getMockMedia();
|
||||||
when(bookmarkDao.getAllBookmarks())
|
when(bookmarkDao.getAllBookmarks())
|
||||||
.thenReturn(getMockBookmarkList());
|
.thenReturn(getMockBookmarkList());
|
||||||
when(okHttpJsonApiClient.getMedia(anyString(), anyBoolean()))
|
when(mediaClient.getMedia(anyString()))
|
||||||
.thenReturn(Single.just(mockMedia));
|
.thenReturn(Single.just(mockMedia));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,9 +76,9 @@ public class BookmarkPicturesControllerTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void loadBookmarkedPicturesForNullMedia() {
|
public void loadBookmarkedPicturesForNullMedia() {
|
||||||
when(okHttpJsonApiClient.getMedia("File:Test1.jpg", false))
|
when(mediaClient.getMedia("File:Test1.jpg"))
|
||||||
.thenReturn(Single.error(new NullPointerException("Error occurred")));
|
.thenReturn(Single.error(new NullPointerException("Error occurred")));
|
||||||
when(okHttpJsonApiClient.getMedia("File:Test2.jpg", false))
|
when(mediaClient.getMedia("File:Test2.jpg"))
|
||||||
.thenReturn(Single.just(getMockMedia()));
|
.thenReturn(Single.just(getMockMedia()));
|
||||||
List<Media> bookmarkedPictures = bookmarkPicturesController.loadBookmarkedPictures().blockingGet();
|
List<Media> bookmarkedPictures = bookmarkPicturesController.loadBookmarkedPictures().blockingGet();
|
||||||
assertEquals(1, bookmarkedPictures.size());
|
assertEquals(1, bookmarkedPictures.size());
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package fr.free.nrw.commons.media
|
package fr.free.nrw.commons.media
|
||||||
|
|
||||||
import fr.free.nrw.commons.Media
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.utils.CommonsDateUtil
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import junit.framework.Assert.*
|
import junit.framework.Assert.*
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
|
|
@ -102,6 +103,68 @@ class MediaClientTest {
|
||||||
assertFalse(checkFileExistsUsingSha)
|
assertFalse(checkFileExistsUsingSha)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getMedia() {
|
||||||
|
val imageInfo = ImageInfo()
|
||||||
|
|
||||||
|
val mwQueryPage = mock(MwQueryPage::class.java)
|
||||||
|
`when`(mwQueryPage.title()).thenReturn("Test")
|
||||||
|
`when`(mwQueryPage.imageInfo()).thenReturn(imageInfo)
|
||||||
|
|
||||||
|
val mwQueryResult = mock(MwQueryResult::class.java)
|
||||||
|
`when`(mwQueryResult.firstPage()).thenReturn(mwQueryPage)
|
||||||
|
val mockResponse = mock(MwQueryResponse::class.java)
|
||||||
|
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||||
|
|
||||||
|
`when`(mediaInterface!!.getMedia(ArgumentMatchers.anyString()))
|
||||||
|
.thenReturn(Observable.just(mockResponse))
|
||||||
|
|
||||||
|
assertEquals("Test", mediaClient!!.getMedia("abcde").blockingGet().filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getMediaNull() {
|
||||||
|
val imageInfo = ImageInfo()
|
||||||
|
|
||||||
|
val mwQueryPage = mock(MwQueryPage::class.java)
|
||||||
|
`when`(mwQueryPage.title()).thenReturn("Test")
|
||||||
|
`when`(mwQueryPage.imageInfo()).thenReturn(imageInfo)
|
||||||
|
|
||||||
|
val mwQueryResult = mock(MwQueryResult::class.java)
|
||||||
|
`when`(mwQueryResult.firstPage()).thenReturn(null)
|
||||||
|
val mockResponse = mock(MwQueryResponse::class.java)
|
||||||
|
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||||
|
|
||||||
|
`when`(mediaInterface!!.getMedia(ArgumentMatchers.anyString()))
|
||||||
|
.thenReturn(Observable.just(mockResponse))
|
||||||
|
|
||||||
|
assertEquals(Media.EMPTY, mediaClient!!.getMedia("abcde").blockingGet())
|
||||||
|
}
|
||||||
|
@Captor
|
||||||
|
private val filenameCaptor: ArgumentCaptor<String>? = null
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getPictureOfTheDay() {
|
||||||
|
val template = "Template:Potd/" + CommonsDateUtil.getIso8601DateFormatShort().format(Date())
|
||||||
|
|
||||||
|
val imageInfo = ImageInfo()
|
||||||
|
|
||||||
|
val mwQueryPage = mock(MwQueryPage::class.java)
|
||||||
|
`when`(mwQueryPage.title()).thenReturn("Test")
|
||||||
|
`when`(mwQueryPage.imageInfo()).thenReturn(imageInfo)
|
||||||
|
|
||||||
|
val mwQueryResult = mock(MwQueryResult::class.java)
|
||||||
|
`when`(mwQueryResult.firstPage()).thenReturn(mwQueryPage)
|
||||||
|
val mockResponse = mock(MwQueryResponse::class.java)
|
||||||
|
`when`(mockResponse.query()).thenReturn(mwQueryResult)
|
||||||
|
|
||||||
|
`when`(mediaInterface!!.getMediaWithGenerator(filenameCaptor!!.capture()))
|
||||||
|
.thenReturn(Observable.just(mockResponse))
|
||||||
|
|
||||||
|
assertEquals("Test", mediaClient!!.getPictureOfTheDay().blockingGet().filename)
|
||||||
|
assertEquals(template, filenameCaptor.value);
|
||||||
|
}
|
||||||
|
|
||||||
@Captor
|
@Captor
|
||||||
private val continuationCaptor: ArgumentCaptor<Map<String, String>>? = null
|
private val continuationCaptor: ArgumentCaptor<Map<String, String>>? = null
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import fr.free.nrw.commons.Media
|
||||||
import fr.free.nrw.commons.TestCommonsApplication
|
import fr.free.nrw.commons.TestCommonsApplication
|
||||||
import fr.free.nrw.commons.kvstore.JsonKvStore
|
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||||
import fr.free.nrw.commons.utils.CommonsDateUtil
|
import fr.free.nrw.commons.utils.CommonsDateUtil
|
||||||
import junit.framework.Assert.assertEquals
|
|
||||||
import okhttp3.HttpUrl
|
import okhttp3.HttpUrl
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.mockwebserver.MockResponse
|
import okhttp3.mockwebserver.MockResponse
|
||||||
|
|
@ -67,83 +66,6 @@ class OkHttpJsonApiClientTest {
|
||||||
campaignsServer.shutdown()
|
campaignsServer.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Test response for getting media without generator
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
fun getMedia() {
|
|
||||||
server.enqueue(getMediaList("", "", "", 1))
|
|
||||||
|
|
||||||
val media = testObject.getMedia("Test.jpg", false)!!.blockingGet()
|
|
||||||
|
|
||||||
assertBasicRequestParameters(server, "GET").let { request ->
|
|
||||||
parseQueryParams(request).let { body ->
|
|
||||||
Assert.assertEquals("json", body["format"])
|
|
||||||
Assert.assertEquals("2", body["formatversion"])
|
|
||||||
Assert.assertEquals("query", body["action"])
|
|
||||||
Assert.assertEquals("Test.jpg", body["titles"])
|
|
||||||
Assert.assertEquals("imageinfo", body["prop"])
|
|
||||||
Assert.assertEquals("url|extmetadata", body["iiprop"])
|
|
||||||
Assert.assertEquals("DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal|Artist|LicenseShortName|LicenseUrl", body["iiextmetadatafilter"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(media is Media)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test response for getting media with generator
|
|
||||||
* Equivalent of testing POTD
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
fun getImageWithGenerator() {
|
|
||||||
val template = "Template:Potd/" + CommonsDateUtil.getIso8601DateFormatShort().format(Date())
|
|
||||||
server.enqueue(getMediaList("", "", "", 1))
|
|
||||||
|
|
||||||
val media = testObject.getMedia(template, true)!!.blockingGet()
|
|
||||||
|
|
||||||
assertBasicRequestParameters(server, "GET").let { request ->
|
|
||||||
parseQueryParams(request).let { body ->
|
|
||||||
Assert.assertEquals("json", body["format"])
|
|
||||||
Assert.assertEquals("2", body["formatversion"])
|
|
||||||
Assert.assertEquals("query", body["action"])
|
|
||||||
Assert.assertEquals(template, body["titles"])
|
|
||||||
Assert.assertEquals("images", body["generator"])
|
|
||||||
Assert.assertEquals("imageinfo", body["prop"])
|
|
||||||
Assert.assertEquals("url|extmetadata", body["iiprop"])
|
|
||||||
Assert.assertEquals("DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal|Artist|LicenseShortName|LicenseUrl", body["iiextmetadatafilter"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(media is Media)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test response for getting picture of the day
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
fun getPictureOfTheDay() {
|
|
||||||
val template = "Template:Potd/" + CommonsDateUtil.getIso8601DateFormatShort().format(Date())
|
|
||||||
server.enqueue(getMediaList("", "", "", 1))
|
|
||||||
|
|
||||||
val media = testObject.pictureOfTheDay?.blockingGet()
|
|
||||||
|
|
||||||
assertBasicRequestParameters(server, "GET").let { request ->
|
|
||||||
parseQueryParams(request).let { body ->
|
|
||||||
Assert.assertEquals("json", body["format"])
|
|
||||||
Assert.assertEquals("2", body["formatversion"])
|
|
||||||
Assert.assertEquals("query", body["action"])
|
|
||||||
Assert.assertEquals(template, body["titles"])
|
|
||||||
Assert.assertEquals("images", body["generator"])
|
|
||||||
Assert.assertEquals("imageinfo", body["prop"])
|
|
||||||
Assert.assertEquals("url|extmetadata", body["iiprop"])
|
|
||||||
Assert.assertEquals("DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal|Artist|LicenseShortName|LicenseUrl", body["iiextmetadatafilter"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(media is Media)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a MockResponse object which contains a list of media pages
|
* Generate a MockResponse object which contains a list of media pages
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -29,10 +29,6 @@ class ReviewHelperTest {
|
||||||
@Mock
|
@Mock
|
||||||
internal var reviewInterface: ReviewInterface? = null
|
internal var reviewInterface: ReviewInterface? = null
|
||||||
@Mock
|
@Mock
|
||||||
internal var okHttpJsonApiClient: OkHttpJsonApiClient? = null
|
|
||||||
@Mock
|
|
||||||
internal var mediaWikiApi: MediaWikiApi? = null
|
|
||||||
@Mock
|
|
||||||
internal var mediaClient: MediaClient? = null
|
internal var mediaClient: MediaClient? = null
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
|
|
@ -68,7 +64,7 @@ class ReviewHelperTest {
|
||||||
|
|
||||||
val media = mock(Media::class.java)
|
val media = mock(Media::class.java)
|
||||||
`when`(media.filename).thenReturn("File:Test.jpg")
|
`when`(media.filename).thenReturn("File:Test.jpg")
|
||||||
`when`(okHttpJsonApiClient?.getMedia(ArgumentMatchers.anyString(), ArgumentMatchers.anyBoolean()))
|
`when`(mediaClient?.getMedia(ArgumentMatchers.anyString()))
|
||||||
.thenReturn(Single.just(media))
|
.thenReturn(Single.just(media))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue