github.com/Axway/agent-sdk@v1.1.101/pkg/agent/teamcache_test.go (about) 1 package agent 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "strings" 8 "testing" 9 10 "github.com/Axway/agent-sdk/pkg/agent/cache" 11 12 "github.com/Axway/agent-sdk/pkg/apic/definitions" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestTeamCache(t *testing.T) { 18 testCases := []struct { 19 name string 20 teams []*definitions.PlatformTeam 21 cached []*definitions.PlatformTeam 22 expectedTeamsInCache int 23 }{ 24 { 25 name: "Should save one team to the cache", 26 expectedTeamsInCache: 1, 27 cached: []*definitions.PlatformTeam{}, 28 teams: []*definitions.PlatformTeam{ 29 { 30 Name: "TeamA", 31 ID: "1", 32 Default: true, 33 }, 34 }, 35 }, 36 { 37 name: "Should save two teams to the cache, and remove a team that was added", 38 expectedTeamsInCache: 2, 39 cached: []*definitions.PlatformTeam{ 40 { 41 Name: "TeamA", 42 ID: "1", 43 Default: true, 44 }, 45 }, 46 teams: []*definitions.PlatformTeam{ 47 { 48 Name: "TeamB", 49 ID: "2", 50 Default: false, 51 }, 52 { 53 Name: "TeamC", 54 ID: "3", 55 Default: false, 56 }, 57 }, 58 }, 59 { 60 name: "Should save 4 teams in the cache", 61 expectedTeamsInCache: 4, 62 cached: []*definitions.PlatformTeam{ 63 { 64 Name: "TeamA", 65 ID: "1", 66 Default: true, 67 }, 68 { 69 Name: "TeamB", 70 ID: "2", 71 Default: false, 72 }, 73 { 74 Name: "TeamC", 75 ID: "3", 76 Default: false, 77 }, 78 }, 79 teams: []*definitions.PlatformTeam{ 80 { 81 Name: "TeamA", 82 ID: "1", 83 Default: true, 84 }, 85 { 86 Name: "TeamB", 87 ID: "2", 88 Default: false, 89 }, 90 { 91 Name: "TeamC", 92 ID: "3", 93 Default: false, 94 }, 95 { 96 Name: "TeamD", 97 ID: "4", 98 Default: false, 99 }, 100 }, 101 }, 102 } 103 104 for _, test := range testCases { 105 106 t.Run(test.name, func(t *testing.T) { 107 s := httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { 108 switch { 109 case strings.Contains(req.RequestURI, "/auth"): 110 token := "{\"access_token\":\"somevalue\",\"expires_in\": 12235677}" 111 resp.Write([]byte(token)) 112 case strings.Contains(req.RequestURI, "platformTeams"): 113 data, _ := json.Marshal(test.teams) 114 resp.Write(data) 115 } 116 })) 117 defer s.Close() 118 119 cfg := createCentralCfg(s.URL, "env") 120 caches := cache.NewAgentCacheManager(cfg, false) 121 122 for _, item := range test.cached { 123 caches.AddTeam(item) 124 } 125 126 resetResources() 127 agent.teamMap = nil 128 err := Initialize(cfg) 129 assert.Nil(t, err) 130 assert.NotNil(t, agent.apicClient) 131 132 job := centralTeamsCache{} 133 134 job.Execute() 135 teams := agent.cacheManager.GetTeamCache().GetKeys() 136 assert.Equal(t, test.expectedTeamsInCache, len(teams)) 137 }) 138 } 139 }