/**
 * Copyright (c) 2020-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.binding.soap.monit;

import java.net.URI;
import java.util.logging.LogRecord;

import org.ow2.petals.binding.soap.SoapConsumeExtFlowStepBeginLogData;
import org.ow2.petals.binding.soap.SoapProvideExtFlowStepBeginLogData;
import org.ow2.petals.commons.log.FlowLogData;

/**
 * Assertion class for unit tests about MONIT trace of BC SOAP
 * 
 * @author Christophe DENEUX - Linagora
 * 
 */
public class Assert extends org.ow2.petals.component.framework.test.Assert {

    public Assert() {
        // Utility class, no constructor
    }

    /**
     * <p>
     * Asserts that a log record is a MONIT trace associated to the start of an external service consumer of the BC
     * SOAP. If it isn't, it throws an {@link AssertionError}.
     * </p>
     * <p>
     * In addition to the checks of {@link #assertMonitConsumerExtBeginLog(String, LogRecord)}, the following checks are
     * applied:
     * </p>
     * <ul>
     * <li>the web-service URL used is the expected one.</li>
     * </ul>
     * 
     * @param prefixMsg
     *            Prefix message to add before the assertion message. No prefix added if {@code null}.
     * @param expectedUrl
     *            The expected web-service URL.
     * @param logRecord
     *            The actual log record to check.
     * @return The flow log data of the log record
     */
    public static FlowLogData assertMonitSoapConsumerExtBegin(final String prefixMsg,
            final URI expectedUrl, final LogRecord logRecord) {

        return Assert.assertMonitSoapConsumerExtBegin(prefixMsg,
                expectedUrl.toASCIIString(), logRecord);
    }

    /**
     * <p>
     * Asserts that a log record is a MONIT trace associated to the start of an external service consumer of the BC
     * SOAP. If it isn't, it throws an {@link AssertionError}.
     * </p>
     * <p>
     * In addition to the checks of {@link #assertMonitConsumerExtBeginLog(String, LogRecord)}, the following checks are
     * applied:
     * </p>
     * <ul>
     * <li>the web-service URL used is the expected one.</li>
     * </ul>
     * 
     * @param prefixMsg
     *            Prefix message to add before the assertion message. No prefix added if {@code null}.
     * @param expectedUrl
     *            The expected web-service URL.
     * @param logRecord
     *            The actual log record to check.
     * @return The flow log data of the log record
     */
    public static FlowLogData assertMonitSoapConsumerExtBegin(final String prefixMsg,
            final String expectedUrl, final LogRecord logRecord) {

        final String reworkedPrefix = prefixMsg == null ? "" : prefixMsg;

        final FlowLogData flowLogData = assertMonitConsumerExtBeginLog(prefixMsg, logRecord);

        assertEquals(reworkedPrefix, expectedUrl,
                flowLogData.get(SoapConsumeExtFlowStepBeginLogData.REQUESTED_URL_KEY));

        return flowLogData;
    }

    /**
     * <p>
     * Asserts that a log record is a MONIT trace associated to the start of an external service provider of the BC
     * SOAP. If it isn't it throws an {@link AssertionError}.
     * </p>
     * <p>
     * In addition to the checks of {@link #assertMonitProviderExtBeginLog(FlowLogData, LogRecord)}, the following
     * checks are applied:
     * </p>
     * <ul>
     * <li>the URL of the external web-service invoked.</li>
     * </ul>
     * 
     * @param previousFlowLogData
     *            The flow attributes of the MONIT trace associated to the step startup of the internal service provider
     *            running on BC side
     * @param logRecord
     *            The actual log record to check
     * @param externalWebServiceUrl
     *            The URL of the external web-service invoked
     * @return The flow log data of the log record
     */
    public static FlowLogData assertMonitSoapProviderExtBeginLog(final FlowLogData previousFlowLogData,
            final LogRecord logRecord, final String externalWebServiceUrl) {

        final FlowLogData flowLogData = Assert.assertMonitProviderExtBeginLog(previousFlowLogData, logRecord);

        assertEquals("Unexpected external web-service URL", externalWebServiceUrl,
                flowLogData.get(SoapProvideExtFlowStepBeginLogData.REQUESTED_URL_KEY));

        return flowLogData;
    }

    /**
     * <p>
     * Asserts that a log record is a MONIT trace associated to the start of an external service consumer of the BC
     * SOAP. If it isn't it throws an {@link AssertionError}.
     * </p>
     * <p>
     * In addition to the checks of {@link Assert#assertMonitConsumerExtBeginLog(LogRecord)}, the following checks are
     * applied:
     * </p>
     * <ul>
     * <li>the URL of the web-service invoked.</li>
     * </ul>
     * 
     * @param logRecord
     *            The actual log record to check
     * @param externalWebServiceUrl
     *            The URL of the external web-service invoked
     * @return The flow log data of the log record
     */
    public static FlowLogData assertMonitSoapConsumerExtBeginLog(final LogRecord logRecord,
            final String externalWebServiceUrl) {

        final FlowLogData flowLogData = Assert.assertMonitConsumerExtBeginLog(logRecord);

        assertEquals("Unexpected web-service URL", externalWebServiceUrl,
                flowLogData.get(SoapProvideExtFlowStepBeginLogData.REQUESTED_URL_KEY));

        return flowLogData;
    }
}
