github.com/Axway/agent-sdk@v1.1.101/pkg/agent/instancevalidator_test.go (about) 1 package agent 2 3 import ( 4 "net/http" 5 "testing" 6 7 "github.com/Axway/agent-sdk/pkg/apic/definitions" 8 9 agentcache "github.com/Axway/agent-sdk/pkg/agent/cache" 10 "github.com/Axway/agent-sdk/pkg/api" 11 "github.com/Axway/agent-sdk/pkg/apic" 12 v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 13 "github.com/Axway/agent-sdk/pkg/config" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func setupCache(externalAPIID, externalAPIName string) (*v1.ResourceInstance, *v1.ResourceInstance) { 18 svc := &v1.ResourceInstance{ 19 ResourceMeta: v1.ResourceMeta{ 20 Name: "service", 21 Metadata: v1.Metadata{ 22 ID: "svc-" + externalAPIID, 23 }, 24 SubResources: map[string]interface{}{ 25 definitions.XAgentDetails: map[string]interface{}{ 26 definitions.AttrExternalAPIID: externalAPIID, 27 definitions.AttrExternalAPIPrimaryKey: "primary-" + externalAPIID, 28 definitions.AttrExternalAPIName: externalAPIName, 29 }, 30 }, 31 }, 32 } 33 instance := &v1.ResourceInstance{ 34 ResourceMeta: v1.ResourceMeta{ 35 Name: "instance", 36 Metadata: v1.Metadata{ 37 ID: "instance-" + externalAPIID, 38 }, 39 SubResources: map[string]interface{}{ 40 definitions.XAgentDetails: map[string]interface{}{ 41 definitions.AttrExternalAPIID: externalAPIID, 42 definitions.AttrExternalAPIPrimaryKey: "primary-" + externalAPIID, 43 definitions.AttrExternalAPIName: externalAPIName, 44 }, 45 }, 46 }, 47 } 48 49 agent.cacheManager = agentcache.NewAgentCacheManager(&config.CentralConfiguration{}, false) 50 agent.cacheManager.AddAPIService(svc) 51 agent.cacheManager.AddAPIServiceInstance(instance) 52 return svc, instance 53 } 54 55 func setupAPICClient(mockResponse []api.MockResponse) { 56 client, httpClient := apic.GetTestServiceClient() 57 httpClient.SetResponses(mockResponse) 58 agent.apicClient = client 59 } 60 61 func setupAPIValidator(apiValidation bool) { 62 setAPIValidator(func(apiID, stageName string) bool { 63 return apiValidation 64 }) 65 } 66 67 func TestValidatorAPIExistsOnDataplane(t *testing.T) { 68 // Setup 69 instanceValidator := newInstanceValidator() 70 setupCache("12345", "test") 71 setupAPIValidator(true) 72 instanceValidator.Execute() 73 i, err := agent.cacheManager.GetAPIServiceInstanceByID("instance-12345") 74 assert.Nil(t, err) 75 assert.NotNil(t, i) 76 77 s := agent.cacheManager.GetAPIServiceWithPrimaryKey("primary-12345") 78 assert.NotNil(t, s) 79 } 80 81 func TestValidatorAPIDoesExistsDeleteService(t *testing.T) { 82 // Setup 83 instanceValidator := newInstanceValidator() 84 setupCache("12345", "test") 85 setupAPICClient([]api.MockResponse{ 86 { 87 RespCode: http.StatusOK, // get instance finalizers 88 }, 89 { 90 RespCode: http.StatusNoContent, // delete instance 91 }, 92 { 93 RespCode: http.StatusNoContent, // delete service 94 }, 95 }) 96 setupAPIValidator(false) 97 instanceValidator.Execute() 98 i, err := agent.cacheManager.GetAPIServiceInstanceByID("instance-12345") 99 assert.NotNil(t, err) 100 assert.Nil(t, i) 101 102 s := agent.cacheManager.GetAPIServiceWithPrimaryKey("primary-12345") 103 assert.Nil(t, s) 104 } 105 106 func TestValidatorAPIDoesExistsDeleteInstance(t *testing.T) { 107 instanceValidator := newInstanceValidator() 108 109 setupCache("12345", "test") 110 instance := &v1.ResourceInstance{ 111 ResourceMeta: v1.ResourceMeta{ 112 Name: "instance123456", 113 Metadata: v1.Metadata{ 114 ID: "instance-" + "123456", 115 }, 116 SubResources: map[string]interface{}{ 117 definitions.XAgentDetails: map[string]interface{}{ 118 definitions.AttrExternalAPIID: "123456", 119 definitions.AttrExternalAPIPrimaryKey: "primary-12345", 120 definitions.AttrExternalAPIName: "test", 121 }, 122 }, 123 }, 124 } 125 agent.cacheManager.AddAPIServiceInstance(instance) 126 setupAPICClient([]api.MockResponse{ 127 { 128 RespCode: http.StatusOK, // get finalizers 129 }, 130 { 131 RespCode: http.StatusNoContent, // delete instance 132 }, 133 }) 134 setAPIValidator(func(apiID, stageName string) bool { 135 return apiID != "12345" 136 }) 137 instanceValidator.Execute() 138 i, err := agent.cacheManager.GetAPIServiceInstanceByID("instance-12345") 139 assert.NotNil(t, err) 140 assert.Nil(t, i) 141 142 s := agent.cacheManager.GetAPIServiceWithPrimaryKey("primary-12345") 143 assert.NotNil(t, s) 144 }