diff --git a/ffconfig/main_test.go b/ffconfig/main_test.go new file mode 100644 index 000000000..f7b136561 --- /dev/null +++ b/ffconfig/main_test.go @@ -0,0 +1,106 @@ +// Copyright © 2022 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed 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 main + +import ( + "fmt" + "os" + "os/exec" + "testing" + + "github.com/stretchr/testify/assert" +) + +var configPath string = "../test/data/config/firefly.core.yaml" + +func TestMainFail(t *testing.T) { + // Run the crashing code when FLAG is set + if os.Getenv("FLAG") == "1" { + main() + return + } + // Run the test in a subprocess + cmd := exec.Command(os.Args[0], "-test.run=TestMainFail") + cmd.Env = append(os.Environ(), "FLAG=1") + err := cmd.Run() + + // Cast the error as *exec.ExitError and compare the result + e, ok := err.(*exec.ExitError) + expectedErrorString := "exit status 1" + assert.Equal(t, true, ok) + assert.Equal(t, expectedErrorString, e.Error()) +} + +func TestConfigMigrateRootCmdErrorNoArgs(t *testing.T) { + rootCmd.SetArgs([]string{}) + defer rootCmd.SetArgs([]string{}) + err := rootCmd.Execute() + assert.Error(t, err) + assert.Regexp(t, "a command is required", err) +} + +func TestConfigMigrateCmdMissingConfig(t *testing.T) { + rootCmd.SetArgs([]string{"migrate"}) + defer rootCmd.SetArgs([]string{}) + err := rootCmd.Execute() + assert.Error(t, err) + assert.Regexp(t, "no such file or directory", err) +} + +func TestConfigMigrateCmd(t *testing.T) { + rootCmd.SetArgs([]string{"migrate", "-f", configPath}) + defer rootCmd.SetArgs([]string{}) + err := rootCmd.Execute() + assert.NoError(t, err) +} + +func TestMain(t *testing.T) { + // Run the exiting code when FLAG is set + if os.Getenv("FLAG") == "0" { + rootCmd.SetArgs([]string{"migrate", "-f", configPath}) + main() + return + } + + // Run the test in a subprocess + cmd := exec.Command(os.Args[0], "-test.run=TestMain") + cmd.Env = append(os.Environ(), "FLAG=0") + err := cmd.Run() + + // Cast the error as *exec.ExitError and compare the result + _, ok := err.(*exec.ExitError) + assert.Equal(t, false, ok) +} + +func TestConfigMigrateCmdWriteOutput(t *testing.T) { + tmpDir, err := os.MkdirTemp(os.TempDir(), "out") + assert.NoError(t, err) + defer os.RemoveAll(tmpDir) + + rootCmd.SetArgs([]string{"migrate", "-f", configPath, "-o", fmt.Sprintf(tmpDir, "out.config")}) + defer rootCmd.SetArgs([]string{}) + err = rootCmd.Execute() + assert.NoError(t, err) +} + +func TestConfigMigrateCmdBadVersion(t *testing.T) { + rootCmd.SetArgs([]string{"migrate", "-f", configPath, "--from", "badversion"}) + defer rootCmd.SetArgs([]string{}) + err := rootCmd.Execute() + assert.Error(t, err) + assert.Regexp(t, "bad 'from' version", err) +} diff --git a/internal/blockchain/bifactory/factory_test.go b/internal/blockchain/bifactory/factory_test.go new file mode 100644 index 000000000..a2b493c79 --- /dev/null +++ b/internal/blockchain/bifactory/factory_test.go @@ -0,0 +1,60 @@ +// Copyright © 2023 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed 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 bifactory + +import ( + "context" + "testing" + + "github.com/hyperledger/firefly-common/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestGetPluginUnknown(t *testing.T) { + ctx := context.Background() + _, err := GetPlugin(ctx, "foo") + assert.Error(t, err) + assert.Regexp(t, "FF10110", err) +} + +func TestGetPluginEthereum(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "ethereum") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +func TestGetPluginFabric(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "fabric") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +func TestGetPluginTezos(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "tezos") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +var root = config.RootSection("di") + +func TestInitConfig(t *testing.T) { + conf := root.SubArray("plugins") + InitConfig(conf) +} diff --git a/internal/coremsgs/es/es_struct_descriptions.go b/internal/coremsgs/es/es_struct_descriptions.go deleted file mode 100644 index cc585b666..000000000 --- a/internal/coremsgs/es/es_struct_descriptions.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright © 2022 Kaleido, Inc. -// -// SPDX-License-Identifier: Apache-2.0 -// -// Licensed 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 es - -import ( - "github.com/hyperledger/firefly-common/pkg/i18n" - "golang.org/x/text/language" -) - -//revive:disable - -/* -This file contains the field level descriptions that are used in -OpenAPI Spec generation. Each struct field that wants to use one of these -needs to have an ffstruct tag on it, indicating the name of the struct. -That will be combined with the JSON field name (note, it is not the GO -field name, but the JSON serialized name), separated by a "." This is the -key used to lookup the translation below. If it is not found, the description -is left blank in the OpenAPI spec - -Example: -// message.go -type Message struct { - Header MessageHeader `ffstruct:"Message" json:"header"` - -// en_translations_descriptions.go -MessageHeader = ffm("Message.header", "The message header") - -*/ - -var ffm = func(key, translation string) i18n.MessageKey { - return i18n.FFM(language.Spanish, key, translation) -} - -var ( - // MessageHeader field descriptions - MessageHeaderID = ffm("MessageHeader.id", "El UUID del mensaje. Único para cada mensaje") -) diff --git a/internal/database/difactory/factory_test.go b/internal/database/difactory/factory_test.go new file mode 100644 index 000000000..f334df087 --- /dev/null +++ b/internal/database/difactory/factory_test.go @@ -0,0 +1,53 @@ +// Copyright © 2023 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed 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 difactory + +import ( + "context" + "testing" + + "github.com/hyperledger/firefly-common/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestGetPluginUnknown(t *testing.T) { + ctx := context.Background() + _, err := GetPlugin(ctx, "foo") + assert.Error(t, err) + assert.Regexp(t, "FF10122", err) +} + +func TestGetPluginPostgres(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "postgres") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +func TestGetPluginSQLite(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "sqlite3") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +var root = config.RootSection("di") + +func TestInitConfig(t *testing.T) { + conf := root.SubArray("plugins") + InitConfig(conf) +} diff --git a/internal/dataexchange/dxfactory/factory_test.go b/internal/dataexchange/dxfactory/factory_test.go new file mode 100644 index 000000000..989570a10 --- /dev/null +++ b/internal/dataexchange/dxfactory/factory_test.go @@ -0,0 +1,46 @@ +// Copyright © 2023 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed 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 dxfactory + +import ( + "context" + "testing" + + "github.com/hyperledger/firefly-common/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestGetPluginUnknown(t *testing.T) { + ctx := context.Background() + _, err := GetPlugin(ctx, "foo") + assert.Error(t, err) + assert.Regexp(t, "FF10213", err) +} + +func TestGetPlugin(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "ffdx") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +var root = config.RootSection("di") + +func TestInitConfig(t *testing.T) { + conf := root.SubArray("plugins") + InitConfig(conf) +} diff --git a/internal/events/eifactory/factory_test.go b/internal/events/eifactory/factory_test.go new file mode 100644 index 000000000..fb964793e --- /dev/null +++ b/internal/events/eifactory/factory_test.go @@ -0,0 +1,59 @@ +// Copyright © 2023 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed 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 eifactory + +import ( + "context" + "testing" + + "github.com/hyperledger/firefly-common/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestGetPluginUnknown(t *testing.T) { + ctx := context.Background() + _, err := GetPlugin(ctx, "foo") + assert.Error(t, err) + assert.Regexp(t, "FF10172", err) +} + +func TestGetPluginWebSockets(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "websockets") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +func TestGetPluginWebHooks(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "webhooks") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +func TestGetPluginEvents(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "system") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +var root = config.RootSection("di") + +func TestInitConfig(t *testing.T) { + InitConfig(root) +} diff --git a/internal/identity/iifactory/factory_test.go b/internal/identity/iifactory/factory_test.go new file mode 100644 index 000000000..3110dbb65 --- /dev/null +++ b/internal/identity/iifactory/factory_test.go @@ -0,0 +1,46 @@ +// Copyright © 2023 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed 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 iifactory + +import ( + "context" + "testing" + + "github.com/hyperledger/firefly-common/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestGetPluginUnknown(t *testing.T) { + ctx := context.Background() + _, err := GetPlugin(ctx, "foo") + assert.Error(t, err) + assert.Regexp(t, "FF10212", err) +} + +func TestGetPlugin(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "onchain") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +var root = config.RootSection("di") + +func TestInitConfig(t *testing.T) { + conf := root.SubArray("plugins") + InitConfig(conf) +} diff --git a/internal/sharedstorage/ssfactory/factory_test.go b/internal/sharedstorage/ssfactory/factory_test.go new file mode 100644 index 000000000..5013d4cf4 --- /dev/null +++ b/internal/sharedstorage/ssfactory/factory_test.go @@ -0,0 +1,46 @@ +// Copyright © 2023 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed 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 ssfactory + +import ( + "context" + "testing" + + "github.com/hyperledger/firefly-common/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestGetPluginUnknown(t *testing.T) { + ctx := context.Background() + _, err := GetPlugin(ctx, "foo") + assert.Error(t, err) + assert.Regexp(t, "FF10134", err) +} + +func TestGetPlugin(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "ipfs") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +var root = config.RootSection("di") + +func TestInitConfig(t *testing.T) { + conf := root.SubArray("plugins") + InitConfig(conf) +} diff --git a/internal/tokens/tifactory/factory_test.go b/internal/tokens/tifactory/factory_test.go new file mode 100644 index 000000000..f1d122932 --- /dev/null +++ b/internal/tokens/tifactory/factory_test.go @@ -0,0 +1,46 @@ +// Copyright © 2023 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed 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 tifactory + +import ( + "context" + "testing" + + "github.com/hyperledger/firefly-common/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestGetPluginUnknown(t *testing.T) { + ctx := context.Background() + _, err := GetPlugin(ctx, "foo") + assert.Error(t, err) + assert.Regexp(t, "FF10272", err) +} + +func TestGetPlugin(t *testing.T) { + ctx := context.Background() + plugin, err := GetPlugin(ctx, "fftokens") + assert.NoError(t, err) + assert.NotNil(t, plugin) +} + +var root = config.RootSection("tokens") + +func TestInitConfig(t *testing.T) { + conf := root.SubArray("plugins") + InitConfig(conf) +}