-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
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,49 @@ | ||
package lib | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
type IBlueprints interface { | ||
GetGroupByCategory(ctx context.Context) (BlueprintGroupByCategoryResponse, error) | ||
GetByTemplateID(ctx context.Context, templateID string) (BlueprintByTemplateIdResponse, error) | ||
} | ||
|
||
type BlueprintService service | ||
|
||
func (b *BlueprintService) GetGroupByCategory(ctx context.Context) (BlueprintGroupByCategoryResponse, error) { | ||
var resp BlueprintGroupByCategoryResponse | ||
URL := b.client.config.BackendURL.JoinPath("blueprints", "group-by-category") | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
_, err = b.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (b *BlueprintService) GetByTemplateID(ctx context.Context, templateID string) (BlueprintByTemplateIdResponse, error) { | ||
var resp BlueprintByTemplateIdResponse | ||
URL := b.client.config.BackendURL.JoinPath("blueprints", templateID) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
_, err = b.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
var _ IBlueprints = &BlueprintService{} |
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,70 @@ | ||
package lib_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/novuhq/go-novu/lib" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func responseStringToStruct(str string, s interface{}) io.Reader { | ||
bb := []byte(str) | ||
json.Unmarshal(bb, s) | ||
return bytes.NewReader(bb) | ||
} | ||
|
||
const templateID = "650ae12e61c036a0a5419480" | ||
|
||
var groupByCategory = `{ | ||
"general": [ | ||
{} | ||
], | ||
"popular": {} | ||
}` | ||
|
||
func TestBlueprintService_GetGroupByCategory_Success(t *testing.T) { | ||
var expectedResponse lib.BlueprintGroupByCategoryResponse | ||
responseStringToStruct(groupByCategory, &expectedResponse) | ||
|
||
httpServer := createTestServer(t, TestServerOptions[io.Reader, *lib.BlueprintGroupByCategoryResponse]{ | ||
expectedURLPath: fmt.Sprintf("/v1/blueprints/group-by-category"), | ||
expectedSentMethod: http.MethodGet, | ||
expectedSentBody: http.NoBody, | ||
responseStatusCode: http.StatusOK, | ||
responseBody: &expectedResponse, | ||
}) | ||
|
||
ctx := context.Background() | ||
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)}) | ||
resp, err := c.BlueprintApi.GetGroupByCategory(ctx) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, resp, expectedResponse) | ||
} | ||
|
||
func TestBlueprintService_GetByTemplateID_Success(t *testing.T) { | ||
var expectedResponse lib.BlueprintByTemplateIdResponse | ||
fileToStruct(filepath.Join("../testdata", "blueprint_response.json"), &expectedResponse) | ||
|
||
httpServer := createTestServer(t, TestServerOptions[io.Reader, *lib.BlueprintByTemplateIdResponse]{ | ||
expectedURLPath: fmt.Sprintf("/v1/blueprints/%s", templateID), | ||
expectedSentMethod: http.MethodGet, | ||
expectedSentBody: http.NoBody, | ||
responseStatusCode: http.StatusOK, | ||
responseBody: &expectedResponse, | ||
}) | ||
|
||
ctx := context.Background() | ||
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)}) | ||
resp, err := c.BlueprintApi.GetByTemplateID(ctx, templateID) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, resp, expectedResponse) | ||
} |
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,31 @@ | ||
{ | ||
"_id": "string", | ||
"name": "string", | ||
"description": "string", | ||
"active": true, | ||
"draft": true, | ||
"preferenceSettings": {}, | ||
"critical": true, | ||
"tags": [ | ||
"string" | ||
], | ||
"steps": [ | ||
{} | ||
], | ||
"_organizationId": "string", | ||
"_creatorId": "string", | ||
"_environmentId": "string", | ||
"triggers": [ | ||
{} | ||
], | ||
"_notificationGroupId": "string", | ||
"_parentId": "string", | ||
"deleted": true, | ||
"deletedAt": "string", | ||
"deletedBy": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"notificationGroup": {}, | ||
"isBlueprint": true, | ||
"blueprintId": "string" | ||
} |