|
4 | 4 | "context"
|
5 | 5 | "encoding/json"
|
6 | 6 | "fmt"
|
| 7 | + "net/http" |
7 | 8 | "net/url"
|
8 | 9 | "os"
|
9 | 10 | "testing"
|
@@ -43,3 +44,169 @@ func TestKindLocationGet(t *testing.T) {
|
43 | 44 | assert.NotEmpty(t, resp, "Response should not be empty")
|
44 | 45 | assert.EqualValues(t, &expected, actual, "Response body should match the one from the server")
|
45 | 46 | }
|
| 47 | + |
| 48 | +// TestKindLocationCreateByID tests functionality of creating a new location. |
| 49 | +func TestKindLocationCreateByID(t *testing.T) { |
| 50 | + const dataFile = "testdata/location_create.json" |
| 51 | + const target = "https://github.com/tdabasinskas/go/backstage/test" |
| 52 | + |
| 53 | + expected := LocationCreateResponse{} |
| 54 | + expectedData, _ := os.ReadFile(dataFile) |
| 55 | + err := json.Unmarshal(expectedData, &expected) |
| 56 | + |
| 57 | + assert.FileExists(t, dataFile, "Test data file should exist") |
| 58 | + assert.NoError(t, err, "Unmarshal should not return an error") |
| 59 | + |
| 60 | + baseURL, _ := url.Parse("https://foo:1234/api") |
| 61 | + defer gock.Off() |
| 62 | + gock.New(baseURL.String()). |
| 63 | + MatchHeader("Accept", "application/json"). |
| 64 | + Post("/catalog/locations"). |
| 65 | + MatchParam("dryRun", "false"). |
| 66 | + Reply(200). |
| 67 | + JSON(&LocationCreateResponse{ |
| 68 | + Location: &LocationResponse{ |
| 69 | + ID: "830d2354-8bbb-42d1-a751-2959f6da5416", |
| 70 | + Type: "url", |
| 71 | + Target: target, |
| 72 | + }, |
| 73 | + Entities: []Entity{}, |
| 74 | + }) |
| 75 | + |
| 76 | + c, _ := NewClient(baseURL.String(), "", nil) |
| 77 | + s := newLocationService(&entityService{ |
| 78 | + client: c, |
| 79 | + apiPath: "/catalog/entities", |
| 80 | + }) |
| 81 | + |
| 82 | + actual, resp, err := s.Create(context.Background(), target, false) |
| 83 | + assert.NoError(t, err, "Get should not return an error") |
| 84 | + assert.NotEmpty(t, resp, "Response should not be empty") |
| 85 | + assert.EqualValues(t, &expected, actual, "Response body should match the one from the server") |
| 86 | +} |
| 87 | + |
| 88 | +// TestKindLocationCreateByID_DryRun tests functionality of creating a new location. |
| 89 | +func TestKindLocationCreateByID_DryRun(t *testing.T) { |
| 90 | + const dataFile = "testdata/location_create_dryrun.json" |
| 91 | + const target = "https://github.com/tdabasinskas/go/backstage/test" |
| 92 | + |
| 93 | + expected := LocationCreateResponse{} |
| 94 | + expectedData, _ := os.ReadFile(dataFile) |
| 95 | + err := json.Unmarshal(expectedData, &expected) |
| 96 | + |
| 97 | + assert.FileExists(t, dataFile, "Test data file should exist") |
| 98 | + assert.NoError(t, err, "Unmarshal should not return an error") |
| 99 | + |
| 100 | + baseURL, _ := url.Parse("https://foo:1234/api") |
| 101 | + defer gock.Off() |
| 102 | + gock.New(baseURL.String()). |
| 103 | + MatchHeader("Accept", "application/json"). |
| 104 | + Post("/catalog/locations"). |
| 105 | + MatchParam("dryRun", "true"). |
| 106 | + Reply(200). |
| 107 | + JSON(&LocationCreateResponse{ |
| 108 | + Location: &LocationResponse{ |
| 109 | + ID: "830d2354-8bbb-42d1-a751-2959f6da5416", |
| 110 | + Type: "url", |
| 111 | + Target: target, |
| 112 | + }, |
| 113 | + Entities: []Entity{}, |
| 114 | + }) |
| 115 | + |
| 116 | + c, _ := NewClient(baseURL.String(), "", nil) |
| 117 | + s := newLocationService(&entityService{ |
| 118 | + client: c, |
| 119 | + apiPath: "/catalog/entities", |
| 120 | + }) |
| 121 | + |
| 122 | + actual, resp, err := s.Create(context.Background(), target, true) |
| 123 | + assert.NoError(t, err, "Get should not return an error") |
| 124 | + assert.NotEmpty(t, resp, "Response should not be empty") |
| 125 | + assert.EqualValues(t, &expected, actual, "Response body should match the one from the server") |
| 126 | +} |
| 127 | + |
| 128 | +// TestKindLocationGetByID tests functionality of getting a location by its ID. |
| 129 | +func TestKindLocationGetByID(t *testing.T) { |
| 130 | + const dataFile = "testdata/location_by_id.json" |
| 131 | + const id = "830d2354-8bbb-42d1-a751-2959f6da5416" |
| 132 | + |
| 133 | + expected := LocationResponse{} |
| 134 | + expectedData, _ := os.ReadFile(dataFile) |
| 135 | + err := json.Unmarshal(expectedData, &expected) |
| 136 | + |
| 137 | + assert.FileExists(t, dataFile, "Test data file should exist") |
| 138 | + assert.NoError(t, err, "Unmarshal should not return an error") |
| 139 | + |
| 140 | + baseURL, _ := url.Parse("https://foo:1234/api") |
| 141 | + defer gock.Off() |
| 142 | + gock.New(baseURL.String()). |
| 143 | + MatchHeader("Accept", "application/json"). |
| 144 | + Get(fmt.Sprintf("/catalog/locations/%s", id)). |
| 145 | + Reply(200). |
| 146 | + File(dataFile) |
| 147 | + |
| 148 | + c, _ := NewClient(baseURL.String(), "", nil) |
| 149 | + s := newLocationService(&entityService{ |
| 150 | + client: c, |
| 151 | + apiPath: "/catalog/entities", |
| 152 | + }) |
| 153 | + |
| 154 | + actual, resp, err := s.GetByID(context.Background(), id) |
| 155 | + assert.NoError(t, err, "Get should not return an error") |
| 156 | + assert.NotEmpty(t, resp, "Response should not be empty") |
| 157 | + assert.EqualValues(t, &expected, actual, "Response body should match the one from the server") |
| 158 | +} |
| 159 | + |
| 160 | +// TestKindLocationList tests functionality of getting all locations. |
| 161 | +func TestKindLocationList(t *testing.T) { |
| 162 | + const dataFile = "testdata/locations.json" |
| 163 | + |
| 164 | + var expected []LocationListResponse |
| 165 | + expectedData, _ := os.ReadFile(dataFile) |
| 166 | + err := json.Unmarshal(expectedData, &expected) |
| 167 | + |
| 168 | + assert.FileExists(t, dataFile, "Test data file should exist") |
| 169 | + assert.NoError(t, err, "Unmarshal should not return an error") |
| 170 | + |
| 171 | + baseURL, _ := url.Parse("https://foo:1234/api") |
| 172 | + defer gock.Off() |
| 173 | + gock.New(baseURL.String()). |
| 174 | + MatchHeader("Accept", "application/json"). |
| 175 | + Get("/catalog/locations"). |
| 176 | + Reply(200). |
| 177 | + File(dataFile) |
| 178 | + |
| 179 | + c, _ := NewClient(baseURL.String(), "", nil) |
| 180 | + s := newLocationService(&entityService{ |
| 181 | + client: c, |
| 182 | + apiPath: "/catalog/entities", |
| 183 | + }) |
| 184 | + |
| 185 | + actual, resp, err := s.List(context.Background()) |
| 186 | + assert.NoError(t, err, "Get should not return an error") |
| 187 | + assert.NotEmpty(t, resp, "Response should not be empty") |
| 188 | + assert.EqualValues(t, expected, actual, "Response body should match the one from the server") |
| 189 | +} |
| 190 | + |
| 191 | +// TestEntityServiceDelete tests the deletion of an entity. |
| 192 | +func TestKindLocationDeleteByID(t *testing.T) { |
| 193 | + const id = "id" |
| 194 | + |
| 195 | + baseURL, _ := url.Parse("https://foo:1234/api") |
| 196 | + defer gock.Off() |
| 197 | + gock.New(baseURL.String()). |
| 198 | + MatchHeader("Accept", "application/json"). |
| 199 | + Delete(fmt.Sprintf("/catalog/locations/%s", id)). |
| 200 | + Reply(http.StatusNoContent) |
| 201 | + |
| 202 | + c, _ := NewClient(baseURL.String(), "", nil) |
| 203 | + s := newLocationService(&entityService{ |
| 204 | + client: c, |
| 205 | + apiPath: "/catalog/entities", |
| 206 | + }) |
| 207 | + |
| 208 | + resp, err := s.DeleteByID(context.Background(), id) |
| 209 | + assert.NoError(t, err, "Delete should not return an error") |
| 210 | + assert.NotEmpty(t, resp, "Response should not be empty") |
| 211 | + assert.EqualValues(t, http.StatusNoContent, resp.StatusCode, "Response status code should match the one from the server") |
| 212 | +} |
0 commit comments