package com.atlassian.bitbucket.test;

import java.util.Locale;

/**
 * Describes and detects the topology of Atlassian products under test for the current func test run (by inspecting the
 * "test.product.topology" system property set (or absent) when func test suites run. At present only topologies
 * involving the Bitbucket product in different roles are tested by our func test suite but could be extended in the
 * future to test multiple products e.g. Bitbucket + External Crowd, Bitbucket + Fisheye, Bitbucket + JIRA,
 * Bitbucket + Fisheye + JIRA etc.
 */
public enum ProductTopology {
    /**
     * A single Bitbucket instance on ports 7990/7999
     */
    BITBUCKET_STANDALONE("bitbucket.standalone") {
        @Override
        public <T> T accept(ProductTopologyVisitor<T> visitor) {
            return visitor.visitStashStandalone();
        }
    },

    /**
     * A pair of Bitbucket instance on ports 7991/7998 & 7992/7997 with a round-robin proxy listening on 7990/7999
     */
    BITBUCKET_CLUSTER_PAIR("bitbucket.cluster.pair") {
        @Override
        public <T> T accept(ProductTopologyVisitor<T> visitor) {
            return visitor.visitStashClusterPair();
        }
    };

    private final String propertyName;

    ProductTopology(String propertyName) {
        this.propertyName = propertyName;
    }

    public abstract <T> T accept(ProductTopologyVisitor<T> visitor);

    public static ProductTopology get() {
        String value = System.getProperty("test.product.topology", BITBUCKET_STANDALONE.propertyName)
                .toLowerCase(Locale.US).trim();
        for (ProductTopology topology : values()) {
            if (topology.propertyName.equals(value)) {
                return topology;
            }
        }
        return BITBUCKET_STANDALONE;
    }
}
