|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" |
| 8 | + management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" |
| 9 | + defs "github.com/Axway/agent-sdk/pkg/apic/definitions" |
| 10 | + "github.com/Axway/agent-sdk/pkg/config" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func makeRI(group, kind, scopeKind, scopeName, name, id string, modTime time.Time) *v1.ResourceInstance { |
| 15 | + return &v1.ResourceInstance{ |
| 16 | + ResourceMeta: v1.ResourceMeta{ |
| 17 | + GroupVersionKind: v1.GroupVersionKind{ |
| 18 | + GroupKind: v1.GroupKind{ |
| 19 | + Group: group, |
| 20 | + Kind: kind, |
| 21 | + }, |
| 22 | + APIVersion: "v1alpha1", |
| 23 | + }, |
| 24 | + Metadata: v1.Metadata{ |
| 25 | + ID: id, |
| 26 | + Scope: v1.MetadataScope{ |
| 27 | + Kind: scopeKind, |
| 28 | + Name: scopeName, |
| 29 | + }, |
| 30 | + Audit: v1.AuditMetadata{ |
| 31 | + ModifyTimestamp: v1.Time(modTime), |
| 32 | + }, |
| 33 | + }, |
| 34 | + Name: name, |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func makeAPIServiceRI(scopeName, name, apiID string, modTime time.Time) *v1.ResourceInstance { |
| 40 | + ri := makeRI("management", management.APIServiceGVK().Kind, "Environment", scopeName, name, apiID, modTime) |
| 41 | + ri.SubResources = map[string]interface{}{ |
| 42 | + defs.XAgentDetails: map[string]interface{}{ |
| 43 | + defs.AttrExternalAPIID: apiID, |
| 44 | + defs.AttrExternalAPIName: name, |
| 45 | + }, |
| 46 | + } |
| 47 | + return ri |
| 48 | +} |
| 49 | + |
| 50 | +func TestResourceCacheKey(t *testing.T) { |
| 51 | + key := ResourceCacheKey("APIService", "env1", "svc1") |
| 52 | + assert.Equal(t, "APIService/env1/svc1", key) |
| 53 | + |
| 54 | + key = ResourceCacheKey("APIService", "", "svc1") |
| 55 | + assert.Equal(t, "APIService//svc1", key) |
| 56 | +} |
| 57 | + |
| 58 | +func TestGetCachedResourcesByKind_APIService(t *testing.T) { |
| 59 | + cm := NewAgentCacheManager(&config.CentralConfiguration{}, false) |
| 60 | + modTime := time.Date(2026, 3, 12, 10, 0, 0, 0, time.UTC) |
| 61 | + |
| 62 | + ri := makeAPIServiceRI("env1", "svc1", "ext-id-1", modTime) |
| 63 | + err := cm.AddAPIService(ri) |
| 64 | + assert.Nil(t, err) |
| 65 | + |
| 66 | + result := cm.GetCachedResourcesByKind("management", management.APIServiceGVK().Kind) |
| 67 | + expectedKey := ResourceCacheKey(management.APIServiceGVK().Kind, "env1", "svc1") |
| 68 | + assert.Len(t, result, 1) |
| 69 | + assert.Contains(t, result, expectedKey) |
| 70 | + assert.Equal(t, modTime, result[expectedKey]) |
| 71 | +} |
| 72 | + |
| 73 | +func TestGetCachedResourcesByKind_APIServiceInstance(t *testing.T) { |
| 74 | + cm := NewAgentCacheManager(&config.CentralConfiguration{}, false) |
| 75 | + modTime := time.Date(2026, 3, 12, 10, 0, 0, 0, time.UTC) |
| 76 | + |
| 77 | + ri := makeRI("management", management.APIServiceInstanceGVK().Kind, "Environment", "env1", "inst1", "id1", modTime) |
| 78 | + cm.AddAPIServiceInstance(ri) |
| 79 | + |
| 80 | + result := cm.GetCachedResourcesByKind("management", management.APIServiceInstanceGVK().Kind) |
| 81 | + expectedKey := ResourceCacheKey(management.APIServiceInstanceGVK().Kind, "env1", "inst1") |
| 82 | + assert.Len(t, result, 1) |
| 83 | + assert.Contains(t, result, expectedKey) |
| 84 | +} |
| 85 | + |
| 86 | +func TestGetCachedResourcesByKind_ManagedApplication(t *testing.T) { |
| 87 | + cm := NewAgentCacheManager(&config.CentralConfiguration{}, false) |
| 88 | + modTime := time.Date(2026, 3, 12, 10, 0, 0, 0, time.UTC) |
| 89 | + |
| 90 | + ri := makeRI("management", management.ManagedApplicationGVK().Kind, "Environment", "env1", "app1", "id1", modTime) |
| 91 | + cm.AddManagedApplication(ri) |
| 92 | + |
| 93 | + result := cm.GetCachedResourcesByKind("management", management.ManagedApplicationGVK().Kind) |
| 94 | + expectedKey := ResourceCacheKey(management.ManagedApplicationGVK().Kind, "env1", "app1") |
| 95 | + assert.Len(t, result, 1) |
| 96 | + assert.Contains(t, result, expectedKey) |
| 97 | +} |
| 98 | + |
| 99 | +func TestGetCachedResourcesByKind_AccessRequest(t *testing.T) { |
| 100 | + cm := NewAgentCacheManager(&config.CentralConfiguration{}, false) |
| 101 | + modTime := time.Date(2026, 3, 12, 10, 0, 0, 0, time.UTC) |
| 102 | + |
| 103 | + ri := makeRI("management", management.AccessRequestGVK().Kind, "Environment", "env1", "ar1", "id1", modTime) |
| 104 | + cm.AddAccessRequest(ri) |
| 105 | + |
| 106 | + result := cm.GetCachedResourcesByKind("management", management.AccessRequestGVK().Kind) |
| 107 | + expectedKey := ResourceCacheKey(management.AccessRequestGVK().Kind, "env1", "ar1") |
| 108 | + assert.Len(t, result, 1) |
| 109 | + assert.Contains(t, result, expectedKey) |
| 110 | +} |
| 111 | + |
| 112 | +func TestGetCachedResourcesByKind_MultipleResourcesSameKind(t *testing.T) { |
| 113 | + cm := NewAgentCacheManager(&config.CentralConfiguration{}, false) |
| 114 | + modTime1 := time.Date(2026, 3, 12, 10, 0, 0, 0, time.UTC) |
| 115 | + modTime2 := time.Date(2026, 3, 12, 11, 0, 0, 0, time.UTC) |
| 116 | + |
| 117 | + svcKind := management.APIServiceGVK().Kind |
| 118 | + ri1 := makeAPIServiceRI("env1", "svc1", "ext-id-1", modTime1) |
| 119 | + ri2 := makeAPIServiceRI("env1", "svc2", "ext-id-2", modTime2) |
| 120 | + cm.AddAPIService(ri1) |
| 121 | + cm.AddAPIService(ri2) |
| 122 | + |
| 123 | + result := cm.GetCachedResourcesByKind("management", svcKind) |
| 124 | + assert.Len(t, result, 2) |
| 125 | + assert.Contains(t, result, ResourceCacheKey(svcKind, "env1", "svc1")) |
| 126 | + assert.Contains(t, result, ResourceCacheKey(svcKind, "env1", "svc2")) |
| 127 | +} |
| 128 | + |
| 129 | +func TestGetCachedResourcesByKind_DifferentScopes(t *testing.T) { |
| 130 | + cm := NewAgentCacheManager(&config.CentralConfiguration{}, false) |
| 131 | + modTime := time.Date(2026, 3, 12, 10, 0, 0, 0, time.UTC) |
| 132 | + |
| 133 | + svcKind := management.APIServiceGVK().Kind |
| 134 | + ri1 := makeAPIServiceRI("env1", "svc1", "ext-id-1", modTime) |
| 135 | + ri2 := makeAPIServiceRI("env2", "svc1", "ext-id-2", modTime) |
| 136 | + cm.AddAPIService(ri1) |
| 137 | + cm.AddAPIService(ri2) |
| 138 | + |
| 139 | + result := cm.GetCachedResourcesByKind("management", svcKind) |
| 140 | + key1 := ResourceCacheKey(svcKind, "env1", "svc1") |
| 141 | + key2 := ResourceCacheKey(svcKind, "env2", "svc1") |
| 142 | + |
| 143 | + assert.Len(t, result, 2) |
| 144 | + assert.Contains(t, result, key1) |
| 145 | + assert.Contains(t, result, key2) |
| 146 | +} |
| 147 | + |
| 148 | +func TestGetCachedResourcesByKind_EmptyCache(t *testing.T) { |
| 149 | + cm := NewAgentCacheManager(&config.CentralConfiguration{}, false) |
| 150 | + |
| 151 | + result := cm.GetCachedResourcesByKind("management", management.APIServiceGVK().Kind) |
| 152 | + assert.Empty(t, result) |
| 153 | +} |
| 154 | + |
| 155 | +func TestGetCachedResourcesByKind_UnknownKindFallsBackToWatchResource(t *testing.T) { |
| 156 | + cm := NewAgentCacheManager(&config.CentralConfiguration{}, false) |
| 157 | + modTime := time.Date(2026, 3, 12, 10, 0, 0, 0, time.UTC) |
| 158 | + |
| 159 | + ri := makeRI("catalog", "SomeCustomKind", "Environment", "env1", "custom1", "id1", modTime) |
| 160 | + cm.AddWatchResource(ri) |
| 161 | + |
| 162 | + result := cm.GetCachedResourcesByKind("catalog", "SomeCustomKind") |
| 163 | + expectedKey := ResourceCacheKey("SomeCustomKind", "env1", "custom1") |
| 164 | + assert.Len(t, result, 1) |
| 165 | + assert.Contains(t, result, expectedKey) |
| 166 | +} |
| 167 | + |
| 168 | +func TestGetCachedResourcesByKind_WatchResourceFiltersGroupAndKind(t *testing.T) { |
| 169 | + cm := NewAgentCacheManager(&config.CentralConfiguration{}, false) |
| 170 | + modTime := time.Date(2026, 3, 12, 10, 0, 0, 0, time.UTC) |
| 171 | + |
| 172 | + ri1 := makeRI("groupA", "KindA", "", "", "res1", "id1", modTime) |
| 173 | + ri2 := makeRI("groupB", "KindB", "", "", "res2", "id2", modTime) |
| 174 | + cm.AddWatchResource(ri1) |
| 175 | + cm.AddWatchResource(ri2) |
| 176 | + |
| 177 | + result := cm.GetCachedResourcesByKind("groupA", "KindA") |
| 178 | + assert.Len(t, result, 1) |
| 179 | + assert.Contains(t, result, ResourceCacheKey("KindA", "", "res1")) |
| 180 | + |
| 181 | + result = cm.GetCachedResourcesByKind("groupB", "KindB") |
| 182 | + assert.Len(t, result, 1) |
| 183 | + assert.Contains(t, result, ResourceCacheKey("KindB", "", "res2")) |
| 184 | +} |
0 commit comments