Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock mix pl tests #482

Merged
merged 17 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extractor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ dependencies {

testImplementation 'junit:junit:4.13.1'
testImplementation "com.squareup.okhttp3:okhttp:3.12.11"
testImplementation 'com.google.code.gson:gson:2.8.6'
Stypox marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,27 @@ public static Map<String, List<String>> headersFromLocalization(@Nullable Locali

return headers;
}

/*//////////////////////////////////////////////////////////////////////////
// Generated
//////////////////////////////////////////////////////////////////////////*/

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Request request = (Request) o;
return httpMethod.equals(request.httpMethod) &&
url.equals(request.url) &&
headers.equals(request.headers) &&
Arrays.equals(dataToSend, request.dataToSend) &&
Objects.equals(localization, request.localization);
}

@Override
public int hashCode() {
int result = Objects.hash(httpMethod, url, headers, localization);
result = 31 * result + Arrays.hashCode(dataToSend);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ public static String getKey() throws IOException, ExtractionException {
return key;
}

static void resetClientVersionAndKey() {
Stypox marked this conversation as resolved.
Show resolved Hide resolved
clientVersion = null;
key = null;
}

public static boolean areHardcodedYoutubeMusicKeysValid() throws IOException, ReCaptchaException {
final String url = "https://music.youtube.com/youtubei/v1/search?alt=json&key=" + HARDCODED_YOUTUBE_MUSIC_KEYS[0];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.schabi.newpipe.downloader;

import org.schabi.newpipe.extractor.downloader.Downloader;

import java.io.IOException;

public class DownloaderFactory {

public final static String RESOURCE_PATH = "src/test/resources/org/schabi/newpipe/extractor/";

private final static DownloaderType DEFAULT_DOWNLOADER = DownloaderType.REAL;

/**
* <p>
* Returns a implementation of a {@link Downloader}.
* </p>
* <p>
* If the system property "downloader" is set and is one of {@link DownloaderType},
* then a downloader of that type is returned.
* It can be passed in with gradle by adding the argument -Ddownloader=abcd,
* where abcd is one of {@link DownloaderType}
* </p>
* <p>
* Otherwise it falls back to {@link DownloaderFactory#DEFAULT_DOWNLOADER}.
* Change this during development on the local machine to use a different downloader.
* </p>
*
* @param path The path to the folder where mocks are saved/retrieved.
* Preferably starting with {@link DownloaderFactory#RESOURCE_PATH}
*/
public Downloader getDownloader(String path) throws IOException {
DownloaderType type;
try {
type = DownloaderType.valueOf(System.getProperty("downloader"));
} catch (Exception e) {
type = DEFAULT_DOWNLOADER;
}

switch (type) {
case REAL:
return DownloaderTestImpl.getInstance();
case MOCK:
return new MockDownloader(path);
case RECORDING:
return new RecordingDownloader(path);
default:
throw new UnsupportedOperationException("Unknown downloader type: " + type.toString());
XiangRongLin marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.schabi.newpipe;
package org.schabi.newpipe.downloader;

import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Request;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.schabi.newpipe.downloader;

public enum DownloaderType {
REAL, MOCK, RECORDING
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.schabi.newpipe.downloader;

import com.google.gson.GsonBuilder;

import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Request;
import org.schabi.newpipe.extractor.downloader.Response;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nonnull;

/**
* <p>
* Mocks requests by using json files created by {@link RecordingDownloader}
* </p>
*/
class MockDownloader extends Downloader {

private final String path;
private final Map<Request, Response> mocks;

public MockDownloader(@Nonnull String path) throws IOException {
this.path = path;
this.mocks = new HashMap<>();
File folder = new File(path);
for (File file : folder.listFiles()) {
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
final FileReader reader = new FileReader(file);
final TestRequestResponse response = new GsonBuilder()
.create()
.fromJson(reader, TestRequestResponse.class);
reader.close();
mocks.put(response.getRequest(), response.getResponse());
}
}
}

@Override
public Response execute(@Nonnull Request request) {
Response result = mocks.get(request);
if (result == null) {
throw new NullPointerException("No mock response for request with url '" + request.url()
+ "' exists in path '" + path + "'.\nPlease make sure to run the tests with " +
"the RecordingDownloader first after changes.");
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.schabi.newpipe.downloader;

import com.google.gson.GsonBuilder;

import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Request;
import org.schabi.newpipe.extractor.downloader.Response;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.annotation.Nonnull;

/**
* <p>
* Relays requests to {@link DownloaderTestImpl} and saves the request/response pair into a json file.
* </p>
* <p>
* Those files are used by {@link MockDownloader}.
* </p>
* <p>
* The files <b>must</b> be created on the local dev environment
* and recreated when the requests made by a test class change.
* </p>
*/
class RecordingDownloader extends Downloader {

public final static String FILE_NAME_PREFIX = "generated_mock_";

private int index = 0;
private final String path;

/**
* Creates the folder described by {@code stringPath} if it does not exists.
* Deletes existing files starting with {@link RecordingDownloader#FILE_NAME_PREFIX}.
* @param stringPath Path to the folder where the json files will be saved to.
*/
public RecordingDownloader(String stringPath) throws IOException {
this.path = stringPath;
Path path = Paths.get(stringPath);
File folder = path.toFile();
if (folder.exists()) {
for (File file : folder.listFiles()) {
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
file.delete();
}
}
} else {
Files.createDirectories(path);
}
}

@Override
public Response execute(@Nonnull Request request) throws IOException, ReCaptchaException {
Downloader downloader = DownloaderTestImpl.getInstance();
Response response = downloader.execute(request);

File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index + ".json");
index++;
outputFile.createNewFile();
FileWriter writer = new FileWriter(outputFile);
new GsonBuilder()
.setPrettyPrinting()
.create()
.toJson(new TestRequestResponse(request, response), writer);
writer.flush();
writer.close();

return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.schabi.newpipe.downloader;

import org.schabi.newpipe.extractor.downloader.Request;
import org.schabi.newpipe.extractor.downloader.Response;

final class TestRequestResponse {
private final Request request;
private final Response response;

public TestRequestResponse(Request request, Response response) {
this.request = request;
this.response = response;
}

public Request getRequest() {
return request;
}

public Response getResponse() {
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCConferenceExtractor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.media_ccc.linkHandler.MediaCCCConferenceLinkHandlerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCStreamExtractor;
import org.schabi.newpipe.extractor.stream.AudioStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.services.DefaultStreamExtractorTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.media_ccc.linkHandler.MediaCCCStreamLinkHandlerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.schabi.newpipe.extractor.services.media_ccc.search;

import org.junit.BeforeClass;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.Page;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeCommentsLinkHandlerFactory;
Expand Down
Loading