/**
 * Copyright (c) 2012-2013 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.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ow2.petals.admin.registry.Endpoint;
import org.ow2.petals.admin.registry.Endpoint.EndpointType;
import org.ow2.petals.admin.registry.RegistryView;

/**
 * 
 * @author Christophe DENEUX - Linagora
 * 
 */
// TODO: Data of unit tests of PetalsCli should be defined in the PetalsCli
// project. These tests classes should be transform in a project
// 'petals-admin-inmemory' where registry is set when deploying artifact.
public class RegistryViewMock implements RegistryView {

    public static final String EP1_NAME = "FaultIntegrationServiceSOAP";

    public static final String EP1_ITF1_NAME = "{http://petals.ow2.org/}FaultIntegrationItf";

    public static final String EP1_SVC1_NAME = "{http://petals.ow2.org/}FaultIntegrationService";

    private final List<Endpoint> endpoints = new ArrayList<Endpoint>();

    public RegistryViewMock() {
        final List<String> itfs = new ArrayList<String>();
        itfs.add(EP1_ITF1_NAME);
        this.endpoints.add(new Endpoint(EP1_NAME, EndpointType.INTERNAL, "0", "petals-bc-soap",
                EP1_SVC1_NAME, itfs));
    }

    @Override
    public List<Endpoint> getAllEndpoints() {
        return this.endpoints;
    }

    @Override
    public Map<String, List<Endpoint>> getListOfEndpointsByServiceName() {
        final Map<String, List<Endpoint>> result = new HashMap<String, List<Endpoint>>();
        for (final Endpoint endpoint : this.endpoints) {
            final String svcName = endpoint.getServiceName();
            if (result.containsKey(svcName)) {
                result.get(svcName).add(endpoint);
            } else {
                result.put(svcName, new ArrayList<Endpoint>());
            }
        }
        return result;
    }

    @Override
    public Map<String, List<Endpoint>> getListOfEndpointsByInterfaceName() {
        final Map<String, List<Endpoint>> result = new HashMap<String, List<Endpoint>>();
        for (final Endpoint endpoint : this.endpoints) {
            for (final String itfName : endpoint.getInterfaceNames()) {
                if (result.containsKey(itfName)) {
                    result.get(itfName).add(endpoint);
                } else {
                    result.put(itfName, new ArrayList<Endpoint>());
                }
            }
        }
        return result;
    }

}
