-
-
Notifications
You must be signed in to change notification settings - Fork 490
Mock mix pl tests #482
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
Merged
Merged
Mock mix pl tests #482
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4dad3d6
Move DownloaderTestImpl into downloader package
XiangRongLin 1bcb9c7
Generate equals and hashCode for Request
XiangRongLin 7c40fb8
Add additional downloader implementations
XiangRongLin e6e8e39
Add DownloaderFactory to return a specific downloader based on 2 vari…
XiangRongLin 285c26e
Adjust YoutubeMixPlaylistExtractorTest to use DownloaderFactory
XiangRongLin f8aa989
Add generated json and manually copy client_version.json
XiangRongLin f447a7a
fix import due to downloader package move
XiangRongLin 35e2997
Add method to allow resetting youtube client version and key
XiangRongLin 255c726
Add resource path base to DownloaderFactory
XiangRongLin f91916c
Remove unnecessary file object creation
XiangRongLin 1ea6c6c
Prefix mock file from RecordingDownloader with "generated_mock_"
XiangRongLin e82cb1e
Remove dependency to commons-io
XiangRongLin 63c237d
Only create directories if they do not exist
XiangRongLin 3455f0f
Add documentation to testing with mocks
XiangRongLin 4af50c6
Use @link for downloader
XiangRongLin 6ca7123
Add comment for usage of YoutubeParsingHelper.resetClientVersionAndKey
XiangRongLin 4f81d9d
Regenerate mock files
XiangRongLin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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; | ||
|
||
/** | ||
* @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
|
||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...rg/schabi/newpipe/DownloaderTestImpl.java → ...ewpipe/downloader/DownloaderTestImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
48 changes: 48 additions & 0 deletions
48
extractor/src/test/java/org/schabi/newpipe/downloader/MockDownloader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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; | ||
|
||
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; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
extractor/src/test/java/org/schabi/newpipe/downloader/RecordingDownloader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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; | ||
|
||
class RecordingDownloader extends Downloader { | ||
|
||
public final static String FILE_NAME_PREFIX = "generated_mock_"; | ||
|
||
private int index = 0; | ||
private final String path; | ||
|
||
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(); | ||
} | ||
} | ||
} | ||
Files.createDirectories(path); | ||
XiangRongLin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
@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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
extractor/src/test/java/org/schabi/newpipe/downloader/TestRequestResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...a/org/schabi/newpipe/extractor/services/media_ccc/search/MediaCCCSearchExtractorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.