Skip to content

Commit

Permalink
first sample test
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmimsft committed Nov 27, 2024
1 parent dcbf17a commit 1567ac9
Showing 1 changed file with 168 additions and 0 deletions.
168 changes: 168 additions & 0 deletions pkg/corerp/api/v20231001preview/applicationsclient_fake_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package v20231001preview_test

import (
"context"
"net/http"
"testing"

armpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy"
azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/stretchr/testify/require"

v20231001preview "github.com/radius-project/radius/pkg/corerp/api/v20231001preview"
"github.com/radius-project/radius/pkg/corerp/api/v20231001preview/fake"
)

func TestApplicationsServer(t *testing.T) {
// Initialize the fake ApplicationsServer with method implementations.
srv := &fake.ApplicationsServer{
CreateOrUpdate: func(
ctx context.Context,
applicationName string,
resource v20231001preview.ApplicationResource,
options *v20231001preview.ApplicationsClientCreateOrUpdateOptions,
) (resp azfake.Responder[v20231001preview.ApplicationsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) {
response := v20231001preview.ApplicationsClientCreateOrUpdateResponse{
ApplicationResource: resource,
}
resp.SetResponse(http.StatusOK, response, nil)
return
},
Get: func(
ctx context.Context,
applicationName string,
options *v20231001preview.ApplicationsClientGetOptions,
) (resp azfake.Responder[v20231001preview.ApplicationsClientGetResponse], errResp azfake.ErrorResponder) {
response := v20231001preview.ApplicationsClientGetResponse{
ApplicationResource: v20231001preview.ApplicationResource{
Name: &applicationName,
},
}
resp.SetResponse(http.StatusOK, response, nil)
return
},
Delete: func(
ctx context.Context,
applicationName string,
options *v20231001preview.ApplicationsClientDeleteOptions,
) (resp azfake.Responder[v20231001preview.ApplicationsClientDeleteResponse], errResp azfake.ErrorResponder) {
response := v20231001preview.ApplicationsClientDeleteResponse{}

resp.SetResponse(http.StatusOK, response, nil)
return
},
GetGraph: func(
ctx context.Context,
applicationName string,
body map[string]any,
options *v20231001preview.ApplicationsClientGetGraphOptions,
) (resp azfake.Responder[v20231001preview.ApplicationsClientGetGraphResponse], errResp azfake.ErrorResponder) {
response := v20231001preview.ApplicationsClientGetGraphResponse{
ApplicationGraphResponse: v20231001preview.ApplicationGraphResponse{
Resources: []*v20231001preview.ApplicationGraphResource{
{
Name: to.Ptr("testApplication"),
ID: to.Ptr("/planes/radius/resourceGroups/radius-rg/providers/Applications.Core/applications/testApplication"),
Connections: []*v20231001preview.ApplicationGraphConnection{
{
Direction: to.Ptr(v20231001preview.DirectionOutbound),
ID: to.Ptr("/planes/radius/resourceGroups/radius-rg/providers/Applications.Core/applications/testApplication/connections/conn1"),
},
},
},
},
},
}

resp.SetResponse(http.StatusOK, response, nil)
return
},
NewListByScopePager: func(
options *v20231001preview.ApplicationsClientListByScopeOptions,
) (resp azfake.PagerResponder[v20231001preview.ApplicationsClientListByScopeResponse]) {
response := v20231001preview.ApplicationsClientListByScopeResponse{
ApplicationResourceListResult: v20231001preview.ApplicationResourceListResult{
Value: []*v20231001preview.ApplicationResource{
{
Name: to.Ptr("testApplication"),
},
},
}}

resp.AddPage(http.StatusOK, response, nil)
return
},
Update: func(
ctx context.Context,
applicationName string,
properties v20231001preview.ApplicationResourceUpdate,
options *v20231001preview.ApplicationsClientUpdateOptions,
) (resp azfake.Responder[v20231001preview.ApplicationsClientUpdateResponse], errResp azfake.ErrorResponder) {
response := v20231001preview.ApplicationsClientUpdateResponse{
ApplicationResource: v20231001preview.ApplicationResource{
Name: &applicationName,
},
}
resp.SetResponse(http.StatusOK, response, nil)
return
},
}

// Create a fake transport using the ApplicationsServer.
// Set up client options with the fake transport.
transport := fake.NewApplicationsServerTransport(srv)
clientOptions := &armpolicy.ClientOptions{ClientOptions: policy.ClientOptions{
Transport: transport,
}}

// Create the ApplicationsClient with the fake transport.
client, err := v20231001preview.NewApplicationsClient("https://MyCompany.com/fake/thing", &azfake.TokenCredential{}, clientOptions)
require.NoError(t, err)

ctx := context.Background()
applicationName := "testApplication"
resource := v20231001preview.ApplicationResource{
Name: &applicationName,
}

// Call CreateOrUpdate.
_, err = client.CreateOrUpdate(ctx, applicationName, resource, nil)
require.NoError(t, err)

// Call Get.
getResp, err := client.Get(ctx, applicationName, nil)
require.NoError(t, err)
require.Equal(t, applicationName, *getResp.Name)

// Call Delete.
_, err = client.Delete(ctx, applicationName, nil)
require.NoError(t, err)

// Call GetGraph.
getGraphResp, err := client.GetGraph(ctx, applicationName, nil, nil)
require.NoError(t, err)
require.Len(t, getGraphResp.Resources, 1)
require.Equal(t, applicationName, *getGraphResp.Resources[0].Name)
require.Len(t, getGraphResp.Resources[0].Connections, 1)
require.Equal(t, v20231001preview.DirectionOutbound, *getGraphResp.Resources[0].Connections[0].Direction)

// Call ListByScope.
pager := client.NewListByScopePager(nil)
var resources []*v20231001preview.ApplicationResource

for pager.More() {
page, err := pager.NextPage(ctx)
require.NoError(t, err)
resources = append(resources, page.ApplicationResourceListResult.Value...)
}

require.Len(t, resources, 1)
require.Equal(t, applicationName, *resources[0].Name)

// Call Update.
updateResp, err := client.Update(ctx, applicationName, v20231001preview.ApplicationResourceUpdate{}, nil)
require.NoError(t, err)
require.Equal(t, applicationName, *updateResp.Name)
}

0 comments on commit 1567ac9

Please sign in to comment.