Search in sources :

Example 1 with FileZipResponse

use of org.nzbhydra.downloading.FileZipResponse in project nzbhydra2 by theotherp.

the class SearchingResultsUiTest method prepareFiveResultsFromTwoIndexers.

protected void prepareFiveResultsFromTwoIndexers() throws Exception {
    replaceConfig(getClass().getResource("twoIndexers.json"));
    mockWebServer.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            int mockServerPort = mockWebServer.getPort();
            String mockServerBaseUrl = "http://127.0.0.1:" + mockServerPort + "/";
            if (request.getRequestUrl().queryParameter("apikey") != null && request.getRequestUrl().queryParameter("apikey").equals("apikey1")) {
                NewznabXmlItem result1 = RssItemBuilder.builder("indexer1-result1").pubDate(Instant.now().minus(1, ChronoUnit.DAYS)).link(mockServerBaseUrl + "nzblink1").hasNfo(false).grabs(1).size(mbToBytes(1)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "5000")))).category("TV").build();
                NewznabXmlItem result2 = RssItemBuilder.builder("indexer1-result2").pubDate(Instant.now().minus(2, ChronoUnit.DAYS)).link(mockServerBaseUrl + "nzblink2").hasNfo(true).grabs(2).size(mbToBytes(2)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "5040")))).category("TV SD").build();
                NewznabXmlItem result3 = RssItemBuilder.builder("indexer1-result3").pubDate(Instant.now().minus(3, ChronoUnit.DAYS)).link(mockServerBaseUrl + "nzblink3").comments("comments").grabs(3).size(mbToBytes(3)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "5030")))).category("TV HD").build();
                NewznabXmlRoot rssRoot = NewznabMockBuilder.getRssRoot(Arrays.asList(result1, result2, result3), 0, 3);
                return new MockResponse().setBody(rssRoot.toXmlString()).setHeader("Content-Type", "application/xml; charset=utf-8");
            } else if (request.getRequestUrl().queryParameter("apikey") != null && request.getRequestUrl().queryParameter("apikey").equals("apikey2")) {
                NewznabXmlItem result5 = RssItemBuilder.builder("indexer2-result2").pubDate(Instant.now().minus(5, ChronoUnit.DAYS)).link(mockServerBaseUrl + "nzblink5").grabs(5).size(mbToBytes(5)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "2040")))).category("Movies HD").build();
                NewznabXmlItem result4 = RssItemBuilder.builder("indexer2-result1").pubDate(Instant.now().minus(4, ChronoUnit.DAYS)).link(mockServerBaseUrl + "nzblink4").grabs(4).size(mbToBytes(4)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "2000")))).category("Movies").build();
                NewznabXmlRoot rssRoot = NewznabMockBuilder.getRssRoot(Arrays.asList(result4, result5), 0, 2);
                return new MockResponse().setBody(rssRoot.toXmlString()).setHeader("Content-Type", "application/xml; charset=utf-8");
            } else if (request.getPath().endsWith("nzbzip")) {
                System.out.println("Returning NZB ZIP response");
                String body = request.getBody().readString(Charset.defaultCharset());
                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    List<Long> ids = objectMapper.readValue(body, new TypeReference<List<Long>>() {
                    });
                    FileZipResponse response = new FileZipResponse(true, "bla", null, ids, Collections.emptyList());
                    return new MockResponse().setBody(objectMapper.writeValueAsString(response)).setHeader("Content-Type", "application/xml; charset=utf-8");
                } catch (IOException e) {
                }
                throw new RuntimeException("Unable to handle nzbzip");
            } else if (request.getPath().contains("nzblink")) {
                System.out.println("Returning NZB");
                return new MockResponse().setBody("nzb").setHeader("Content-Type", "application/x-nzb; charset=utf-8");
            } else {
                throw new RuntimeException("Unexpected api key " + request.getRequestUrl().queryParameter("apikey"));
            }
        }
    });
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) NewznabAttribute(org.nzbhydra.mapping.newznab.xml.NewznabAttribute) NewznabXmlRoot(org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot) ArrayList(java.util.ArrayList) NewznabXmlItem(org.nzbhydra.mapping.newznab.xml.NewznabXmlItem) IOException(java.io.IOException) Dispatcher(okhttp3.mockwebserver.Dispatcher) ArrayList(java.util.ArrayList) List(java.util.List) FileZipResponse(org.nzbhydra.downloading.FileZipResponse) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Dispatcher (okhttp3.mockwebserver.Dispatcher)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 FileZipResponse (org.nzbhydra.downloading.FileZipResponse)1 NewznabAttribute (org.nzbhydra.mapping.newznab.xml.NewznabAttribute)1 NewznabXmlItem (org.nzbhydra.mapping.newznab.xml.NewznabXmlItem)1 NewznabXmlRoot (org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot)1