<commit_before>/*
 * SportChef – Sports Competition Management Software
 * Copyright (C) 2016 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.sportchef.business.event.control;

import ch.sportchef.business.event.entity.Event;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.needle4j.annotation.ObjectUnderTest;
import org.needle4j.junit.NeedleBuilders;
import org.needle4j.junit.NeedleRule;

import javax.imageio.ImageIO;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.ws.rs.NotFoundException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;

public class EventImageServiceTest {

    private static final String TEST_IMAGE_NAME = "test-350x200.png";

    private static String realUserHome;
    private static String tempUserHome;
    private static File imageUploadPath;

    @BeforeClass
    public static void setUp() throws IOException {
        realUserHome = System.getProperty("user.home");
        tempUserHome = Files.createTempDirectory("sportchef-").toString();
        System.setProperty("user.home", tempUserHome);

        final String imageUploadFolder = String.format("%s%s.sportchef%simages%sevents", //NON-NLS
                tempUserHome, File.separator, File.separator, File.separator);
        imageUploadPath = new File(imageUploadFolder);
    }

    @AfterClass
    public static void tearDown() {
        System.setProperty("user.home", realUserHome);
    }

    @Rule
    public NeedleRule needleRule = NeedleBuilders.needleMockitoRule().build();

    @ObjectUnderTest(postConstruct = true)
    private EventImageService eventImageService;

    @Inject
    private EventService eventServiceMock;

    private byte[] readTestImage() throws URISyntaxException, IOException {
        final Thread currentThread = Thread.currentThread();
        final ClassLoader classLoader = currentThread.getContextClassLoader();
        final URL url = classLoader.getResource(TEST_IMAGE_NAME);
        final URI uri = url.toURI();

        return Files.readAllBytes(Paths.get(uri));
    }

    private byte[] prepareAndReturnTestImage(@NotNull final Long eventId) throws URISyntaxException, IOException {
        final byte[] bytes = readTestImage();
        if (!imageUploadPath.exists()) {
            imageUploadPath.mkdirs();
        }
        Files.write(Paths.get(imageUploadPath.getPath(), String.valueOf(eventId).concat(".png")), bytes);

        return bytes;
    }

    private BufferedImage readStoredImage(@NotNull final Long eventId) throws IOException {
        final File file = Paths.get(imageUploadPath.getPath(), String.valueOf(eventId).concat(".png")).toFile();
        return file.exists() ? ImageIO.read(file) : null;
    }

    @Test
    public void getImage() throws IOException, URISyntaxException {
        // arrange
        final Long eventId = 1L;
        final byte[] bytes = prepareAndReturnTestImage(eventId);<commit_msg>#264 Fixed "Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes."<commit_after>/*
 * SportChef – Sports Competition Management Software
 * Copyright (C) 2016 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.sportchef.business.event.control;

import ch.sportchef.business.event.entity.Event;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.needle4j.annotation.ObjectUnderTest;
import org.needle4j.junit.NeedleBuilders;
import org.needle4j.junit.NeedleRule;

import javax.imageio.ImageIO;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.ws.rs.NotFoundException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;

public class EventImageServiceTest {

    private static final String TEST_IMAGE_NAME = "test-350x200.png";

    private static String realUserHome;
    private static String tempUserHome;
    private static File imageUploadPath;

    @Rule
    public NeedleRule needleRule = NeedleBuilders.needleMockitoRule().build();

    @ObjectUnderTest(postConstruct = true)
    private EventImageService eventImageService;

    @Inject
    private EventService eventServiceMock;

    @BeforeClass
    public static void setUp() throws IOException {
        realUserHome = System.getProperty("user.home");
        tempUserHome = Files.createTempDirectory("sportchef-").toString();
        System.setProperty("user.home", tempUserHome);

        final String imageUploadFolder = String.format("%s%s.sportchef%simages%sevents", //NON-NLS
                tempUserHome, File.separator, File.separator, File.separator);
        imageUploadPath = new File(imageUploadFolder);
    }

    @AfterClass
    public static void tearDown() {
        System.setProperty("user.home", realUserHome);
    }

    private byte[] readTestImage() throws URISyntaxException, IOException {
        final Thread currentThread = Thread.currentThread();
        final ClassLoader classLoader = currentThread.getContextClassLoader();
        final URL url = classLoader.getResource(TEST_IMAGE_NAME);
        final URI uri = url.toURI();

        return Files.readAllBytes(Paths.get(uri));
    }

    private byte[] prepareAndReturnTestImage(@NotNull final Long eventId) throws URISyntaxException, IOException {
        final byte[] bytes = readTestImage();
        if (!imageUploadPath.exists()) {
            imageUploadPath.mkdirs();
        }
        Files.write(Paths.get(imageUploadPath.getPath(), String.valueOf(eventId).concat(".png")), bytes);

        return bytes;
    }

    private BufferedImage readStoredImage(@NotNull final Long eventId) throws IOException {
        final File file = Paths.get(imageUploadPath.getPath(), String.valueOf(eventId).concat(".png")).toFile();
        return file.exists() ? ImageIO.read(file) : null;
    }

    @Test
    public void getImage() throws IOException, URISyntaxException {
        // arrange
        final Long eventId = 1L;
        final byte[] bytes = prepareAndReturnTestImage(eventId);
{'fraction_non_alphanumeric': 0.09260832625318606, 'fraction_numerical': 0.004005340453938585, 'mean_word_length': 3.9400479616306954, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3230973', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2234, 'n_words': 653}