/**
 * Copyright (c) 2022-2024 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library 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 Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.bc.quartz;

import java.io.File;

import javax.xml.namespace.QName;

import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;
import org.ow2.petals.commons.log.PetalsExecutionContext;
import org.ow2.petals.component.framework.junit.Component;
import org.ow2.petals.component.framework.junit.helpers.SimpleComponent;
import org.ow2.petals.component.framework.junit.impl.ComponentConfiguration;
import org.ow2.petals.component.framework.junit.rule.ComponentUnderTest;
import org.ow2.petals.component.framework.junit.rule.ParameterGenerator;

public abstract class AbstractComponentWithPlaceholdersTest extends AbstractComponentTest {

    protected static final TemporaryFolder TEMP_FOLDER = new TemporaryFolder();

    protected static String COMPONENT_PROPERTIES_FILE;

    private static final ComponentConfiguration CONFIGURATION = new ComponentConfiguration(
            COMPONENT_CONFIGURATION_NAME);

    protected static final Component COMPONENT_UNDER_TEST = new ComponentUnderTest(CONFIGURATION)
            .addLogHandler(IN_MEMORY_LOG_HANDLER.getHandler())
            .setParameter(new QName("http://petals.ow2.org/components/extensions/version-5", "properties-file"),
                    new ParameterGenerator() {

                        @Override
                        public String generate() throws Exception {
                            final File componentPropertiesFile = TEMP_FOLDER.newFile("component-properties.properties");
                            COMPONENT_PROPERTIES_FILE = componentPropertiesFile.getAbsolutePath();
                            return COMPONENT_PROPERTIES_FILE;
                        }

                    })
            .registerExternalServiceProvider(HELLO_ENDPOINT_NAME, HELLO_SERVICE, HELLO_INTERFACE);

    protected static final SimpleComponent COMPONENT = new SimpleComponent(COMPONENT_UNDER_TEST);

    @ClassRule
    public static final TestRule chain = RuleChain.outerRule(TEMP_FOLDER).around(IN_MEMORY_LOG_HANDLER)
            .around(COMPONENT_UNDER_TEST);

    @Before
    public void clearJBIMessages() {
        // we want to clear them inbetween tests
        COMPONENT_UNDER_TEST.clearRequestsFromConsumer();
        COMPONENT_UNDER_TEST.clearResponsesFromProvider();
        // note: incoming messages queue can't be cleared because it is the job of the tested component to well handle
        // any situation
        // JUnit is susceptible to reuse threads apparently
        PetalsExecutionContext.clear();
    }

    /**
     * We undeploy services after each test (because the component is static and lives during the whole suite of tests)
     */
    @After
    public void after() {

        COMPONENT_UNDER_TEST.undeployAllServices();

    }
}
