/**
 * Copyright (c) 2016-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.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

import javax.xml.namespace.QName;

import org.junit.After;
import org.junit.Before;
import org.ow2.petals.component.framework.junit.impl.ConsumesServiceConfiguration;
import org.ow2.petals.junit.rules.log.handler.InMemoryLogHandler;

public abstract class AbstractComponentTest extends AbstractTest {

    protected static final String FAULT = "<c/>";

    protected static final String OUT = "<b/>";

    protected static final String IN = "<a/>";

    protected static final Exception ERROR = new Exception("exchange arriving too late");
    static {
        // we don't really care about the stacktrace
        ERROR.setStackTrace(new StackTraceElement[0]);
    }

    protected static final String SU_CONSUMER_NAME = "suc";

    protected static final String HELLO_NS = "http://petals.ow2.org";

    protected static final QName HELLO_INTERFACE = new QName(HELLO_NS, "HelloInterface");

    protected static final QName HELLO_SERVICE = new QName(HELLO_NS, "HelloService");

    protected static final QName SAY_HELLO_OPERATION = new QName(HELLO_NS, "sayHello");

    protected static final QName PRINT_HELLO_OPERATION = new QName(HELLO_NS, "printHello");

    protected static final String HELLO_ENDPOINT_NAME = "helloEndpoint";

    protected static final long HELLO_TIMEOUT = 5000;

    protected static final String VALID_SU = "valid-su";

    protected static final String COMPONENT_CONFIGURATION_NAME = "Quartz";

    protected static final InMemoryLogHandler IN_MEMORY_LOG_HANDLER = new InMemoryLogHandler();

    /**
     * All log traces must be cleared before starting a unit test (because the log handler is static and lives during
     * the whole suite of tests)
     */
    @Before
    public void clearLogTraces() {
        IN_MEMORY_LOG_HANDLER.clear();
    }

    @After
    public void after() {

        // asserts are ALWAYS a bug!
        final Formatter formatter = new SimpleFormatter();
        for (final LogRecord r : IN_MEMORY_LOG_HANDLER.getAllRecords()) {
            assertFalse("Got a log with an assertion: " + formatter.format(r),
                    r.getThrown() instanceof AssertionError || r.getMessage().contains("AssertionError"));
        }
    }

    protected static ConsumesServiceConfiguration createHelloConsumes(final boolean specifyService,
            final boolean specifyEndpoint, final String cronExpression) {
        return createHelloConsumes(specifyService, specifyEndpoint, true, cronExpression);
    }

    protected static ConsumesServiceConfiguration createHelloConsumes(final boolean specifyService,
            final boolean specifyEndpoint, final boolean specifyOperation, final String cronExpression) {

        // can't have endpoint specified without service
        assert !specifyEndpoint || specifyService;

        final ConsumesServiceConfiguration serviceConfiguration = new ConsumesServiceConfiguration(HELLO_INTERFACE,
                specifyService ? HELLO_SERVICE : null, specifyEndpoint ? HELLO_ENDPOINT_NAME : null);
        if (specifyOperation) {
            serviceConfiguration.setOperation(SAY_HELLO_OPERATION);
        }
        serviceConfiguration.setTimeout(HELLO_TIMEOUT);
        if (cronExpression != null) {
            serviceConfiguration.setParameter(
                    new QName("http://petals.ow2.org/components/quartz/version-1", "cron-expression"), cronExpression);
        }
        serviceConfiguration.setParameter(new QName("http://petals.ow2.org/components/quartz/version-1", "content"),
                "<![CDATA[<text>hello</text>]]>");

        return serviceConfiguration;
    }
}
