github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/api/in_memory_repository_test.go (about) 1 package api 2 3 import ( 4 "testing" 5 6 "github.com/hellofresh/janus/pkg/proxy" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestAPIInMemoryRepository(t *testing.T) { 11 t.Parallel() 12 13 tests := []struct { 14 scenario string 15 function func(*testing.T, *InMemoryRepository) 16 }{ 17 { 18 scenario: "api in memory add missing name", 19 function: testAddMissingName, 20 }, 21 { 22 scenario: "api in memory remove existent definition", 23 function: testRemoveExistentDefinition, 24 }, 25 { 26 scenario: "api in memory remove inexistent definition", 27 function: testRemoveInexistentDefinition, 28 }, 29 { 30 scenario: "api in memory find all", 31 function: testFindAll, 32 }, 33 { 34 scenario: "api in memory find by name", 35 function: testFindByName, 36 }, 37 { 38 scenario: "api in memory not find by name", 39 function: testNotFindByName, 40 }, 41 } 42 43 for _, test := range tests { 44 t.Run(test.scenario, func(t *testing.T) { 45 repo := newInMemoryRepo() 46 test.function(t, repo) 47 }) 48 } 49 } 50 51 func testAddMissingName(t *testing.T, repo *InMemoryRepository) { 52 err := repo.add(NewDefinition()) 53 assert.Error(t, err) 54 } 55 56 func testRemoveExistentDefinition(t *testing.T, repo *InMemoryRepository) { 57 err := repo.remove("test1") 58 assert.NoError(t, err) 59 } 60 61 func testRemoveInexistentDefinition(t *testing.T, repo *InMemoryRepository) { 62 err := repo.remove("test") 63 assert.Error(t, err) 64 } 65 66 func testFindAll(t *testing.T, repo *InMemoryRepository) { 67 results, err := repo.FindAll() 68 assert.NoError(t, err) 69 assert.Len(t, results, 3) 70 } 71 72 func testFindByName(t *testing.T, repo *InMemoryRepository) { 73 definition, err := repo.findByName("test1") 74 assert.NoError(t, err) 75 assert.NotNil(t, definition) 76 } 77 78 func testNotFindByName(t *testing.T, repo *InMemoryRepository) { 79 definition, err := repo.findByName("invalid") 80 assert.Error(t, err) 81 assert.Nil(t, definition) 82 } 83 84 func newInMemoryRepo() *InMemoryRepository { 85 repo := NewInMemoryRepository() 86 87 repo.add(&Definition{ 88 Name: "test1", 89 Active: true, 90 Proxy: &proxy.Definition{ 91 ListenPath: "/test1", 92 Upstreams: &proxy.Upstreams{ 93 Targets: []*proxy.Target{ 94 { 95 Target: "http://test1.com", 96 }, 97 }, 98 }, 99 }, 100 HealthCheck: HealthCheck{ 101 URL: "http://test1.com/status.com", 102 Timeout: 5, 103 }, 104 }) 105 106 repo.add(&Definition{ 107 Name: "test2", 108 Active: true, 109 Proxy: &proxy.Definition{ 110 ListenPath: "/test2", 111 Upstreams: &proxy.Upstreams{ 112 Targets: []*proxy.Target{ 113 { 114 Target: "http://test2.com", 115 }, 116 }, 117 }, 118 }, 119 }) 120 121 repo.add(&Definition{ 122 Name: "test3", 123 Proxy: &proxy.Definition{ 124 ListenPath: "/test3", 125 Upstreams: &proxy.Upstreams{ 126 Targets: []*proxy.Target{ 127 { 128 Target: "http://test3.com", 129 }, 130 }, 131 }, 132 }, 133 HealthCheck: HealthCheck{ 134 URL: "http://test3.com/status", 135 Timeout: 5, 136 }, 137 }) 138 139 return repo 140 }