-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GetDocuments API returning document summaries (#909)
- Loading branch information
1 parent
4226417
commit 275cdff
Showing
11 changed files
with
1,357 additions
and
903 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
//go:build integration | ||
|
||
/* | ||
* Copyright 2024 The Yorkie Authors. All rights reserved. | ||
* | ||
* 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 integration | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/yorkie-team/yorkie/api/types" | ||
"github.com/yorkie-team/yorkie/client" | ||
"github.com/yorkie-team/yorkie/pkg/document" | ||
"github.com/yorkie-team/yorkie/test/helper" | ||
) | ||
|
||
// documentSummaries represents a list of document documentSummaries. | ||
type documentSummaries struct { | ||
Documents []*types.DocumentSummary `json:"documents"` | ||
} | ||
|
||
// documentSummary represents a summary of a document. | ||
type documentSummary struct { | ||
Document *types.DocumentSummary `json:"document"` | ||
} | ||
|
||
func TestRESTAPI(t *testing.T) { | ||
project, docs := createProjectAndDocuments(t, 3) | ||
|
||
t.Run("document retrieval test", func(t *testing.T) { | ||
httpClient := http.Client{} | ||
|
||
url := fmt.Sprintf("http://%s/yorkie.v1.AdminService/GetDocument", defaultServer.RPCAddr()) | ||
req, err := http.NewRequest("POST", url, strings.NewReader( | ||
fmt.Sprintf(`{"project_name": "%s", "document_key": "%s"}`, project.Name, docs[0].Key()), | ||
)) | ||
assert.NoError(t, err) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", project.SecretKey) | ||
|
||
res, err := httpClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
|
||
resBody, err := io.ReadAll(res.Body) | ||
assert.NoError(t, err) | ||
|
||
summary := &documentSummary{} | ||
assert.NoError(t, json.Unmarshal(resBody, summary)) | ||
assert.Equal(t, docs[0].Key(), summary.Document.Key) | ||
}) | ||
|
||
t.Run("bulk document retrieval test", func(t *testing.T) { | ||
httpClient := http.Client{} | ||
|
||
url := fmt.Sprintf("http://%s/yorkie.v1.AdminService/GetDocuments", defaultServer.RPCAddr()) | ||
req, err := http.NewRequest("POST", url, strings.NewReader( | ||
fmt.Sprintf(`{"project_name": "%s", "document_keys": ["%s", "%s"]}`, project.Name, docs[0].Key(), docs[1].Key()), | ||
)) | ||
assert.NoError(t, err) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", project.SecretKey) | ||
|
||
res, err := httpClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
|
||
resBody, err := io.ReadAll(res.Body) | ||
assert.NoError(t, err) | ||
|
||
summaries := &documentSummaries{} | ||
assert.NoError(t, json.Unmarshal(resBody, summaries)) | ||
assert.Len(t, summaries.Documents, 2) | ||
}) | ||
|
||
t.Run("list documents test", func(t *testing.T) { | ||
httpClient := http.Client{} | ||
|
||
url := fmt.Sprintf("http://%s/yorkie.v1.AdminService/ListDocuments", defaultServer.RPCAddr()) | ||
req, err := http.NewRequest("POST", url, strings.NewReader( | ||
fmt.Sprintf(`{"project_name": "%s", "document_key": "test"}`, project.Name), | ||
)) | ||
assert.NoError(t, err) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", project.SecretKey) | ||
|
||
res, err := httpClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
|
||
resBody, err := io.ReadAll(res.Body) | ||
assert.NoError(t, err) | ||
|
||
summaries := &documentSummaries{} | ||
assert.NoError(t, json.Unmarshal(resBody, summaries)) | ||
assert.Len(t, summaries.Documents, 3) | ||
}) | ||
} | ||
|
||
func createProjectAndDocuments(t *testing.T, count int) (*types.Project, []*document.Document) { | ||
ctx := context.Background() | ||
project, err := defaultServer.CreateProject(ctx, t.Name()) | ||
assert.NoError(t, err) | ||
|
||
cli, err := client.Dial(defaultServer.RPCAddr(), client.WithAPIKey(project.PublicKey)) | ||
assert.NoError(t, err) | ||
assert.NoError(t, cli.Activate(ctx)) | ||
|
||
var docs []*document.Document | ||
for i := 0; i < count; i++ { | ||
doc := document.New(helper.TestDocKey(t, i)) | ||
assert.NoError(t, cli.Attach(ctx, doc)) | ||
docs = append(docs, doc) | ||
} | ||
|
||
return project, docs | ||
} |