<commit_before>/*
 * Copyright 2017 Hewlett-Packard Enterprise Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.frontend.reports.powerpoint;

import com.hp.autonomy.frontend.reports.powerpoint.dto.DategraphData;
import com.hp.autonomy.frontend.reports.powerpoint.dto.ListData;
import com.hp.autonomy.frontend.reports.powerpoint.dto.ReportData;
import com.hp.autonomy.frontend.reports.powerpoint.dto.SunburstData;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import static java.io.File.createTempFile;

@RunWith(MockitoJUnitRunner.class)
public class PowerPointServiceImplTest {

    private static final String sampleJPEGWithoutHeader = <KEY>";
    private static final String sampleJPEGImage = "data:image/jpeg;base64," + sampleJPEGWithoutHeader;
    private static final String samplePNGImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA<KEY>;

    private PowerPointServiceImpl pptxService;

    @Before
    public void before() {
        pptxService = new PowerPointServiceImpl();
    }

    private void testWrite(final XMLSlideShow pptx) throws IOException {
        final File temp = createTempFile("temp", ".pptx");
        temp.deleteOnExit();
        pptx.write(new FileOutputStream(temp));
    }

    @Test
    public void testDateGraphTwoAxes() throws SlideShowTemplate.LoadException, IOException {
        final DategraphData data = createTwoAxisDategraphData();

        final XMLSlideShow pptx = pptxService.graph(data);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }

    @Test
    public void testDateGraphSingleAxis() throws SlideShowTemplate.LoadException, IOException {
        final DategraphData data = createSingleAxisDategraphData();

        final XMLSlideShow pptx = pptxService.graph(data);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }

    @Test
    public void testDateGraphMultipleSeries() throws SlideShowTemplate.LoadException, IOException {
        final DategraphData data = createTwoAxisMultipleSeriesDategraphData();

        final XMLSlideShow pptx = pptxService.graph(data);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }

    @Test
    public void testSunburst() throws SlideShowTemplate.LoadException, IOException {
        final SunburstData sunburst = createSunburstData();

        final XMLSlideShow pptx = pptxService.sunburst(sunburst);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }


    @Test
    public void testListSingle() throws SlideShowTemplate.LoadException, IOException {
        final ListData listData = new ListData(new ListData.Document[]{
            new ListData.Document("title1", "5 months ago", "reference", "summary", sampleJPEGWithoutHeader)
        });

        final XMLSlideShow pptx = pptxService.list("Showing 1 to 1 of 1 results", "Sort by Relevance", listData);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }

    @Test
    public void testListPagination() throws SlideShowTemplate.LoadException, IOException {
        final ListData listData = new ListData(new ListData.Document[]{
            new ListData.Document("title1", "5 months ago", "reference", "summary", null),
            new ListData.Document("title2", "5 months ago", null, "summary", null),
            new ListData.Document("title3", null, "reference", "summary", null),
            new ListData.Document("title4", "5 months ago", "reference", null, null),
            new ListData.Document("title5", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title6", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title7", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title8", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title9", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title10", "5 months ago", "reference", "summary", samplePNGImage)
        });

        final XMLSlideShow pptx = pptxService.list("Showing 1 to 10 of 10 results", "Sort by Relevance", listData);
        testWrite(pptx);

        Assert.assertTrue(pptx.getSlides().size() > 1);
    }

    @Test
    public void testReport() throws SlideShowTemplate.LoadException, IOException {
        final DategraphData dategraph = createTwoAxisDategraphData();

        final SunburstData sunburst = createSunburstData();

        final SunburstData bottomRightSunburst = new SunburstData(
                new String[] { "Cyan", "Magenta", "Yellow", "Black"},
                new double[] { 0.994, 0, 0.231, 0.337 },
                "CMYK Colours"
        );

        final String titleFont = "Times New Roman";<commit_msg>Add system flag to optionally keep the powerpoint files instead of deleting them after the test. Add test for the table view.<commit_after>/*
 * Copyright 2017 Hewlett-Packard Enterprise Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.frontend.reports.powerpoint;

import com.hp.autonomy.frontend.reports.powerpoint.dto.DategraphData;
import com.hp.autonomy.frontend.reports.powerpoint.dto.ListData;
import com.hp.autonomy.frontend.reports.powerpoint.dto.ReportData;
import com.hp.autonomy.frontend.reports.powerpoint.dto.SunburstData;
import com.hp.autonomy.frontend.reports.powerpoint.dto.TableData;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import static java.io.File.createTempFile;

@RunWith(MockitoJUnitRunner.class)
public class PowerPointServiceImplTest {

    private static final String sampleJPEGWithoutHeader = "/9j/4AAQSkZ<KEY>BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAAeADIDASIAAhEBAxEB/8QAGQABAQEBAQEAAAAAAAAAAAAAAAcGBQQI/8QAJRAAAQQCAgICAgMAAAAAAAAAAQIDBAUABgcREiEIEyMxMkFR/8QAGAEBAQEBAQAAAAAAAAAAAAAAAAUDBgT/xAAjEQEAAQQBBAIDAAAAAAAAAAABEQACAwQhBRIxURNBBhax/9oADAMBAAIRAxEAPwD7NxjMtypuUrjzjjZN4hVC7N+jrX5qIqVBPmUJJ7USR0gfyUR78QroE9A9Zra+Tbz2a+Im69LT65WDl4Oa4KtTjIPK+X/HmswqJneIdjW2s+qiWdgwBHAgof8ASVFJf8nArorCWvtWEFJUB3msY5/06ZyY/wAWQa23k2UWUiHIkNIYLTTimg6CWy6JH1+JH5Q0W+/XllnN+LdYwDdfr3doXXT9dtqCj4Tkj2IkiUhqmYybcJcq2/KsTZ5VrqUqkFHsEyoYDpbIcQyso6JS4r8qSk+foJBUAkqAJyk5L39HN0zZu1dgC+3zCP1PkkpTGMZ46Uzl7VrlbuGs22pXAcMC6gv18r6leK/qdbKF+J/o9KPR/wBzqYzTFkvw3mTGxcMj6Tw0qTMfHHW4bsSTA3ndYchqBFrJ0iJZoju2kaMsqYRIW20CCgEoCmvrV4eiT7z2Xvx/1DZN8h79c3N9KfgWTFtGguyGnI7MlkDwLa1Nl9tHYCi0h0Nk/tPvKbjK/wCx9V7/AJPme6EniYYk8egD0AEAFJrJ6JxvUceythfpbO0eY2O1fuXosp1C2Y0h5RW79PSApKVKV2QpSv0Ous1mMZL2dnLuZXNnu7rmJfcEfwpTGMZhSv/Z";
    private static final String sampleJPEGImage = "data:image/jpeg;base64," + sampleJPEGWithoutHeader;
    private static final String samplePNGImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAFCAIAAAAVLyF7AAAAPUlEQVQI14WNSQoAMAgDo/b/Lxamh9JiF+gcZJJDlCbA1z0zawXcvq5HhApmVr1GSa6d55NFO/IYA47VQQfmMSztMBTTBAAAAABJRU5ErkJggg==";

    private PowerPointServiceImpl pptxService;

    @Before
    public void before() {
        pptxService = new PowerPointServiceImpl();
    }

    private void testWrite(final XMLSlideShow pptx) throws IOException {
        final File temp = createTempFile("temp", ".pptx");

        if (!Boolean.valueOf(System.getProperty("keep.powerpoint.output", "false"))) {
            temp.deleteOnExit();
        }

        pptx.write(new FileOutputStream(temp));
    }

    @Test
    public void testDateGraphTwoAxes() throws SlideShowTemplate.LoadException, IOException {
        final DategraphData data = createTwoAxisDategraphData();

        final XMLSlideShow pptx = pptxService.graph(data);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }

    @Test
    public void testDateGraphSingleAxis() throws SlideShowTemplate.LoadException, IOException {
        final DategraphData data = createSingleAxisDategraphData();

        final XMLSlideShow pptx = pptxService.graph(data);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }

    @Test
    public void testDateGraphMultipleSeries() throws SlideShowTemplate.LoadException, IOException {
        final DategraphData data = createTwoAxisMultipleSeriesDategraphData();

        final XMLSlideShow pptx = pptxService.graph(data);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }

    @Test
    public void testSunburst() throws SlideShowTemplate.LoadException, IOException {
        final SunburstData sunburst = createSunburstData();

        final XMLSlideShow pptx = pptxService.sunburst(sunburst);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }


    @Test
    public void testListSingle() throws SlideShowTemplate.LoadException, IOException {
        final ListData listData = new ListData(new ListData.Document[]{
            new ListData.Document("title1", "5 months ago", "reference", "summary", sampleJPEGWithoutHeader)
        });

        final XMLSlideShow pptx = pptxService.list("Showing 1 to 1 of 1 results", "Sort by Relevance", listData);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }

    @Test
    public void testListPagination() throws SlideShowTemplate.LoadException, IOException {
        final ListData listData = new ListData(new ListData.Document[]{
            new ListData.Document("title1", "5 months ago", "reference", "summary", null),
            new ListData.Document("title2", "5 months ago", null, "summary", null),
            new ListData.Document("title3", null, "reference", "summary", null),
            new ListData.Document("title4", "5 months ago", "reference", null, null),
            new ListData.Document("title5", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title6", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title7", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title8", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title9", "5 months ago", "reference", "summary", sampleJPEGImage),
            new ListData.Document("title10", "5 months ago", "reference", "summary", samplePNGImage)
        });

        final XMLSlideShow pptx = pptxService.list("Showing 1 to 10 of 10 results", "Sort by Relevance", listData);
        testWrite(pptx);

        Assert.assertTrue(pptx.getSlides().size() > 1);
    }

    @Test
    public void testTable() throws SlideShowTemplate.LoadException, IOException {
        final TableData tableData = new TableData(new String[]{
            "Animal", "Count",
            "Cat", "1",
            "Dog", "1",
            "Mouse", "1",
            "Fish", "1"
        }, 4, 2);

        final XMLSlideShow pptx = pptxService.table("Animals", tableData);
        testWrite(pptx);

        Assert.assertEquals(pptx.getSlides().size(), 1);
    }

    @Test
    public void testReport() throws SlideShowTemplate.LoadException, IOException {
        final DategraphData dategraph = createTwoAxisDategraphData();

        final SunburstData sunburst = createSunburstData();

        final SunburstData bottomRightSunburst = new SunburstData(
                new String[] { "Cyan", "Magenta", "Yellow", "Black"},
                new double[] { 0.994, 0, 0.231, 0.337 },
                "CMYK Colours"
        );

        final String titleFont = "Times New Roman";
{'fraction_non_alphanumeric': 0.10487864274697621, 'fraction_numerical': 0.019887977920285738, 'mean_word_length': 3.858044164037855, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1120816', 'n_tokens_mistral': 4176, 'n_tokens_neox': 3917, 'n_words': 790}