github.com/Axway/agent-sdk@v1.1.101/pkg/agent/handler/apiservice_test.go (about) 1 package handler 2 3 import ( 4 "testing" 5 6 agentcache "github.com/Axway/agent-sdk/pkg/agent/cache" 7 apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 8 catalog "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/catalog/v1alpha1" 9 management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" 10 "github.com/Axway/agent-sdk/pkg/apic/definitions" 11 "github.com/Axway/agent-sdk/pkg/config" 12 "github.com/Axway/agent-sdk/pkg/watchmanager/proto" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestNewAPISvcHandler(t *testing.T) { 17 tests := []struct { 18 name string 19 hasError bool 20 resource *apiv1.ResourceInstance 21 action proto.Event_Type 22 }{ 23 { 24 name: "should save a ResourceClient that has an externalAPIID attribute, and no externalAPIPrimaryKey attribute", 25 hasError: false, 26 action: proto.Event_CREATED, 27 resource: &apiv1.ResourceInstance{ 28 ResourceMeta: apiv1.ResourceMeta{ 29 GroupVersionKind: apiv1.GroupVersionKind{ 30 GroupKind: apiv1.GroupKind{ 31 Kind: management.APIServiceGVK().Kind, 32 }, 33 }, 34 SubResources: map[string]interface{}{ 35 definitions.XAgentDetails: map[string]interface{}{ 36 definitions.AttrExternalAPIID: "123", 37 definitions.AttrExternalAPIName: "name", 38 }, 39 }, 40 }, 41 }, 42 }, 43 { 44 name: "should save a ResourceClient that has an externalAPIID attribute, and has the externalAPIPrimaryKey attribute", 45 hasError: false, 46 action: proto.Event_UPDATED, 47 resource: &apiv1.ResourceInstance{ 48 ResourceMeta: apiv1.ResourceMeta{ 49 GroupVersionKind: apiv1.GroupVersionKind{ 50 GroupKind: apiv1.GroupKind{ 51 Kind: management.APIServiceGVK().Kind, 52 }, 53 }, 54 SubResources: map[string]interface{}{ 55 definitions.XAgentDetails: map[string]interface{}{ 56 definitions.AttrExternalAPIID: "123", 57 definitions.AttrExternalAPIPrimaryKey: "abc", 58 definitions.AttrExternalAPIName: "name", 59 }, 60 }, 61 }, 62 }, 63 }, 64 { 65 name: "should fail to save the item to the cache when the externalAPIID attribute is not found", 66 hasError: true, 67 action: proto.Event_CREATED, 68 resource: &apiv1.ResourceInstance{ 69 ResourceMeta: apiv1.ResourceMeta{ 70 GroupVersionKind: apiv1.GroupVersionKind{ 71 GroupKind: apiv1.GroupKind{ 72 Kind: management.APIServiceGVK().Kind, 73 }, 74 }, 75 SubResources: map[string]interface{}{ 76 definitions.XAgentDetails: map[string]interface{}{}, 77 }, 78 }, 79 }, 80 }, 81 { 82 name: "should handle a delete action", 83 hasError: false, 84 action: proto.Event_DELETED, 85 resource: &apiv1.ResourceInstance{ 86 ResourceMeta: apiv1.ResourceMeta{ 87 GroupVersionKind: apiv1.GroupVersionKind{ 88 GroupKind: apiv1.GroupKind{ 89 Kind: management.APIServiceGVK().Kind, 90 }, 91 }, 92 SubResources: map[string]interface{}{ 93 definitions.XAgentDetails: map[string]interface{}{ 94 definitions.AttrExternalAPIID: "123", 95 definitions.AttrExternalAPIName: "name", 96 }, 97 }, 98 }, 99 }, 100 }, 101 { 102 name: "should return nil when the ResourceClient kind is not an APIService", 103 hasError: false, 104 action: proto.Event_CREATED, 105 resource: &apiv1.ResourceInstance{ 106 ResourceMeta: apiv1.ResourceMeta{ 107 GroupVersionKind: apiv1.GroupVersionKind{ 108 GroupKind: apiv1.GroupKind{ 109 Kind: catalog.CategoryGVK().Kind, 110 }, 111 }, 112 SubResources: map[string]interface{}{}, 113 }, 114 }, 115 }, 116 } 117 cacheManager := agentcache.NewAgentCacheManager(&config.CentralConfiguration{}, false) 118 119 for _, tc := range tests { 120 t.Run(tc.name, func(t *testing.T) { 121 handler := NewAPISvcHandler(cacheManager, "") 122 123 err := handler.Handle(NewEventContext(tc.action, nil, tc.resource.Kind, tc.resource.Name), nil, tc.resource) 124 if tc.hasError { 125 assert.NotNil(t, err) 126 } else { 127 assert.Nil(t, err) 128 } 129 }) 130 } 131 132 }