|
| 1 | +package job_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/neo4j/cli/neo4j-cli/aura/internal/test/testutils" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCancelImportJob(t *testing.T) { |
| 11 | + organizationId := "f607bebe-0cc0-4166-b60c-b4eed69ee7ee" |
| 12 | + projectId := "f607bebe-0cc0-4166-b60c-b4eed69ee7ee" |
| 13 | + jobId := "87d485b4-73fc-4a7f-bb03-720f4672947e" |
| 14 | + |
| 15 | + helper := testutils.NewAuraTestHelper(t) |
| 16 | + defer helper.Close() |
| 17 | + |
| 18 | + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v2beta1/organizations/%s/projects/%s/import/jobs/%s/cancellation", organizationId, projectId, jobId), http.StatusOK, fmt.Sprintf(` |
| 19 | + { |
| 20 | + "data": {"id": "%s"} |
| 21 | + } |
| 22 | + `, jobId)) |
| 23 | + |
| 24 | + helper.SetConfigValue("aura.beta-enabled", true) |
| 25 | + |
| 26 | + helper.ExecuteCommand(fmt.Sprintf("import job cancel --organization-id=%s --project-id=%s %s", organizationId, projectId, jobId)) |
| 27 | + |
| 28 | + mockHandler.AssertCalledTimes(1) |
| 29 | + mockHandler.AssertCalledWithMethod(http.MethodPost) |
| 30 | + |
| 31 | + helper.AssertErr("") |
| 32 | + helper.AssertOutJson(fmt.Sprintf(` |
| 33 | + { |
| 34 | + "data": {"id": "%s"} |
| 35 | + } |
| 36 | + `, jobId)) |
| 37 | +} |
| 38 | + |
| 39 | +func TestCancelImportJobError(t *testing.T) { |
| 40 | + organizationId := "f607bebe-0cc0-4166-b60c-b4eed69ee7ee" |
| 41 | + projectId := "f607bebe-0cc0-4166-b60c-b4eed69ee7ee" |
| 42 | + jobId := "87d485b4-73fc-4a7f-bb03-720f4672947e" |
| 43 | + |
| 44 | + testCases := map[string]struct { |
| 45 | + executeCommand string |
| 46 | + statusCode int |
| 47 | + expectedCalledTimes int |
| 48 | + expectedError string |
| 49 | + returnBody string |
| 50 | + }{ |
| 51 | + "correct command with error response 1": { |
| 52 | + executeCommand: fmt.Sprintf("import job cancel --organization-id=%s --project-id=%s %s", organizationId, projectId, jobId), |
| 53 | + statusCode: http.StatusBadRequest, |
| 54 | + expectedCalledTimes: 1, |
| 55 | + expectedError: "Error: [The job 87d485b4-73fc-4a7f-bb03-720f4672947e has requested to cancel]", |
| 56 | + returnBody: `{ |
| 57 | + "errors": [ |
| 58 | + { |
| 59 | + "message": "The job 87d485b4-73fc-4a7f-bb03-720f4672947e has requested to cancel", |
| 60 | + "reason": "Requested" |
| 61 | + } |
| 62 | + ] |
| 63 | + }`, |
| 64 | + }, |
| 65 | + "correct command with error response 2": { |
| 66 | + executeCommand: fmt.Sprintf("import job cancel --organization-id=%s --project-id=%s %s", organizationId, projectId, jobId), |
| 67 | + statusCode: http.StatusMethodNotAllowed, |
| 68 | + expectedCalledTimes: 1, |
| 69 | + expectedError: "Error: [string]", |
| 70 | + returnBody: `{ |
| 71 | + "errors": [ |
| 72 | + { |
| 73 | + "message": "string", |
| 74 | + "reason": "string", |
| 75 | + "field": "string" |
| 76 | + } |
| 77 | + ] |
| 78 | + }`, |
| 79 | + }, |
| 80 | + "incorrect command with missing organization id": { |
| 81 | + executeCommand: fmt.Sprintf("import job cancel --project-id=%s %s", projectId, jobId), |
| 82 | + statusCode: http.StatusBadRequest, |
| 83 | + expectedCalledTimes: 0, |
| 84 | + expectedError: "Error: required flag(s) \"organization-id\" not set", |
| 85 | + returnBody: ``, |
| 86 | + }, |
| 87 | + "incorrect command with missing project id": { |
| 88 | + executeCommand: fmt.Sprintf("import job cancel --organization-id=%s %s", organizationId, jobId), |
| 89 | + statusCode: http.StatusBadRequest, |
| 90 | + expectedCalledTimes: 0, |
| 91 | + expectedError: "Error: required flag(s) \"project-id\" not set", |
| 92 | + returnBody: ``, |
| 93 | + }, |
| 94 | + "incorrect command with missing job id": { |
| 95 | + executeCommand: fmt.Sprintf("import job cancel --organization-id=%s --project-id=%s", organizationId, projectId), |
| 96 | + statusCode: http.StatusBadRequest, |
| 97 | + expectedCalledTimes: 0, |
| 98 | + expectedError: "Error: accepts 1 arg(s), received 0", |
| 99 | + returnBody: ``, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for name, testCase := range testCases { |
| 104 | + t.Run(name, func(t *testing.T) { |
| 105 | + helper := testutils.NewAuraTestHelper(t) |
| 106 | + defer helper.Close() |
| 107 | + |
| 108 | + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v2beta1/organizations/%s/projects/%s/import/jobs/%s/cancellation", organizationId, projectId, jobId), testCase.statusCode, testCase.returnBody) |
| 109 | + |
| 110 | + helper.SetConfigValue("aura.beta-enabled", true) |
| 111 | + |
| 112 | + helper.ExecuteCommand(testCase.executeCommand) |
| 113 | + |
| 114 | + mockHandler.AssertCalledTimes(testCase.expectedCalledTimes) |
| 115 | + if testCase.expectedCalledTimes > 0 { |
| 116 | + mockHandler.AssertCalledWithMethod(http.MethodPost) |
| 117 | + } |
| 118 | + |
| 119 | + helper.AssertErr(testCase.expectedError) |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments