func Test_LoadDatastore(t *testing.T) {
	testConfig, _ = config.LoadConfig(testConfigFile)
	testConfig.Datastore.TypeConfig["mapping_file"] = testEssMapFile

	ds, err := LoadDatastore(&testConfig.Datastore, simplelog.NewStdLogger())
	if err != nil {
		t.Fatalf("%s", err)
	}
	t.Logf("%v", ds)
}
func NewConfigService(cfg *config.Config, logger *simplelog.Logger) (*ConfigService, error) {
	cfgSvc := ConfigService{
		ConfigFile: cfg.Http.ClientConf,
		epConfig:   cfg.Http.Endpoints,
	}

	if logger != nil {
		cfgSvc.logger = logger
	} else {
		cfgSvc.logger = simplelog.NewStdLogger()
	}

	_, err := ioutil.ReadFile(cfgSvc.ConfigFile)
	if err != nil {
		return &cfgSvc, err
	}

	cfgSvc.logger.Info.Printf("UI config: %s\n", cfgSvc.ConfigFile)
	cfgSvc.RegisterHandler()

	return &cfgSvc, nil
}
func NewDashboardHTTPService(cfg *config.Config, logger *simplelog.Logger) (*DashboardHTTPService, error) {
	var (
		d = DashboardHTTPService{
			logger:        logger,
			endpointParts: make([]string, 0),
		}
		err error
	)

	if d.logger == nil {
		d.logger = simplelog.NewStdLogger()
	}

	d.logger.Trace.Printf("Loading datastore with config: %v...\n", cfg.Datastore)
	if d.datastore, err = datastores.LoadDatastore(&cfg.Datastore, d.logger); err != nil {
		return &d, err
	}
	d.logger.Debug.Printf("Datastore loaded: %s", cfg.Datastore.Type)

	d.setEndpointParts(cfg.Http.Endpoints.Dashboards)
	d.registerEndpoints(cfg.Http)

	return &d, nil
}
package datastores

import (
	"encoding/json"
	"github.com/euforia/simplelog"
	"github.com/metrilyx/metrilyx-dashboarder/metrilyx/config"
	schemas "github.com/metrilyx/metrilyx-dashboarder/metrilyx/dashboards/schemas/v3"
	"io/ioutil"
	"path/filepath"
	"testing"
)

var (
	testLogger          = simplelog.NewStdLogger()
	testDashId          = "Metrilyx"
	testDashJsonFile, _ = filepath.Abs("../../../testdata/metrilyx.json")
	testConfigFile, _   = filepath.Abs("../../../etc/metrilyx/metrilyx.json.sample")
	testEssMapFile, _   = filepath.Abs("../../../etc/metrilyx/ess-dashboard-mapping.json")
	testConfig          *config.Config
	testEssDs           *ElasticsearchDatastore
	testDash            = schemas.NewDashboard(true)
)

func Test_ElasticsearchDatastore(t *testing.T) {
	var (
		err error
	)

	testConfig, err = config.LoadConfig(testConfigFile)
	if err != nil {
		t.Fatalf("FAILED: %s", err)
func Test_LoadDatastore_NOT_SUPPORTED(t *testing.T) {
	testConfig.Datastore.Type = "hoopla"
	if _, err := LoadDatastore(&testConfig.Datastore, simplelog.NewStdLogger()); err == nil {
		t.Fatalf("%s", err)
	}
}