<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.find.idol.customization;

import com.hp.autonomy.frontend.find.core.test.AbstractFindIT;
import com.hp.autonomy.frontend.find.core.web.FindController;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.RequestBuilder;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@DirtiesContext
public class CustomizationReloadControllerIT extends AbstractFindIT {
    private static final String UUID = "b1c71fad-a52d-47bf-a121-f71500bd7ddb";
    private static final String DASHBOARD_CONFIG = TEST_DIR + "/customization/dashboards.json";
    private static final String DASHBOARD_CONFIG_BACKUP = TEST_DIR + "/customization/dashboards.json.bak";
    private static final String REPLACEMENT_CONFIG = "target/test-classes/DashboardControllerIT-Config-1.json";

    @Override
    @Before
    public void setUp() {
        super.setUp();
        // Back up current config file
        copyFileReplaceExisting(DASHBOARD_CONFIG, DASHBOARD_CONFIG_BACKUP);
    }

    @After
    public void tearDown() {
        // Restore original config file, delete backup
        moveFileReplaceExisting(DASHBOARD_CONFIG_BACKUP, DASHBOARD_CONFIG);
    }

    @Test
    public void testReloadConfig() throws Exception {
        final String replacementConfigContents = new String(Files.readAllBytes(Paths.get(REPLACEMENT_CONFIG)), "UTF-8");
        assertTrue("Replacement config contains UUID", replacementConfigContents.contains(UUID));

        currentConfigContainsUUID(false);

        // Replace current config file
        copyFileReplaceExisting(REPLACEMENT_CONFIG, DASHBOARD_CONFIG);

        triggerConfigReload();

        currentConfigContainsUUID(true);
    }

    private void copyFileReplaceExisting(final String from, final String to) {
        final Path fromPath = Paths.get(from);<commit_msg>(IDOL-3372) The Spring update seems to make this test fail on fenkins, but not on Travis or locally. Modify it so it'll only backup the dashboard config if it exists; given that we'll delete-and-recreate the config directories for the test anyway.<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.find.idol.customization;

import com.hp.autonomy.frontend.find.core.test.AbstractFindIT;
import com.hp.autonomy.frontend.find.core.web.FindController;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.RequestBuilder;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@DirtiesContext
public class CustomizationReloadControllerIT extends AbstractFindIT {
    private static final String UUID = "b1c71fad-a52d-47bf-a121-f71500bd7ddb";
    private static final String DASHBOARD_CONFIG = TEST_DIR + "/customization/dashboards.json";
    private static final String DASHBOARD_CONFIG_BACKUP = TEST_DIR + "/customization/dashboards.json.bak";
    private static final String REPLACEMENT_CONFIG = "target/test-classes/DashboardControllerIT-Config-1.json";

    @Override
    @Before
    public void setUp() {
        super.setUp();
        // Back up current config file
        if (Files.exists(Paths.get(DASHBOARD_CONFIG))) {
            copyFileReplaceExisting(DASHBOARD_CONFIG, DASHBOARD_CONFIG_BACKUP);
        }
    }

    @After
    public void tearDown() {
        // Restore original config file, delete backup
        if (Files.exists(Paths.get(DASHBOARD_CONFIG_BACKUP))) {
            moveFileReplaceExisting(DASHBOARD_CONFIG_BACKUP, DASHBOARD_CONFIG);
        }
    }

    @Test
    public void testReloadConfig() throws Exception {
        final String replacementConfigContents = new String(Files.readAllBytes(Paths.get(REPLACEMENT_CONFIG)), "UTF-8");
        assertTrue("Replacement config contains UUID", replacementConfigContents.contains(UUID));

        currentConfigContainsUUID(false);

        // Replace current config file
        copyFileReplaceExisting(REPLACEMENT_CONFIG, DASHBOARD_CONFIG);

        triggerConfigReload();

        currentConfigContainsUUID(true);
    }

    private void copyFileReplaceExisting(final String from, final String to) {
        final Path fromPath = Paths.get(from);
{'fraction_non_alphanumeric': 0.08677049460893377, 'fraction_numerical': 0.008214958069484853, 'mean_word_length': 4.461682242990654, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2411076', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1636, 'n_words': 377}