package biz

import "testing"

func TestCommunicationIntegrationDefinitionsAndValidation(t *testing.T) {
	for _, target := range []struct{ kind, provider string }{
		{IntegrationKindMQ, "emqx"},
		{IntegrationKindMQ, "rabbitmq"},
		{IntegrationKindWebSocket, "melody"},
	} {
		values := DefaultIntegrationConfig(target.kind, target.provider)
		if len(values) == 0 {
			t.Fatalf("default config missing for %s/%s", target.kind, target.provider)
		}
		if err := ValidateIntegrationConfig(target.kind, target.provider, values); err != nil {
			t.Fatalf("default config invalid for %s/%s: %v", target.kind, target.provider, err)
		}
	}
}

func TestCommunicationIntegrationValidationRejectsInvalidValues(t *testing.T) {
	rabbit := DefaultIntegrationConfig(IntegrationKindMQ, "rabbitmq")
	rabbit["port"] = 0
	if err := ValidateIntegrationConfig(IntegrationKindMQ, "rabbitmq", rabbit); err == nil {
		t.Fatal("invalid rabbitmq port was accepted")
	}
	websocket := DefaultIntegrationConfig(IntegrationKindWebSocket, "melody")
	websocket["path"] = "ws"
	if err := ValidateIntegrationConfig(IntegrationKindWebSocket, "melody", websocket); err == nil {
		t.Fatal("invalid websocket path was accepted")
	}
}
