github.com/Axway/agent-sdk@v1.1.101/pkg/agent/handler/instance_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/config" 11 "github.com/Axway/agent-sdk/pkg/watchmanager/proto" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestNewInstanceHandler(t *testing.T) { 16 tests := []struct { 17 name string 18 hasError bool 19 resource *apiv1.ResourceInstance 20 action proto.Event_Type 21 }{ 22 { 23 name: "should save an API Service Instance", 24 hasError: false, 25 action: proto.Event_CREATED, 26 resource: &apiv1.ResourceInstance{ 27 ResourceMeta: apiv1.ResourceMeta{ 28 Name: "name", 29 Title: "title", 30 Metadata: apiv1.Metadata{ 31 ID: "123", 32 }, 33 GroupVersionKind: apiv1.GroupVersionKind{ 34 GroupKind: apiv1.GroupKind{ 35 Kind: management.APIServiceInstanceGVK().Kind, 36 }, 37 }, 38 }, 39 }, 40 }, 41 { 42 name: "should update an API Service Instance", 43 hasError: false, 44 action: proto.Event_UPDATED, 45 resource: &apiv1.ResourceInstance{ 46 ResourceMeta: apiv1.ResourceMeta{ 47 Name: "name", 48 Title: "title", 49 Metadata: apiv1.Metadata{ 50 ID: "123", 51 }, 52 GroupVersionKind: apiv1.GroupVersionKind{ 53 GroupKind: apiv1.GroupKind{ 54 Kind: management.APIServiceInstanceGVK().Kind, 55 }, 56 }, 57 }, 58 }, 59 }, 60 { 61 name: "should add another API Service Instance", 62 hasError: false, 63 action: proto.Event_CREATED, 64 resource: &apiv1.ResourceInstance{ 65 ResourceMeta: apiv1.ResourceMeta{ 66 Name: "name", 67 Title: "title", 68 Metadata: apiv1.Metadata{ 69 ID: "1234", 70 }, 71 GroupVersionKind: apiv1.GroupVersionKind{ 72 GroupKind: apiv1.GroupKind{ 73 Kind: management.APIServiceInstanceGVK().Kind, 74 }, 75 }, 76 }, 77 }, 78 }, 79 { 80 name: "should update an API Service Instance subresource", 81 hasError: false, 82 action: proto.Event_SUBRESOURCEUPDATED, 83 resource: &apiv1.ResourceInstance{ 84 ResourceMeta: apiv1.ResourceMeta{ 85 Name: "name", 86 Title: "title", 87 Metadata: apiv1.Metadata{ 88 ID: "12345", 89 }, 90 GroupVersionKind: apiv1.GroupVersionKind{ 91 GroupKind: apiv1.GroupKind{ 92 Kind: management.APIServiceInstanceGVK().Kind, 93 }, 94 }, 95 }, 96 }, 97 }, 98 { 99 name: "should delete an API Service Instance", 100 hasError: false, 101 action: proto.Event_DELETED, 102 resource: &apiv1.ResourceInstance{ 103 ResourceMeta: apiv1.ResourceMeta{ 104 Name: "name", 105 Title: "title", 106 Metadata: apiv1.Metadata{ 107 ID: "123", 108 }, 109 GroupVersionKind: apiv1.GroupVersionKind{ 110 GroupKind: apiv1.GroupKind{ 111 Kind: management.APIServiceInstanceGVK().Kind, 112 }, 113 }, 114 }, 115 }, 116 }, 117 { 118 name: "should return nil when the kind is not an API Service Instance", 119 hasError: false, 120 action: proto.Event_UPDATED, 121 resource: &apiv1.ResourceInstance{ 122 ResourceMeta: apiv1.ResourceMeta{ 123 Name: "name", 124 Title: "title", 125 Metadata: apiv1.Metadata{ 126 ID: "123", 127 }, 128 GroupVersionKind: apiv1.GroupVersionKind{ 129 GroupKind: apiv1.GroupKind{ 130 Kind: catalog.CategoryGVK().Kind, 131 }, 132 }, 133 }, 134 }, 135 }, 136 } 137 138 cacheManager := agentcache.NewAgentCacheManager(&config.CentralConfiguration{}, false) 139 for _, tc := range tests { 140 t.Run(tc.name, func(t *testing.T) { 141 handler := NewInstanceHandler(cacheManager, "") 142 err := handler.Handle(NewEventContext(tc.action, nil, tc.resource.Kind, tc.resource.Name), nil, tc.resource) 143 if tc.hasError { 144 assert.NotNil(t, err) 145 } else { 146 if tc.resource.ResourceMeta.GroupVersionKind.GroupKind.Kind == management.APIServiceInstanceGVK().Kind && 147 (tc.action == proto.Event_CREATED || 148 tc.action == proto.Event_UPDATED || 149 tc.action == proto.Event_SUBRESOURCEUPDATED) { 150 v, err := cacheManager.GetAPIServiceInstanceByID(tc.resource.Metadata.ID) 151 assert.NoError(t, err) 152 assert.NotNil(t, v) 153 } 154 assert.Nil(t, err) 155 } 156 }) 157 } 158 159 }