<commit_before>package eu.freme.eservices.epublishing.webservice;

import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
import org.junit.Test;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.epub.EpubReader;
import org.junit.Assert;

/**
 * <p>
 * Basic e-publishing test</p>
 *
 * <p>
 * Copyright 2015 MMLab, UGent</p>
 *
 * @author <NAME>
 */
public class EPubTest extends ClientAndLocalServerBase {

    @Test
    public void TestAlice() throws IOException {
        String title = "Alice in Utopia";
        String author = "<NAME>";
        String author2 = "<NAME>";
        String description = "Dit is een heel mooi boekske.";

        File zippedHTMLFile = new File("src/test/resources/alice.zip");
        System.out.println("Converting " + zippedHTMLFile + " to EPUB format.");

        Metadata metadata = new Metadata();
        ArrayList<String> titles = new ArrayList<>();
        ArrayList<String> authors = new ArrayList<>();
        titles.add(title);
        authors.add(author);
        authors.add(author2);
        metadata.setTitles(titles);
        metadata.setAuthors(authors);
        metadata.setDescription(description);
        Identifier id = new Identifier(null, "urn:ean:1234-7956-1356-1123");
        metadata.setIdentifier(id);

        Gson gson = new Gson();

        // create body part containing the ePub file
        FileDataBodyPart ePubFilePart = new FileDataBodyPart("htmlZip", zippedHTMLFile);

        // the form, to which parameters and binary content can be added
        FormDataMultiPart multiPart = new FormDataMultiPart();
        multiPart.bodyPart(ePubFilePart);			// add ePub
        multiPart.field("metadata", gson.toJson(metadata));
        System.out.println(gson.toJson(metadata));

        // now send it to the service
        Response response = target.path("epublish/html").request().post(Entity.entity(multiPart, multiPart.getMediaType()));
        InputStream in = response.readEntity(InputStream.class);

        // write it to a (temporary) file
        File ePubFile = File.createTempFile("alice", ".epub");
        System.out.println("Writing result to " + ePubFile);
        IOUtils.copy(in, new FileOutputStream(ePubFile));

        //read file and perform checks
        EpubReader r = new EpubReader();
        Book b = r.readEpub(new FileInputStream(ePubFile));
        List<String> bookTitles = b.getMetadata().getTitles();
        List<Author> bookAuthors = b.getMetadata().getAuthors();
        List<String> bookAuthorsNames = new ArrayList<String>();

        for (Author a : bookAuthors) {
            bookAuthorsNames.add(a.getFirstname() + " " + a.getLastname());
        }

        Assert.assertTrue(bookTitles.containsAll(titles));
        Assert.assertTrue(titles.containsAll(bookTitles));
        System.out.println(bookAuthorsNames);
        Assert.assertTrue(bookAuthorsNames.containsAll(authors));
        Assert.assertTrue(authors.containsAll(bookAuthorsNames));
    }

