From 310b5b533e2da4aa2088b84bccd92d0999ed2f00 Mon Sep 17 00:00:00 2001 From: novikovmu Date: Thu, 22 Sep 2022 17:54:50 +0300 Subject: [PATCH] MSP-1021 init api test --- tests/api_script_test.go | 191 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 tests/api_script_test.go diff --git a/tests/api_script_test.go b/tests/api_script_test.go new file mode 100644 index 0000000..bc4a693 --- /dev/null +++ b/tests/api_script_test.go @@ -0,0 +1,191 @@ +package tests + +import ( + "context" + "testing" + + "github.com/integration-system/isp-kit/grpc/client" + "github.com/integration-system/isp-kit/test" + "github.com/integration-system/isp-kit/test/grpct" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "isp-script-service/assembly" + "isp-script-service/conf" + "isp-script-service/domain" +) + +func TestApiScriptTestSuite(t *testing.T) { + suite.Run(t, &ApiScript{}) +} + +type ApiScript struct { + suite.Suite + test *test.Test + require *require.Assertions + apiCli *client.Client +} + +func (s *ApiScript) SetupSuite() { + s.test, s.require = test.New(s.T()) + + cli := s.initMockServer() + + cfg := conf.Remote{ + ScriptExecutionTimeoutMs: 10000, + Scripts: []conf.ScriptDefinition{ + { + Id: "1", + Script: "return true", + }, + { + Id: "2", + Script: "return false", + }, + }, + } + + locator := assembly.NewLocator(s.test.Logger(), cli) + handler := locator.Handler(cfg) + + _, s.apiCli = grpct.TestServer(s.test, handler) +} + +func (s *ApiScript) TestExternalInvoke() { + req := domain.ExecuteRequest{ + Script: "return external.invoke('mock', true, {})", + Arg: struct{}{}, + } + res := domain.ScriptResp{} + err := s.apiCli.Invoke("script/execute"). + JsonRequestBody(req). + ReadJsonResponse(&res). + Do(context.Background()) + s.Require().NoError(err) + s.Require().Equal(res.Result, true) +} + +func (s *ApiScript) TestExecuteHappyPath() { + req := domain.ExecuteRequest{ + Script: "return arg.a", + Arg: map[string]bool{ + "a": true, + }, + } + res := domain.ScriptResp{} + err := s.apiCli.Invoke("script/execute"). + JsonRequestBody(req). + ReadJsonResponse(&res). + Do(context.Background()) + s.Require().NoError(err) + s.Require().Equal(res.Result, true) +} + +func (s *ApiScript) TestExecuteByIdHappyPath() { + req := domain.ExecuteByIdRequest{ + Id: "1", + Arg: struct{}{}, + } + res := domain.ScriptResp{} + err := s.apiCli.Invoke("script/execute_by_id"). + JsonRequestBody(req). + ReadJsonResponse(&res). + Do(context.Background()) + s.Require().NoError(err) + s.Require().Equal(res.Result, true) +} + +func (s *ApiScript) TestExecuteByIdNotFound() { + req := domain.ExecuteByIdRequest{ + Id: "unknown", + Arg: struct{}{}, + } + res := domain.ScriptResp{} + err := s.apiCli.Invoke("script/execute_by_id"). + JsonRequestBody(req). + ReadJsonResponse(&res). + Do(context.Background()) + s.Require().Error(err) + st, ok := status.FromError(err) + s.Require().True(ok) + s.Require().Equal(codes.NotFound, st.Code()) +} + +func (s *ApiScript) TestBatchExecuteHappyPath() { + req := []domain.ExecuteByIdRequest{ + { + Id: "1", + Arg: struct{}{}, + }, + { + Id: "2", + Arg: struct{}{}, + }, + } + res := make([]domain.ScriptResp, 0) + err := s.apiCli.Invoke("script/batch_execute"). + JsonRequestBody(req). + ReadJsonResponse(&res). + Do(context.Background()) + s.Require().NoError(err) + expected := []domain.ScriptResp{ + {Result: true, Error: nil}, + {Result: false, Error: nil}, + } + s.Require().ElementsMatch(expected, res) +} + +func (s *ApiScript) TestBatchExecuteMixedNotFound() { + req := []domain.ExecuteByIdRequest{ + { + Id: "1", + Arg: struct{}{}, + }, + { + Id: "unknown", + Arg: struct{}{}, + }, + } + res := make([]domain.ScriptResp, 0) + err := s.apiCli.Invoke("script/batch_execute"). + JsonRequestBody(req). + ReadJsonResponse(&res). + Do(context.Background()) + s.Require().NoError(err) + expected := []domain.ScriptResp{ + {Result: true, Error: nil}, + {Result: nil, Error: &domain.Error{ + Type: domain.ErrorCompile, + Description: "not defined script for id unknown", + }}, + } + s.Require().ElementsMatch(expected, res) +} + +func (s *ApiScript) TestBatchExecuteByIdHappyPath() { + req := domain.BatchExecuteByIdsRequest{ + Ids: []string{"1", "2"}, + Arg: struct{}{}, + } + res := make([]domain.ScriptResp, 0) + err := s.apiCli.Invoke("script/batch_execute_by_ids"). + JsonRequestBody(req). + ReadJsonResponse(&res). + Do(context.Background()) + s.Require().NoError(err) + expected := []domain.ScriptResp{ + {Result: true, Error: nil}, + {Result: false, Error: nil}, + } + s.Require().ElementsMatch(expected, res) +} + +func (s *ApiScript) initMockServer() *client.Client { + server, cli := grpct.NewMock(s.test) + server.Mock("mock", func(req bool) bool { + return req + }) + + return cli +}