/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.chain2.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.stream.Stream;

import org.apache.commons.chain2.Catalog;
import org.apache.commons.chain2.CatalogFactory;
import org.apache.commons.chain2.Command;
import org.apache.commons.chain2.Context;
import org.apache.commons.chain2.testutils.NonDelegatingCommand;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * <p>Test case for the <code>CatalogFactoryBase</code> class.</p>
 *
 * @version $Revision$ $Date$
 */
public class CatalogFactoryBaseTestCase {


    // ---------------------------------------------------- Instance Variables


    /**
     * <p>The {@link CatalogFactoryBase} instance under test.</p>
     */
    private CatalogFactory<String, Object, Context<String, Object>> factory = null;


    // -------------------------------------------------- Overall Test Methods


    /**
     * <p>Set up instance variables required by this test case.</p>
     */
    @Before
    public void setUp() {
        CatalogFactoryBase.clear();
        this.factory = CatalogFactoryBase.getInstance();
    }

    /**
     * <p>Tear down instance variables required by this test case.</p>
     */
    @After
    public void tearDown() {
        this.factory = null;
        CatalogFactoryBase.clear();
    }


    // ------------------------------------------------ Individual Test Methods


    /**
     * <p>Test a pristine instance of {@link CatalogFactoryBase}.</p>
     */
    @Test
    public void testPristine() {

        assertNotNull(this.factory);
        assertNull(this.factory.getCatalog());
        assertNull(this.factory.getCatalog("foo"));
        assertEquals(0, getCatalogCount());

    }


    /**
     * <p>Test the default {@link Catalog} instance.</p>
     */
    @Test
    public void testDefaultCatalog() {

        Catalog<String, Object, Context<String, Object>> catalog = new CatalogBase<>();
        this.factory.setCatalog(catalog);
        assertSame(catalog, this.factory.getCatalog());
        assertEquals(0, getCatalogCount());

    }


    /**
     * <p>Test adding a specifically named {@link Catalog} instance.</p>
     */
    @Test
    public void testSpecificCatalog() {

        Catalog<String, Object, Context<String, Object>> catalog = new CatalogBase<>();
        this.factory.setCatalog(catalog);
        catalog = new CatalogBase<>();
        this.factory.addCatalog("foo", catalog);
        assertSame(catalog, this.factory.getCatalog("foo"));
        assertEquals(1, getCatalogCount());
        this.factory.addCatalog("foo", new CatalogBase<>());
        assertEquals(1, getCatalogCount());
        assertTrue(!(catalog == this.factory.getCatalog("foo")));
        CatalogFactoryBase.clear();
        this.factory = CatalogFactoryBase.getInstance();
        assertEquals(0, getCatalogCount());

    }


    /**
     * <p>Test <code>getCatalog()</code> method.</p>
     */
    @Test
    public void testCatalogIdentifier() {

        Catalog<String, Object, Context<String, Object>> defaultCatalog = new CatalogBase<>();
        Command<String, Object, Context<String, Object>> defaultFoo = new NonDelegatingCommand();
        defaultCatalog.addCommand("foo", defaultFoo);
        Command<String, Object, Context<String, Object>> fallback = new NonDelegatingCommand();
        defaultCatalog.addCommand("noSuchCatalog:fallback", fallback);

        this.factory.setCatalog(defaultCatalog);

        Catalog<String, Object, Context<String, Object>> specificCatalog = new CatalogBase<>();
        Command<String, Object, Context<String, Object>> specificFoo = new NonDelegatingCommand();
        specificCatalog.addCommand("foo", specificFoo);
        this.factory.addCatalog("specific", specificCatalog);

        Command<String, Object, Context<String, Object>> command = this.factory.getCommand("foo");
        assertSame(defaultFoo, command);

        command = this.factory.getCommand("specific:foo");
        assertSame(specificFoo, command);

        command = this.factory.getCommand("void");
        assertNull(command);

        command = this.factory.getCommand("foo:void");
        assertNull(command);

        command = this.factory.getCommand("specific:void");
        assertNull(command);

        command = this.factory.getCommand("noSuchCatalog:fallback");
        assertNull(command);

        try {
            command = this.factory.getCommand("multiple:delimiters:reserved");
            assertNull(command);
            fail("A command ID with more than one delimiter "
                    + "should throw an IllegalArgumentException");
        } catch (final IllegalArgumentException ex) {
            // expected behavior
            assertEquals(IllegalArgumentException.class, ex.getClass());
        }

    }


    // ------------------------------------------------------- Support Methods


    /**
     * <p>Return the number of {@link Catalog}s defined in our
     * {@link CatalogFactoryBase}.</p>
     * @return long
     */
    private long getCatalogCount() {

        Stream<String> names = this.factory.getNames();
        assertNotNull(names);

        return names.count();
    }
}