    @Test
    public void TestSections() throws IOException {
        String title = "Alice in Utopia";
        String author = "<NAME>";

        File zippedHTMLFile = new File("src/test/resources/alice.zip");
        System.out.println("Converting " + zippedHTMLFile + " to EPUB format.");

        Metadata metadata = new Metadata();
        ArrayList<String> titles = new ArrayList<>();
        ArrayList<String> authors = new ArrayList<>();
        ArrayList<Section> toc = new ArrayList<>();
        titles.add(title);
        authors.add(author);
        metadata.setTitles(titles);
        metadata.setAuthors(authors);
        toc.add(new Section("Chapter 1", "01.xhtml"));
        toc.add(new Section("Chapter 2", "02.xhtml"));
        metadata.setTableOfContents(toc);

        Gson gson = new Gson();

        // create body part containing the ePub file
        FileDataBodyPart ePubFilePart = new FileDataBodyPart("htmlZip", zippedHTMLFile);

        // the form, to which parameters and binary content can be added
        FormDataMultiPart multiPart = new FormDataMultiPart();
        multiPart.bodyPart(ePubFilePart);			// add ePub
        multiPart.field("metadata", gson.toJson(metadata));

        // now send it to the service
        Response response = target.path("epublish/html").request().post(Entity.entity(multiPart, multiPart.getMediaType()));
        InputStream in = response.readEntity(InputStream.class);

        // write it to a (temporary) file
        File ePubFile = File.createTempFile("alice", ".epub");
        System.out.println("Writing result to " + ePubFile);
        IOUtils.copy(in, new FileOutputStream(ePubFile));

        //read file and perform checks
        EpubReader r = new EpubReader();
        Book b = r.readEpub(new FileInputStream(ePubFile));
        List<String> bookTitles = b.getMetadata().getTitles();
        List<Author> bookAuthors = b.getMetadata().getAuthors();
        List<String> bookAuthorsNames = new ArrayList<String>();

        for (Author a : bookAuthors) {
            bookAuthorsNames.add(a.getFirstname() + " " + a.getLastname());
        }

        Assert.assertTrue("All titles are in the EPUB.", bookTitles.containsAll(titles));
        Assert.assertTrue("All EPUB titles are in the given titles.", titles.containsAll(bookTitles));
        System.out.println(bookAuthorsNames);
        Assert.assertTrue("All authors are in the EPUB.", bookAuthorsNames.containsAll(authors));
        Assert.assertTrue("All EPUB authors are in the given authors.", authors.containsAll(bookAuthorsNames));
    }

