Skip to content

Commit

Permalink
fix: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
elle-le committed Nov 22, 2024
1 parent 870b831 commit 0c5b64f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
6 changes: 3 additions & 3 deletions internal/clients/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ func (s *SubscriptionTypeMapper) ConvertToCreatePayload(cr *v1alpha1.Subscriptio

func (s *SubscriptionTypeMapper) ConvertToClientParams(cr *v1alpha1.Subscription) map[string]map[string]interface{} {
type subparams map[string]map[string]interface{}
var sp subparams
var subscriptionParams subparams

err := json.Unmarshal(cr.Spec.ForProvider.SubscriptionParameters, &sp)
err := json.Unmarshal(cr.Spec.ForProvider.SubscriptionParameters, &subscriptionParams)
if err != nil {
return nil
}

return sp
return subscriptionParams
}

func (s *SubscriptionTypeMapper) ConvertToUpdatePayload(cr *v1alpha1.Subscription) SubscriptionPut {
Expand Down
50 changes: 44 additions & 6 deletions internal/clients/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package subscription

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

Expand Down Expand Up @@ -103,6 +104,14 @@ func TestSubscriptionApiHandler_CreateSubscription(t *testing.T) {
appName: "name1",
CreateSubscriptionRequestPayload: saas_client.CreateSubscriptionRequestPayload{
PlanName: internal.Ptr("plan2"),
SubscriptionParams: map[string]map[string]interface{}{
"param1": {
"key1": "value1",
},
"param2": {
"key2": "value2",
},
},
},
},
mockSubscriptionApi: apiMockPOST(
Expand All @@ -117,6 +126,13 @@ func TestSubscriptionApiHandler_CreateSubscription(t *testing.T) {
appName: "name1",
CreateSubscriptionRequestPayload: saas_client.CreateSubscriptionRequestPayload{
PlanName: internal.Ptr("plan2"),
SubscriptionParams: map[string]map[string]interface{}{
"param1": {
"key1": "value1",
},
"param2": {
"key2": "value2",
},
},
},
mockSubscriptionApi: apiMockPOST(
Expand Down Expand Up @@ -247,7 +263,8 @@ func TestSubscriptionApiHandler_UpdateSubscription(t *testing.T) {
}

func TestSubscriptionTypeMapper_ConvertToCreatePayload(t *testing.T) {
cr := NewSubscription("someName", "name1", "plan2")
raw := json.RawMessage(`{"name": "John", "age": 30}`)
cr := NewSubscription("someName", "name1", "plan2", raw)

uut := NewSubscriptionTypeMapper()
mapped := uut.ConvertToCreatePayload(cr)
Expand All @@ -258,8 +275,27 @@ func TestSubscriptionTypeMapper_ConvertToCreatePayload(t *testing.T) {

}

func TestSubscriptionTypeMapper_ConvertToClientParams(t *testing.T) {
raw := json.RawMessage(`{"param1": {"key1": "value1"}, "param2": {"key2": "value2"}}`)
cr := &v1alpha1.Subscription{
Spec: v1alpha1.SubscriptionSpec{
ForProvider: v1alpha1.SubscriptionParameters{
SubscriptionParameters: raw,
},
},
}

uut := NewSubscriptionTypeMapper()
params := uut.ConvertToClientParams(cr)

assert.NotNil(t, params)
assert.Equal(t, "value1", params["param1"]["key1"])
assert.Equal(t, "value2", params["param2"]["key2"])
}

func TestSubscriptionTypeMapper_IsSynced(t *testing.T) {
cr := NewSubscription("someName", "name1", "plan2")
raw := json.RawMessage(`{"name": "John", "age": 30}`)
cr := NewSubscription("someName", "name1", "plan2", raw)
get := &SubscriptionGet{
AppName: internal.Ptr("anotherName"),
PlanName: internal.Ptr("anotherPlan"),
Expand Down Expand Up @@ -310,14 +346,15 @@ func TestSubscriptionTypeMapper_IsAvailable(t *testing.T) {
}

func TestSubscriptionTypeMapper_SyncStatus(t *testing.T) {
raw := json.RawMessage(`{"name": "John", "age": 30}`)
tests := map[string]struct {
cr *v1alpha1.Subscription
apiRes *SubscriptionGet

expectedCr *v1alpha1.Subscription
}{
"SetState": {
cr: NewSubscription("someName", "name1", "plan2"),
cr: NewSubscription("someName", "name1", "plan2", raw),
apiRes: &SubscriptionGet{
AppName: internal.Ptr("name1"),
PlanName: internal.Ptr("plan2"),
Expand Down Expand Up @@ -374,13 +411,14 @@ func apiMockDELETE(statusCode int, apiError error) *MockSubscriptionOperationsCo
return apiMock
}

func NewSubscription(crName string, appName string, planName string) *v1alpha1.Subscription {
func NewSubscription(crName string, appName string, planName string, subscriptionParameters json.RawMessage) *v1alpha1.Subscription {
cr := &v1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{Name: crName},
Spec: v1alpha1.SubscriptionSpec{
ForProvider: v1alpha1.SubscriptionParameters{
AppName: appName,
PlanName: planName,
AppName: appName,
PlanName: planName,
SubscriptionParameters: subscriptionParameters,
},
},
}
Expand Down

0 comments on commit 0c5b64f

Please sign in to comment.