diff --git a/CREDITS b/CREDITS index 92e9c9e..f997853 100644 --- a/CREDITS +++ b/CREDITS @@ -266,6 +266,27 @@ SOFTWARE. ================================================================ +github.com/davecgh/go-spew +https://github.com/davecgh/go-spew +---------------------------------------------------------------- +ISC License + +Copyright (c) 2012-2016 Dave Collins + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +================================================================ + github.com/dustin/go-humanize https://github.com/dustin/go-humanize ---------------------------------------------------------------- @@ -1189,6 +1210,61 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +================================================================ + +github.com/labstack/echo/v4 +https://github.com/labstack/echo/v4 +---------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2021 LabStack + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +================================================================ + +github.com/labstack/gommon +https://github.com/labstack/gommon +---------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2018 labstack + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + ================================================================ github.com/manifoldco/promptui @@ -1625,6 +1701,89 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +================================================================ + +github.com/stretchr/testify +https://github.com/stretchr/testify +---------------------------------------------------------------- +MIT License + +Copyright (c) 2012-2020 Mat Ryer, Tyler Bunnell and contributors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +================================================================ + +github.com/valyala/bytebufferpool +https://github.com/valyala/bytebufferpool +---------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2016 Aliaksandr Valialkin, VertaMedia + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + +================================================================ + +github.com/valyala/fasttemplate +https://github.com/valyala/fasttemplate +---------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2015 Aliaksandr Valialkin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + ================================================================ go.uber.org/mock @@ -1835,6 +1994,39 @@ https://go.uber.org/mock ================================================================ +golang.org/x/crypto +https://golang.org/x/crypto +---------------------------------------------------------------- +Copyright 2009 The Go Authors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google LLC nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +================================================================ + golang.org/x/mod https://golang.org/x/mod ---------------------------------------------------------------- @@ -1868,6 +2060,72 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ================================================================ +golang.org/x/net +https://golang.org/x/net +---------------------------------------------------------------- +Copyright 2009 The Go Authors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google LLC nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +================================================================ + +golang.org/x/sync +https://golang.org/x/sync +---------------------------------------------------------------- +Copyright 2009 The Go Authors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google LLC nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +================================================================ + golang.org/x/sys https://golang.org/x/sys ---------------------------------------------------------------- @@ -1904,7 +2162,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. golang.org/x/term https://golang.org/x/term ---------------------------------------------------------------- -Copyright (c) 2009 The Go Authors. All rights reserved. +Copyright 2009 The Go Authors. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are @@ -1916,7 +2174,73 @@ notice, this list of conditions and the following disclaimer. copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - * Neither the name of Google Inc. nor the names of its + * Neither the name of Google LLC nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +================================================================ + +golang.org/x/text +https://golang.org/x/text +---------------------------------------------------------------- +Copyright 2009 The Go Authors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google LLC nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +================================================================ + +golang.org/x/time +https://golang.org/x/time +---------------------------------------------------------------- +Copyright 2009 The Go Authors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. @@ -1967,6 +2291,62 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ================================================================ +gopkg.in/yaml.v3 +https://gopkg.in/yaml.v3 +---------------------------------------------------------------- + +This project is covered by two different licenses: MIT and Apache. + +#### MIT License #### + +The following files were ported to Go from C files of libyaml, and thus +are still covered by their original MIT license, with the additional +copyright staring in 2011 when the project was ported over: + + apic.go emitterc.go parserc.go readerc.go scannerc.go + writerc.go yamlh.go yamlprivateh.go + +Copyright (c) 2006-2010 Kirill Simonov +Copyright (c) 2006-2011 Kirill Simonov + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +### Apache License ### + +All the remaining project files are covered by the Apache license: + +Copyright (c) 2011-2019 Canonical Ltd + +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. + +================================================================ + modernc.org/cc/v4 https://modernc.org/cc/v4 ---------------------------------------------------------------- diff --git a/Makefile.toml b/Makefile.toml index fa9e85a..e099856 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -19,16 +19,20 @@ mockgen -source=./app/infrastructure/database/connection.go -destination=./app/i mockgen -source=./app/infrastructure/database/connection_manager.go -destination=./app/infrastructure/database/connection_manager_mock.go -package=database # ./app/domain mockgen -source=./app/domain/jrp/history/history_repository.go -destination=./app/domain/jrp/history/history_repository_mock.go -package=history +# ./app/presentation/api/jrp/server +mockgen -source=./app/presentation/api/jrp/server/server.go -destination=./app/presentation/api/jrp/server/server_mock.go -package=server # ./app/presentation/cli/jrp/command mockgen -source=./app/presentation/cli/jrp/command/command.go -destination=./app/presentation/cli/jrp/command/command_mock.go -package=command # ./pkg/proxy mockgen -source=./pkg/proxy/buffer.go -destination=./pkg/proxy/buffer_mock.go -package=proxy mockgen -source=./pkg/proxy/cobra.go -destination=./pkg/proxy/cobra_mock.go -package=proxy mockgen -source=./pkg/proxy/debug.go -destination=./pkg/proxy/debug_mock.go -package=proxy +mockgen -source=./pkg/proxy/echo.go -destination=./pkg/proxy/echo_mock.go -package=proxy mockgen -source=./pkg/proxy/envconfig.go -destination=./pkg/proxy/envconfig_mock.go -package=proxy mockgen -source=./pkg/proxy/gzip.go -destination=./pkg/proxy/gzip_mock.go -package=proxy mockgen -source=./pkg/proxy/http.go -destination=./pkg/proxy/http_mock.go -package=proxy mockgen -source=./pkg/proxy/io.go -destination=./pkg/proxy/io_mock.go -package=proxy +mockgen -source=./pkg/proxy/json.go -destination=./pkg/proxy/json_mock.go -package=proxy mockgen -source=./pkg/proxy/keyboard.go -destination=./pkg/proxy/keyboard_mock.go -package=proxy mockgen -source=./pkg/proxy/os.go -destination=./pkg/proxy/os_mock.go -package=proxy mockgen -source=./pkg/proxy/pflag.go -destination=./pkg/proxy/pflag_mock.go -package=proxy @@ -41,6 +45,7 @@ mockgen -source=./pkg/proxy/tablewriter.go -destination=./pkg/proxy/tablewriter_ mockgen -source=./pkg/utility/capture.go -destination=./pkg/utility/capture_mock.go -package=utility mockgen -source=./pkg/utility/download_util.go -destination=./pkg/utility/download_util_mock.go -package=utility mockgen -source=./pkg/utility/file_util.go -destination=./pkg/utility/file_util_mock.go -package=utility +mockgen -source=./pkg/utility/json_util.go -destination=./pkg/utility/json_util_mock.go -package=utility mockgen -source=./pkg/utility/keyboard_util.go -destination=./pkg/utility/keyboard_util_mock.go -package=utility mockgen -source=./pkg/utility/prompt_util.go -destination=./pkg/utility/prompt_util_mock.go -package=utility mockgen -source=./pkg/utility/rand_util.go -destination=./pkg/utility/rand_util_mock.go -package=utility diff --git a/app/presentation/api/jrp/config/config.go b/app/presentation/api/jrp/config/config.go new file mode 100644 index 0000000..75e9729 --- /dev/null +++ b/app/presentation/api/jrp/config/config.go @@ -0,0 +1,85 @@ +package config + +import ( + "path/filepath" + "strings" + + baseConfig "github.com/yanosea/jrp/v2/app/config" + "github.com/yanosea/jrp/v2/app/infrastructure/database" + + "github.com/yanosea/jrp/v2/pkg/proxy" + "github.com/yanosea/jrp/v2/pkg/utility" +) + +// JrpServerConfigurator is an interface that gets the configuration of the Jrp server application. +type JrpServerConfigurator interface { + GetConfig() (*JrpServerConfig, error) +} + +// ServerConfigurator is a struct that implements the JrpServerConfigurator interface. +type ServerConfigurator struct { + *baseConfig.BaseConfigurator +} + +// NewJrpServerConfigurator creates a new JrpServerConfigurator. +func NewJrpServerConfigurator( + envconfigProxy proxy.Envconfig, + fileUtil utility.FileUtil, +) JrpServerConfigurator { + return &ServerConfigurator{ + BaseConfigurator: baseConfig.NewConfigurator( + envconfigProxy, + fileUtil, + ), + } +} + +// JrpServerConfig is a struct that contains the configuration of the Jrp server application. +type JrpServerConfig struct { + baseConfig.JrpConfig + JrpPort string +} + +// envConfig is a struct that contains the environment variables. +type envConfig struct { + JrpPort string `envconfig:"JRP_SERVER_PORT" default:"8080"` + WnJpnDBType database.DBType `envconfig:"JRP_SERVER_WNJPN_DB_TYPE" default:"sqlite"` + WnJpnDBDsn string `envconfig:"JRP_SERVER_WNJPN_DB" default:"XDG_DATA_HOME/jrp/wnjpn.db"` +} + +// GetConfig gets the configuration of the Jrp server application. +func (c *ServerConfigurator) GetConfig() (*JrpServerConfig, error) { + var env envConfig + if err := c.Envconfig.Process("", &env); err != nil { + return nil, err + } + + config := &JrpServerConfig{ + JrpConfig: baseConfig.JrpConfig{ + WNJpnDBType: env.WnJpnDBType, + WNJpnDBDsn: env.WnJpnDBDsn, + }, + JrpPort: env.JrpPort, + } + + if config.WNJpnDBType == database.SQLite { + xdgDataHome, err := c.FileUtil.GetXDGDataHome() + if err != nil { + return nil, err + } + + config.WNJpnDBDsn = strings.Replace( + config.WNJpnDBDsn, + "XDG_DATA_HOME", + xdgDataHome, + 1, + ) + if err := c.FileUtil.MkdirIfNotExist( + filepath.Dir(config.WNJpnDBDsn), + ); err != nil { + return nil, err + } + } + + return config, nil +} diff --git a/app/presentation/api/jrp/config/config_test.go b/app/presentation/api/jrp/config/config_test.go new file mode 100644 index 0000000..46625ea --- /dev/null +++ b/app/presentation/api/jrp/config/config_test.go @@ -0,0 +1,181 @@ +package config + +import ( + "errors" + "reflect" + "testing" + + baseConfig "github.com/yanosea/jrp/v2/app/config" + "github.com/yanosea/jrp/v2/app/infrastructure/database" + + "github.com/yanosea/jrp/v2/pkg/proxy" + "github.com/yanosea/jrp/v2/pkg/utility" + + "go.uber.org/mock/gomock" +) + +func TestNewJrpServerConfigurator(t *testing.T) { + envconfig := proxy.NewEnvconfig() + fileUtil := utility.NewFileUtil( + proxy.NewGzip(), + proxy.NewIo(), + proxy.NewOs(), + ) + + type args struct { + envconfigProxy proxy.Envconfig + fileUtil utility.FileUtil + } + tests := []struct { + name string + args args + want JrpServerConfigurator + }{ + { + name: "positive testing", + args: args{ + envconfigProxy: envconfig, + fileUtil: fileUtil, + }, + want: &ServerConfigurator{ + BaseConfigurator: baseConfig.NewConfigurator( + envconfig, + fileUtil, + ), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewJrpServerConfigurator(tt.args.envconfigProxy, tt.args.fileUtil); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewJrpServerConfigurator() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestServerConfigurator_GetConfig(t *testing.T) { + type fields struct { + BaseConfigurator *baseConfig.BaseConfigurator + } + tests := []struct { + name string + fields fields + want *JrpServerConfig + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + }{ + { + name: "positive testing", + fields: fields{ + BaseConfigurator: &baseConfig.BaseConfigurator{ + Envconfig: nil, + FileUtil: nil, + }}, + want: &JrpServerConfig{ + JrpConfig: baseConfig.JrpConfig{ + WNJpnDBType: database.SQLite, + WNJpnDBDsn: "~/.local/share/jrp/wnjpn.db", + }, + }, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockEnvconfig := proxy.NewMockEnvconfig(mockCtrl) + mockEnvconfig.EXPECT().Process("", gomock.Any()).DoAndReturn( + func(_ string, cfg *envConfig) error { + cfg.WnJpnDBType = database.SQLite + cfg.WnJpnDBDsn = "XDG_DATA_HOME/jrp/wnjpn.db" + return nil + }) + mockFileUtil := utility.NewMockFileUtil(mockCtrl) + mockFileUtil.EXPECT().GetXDGDataHome().Return("~/.local/share", nil) + mockFileUtil.EXPECT().MkdirIfNotExist("~/.local/share/jrp").Return(nil) + tt.BaseConfigurator.Envconfig = mockEnvconfig + tt.BaseConfigurator.FileUtil = mockFileUtil + }, + }, + { + name: "negative testing (c.Envconfig.Process(\"\", &config) failed)", + fields: fields{ + BaseConfigurator: &baseConfig.BaseConfigurator{ + Envconfig: nil, + FileUtil: nil, + }}, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockEnvconfig := proxy.NewMockEnvconfig(mockCtrl) + mockEnvconfig.EXPECT().Process("", gomock.Any()).Return(errors.New("Envconfig.Process() failed")) + tt.BaseConfigurator.Envconfig = mockEnvconfig + }, + }, + { + name: "negative testing (c.FileUtil.GetXDGDataHome() failed)", + fields: fields{ + BaseConfigurator: &baseConfig.BaseConfigurator{ + Envconfig: nil, + FileUtil: nil, + }}, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockEnvconfig := proxy.NewMockEnvconfig(mockCtrl) + mockEnvconfig.EXPECT().Process("", gomock.Any()).DoAndReturn( + func(_ string, cfg *envConfig) error { + cfg.WnJpnDBType = database.SQLite + cfg.WnJpnDBDsn = "XDG_DATA_HOME/jrp/wnjpn.db" + return nil + }) + mockFileUtil := utility.NewMockFileUtil(mockCtrl) + mockFileUtil.EXPECT().GetXDGDataHome().Return("", errors.New("FileUtil.GetXDGDataHome() failed")) + tt.BaseConfigurator.Envconfig = mockEnvconfig + tt.BaseConfigurator.FileUtil = mockFileUtil + }, + }, + { + name: "negative testing (c.FileUtil.MkdirIfNotExist(filepath.Dir(config.WNJpnDBConnectionString)) failed)", + fields: fields{ + BaseConfigurator: &baseConfig.BaseConfigurator{ + Envconfig: nil, + FileUtil: nil, + }, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockEnvconfig := proxy.NewMockEnvconfig(mockCtrl) + mockEnvconfig.EXPECT().Process("", gomock.Any()).DoAndReturn( + func(_ string, cfg *envConfig) error { + cfg.WnJpnDBType = database.SQLite + cfg.WnJpnDBDsn = "XDG_DATA_HOME/jrp/wnjpn.db" + return nil + }) + mockFileUtil := utility.NewMockFileUtil(mockCtrl) + mockFileUtil.EXPECT().GetXDGDataHome().Return("~/.local/share", nil) + mockFileUtil.EXPECT().MkdirIfNotExist("~/.local/share/jrp").Return(errors.New("FileUtil.MkdirIfNotExist() failed")) + tt.BaseConfigurator.Envconfig = mockEnvconfig + tt.BaseConfigurator.FileUtil = mockFileUtil + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + if tt.setup != nil { + tt.setup(mockCtrl, &tt.fields) + } + c := &ServerConfigurator{ + BaseConfigurator: tt.fields.BaseConfigurator, + } + got, err := c.GetConfig() + if (err != nil) != tt.wantErr { + t.Errorf("ServerConfigurator.GetConfig() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("ServerConfigurator.GetConfig() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/app/presentation/api/jrp/config/godoc.go b/app/presentation/api/jrp/config/godoc.go new file mode 100644 index 0000000..8252b4e --- /dev/null +++ b/app/presentation/api/jrp/config/godoc.go @@ -0,0 +1,2 @@ +// Package config provides the configuration of the jrp server application. +package config diff --git a/app/presentation/api/jrp/formatter/formatter.go b/app/presentation/api/jrp/formatter/formatter.go new file mode 100644 index 0000000..a9fee45 --- /dev/null +++ b/app/presentation/api/jrp/formatter/formatter.go @@ -0,0 +1,24 @@ +package formatter + +import ( + "errors" +) + +// Formatter is an interface that formats the output of jrp cli. +type Formatter interface { + Format(result interface{}) ([]byte, error) +} + +// NewFormatter returns a new instance of the Formatter interface. +func NewFormatter( + format string, +) (Formatter, error) { + var f Formatter + switch format { + case "json": + f = NewJsonFormatter() + default: + return nil, errors.New("invalid format") + } + return f, nil +} diff --git a/app/presentation/api/jrp/formatter/formatter_test.go b/app/presentation/api/jrp/formatter/formatter_test.go new file mode 100644 index 0000000..96040ea --- /dev/null +++ b/app/presentation/api/jrp/formatter/formatter_test.go @@ -0,0 +1,48 @@ +package formatter + +import ( + "reflect" + "testing" +) + +func TestNewFormatter(t *testing.T) { + type args struct { + format string + } + tests := []struct { + name string + args args + want Formatter + wantErr bool + }{ + { + name: "positive testing (format is json)", + args: args{ + format: "json", + }, + want: &JsonFormatter{}, + wantErr: false, + }, + + { + name: "negative testing (format is invalid)", + args: args{ + format: "invalid", + }, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := NewFormatter(tt.args.format) + if (err != nil) != tt.wantErr { + t.Errorf("NewFormatter() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewFormatter() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/app/presentation/api/jrp/formatter/godoc.go b/app/presentation/api/jrp/formatter/godoc.go new file mode 100644 index 0000000..fa72f11 --- /dev/null +++ b/app/presentation/api/jrp/formatter/godoc.go @@ -0,0 +1,2 @@ +// Package formatter provides the formatter for the jrp. +package formatter diff --git a/app/presentation/api/jrp/formatter/json.go b/app/presentation/api/jrp/formatter/json.go new file mode 100644 index 0000000..0bf74ef --- /dev/null +++ b/app/presentation/api/jrp/formatter/json.go @@ -0,0 +1,53 @@ +package formatter + +import ( + "errors" + + jrpApp "github.com/yanosea/jrp/v2/app/application/jrp" + + "github.com/yanosea/jrp/v2/pkg/proxy" + "github.com/yanosea/jrp/v2/pkg/utility" +) + +// JsonFormatter is a struct that formats the output of jrp server. +type JsonFormatter struct{} + +// ResponseOutputDto is a struct that represents the response of jrp server. +type ResponseOutputDto struct { + Body []byte `json:"body"` +} + +// JrpJsonOutputDto is a struct that represents the output json of jrp server. +type JrpJsonOutputDto struct { + Phrase string `json:"phrase"` +} + +var ( + // Ju is a variable that contains the JsonUtil struct for injecting dependencies in testing. + Ju = utility.NewJsonUtil(proxy.NewJson()) +) + +// NewJsonFormatter returns a new instance of the JsonFormatter struct. +func NewJsonFormatter() *JsonFormatter { + return &JsonFormatter{} +} + +// Format formats the output of jrp server. +func (f *JsonFormatter) Format(result interface{}) ([]byte, error) { + var formatted []byte + var err error + switch v := result.(type) { + case *jrpApp.GenerateJrpUseCaseOutputDto: + jjoDto := JrpJsonOutputDto{Phrase: v.Phrase} + gjj, err := Ju.Marshal(jjoDto) + if err != nil { + return nil, err + } + gjroDto := ResponseOutputDto{Body: gjj} + formatted = gjroDto.Body + default: + formatted = nil + err = errors.New("invalid result type") + } + return formatted, err +} diff --git a/app/presentation/api/jrp/formatter/json_test.go b/app/presentation/api/jrp/formatter/json_test.go new file mode 100644 index 0000000..70e50c5 --- /dev/null +++ b/app/presentation/api/jrp/formatter/json_test.go @@ -0,0 +1,114 @@ +package formatter + +import ( + "errors" + "reflect" + "testing" + + jrpApp "github.com/yanosea/jrp/v2/app/application/jrp" + + "github.com/yanosea/jrp/v2/pkg/proxy" + "github.com/yanosea/jrp/v2/pkg/utility" + + "go.uber.org/mock/gomock" +) + +func TestNewJsonFormatter(t *testing.T) { + tests := []struct { + name string + want *JsonFormatter + }{ + { + name: "positive testing", + want: &JsonFormatter{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewJsonFormatter(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewJsonFormatter() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestJsonFormatter_Format(t *testing.T) { + origJu := Ju + + type args struct { + result interface{} + } + tests := []struct { + name string + f *JsonFormatter + args args + want []byte + wantErr bool + setup func(mockCtrl *gomock.Controller) + cleanup func() + }{ + { + name: "positive testing (result is *jrpApp.GenerateJrpUseCaseOutputDto)", + args: args{ + result: &jrpApp.GenerateJrpUseCaseOutputDto{ + Phrase: "test", + }, + }, + want: []byte(`{"phrase":"test"}`), + wantErr: false, + setup: nil, + cleanup: nil, + }, + { + name: "negative testing (result is *jrpApp.GenerateJrpUseCaseOutputDto, Ju.Marshal(jjoDto) failed)", + args: args{ + result: &jrpApp.GenerateJrpUseCaseOutputDto{ + Phrase: "test", + }, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller) { + mockJson := proxy.NewMockJson(mockCtrl) + mockJson.EXPECT().Marshal(gomock.Any()).Return(nil, errors.New("JsonUtil.Marshal() failed")) + Ju = utility.NewJsonUtil(mockJson) + }, + cleanup: func() { + Ju = origJu + }, + }, + { + name: "negative testing (result is invalid)", + args: args{ + result: "invalid", + }, + want: nil, + wantErr: true, + setup: nil, + cleanup: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + if tt.setup != nil { + tt.setup(mockCtrl) + } + defer func() { + if tt.cleanup != nil { + tt.cleanup() + } + }() + f := &JsonFormatter{} + got, err := f.Format(tt.args.result) + if (err != nil) != tt.wantErr { + t.Errorf("JsonFormatter.Format() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("JsonFormatter.Format() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/app/presentation/api/jrp/godoc.go b/app/presentation/api/jrp/godoc.go new file mode 100644 index 0000000..c986345 --- /dev/null +++ b/app/presentation/api/jrp/godoc.go @@ -0,0 +1,2 @@ +// Package main provides the entry point of the api application. +package main diff --git a/app/presentation/api/jrp/main.go b/app/presentation/api/jrp/main.go new file mode 100644 index 0000000..039ce35 --- /dev/null +++ b/app/presentation/api/jrp/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "os" + + "github.com/yanosea/jrp/v2/app/presentation/api/jrp/server" + + "github.com/yanosea/jrp/v2/pkg/proxy" + "github.com/yanosea/jrp/v2/pkg/utility" +) + +// JrpApiServerParams is a struct that represents the options of jrp api sever. +type JrpApiServerParams struct { + // Echos is a proxy of labstack/echo/v4. + Echos proxy.Echos + // Envconfig is a proxy of kelseyhightower/envconfig. + Envconfig proxy.Envconfig + // FileUtil provides the file utility. + FileUtil utility.FileUtil + // Sql is a proxy of database/sql. + Sql proxy.Sql +} + +var ( + // exit is a variable that contains the os.Exit function for injecting dependencies in testing. + exit = os.Exit + // jrpApiServerParams is a variable that contains the jrpApiServerParams struct. + jrpApiServerParams = JrpApiServerParams{ + Echos: proxy.NewEchos(), + Envconfig: proxy.NewEnvconfig(), + FileUtil: utility.NewFileUtil( + proxy.NewGzip(), + proxy.NewIo(), + proxy.NewOs(), + ), + Sql: proxy.NewSql(), + } +) + +// main is the entry point of jrp api server. +func main() { + serv := server.NewServer( + jrpApiServerParams.Echos, + ) + if exitCode := serv.Init( + jrpApiServerParams.Envconfig, + jrpApiServerParams.FileUtil, + jrpApiServerParams.Sql, + ); exitCode != 0 { + exit(exitCode) + } + + exit(serv.Run()) +} diff --git a/app/presentation/api/jrp/main_test.go b/app/presentation/api/jrp/main_test.go new file mode 100644 index 0000000..753a201 --- /dev/null +++ b/app/presentation/api/jrp/main_test.go @@ -0,0 +1,78 @@ +package main + +import ( + "os" + "path/filepath" + "testing" + + "github.com/yanosea/jrp/v2/app/presentation/api/jrp/server" + + "github.com/yanosea/jrp/v2/pkg/proxy" + + "go.uber.org/mock/gomock" +) + +func Test_main(t *testing.T) { + os.Setenv("JRP_SERVER_WNJPN_DB_TYPE", "sqlite") + os.Setenv("JRP_SERVER_WNJPN_DB", filepath.Join(os.TempDir(), "wnjpn.db")) + origExit := exit + exit = func(code int) {} + defer func() { + exit = origExit + }() + origJrpApiServerParams := jrpApiServerParams + + tests := []struct { + name string + setup func(mockCtrl *gomock.Controller) + clear func() + }{ + { + name: "positive testing", + setup: func(mockCtrl *gomock.Controller) { + mockGroup := proxy.NewMockGroup(mockCtrl) + mockGroup.EXPECT().GET("/jrp", gomock.Any()) + mockEcho := proxy.NewMockEcho(mockCtrl) + mockEcho.EXPECT().Use(gomock.Any()) + mockEcho.EXPECT().Use(gomock.Any()) + mockEcho.EXPECT().Group("/api").Return(mockGroup) + mockEcho.EXPECT().Start(":8080") + mockLogger := proxy.NewMockLogger(mockCtrl) + mockEchos := proxy.NewMockEchos(mockCtrl) + mockEchos.EXPECT().NewEcho().Return(mockEcho, mockLogger) + jrpApiServerParams.Echos = mockEchos + }, + clear: func() { + jrpApiServerParams = origJrpApiServerParams + }, + }, + { + name: "negative testing (serv.Init() failed)", + setup: func(mockCtrl *gomock.Controller) { + mockServ := server.NewMockServer(mockCtrl) + mockServ.EXPECT().Init(gomock.Any(), gomock.Any(), gomock.Any()).Return(1) + mockServ.EXPECT().Run() + origNewServer := server.NewServer + server.NewServer = func(echo proxy.Echos) server.Server { + return mockServ + } + t.Cleanup(func() { + server.NewServer = origNewServer + }) + }, + clear: func() { + jrpApiServerParams = origJrpApiServerParams + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + if tt.setup != nil { + tt.setup(mockCtrl) + } + main() + }) + } +} diff --git a/app/presentation/api/jrp/server/godoc.go b/app/presentation/api/jrp/server/godoc.go new file mode 100644 index 0000000..1085088 --- /dev/null +++ b/app/presentation/api/jrp/server/godoc.go @@ -0,0 +1,2 @@ +// Package server provides the server application for the jrp. +package server diff --git a/app/presentation/api/jrp/server/jrp/get.go b/app/presentation/api/jrp/server/jrp/get.go new file mode 100644 index 0000000..8efa03f --- /dev/null +++ b/app/presentation/api/jrp/server/jrp/get.go @@ -0,0 +1,81 @@ +package jrp + +import ( + "net/http" + + "github.com/labstack/echo/v4" + "github.com/labstack/gommon/log" + + jrpApp "github.com/yanosea/jrp/v2/app/application/jrp" + wnjpnApp "github.com/yanosea/jrp/v2/app/application/wnjpn" + "github.com/yanosea/jrp/v2/app/infrastructure/database" + "github.com/yanosea/jrp/v2/app/infrastructure/wnjpn/query_service" + "github.com/yanosea/jrp/v2/app/presentation/api/jrp/formatter" + + "github.com/yanosea/jrp/v2/pkg/proxy" +) + +var ( + format = "json" +) + +// BindGetJrpHandler binds the getJrp handler to the server. +func BindGetJrpHandler(g proxy.Group) { + g.GET("/jrp", getJrp) +} + +// getJrp is a handler that returns a random Japanese phrase. +func getJrp(c echo.Context) error { + connManager := database.GetConnectionManager() + if connManager == nil { + log.Error("Connection manager is not initialized...") + return c.NoContent(http.StatusInternalServerError) + } + + if _, err := connManager.GetConnection(database.WNJpnDB); err != nil { + log.Error("Failed to get a connection to the database...") + return c.NoContent(http.StatusInternalServerError) + } + + wordQueryService := query_service.NewWordQueryService() + fwuc := wnjpnApp.NewFetchWordsUseCase(wordQueryService) + + fwoDtos, err := fwuc.Run( + c.Request().Context(), + "jpn", + []string{"a", "v", "n"}, + ) + if err != nil { + log.Error("Failed to fetch words...") + return c.NoContent(http.StatusInternalServerError) + } + + var gjiDtos []*jrpApp.GenerateJrpUseCaseInputDto + for _, fwoDto := range fwoDtos { + gjiDto := &jrpApp.GenerateJrpUseCaseInputDto{ + WordID: fwoDto.WordID, + Lang: fwoDto.Lang, + Lemma: fwoDto.Lemma, + Pron: fwoDto.Pron, + Pos: fwoDto.Pos, + } + gjiDtos = append(gjiDtos, gjiDto) + } + + gjuc := jrpApp.NewGenerateJrpUseCase() + gjoDto := gjuc.RunWithRandom(gjiDtos) + + f, err := formatter.NewFormatter(format) + if err != nil { + log.Error("Failed to create a new formatter...") + return c.NoContent(http.StatusInternalServerError) + } + + body, err := f.Format(gjoDto) + if body == nil || err != nil { + log.Error("Failed to format the output...") + return c.NoContent(http.StatusInternalServerError) + } + + return c.JSONBlob(http.StatusOK, body) +} diff --git a/app/presentation/api/jrp/server/jrp/get_test.go b/app/presentation/api/jrp/server/jrp/get_test.go new file mode 100644 index 0000000..37f18c6 --- /dev/null +++ b/app/presentation/api/jrp/server/jrp/get_test.go @@ -0,0 +1,239 @@ +package jrp + +import ( + "errors" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "github.com/labstack/echo/v4" + + jrpApp "github.com/yanosea/jrp/v2/app/application/jrp" + wnjpnApp "github.com/yanosea/jrp/v2/app/application/wnjpn" + "github.com/yanosea/jrp/v2/app/infrastructure/database" + "github.com/yanosea/jrp/v2/app/presentation/api/jrp/formatter" + + "github.com/yanosea/jrp/v2/pkg/proxy" + "github.com/yanosea/jrp/v2/pkg/utility" + + "go.uber.org/mock/gomock" +) + +func TestBindGetJrpHandler(t *testing.T) { + type args struct { + g proxy.Group + } + tests := []struct { + name string + args args + setup func(mockCtrl *gomock.Controller, tt *args) + }{ + { + name: "positive testing", + args: args{ + g: nil, + }, + setup: func(mockCtrl *gomock.Controller, tt *args) { + mockGroup := proxy.NewMockGroup(mockCtrl) + mockGroup.EXPECT().GET("/jrp", gomock.Any()) + tt.g = mockGroup + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + if tt.setup != nil { + tt.setup(mockCtrl, &tt.args) + } + BindGetJrpHandler(tt.args.g) + }) + } +} + +func Test_getJrp(t *testing.T) { + origFormat := format + origJu := formatter.Ju + origFunc := database.GetConnectionManagerFunc + origNewFetchWordsUseCase := wnjpnApp.NewFetchWordsUseCase + duc := jrpApp.NewDownloadUseCase() + if err := duc.Run(filepath.Join(os.TempDir(), "wnjpn.db")); err != nil && err.Error() != "wnjpn.db already exists" { + t.Errorf("Failed to download WordNet Japan DB file: %v", err) + } + + type args struct { + c echo.Context + } + tests := []struct { + name string + args args + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *args) + cleanup func() + }{ + { + name: "positive testing", + args: args{ + c: nil, + }, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *args) { + cm := database.NewConnectionManager(proxy.NewSql()) + if err := cm.InitializeConnection( + database.ConnectionConfig{ + DBName: database.WNJpnDB, + DBType: database.SQLite, + DSN: filepath.Join(os.TempDir(), "wnjpn.db"), + }, + ); err != nil { + t.Errorf("Failed to initialize connection: %v", err) + } + tt.c = echo.New().NewContext(httptest.NewRequest(http.MethodGet, "/api/jrp", nil), httptest.NewRecorder()) + }, + cleanup: func() { + if err := database.ResetConnectionManager(); err != nil { + t.Errorf("Failed to reset connection manager: %v", err) + } + }, + }, + { + name: "negative testing (connManager == nil)", + args: args{ + c: nil, + }, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *args) { + tt.c = echo.New().NewContext(httptest.NewRequest(http.MethodGet, "/api/jrp", nil), httptest.NewRecorder()) + }, + cleanup: nil, + }, + { + name: "negative testing (connectionManager.GetConnection(WNJpnDB) failed)", + args: args{ + c: nil, + }, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *args) { + mockConnManager := database.NewMockConnectionManager(mockCtrl) + mockConnManager.EXPECT().GetConnection(database.WNJpnDB).Return(nil, errors.New("ConnectionManager.GetConnection() failed")) + database.GetConnectionManagerFunc = func() database.ConnectionManager { + return mockConnManager + } + tt.c = echo.New().NewContext(httptest.NewRequest(http.MethodGet, "/api/jrp", nil), httptest.NewRecorder()) + }, + cleanup: func() { + database.GetConnectionManagerFunc = origFunc + }, + }, + { + name: "negative testing (fwuc.Run() failed)", + args: args{ + c: nil, + }, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *args) { + cm := database.NewConnectionManager(proxy.NewSql()) + if err := cm.InitializeConnection( + database.ConnectionConfig{ + DBName: database.WNJpnDB, + DBType: database.SQLite, + DSN: filepath.Join(os.TempDir(), "wnjpn.db"), + }, + ); err != nil { + t.Errorf("Failed to initialize connection: %v", err) + } + mockWordQueryService := wnjpnApp.NewMockWordQueryService(mockCtrl) + mockWordQueryService.EXPECT(). + FindByLangIsAndPosIn(gomock.Any(), "jpn", gomock.Any()). + Return(nil, errors.New("WordQueryService.FindByLangIsAndPosIn() failed")) + origNewFetchWordsUseCase := wnjpnApp.NewFetchWordsUseCase + wnjpnApp.NewFetchWordsUseCase = func(wordQueryService wnjpnApp.WordQueryService) *wnjpnApp.FetchWordsUseCaseStruct { + return origNewFetchWordsUseCase(mockWordQueryService) + } + tt.c = echo.New().NewContext(httptest.NewRequest(http.MethodGet, "/api/jrp", nil), httptest.NewRecorder()) + }, + cleanup: func() { + if err := database.ResetConnectionManager(); err != nil { + t.Errorf("Failed to reset connection manager: %v", err) + } + wnjpnApp.NewFetchWordsUseCase = origNewFetchWordsUseCase + }, + }, + { + name: "negative testing (formatter.NewFormatter(format) failed)", + args: args{ + c: nil, + }, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *args) { + cm := database.NewConnectionManager(proxy.NewSql()) + if err := cm.InitializeConnection( + database.ConnectionConfig{ + DBName: database.WNJpnDB, + DBType: database.SQLite, + DSN: filepath.Join(os.TempDir(), "wnjpn.db"), + }, + ); err != nil { + t.Errorf("Failed to initialize connection: %v", err) + } + format = "test" + tt.c = echo.New().NewContext(httptest.NewRequest(http.MethodGet, "/api/jrp", nil), httptest.NewRecorder()) + }, + cleanup: func() { + if err := database.ResetConnectionManager(); err != nil { + t.Errorf("Failed to reset connection manager: %v", err) + } + format = origFormat + }, + }, + { + name: "negative testing (f.Format() failed)", + args: args{ + c: nil, + }, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *args) { + cm := database.NewConnectionManager(proxy.NewSql()) + if err := cm.InitializeConnection( + database.ConnectionConfig{ + DBName: database.WNJpnDB, + DBType: database.SQLite, + DSN: filepath.Join(os.TempDir(), "wnjpn.db"), + }, + ); err != nil { + t.Errorf("Failed to initialize connection: %v", err) + } + mockJu := utility.NewMockJsonUtil(mockCtrl) + mockJu.EXPECT().Marshal(gomock.Any()).Return(nil, errors.New("JsonUtil.Marshal() failed")) + formatter.Ju = mockJu + tt.c = echo.New().NewContext(httptest.NewRequest(http.MethodGet, "/api/jrp", nil), httptest.NewRecorder()) + }, + cleanup: func() { + if err := database.ResetConnectionManager(); err != nil { + t.Errorf("Failed to reset connection manager: %v", err) + } + formatter.Ju = origJu + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + if tt.setup != nil { + tt.setup(mockCtrl, &tt.args) + } + defer func() { + if tt.cleanup != nil { + tt.cleanup() + } + }() + if err := getJrp(tt.args.c); (err != nil) != tt.wantErr { + t.Errorf("getJrp() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/app/presentation/api/jrp/server/jrp/godoc.go b/app/presentation/api/jrp/server/jrp/godoc.go new file mode 100644 index 0000000..417ba48 --- /dev/null +++ b/app/presentation/api/jrp/server/jrp/godoc.go @@ -0,0 +1,2 @@ +// Package jrp provides the entry points for the Jrp server client. +package jrp diff --git a/app/presentation/api/jrp/server/route.go b/app/presentation/api/jrp/server/route.go new file mode 100644 index 0000000..abc7002 --- /dev/null +++ b/app/presentation/api/jrp/server/route.go @@ -0,0 +1,13 @@ +package server + +import ( + "github.com/yanosea/jrp/v2/app/presentation/api/jrp/server/jrp" + + "github.com/yanosea/jrp/v2/pkg/proxy" +) + +// Bind binds the routes to the server. +func Bind(e proxy.Echo) { + apiGroup := e.Group("/api") + jrp.BindGetJrpHandler(apiGroup) +} diff --git a/app/presentation/api/jrp/server/route_test.go b/app/presentation/api/jrp/server/route_test.go new file mode 100644 index 0000000..9eef472 --- /dev/null +++ b/app/presentation/api/jrp/server/route_test.go @@ -0,0 +1,44 @@ +package server + +import ( + "testing" + + "github.com/yanosea/jrp/v2/pkg/proxy" + + "go.uber.org/mock/gomock" +) + +func TestBind(t *testing.T) { + type args struct { + e proxy.Echo + } + tests := []struct { + name string + args args + setup func(mockCtrl *gomock.Controller, tt *args) + }{ + { + name: "positive testing", + args: args{ + e: nil, + }, + setup: func(mockCtrl *gomock.Controller, tt *args) { + mockGroup := proxy.NewMockGroup(mockCtrl) + mockGroup.EXPECT().GET("/jrp", gomock.Any()) + mockEcho := proxy.NewMockEcho(mockCtrl) + mockEcho.EXPECT().Group("/api").Return(mockGroup) + tt.e = mockEcho + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + if tt.setup != nil { + tt.setup(mockCtrl, &tt.args) + } + Bind(tt.args.e) + }) + } +} diff --git a/app/presentation/api/jrp/server/server.go b/app/presentation/api/jrp/server/server.go new file mode 100644 index 0000000..c9b78e4 --- /dev/null +++ b/app/presentation/api/jrp/server/server.go @@ -0,0 +1,109 @@ +package server + +import ( + "errors" + + "github.com/labstack/echo/v4/middleware" + + "github.com/yanosea/jrp/v2/app/infrastructure/database" + "github.com/yanosea/jrp/v2/app/presentation/api/jrp/config" + + "github.com/yanosea/jrp/v2/pkg/proxy" + "github.com/yanosea/jrp/v2/pkg/utility" +) + +var ( + // NewServer is a variable holding the current server creation function. + NewServer CreateServerFunc = newServer +) + +// Server is an interface that provides a proxy of the methods of jrp server. +type Server interface { + Init(envconfig proxy.Envconfig, fileUtil utility.FileUtil, sql proxy.Sql) int + Run() int +} + +// server is a struct that represents the server interface of jrp server. +type server struct { + ConnectionManager database.ConnectionManager + Echos proxy.Echos + Logger proxy.Logger + Port string + Route proxy.Echo +} + +// CreateServerFunc is a function type for creating new server instances. +type CreateServerFunc func(echo proxy.Echos) Server + +// newServer is the default implementation of CreateServerFunc. +func newServer(echos proxy.Echos) Server { + return &server{ + ConnectionManager: nil, + Echos: echos, + Logger: nil, + Port: "", + Route: nil, + } +} + +// Init initializes the server. +func (s *server) Init( + envconfig proxy.Envconfig, + fileUtil utility.FileUtil, + sql proxy.Sql, +) int { + s.Route, s.Logger = s.Echos.NewEcho() + s.Route.Use(middleware.Logger()) + s.Route.Use(middleware.Recover()) + Bind(s.Route) + + configurator := config.NewJrpServerConfigurator(envconfig, fileUtil) + conf, err := configurator.GetConfig() + if err != nil { + s.Logger.Fatal(err) + return 1 + } + + s.Port = conf.JrpPort + + if s.ConnectionManager == nil { + s.ConnectionManager = database.NewConnectionManager(sql) + } + + dbConfig := database.ConnectionConfig{ + DBName: database.WNJpnDB, + DBType: conf.WNJpnDBType, + DSN: conf.WNJpnDBDsn, + } + + if conf.WNJpnDBType == database.SQLite && !fileUtil.IsExist(conf.WNJpnDBDsn) { + s.Logger.Fatal(errors.New("WordNet Japanese database is not found...")) + return 1 + } + + if err := s.ConnectionManager.InitializeConnection(dbConfig); err != nil { + s.Logger.Fatal(err) + return 1 + } + + return 0 +} + +// Run runs the server. +func (s *server) Run() (exitCode int) { + defer func() { + if s.ConnectionManager != nil { + if err := s.ConnectionManager.CloseAllConnections(); err != nil { + s.Logger.Fatal(err) + exitCode = 1 + } + } + }() + + if err := s.Route.Start(":" + s.Port); err != nil { + s.Logger.Fatal(err) + exitCode = 1 + } + + return +} diff --git a/app/presentation/api/jrp/server/server_mock.go b/app/presentation/api/jrp/server/server_mock.go new file mode 100644 index 0000000..89309ca --- /dev/null +++ b/app/presentation/api/jrp/server/server_mock.go @@ -0,0 +1,69 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./app/presentation/api/jrp/server/server.go +// +// Generated by this command: +// +// mockgen -source=./app/presentation/api/jrp/server/server.go -destination=./app/presentation/api/jrp/server/server_mock.go -package=server +// + +// Package server is a generated GoMock package. +package server + +import ( + reflect "reflect" + + proxy "github.com/yanosea/jrp/v2/pkg/proxy" + utility "github.com/yanosea/jrp/v2/pkg/utility" + gomock "go.uber.org/mock/gomock" +) + +// MockServer is a mock of Server interface. +type MockServer struct { + ctrl *gomock.Controller + recorder *MockServerMockRecorder +} + +// MockServerMockRecorder is the mock recorder for MockServer. +type MockServerMockRecorder struct { + mock *MockServer +} + +// NewMockServer creates a new mock instance. +func NewMockServer(ctrl *gomock.Controller) *MockServer { + mock := &MockServer{ctrl: ctrl} + mock.recorder = &MockServerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockServer) EXPECT() *MockServerMockRecorder { + return m.recorder +} + +// Init mocks base method. +func (m *MockServer) Init(envconfig proxy.Envconfig, fileUtil utility.FileUtil, sql proxy.Sql) int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Init", envconfig, fileUtil, sql) + ret0, _ := ret[0].(int) + return ret0 +} + +// Init indicates an expected call of Init. +func (mr *MockServerMockRecorder) Init(envconfig, fileUtil, sql any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Init", reflect.TypeOf((*MockServer)(nil).Init), envconfig, fileUtil, sql) +} + +// Run mocks base method. +func (m *MockServer) Run() int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Run") + ret0, _ := ret[0].(int) + return ret0 +} + +// Run indicates an expected call of Run. +func (mr *MockServerMockRecorder) Run() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Run", reflect.TypeOf((*MockServer)(nil).Run)) +} diff --git a/app/presentation/api/jrp/server/server_test.go b/app/presentation/api/jrp/server/server_test.go new file mode 100644 index 0000000..637b4ca --- /dev/null +++ b/app/presentation/api/jrp/server/server_test.go @@ -0,0 +1,419 @@ +package server + +import ( + "errors" + o "os" + "path/filepath" + "reflect" + "testing" + + jrpApp "github.com/yanosea/jrp/v2/app/application/jrp" + "github.com/yanosea/jrp/v2/app/infrastructure/database" + + "github.com/yanosea/jrp/v2/pkg/proxy" + "github.com/yanosea/jrp/v2/pkg/utility" + + "go.uber.org/mock/gomock" +) + +func Test_newServer(t *testing.T) { + echos := proxy.NewEchos() + + type args struct { + echos proxy.Echos + } + tests := []struct { + name string + args args + want Server + }{ + { + name: "positeive testing", + args: args{ + echos: echos, + }, + want: &server{ + ConnectionManager: nil, + Echos: echos, + Logger: nil, + Port: "", + Route: nil, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := newServer(tt.args.echos); !reflect.DeepEqual(got, tt.want) { + t.Errorf("newServer() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_server_Init(t *testing.T) { + echos := proxy.NewEchos() + duc := jrpApp.NewDownloadUseCase() + if err := duc.Run(filepath.Join(o.TempDir(), "wnjpn.db")); err != nil && err.Error() != "wnjpn.db already exists" { + t.Errorf("Failed to download WordNet Japan DB file: %v", err) + } + + type fields struct { + ConnectionManager database.ConnectionManager + Echos proxy.Echos + Logger proxy.Logger + Port string + Route proxy.Echo + } + type args struct { + envconfig proxy.Envconfig + fileUtil utility.FileUtil + sql proxy.Sql + } + tests := []struct { + name string + fields fields + args args + want int + setup func(mockCtrl *gomock.Controller, ta *args, tf *fields) + cleanup func() + }{ + { + name: "positive testing", + fields: fields{ + ConnectionManager: nil, + Echos: echos, + Logger: nil, + Port: "", + Route: nil, + }, + args: args{ + envconfig: proxy.NewEnvconfig(), + fileUtil: utility.NewFileUtil( + proxy.NewGzip(), + proxy.NewIo(), + proxy.NewOs(), + ), + sql: proxy.NewSql(), + }, + want: 0, + setup: func(_ *gomock.Controller, _ *args, _ *fields) { + if err := o.Setenv("JRP_SERVER_PORT", "8080"); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + if err := o.Setenv("JRP_SERVER_WNJPN_DB_TYPE", "sqlite"); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + if err := o.Setenv("JRP_SERVER_WNJPN_DB", filepath.Join(o.TempDir(), "wnjpn.db")); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + }, + cleanup: func() { + if err := database.ResetConnectionManager(); err != nil { + t.Errorf("Failed to reset connection manager: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_PORT"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_WNJPN_DB_TYPE"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_WNJPN_DB"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + }, + }, + { + name: "negative testing (configurator.GetConfig() failed)", + fields: fields{ + ConnectionManager: nil, + Echos: nil, + Logger: nil, + Port: "", + Route: nil, + }, + args: args{ + envconfig: nil, + fileUtil: utility.NewFileUtil( + proxy.NewGzip(), + proxy.NewIo(), + proxy.NewOs(), + ), + sql: proxy.NewSql(), + }, + want: 1, + setup: func(mockCtrl *gomock.Controller, ta *args, tf *fields) { + if err := o.Setenv("JRP_SERVER_PORT", "8080"); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + if err := o.Setenv("JRP_SERVER_WNJPN_DB_TYPE", "sqlite"); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + if err := o.Setenv("JRP_SERVER_WNJPN_DB", filepath.Join(o.TempDir(), "wnjpn.db")); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + mockGroup := proxy.NewMockGroup(mockCtrl) + mockGroup.EXPECT().GET(gomock.Any(), gomock.Any()) + mockEcho := proxy.NewMockEcho(mockCtrl) + mockEcho.EXPECT().Use(gomock.Any()) + mockEcho.EXPECT().Use(gomock.Any()) + mockEcho.EXPECT().Group(gomock.Any()).Return(mockGroup) + mockLogger := proxy.NewMockLogger(mockCtrl) + mockLogger.EXPECT().Fatal(gomock.Any()) + mockEchos := proxy.NewMockEchos(mockCtrl) + mockEchos.EXPECT().NewEcho().Return(mockEcho, mockLogger) + tf.Echos = mockEchos + mockEnvconfig := proxy.NewMockEnvconfig(mockCtrl) + mockEnvconfig.EXPECT().Process("", gomock.Any()).Return(errors.New("EnvconfigProxy.Process() failed")) + ta.envconfig = mockEnvconfig + }, + cleanup: func() { + if err := database.ResetConnectionManager(); err != nil { + t.Errorf("Failed to reset connection manager: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_PORT"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_WNJPN_DB_TYPE"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_WNJPN_DB"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + }, + }, + { + name: "negative testing (conf.WNJpnDBType == database.SQLite && !fileUtil.IsExist(conf.WNJpnDBDsn))", + fields: fields{ + ConnectionManager: nil, + Echos: echos, + Logger: nil, + Port: "", + Route: nil, + }, + args: args{ + envconfig: proxy.NewEnvconfig(), + fileUtil: nil, + sql: proxy.NewSql(), + }, + want: 1, + setup: func(mockCtrl *gomock.Controller, ta *args, tf *fields) { + if err := o.Setenv("JRP_SERVER_PORT", "8080"); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + if err := o.Setenv("JRP_SERVER_WNJPN_DB_TYPE", "sqlite"); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + if err := o.Setenv("JRP_SERVER_WNJPN_DB", filepath.Join(o.TempDir(), "wnjpn.db")); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + mockGroup := proxy.NewMockGroup(mockCtrl) + mockGroup.EXPECT().GET(gomock.Any(), gomock.Any()) + mockEcho := proxy.NewMockEcho(mockCtrl) + mockEcho.EXPECT().Use(gomock.Any()) + mockEcho.EXPECT().Use(gomock.Any()) + mockEcho.EXPECT().Group(gomock.Any()).Return(mockGroup) + mockLogger := proxy.NewMockLogger(mockCtrl) + mockLogger.EXPECT().Fatal(gomock.Any()) + mockEchos := proxy.NewMockEchos(mockCtrl) + mockEchos.EXPECT().NewEcho().Return(mockEcho, mockLogger) + tf.Echos = mockEchos + mockFileUtil := utility.NewMockFileUtil(mockCtrl) + mockFileUtil.EXPECT().GetXDGDataHome().Return("~/.local/share", nil) + mockFileUtil.EXPECT().MkdirIfNotExist(gomock.Any()).Return(nil) + mockFileUtil.EXPECT().IsExist(filepath.Join(o.TempDir(), "wnjpn.db")).Return(false) + ta.fileUtil = mockFileUtil + }, + cleanup: func() { + if err := database.ResetConnectionManager(); err != nil { + t.Errorf("Failed to reset connection manager: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_PORT"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_WNJPN_DB_TYPE"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_WNJPN_DB"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + }, + }, + { + name: "negative testing (WNJpnDB InitializeConnection failed)", + fields: fields{ + ConnectionManager: nil, + Echos: echos, + Logger: nil, + Port: "", + Route: nil, + }, + args: args{ + envconfig: proxy.NewEnvconfig(), + fileUtil: utility.NewFileUtil( + proxy.NewGzip(), + proxy.NewIo(), + proxy.NewOs(), + ), + sql: proxy.NewSql(), + }, + want: 1, + setup: func(mockCtrl *gomock.Controller, ta *args, tf *fields) { + if err := o.Setenv("JRP_SERVER_PORT", "8080"); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + if err := o.Setenv("JRP_SERVER_WNJPN_DB_TYPE", "sqlite"); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + if err := o.Setenv("JRP_SERVER_WNJPN_DB", filepath.Join(o.TempDir(), "wnjpn.db")); err != nil { + t.Errorf("Failed to set environment variable: %v", err) + } + mockGroup := proxy.NewMockGroup(mockCtrl) + mockGroup.EXPECT().GET(gomock.Any(), gomock.Any()) + mockEcho := proxy.NewMockEcho(mockCtrl) + mockEcho.EXPECT().Use(gomock.Any()) + mockEcho.EXPECT().Use(gomock.Any()) + mockEcho.EXPECT().Group(gomock.Any()).Return(mockGroup) + mockLogger := proxy.NewMockLogger(mockCtrl) + mockLogger.EXPECT().Fatal(gomock.Any()) + mockEchos := proxy.NewMockEchos(mockCtrl) + mockEchos.EXPECT().NewEcho().Return(mockEcho, mockLogger) + tf.Echos = mockEchos + mockConnectionManager := database.NewMockConnectionManager(mockCtrl) + mockConnectionManager.EXPECT().InitializeConnection(gomock.Any()).Return(errors.New("ConnectionManager.InitializeConnection() failed")) + tf.ConnectionManager = mockConnectionManager + }, + cleanup: func() { + if err := database.ResetConnectionManager(); err != nil { + t.Errorf("Failed to reset connection manager: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_PORT"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_WNJPN_DB_TYPE"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + if err := o.Unsetenv("JRP_SERVER_WNJPN_DB"); err != nil { + t.Errorf("Failed to unset environment variable: %v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + if tt.setup != nil { + tt.setup(mockCtrl, &tt.args, &tt.fields) + } + defer func() { + if tt.cleanup != nil { + tt.cleanup() + } + }() + s := &server{ + ConnectionManager: tt.fields.ConnectionManager, + Echos: tt.fields.Echos, + Logger: tt.fields.Logger, + Port: tt.fields.Port, + Route: tt.fields.Route, + } + if got := s.Init(tt.args.envconfig, tt.args.fileUtil, tt.args.sql); got != tt.want { + t.Errorf("server.Init() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_server_Run(t *testing.T) { + type fields struct { + ConnectionManager database.ConnectionManager + Echos proxy.Echos + Logger proxy.Logger + Port string + Route proxy.Echo + } + tests := []struct { + name string + fields fields + wantExitCode int + setup func(mockCtrl *gomock.Controller, tt *fields) + }{ + { + name: "positive testing", + fields: fields{ + ConnectionManager: nil, + Echos: proxy.NewEchos(), + Logger: nil, + Port: "8080", + Route: nil, + }, + wantExitCode: 0, + setup: func(mockCtrl *gomock.Controller, tf *fields) { + mockEcho := proxy.NewMockEcho(mockCtrl) + mockEcho.EXPECT().Start(":" + tf.Port).Return(nil) + tf.Route = mockEcho + }, + }, + { + name: "negative testing (s.Route.Start(\":\" + s.Port) failed)", + fields: fields{ + ConnectionManager: nil, + Echos: proxy.NewEchos(), + Logger: nil, + Port: "8080", + Route: nil, + }, + wantExitCode: 1, + setup: func(mockCtrl *gomock.Controller, tf *fields) { + mockEcho := proxy.NewMockEcho(mockCtrl) + mockEcho.EXPECT().Start(":" + tf.Port).Return(errors.New("EchoProxy.Start() failed")) + mockLogger := proxy.NewMockLogger(mockCtrl) + mockLogger.EXPECT().Fatal(gomock.Any()) + tf.Route = mockEcho + tf.Logger = mockLogger + }, + }, + { + name: "negative testing (s.ConnectionManager.CloseAllConnections() failed)", + fields: fields{ + ConnectionManager: nil, + Echos: proxy.NewEchos(), + Logger: nil, + Port: "8080", + Route: nil, + }, + wantExitCode: 1, + setup: func(mockCtrl *gomock.Controller, tf *fields) { + mockConnectionManager := database.NewMockConnectionManager(mockCtrl) + mockConnectionManager.EXPECT().CloseAllConnections().Return(errors.New("ConnectionManager.CloseAllConnections() failed")) + mockEcho := proxy.NewMockEcho(mockCtrl) + mockEcho.EXPECT().Start(":" + tf.Port).Return(errors.New("EchoProxy.Start() failed")) + mockLogger := proxy.NewMockLogger(mockCtrl) + mockLogger.EXPECT().Fatal(gomock.Any()) + mockLogger.EXPECT().Fatal(gomock.Any()) + tf.ConnectionManager = mockConnectionManager + tf.Route = mockEcho + tf.Logger = mockLogger + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + if tt.setup != nil { + tt.setup(mockCtrl, &tt.fields) + } + s := &server{ + ConnectionManager: tt.fields.ConnectionManager, + Echos: tt.fields.Echos, + Logger: tt.fields.Logger, + Port: tt.fields.Port, + Route: tt.fields.Route, + } + if gotExitCode := s.Run(); gotExitCode != tt.wantExitCode { + t.Errorf("server.Run() = %v, want %v", gotExitCode, tt.wantExitCode) + } + }) + } +} diff --git a/docs/coverage.html b/docs/coverage.html index 831a7e9..98db774 100644 --- a/docs/coverage.html +++ b/docs/coverage.html @@ -87,79 +87,95 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + @@ -1901,7 +1917,440 @@ } -