    @Test
    public void TestCoverImage() throws IOException {
        String title = "Alice in Utopia";
        String author = "<NAME>";
        String coverImage = "cover.jpeg";

        File zippedHTMLFile = new File("src/test/resources/alice.zip");
        File coverFile = new File("src/test/resources/" + coverImage);

        Metadata metadata = new Metadata();
        ArrayList<String> titles = new ArrayList<>();
        ArrayList<String> authors = new ArrayList<>();
        ArrayList<Section> toc = new ArrayList<>();
        titles.add(title);
        authors.add(author);
        metadata.setTitles(titles);
        metadata.setAuthors(authors);<commit_msg>Deactivate old unit tests.<commit_after>package eu.freme.eservices.epublishing.webservice;

import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
import org.junit.Test;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.epub.EpubReader;
import org.junit.Assert;

/**
 * <p>
 * Basic e-publishing test</p>
 *
 * <p>
 * Copyright 2015 MMLab, UGent</p>
 *
 * @author <NAME>
 */
public class EPubTest extends ClientAndLocalServerBase {

    public void TestAlice() throws IOException {
        String title = "Alice in Utopia";
        String author = "<NAME>";
        String author2 = "<NAME>";
        String description = "Dit is een heel mooi boekske.";

        File zippedHTMLFile = new File("src/test/resources/alice.zip");
        System.out.println("Converting " + zippedHTMLFile + " to EPUB format.");

        Metadata metadata = new Metadata();
        ArrayList<String> titles = new ArrayList<>();
        ArrayList<String> authors = new ArrayList<>();
        titles.add(title);
        authors.add(author);
        authors.add(author2);
        metadata.setTitles(titles);
        metadata.setAuthors(authors);
        metadata.setDescription(description);
        Identifier id = new Identifier(null, "urn:ean:1234-7956-1356-1123");
        metadata.setIdentifier(id);

        Gson gson = new Gson();

        // create body part containing the ePub file
        FileDataBodyPart ePubFilePart = new FileDataBodyPart("htmlZip", zippedHTMLFile);

        // the form, to which parameters and binary content can be added
        FormDataMultiPart multiPart = new FormDataMultiPart();
        multiPart.bodyPart(ePubFilePart);			// add ePub
        multiPart.field("metadata", gson.toJson(metadata));
        System.out.println(gson.toJson(metadata));

        // now send it to the service
        Response response = target.path("epublish/html").request().post(Entity.entity(multiPart, multiPart.getMediaType()));
        InputStream in = response.readEntity(InputStream.class);

        // write it to a (temporary) file
        File ePubFile = File.createTempFile("alice", ".epub");
        System.out.println("Writing result to " + ePubFile);
        IOUtils.copy(in, new FileOutputStream(ePubFile));

        //read file and perform checks
        EpubReader r = new EpubReader();
        Book b = r.readEpub(new FileInputStream(ePubFile));
        List<String> bookTitles = b.getMetadata().getTitles();
        List<Author> bookAuthors = b.getMetadata().getAuthors();
        List<String> bookAuthorsNames = new ArrayList<String>();

        for (Author a : bookAuthors) {
            bookAuthorsNames.add(a.getFirstname() + " " + a.getLastname());
        }

        Assert.assertTrue(bookTitles.containsAll(titles));
        Assert.assertTrue(titles.containsAll(bookTitles));
        System.out.println(bookAuthorsNames);
        Assert.assertTrue(bookAuthorsNames.containsAll(authors));
        Assert.assertTrue(authors.containsAll(bookAuthorsNames));
    }

    public void TestSections() throws IOException {
        String title = "Alice in Utopia";
        String author = "<NAME>";

        File zippedHTMLFile = new File("src/test/resources/alice.zip");
        System.out.println("Converting " + zippedHTMLFile + " to EPUB format.");

        Metadata metadata = new Metadata();
        ArrayList<String> titles = new ArrayList<>();
        ArrayList<String> authors = new ArrayList<>();
        ArrayList<Section> toc = new ArrayList<>();
        titles.add(title);
        authors.add(author);
        metadata.setTitles(titles);
        metadata.setAuthors(authors);
        toc.add(new Section("Chapter 1", "01.xhtml"));
        toc.add(new Section("Chapter 2", "02.xhtml"));
        metadata.setTableOfContents(toc);

        Gson gson = new Gson();

        // create body part containing the ePub file
        FileDataBodyPart ePubFilePart = new FileDataBodyPart("htmlZip", zippedHTMLFile);

        // the form, to which parameters and binary content can be added
        FormDataMultiPart multiPart = new FormDataMultiPart();
        multiPart.bodyPart(ePubFilePart);			// add ePub
        multiPart.field("metadata", gson.toJson(metadata));

        // now send it to the service
        Response response = target.path("epublish/html").request().post(Entity.entity(multiPart, multiPart.getMediaType()));
        InputStream in = response.readEntity(InputStream.class);

        // write it to a (temporary) file
        File ePubFile = File.createTempFile("alice", ".epub");
        System.out.println("Writing result to " + ePubFile);
        IOUtils.copy(in, new FileOutputStream(ePubFile));

        //read file and perform checks
        EpubReader r = new EpubReader();
        Book b = r.readEpub(new FileInputStream(ePubFile));
        List<String> bookTitles = b.getMetadata().getTitles();
        List<Author> bookAuthors = b.getMetadata().getAuthors();
        List<String> bookAuthorsNames = new ArrayList<String>();

        for (Author a : bookAuthors) {
            bookAuthorsNames.add(a.getFirstname() + " " + a.getLastname());
        }

        Assert.assertTrue("All titles are in the EPUB.", bookTitles.containsAll(titles));
        Assert.assertTrue("All EPUB titles are in the given titles.", titles.containsAll(bookTitles));
        System.out.println(bookAuthorsNames);
        Assert.assertTrue("All authors are in the EPUB.", bookAuthorsNames.containsAll(authors));
        Assert.assertTrue("All EPUB authors are in the given authors.", authors.containsAll(bookAuthorsNames));
    }

    public void TestCoverImage() throws IOException {
        String title = "Alice in Utopia";
        String author = "<NAME>";
        String coverImage = "cover.jpeg";

        File zippedHTMLFile = new File("src/test/resources/alice.zip");
        File coverFile = new File("src/test/resources/" + coverImage);

        Metadata metadata = new Metadata();
        ArrayList<String> titles = new ArrayList<>();
        ArrayList<String> authors = new ArrayList<>();
        ArrayList<Section> toc = new ArrayList<>();
        titles.add(title);
        authors.add(author);
        metadata.setTitles(titles);
        metadata.setAuthors(authors);
{'fraction_non_alphanumeric': 0.12119407429464751, 'fraction_numerical': 0.004168837936425221, 'mean_word_length': 3.085766423357664, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7392819', 'n_tokens_mistral': 3905, 'n_tokens_neox': 3778, 'n_words': 881}