github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/datastream/stream_activation_test.go (about) 1 package datastream 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestDs_ActivateStream(t *testing.T) { 15 tests := map[string]struct { 16 request ActivateStreamRequest 17 responseStatus int 18 responseBody string 19 expectedPath string 20 expectedResponse *ActivateStreamResponse 21 withError error 22 }{ 23 "202 accepted": { 24 request: ActivateStreamRequest{StreamID: 3}, 25 responseStatus: http.StatusAccepted, 26 responseBody: ` 27 { 28 "streamVersionKey": { 29 "streamId": 1, 30 "streamVersionId": 3 31 } 32 } 33 `, 34 expectedPath: "/datastream-config-api/v1/log/streams/3/activate", 35 expectedResponse: &ActivateStreamResponse{ 36 StreamVersionKey: StreamVersionKey{ 37 StreamID: 1, 38 StreamVersionID: 3, 39 }, 40 }, 41 }, 42 "validation error": { 43 request: ActivateStreamRequest{}, 44 withError: ErrStructValidation, 45 }, 46 "400 bad request": { 47 request: ActivateStreamRequest{StreamID: 123}, 48 responseStatus: http.StatusBadRequest, 49 responseBody: ` 50 { 51 "type": "bad-request", 52 "title": "Bad Request", 53 "detail": "", 54 "instance": "df22bc0f-ca8d-4bdb-afea-ffdeef819e22", 55 "statusCode": 400, 56 "errors": [ 57 { 58 "type": "bad-request", 59 "title": "Bad Request", 60 "detail": "Stream does not exist. Please provide valid stream." 61 } 62 ] 63 } 64 `, 65 expectedPath: "/datastream-config-api/v1/log/streams/123/activate", 66 withError: &Error{ 67 Type: "bad-request", 68 Title: "Bad Request", 69 Instance: "df22bc0f-ca8d-4bdb-afea-ffdeef819e22", 70 StatusCode: http.StatusBadRequest, 71 Errors: []RequestErrors{ 72 { 73 Type: "bad-request", 74 Title: "Bad Request", 75 Detail: "Stream does not exist. Please provide valid stream.", 76 }, 77 }, 78 }, 79 }, 80 } 81 82 for name, test := range tests { 83 t.Run(name, func(t *testing.T) { 84 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 85 assert.Equal(t, test.expectedPath, r.URL.String()) 86 assert.Equal(t, http.MethodPut, r.Method) 87 w.WriteHeader(test.responseStatus) 88 _, err := w.Write([]byte(test.responseBody)) 89 assert.NoError(t, err) 90 })) 91 client := mockAPIClient(t, mockServer) 92 result, err := client.ActivateStream(context.Background(), test.request) 93 if test.withError != nil { 94 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 95 return 96 } 97 require.NoError(t, err) 98 assert.Equal(t, test.expectedResponse, result) 99 }) 100 } 101 } 102 103 func TestDs_DeactivateStream(t *testing.T) { 104 tests := map[string]struct { 105 request DeactivateStreamRequest 106 responseStatus int 107 responseBody string 108 expectedPath string 109 expectedResponse *DeactivateStreamResponse 110 withError error 111 }{ 112 "202 accepted": { 113 request: DeactivateStreamRequest{StreamID: 3}, 114 responseStatus: http.StatusAccepted, 115 responseBody: ` 116 { 117 "streamVersionKey": { 118 "streamId": 1, 119 "streamVersionId": 3 120 } 121 } 122 `, 123 expectedPath: "/datastream-config-api/v1/log/streams/3/deactivate", 124 expectedResponse: &DeactivateStreamResponse{ 125 StreamVersionKey: StreamVersionKey{ 126 StreamID: 1, 127 StreamVersionID: 3, 128 }, 129 }, 130 }, 131 "validation error": { 132 request: DeactivateStreamRequest{}, 133 withError: ErrStructValidation, 134 }, 135 "400 bad request": { 136 request: DeactivateStreamRequest{StreamID: 123}, 137 responseStatus: http.StatusBadRequest, 138 responseBody: ` 139 { 140 "type": "bad-request", 141 "title": "Bad Request", 142 "detail": "", 143 "instance": "df22bc0f-ca8d-4bdb-afea-ffdeef819e22", 144 "statusCode": 400, 145 "errors": [ 146 { 147 "type": "bad-request", 148 "title": "Bad Request", 149 "detail": "Stream does not exist. Please provide valid stream." 150 } 151 ] 152 } 153 `, 154 expectedPath: "/datastream-config-api/v1/log/streams/123/deactivate", 155 withError: &Error{ 156 Type: "bad-request", 157 Title: "Bad Request", 158 Instance: "df22bc0f-ca8d-4bdb-afea-ffdeef819e22", 159 StatusCode: http.StatusBadRequest, 160 Errors: []RequestErrors{ 161 { 162 Type: "bad-request", 163 Title: "Bad Request", 164 Detail: "Stream does not exist. Please provide valid stream.", 165 }, 166 }, 167 }, 168 }, 169 } 170 171 for name, test := range tests { 172 t.Run(name, func(t *testing.T) { 173 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 174 assert.Equal(t, test.expectedPath, r.URL.String()) 175 assert.Equal(t, http.MethodPut, r.Method) 176 w.WriteHeader(test.responseStatus) 177 _, err := w.Write([]byte(test.responseBody)) 178 assert.NoError(t, err) 179 })) 180 client := mockAPIClient(t, mockServer) 181 result, err := client.DeactivateStream(context.Background(), test.request) 182 if test.withError != nil { 183 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 184 return 185 } 186 require.NoError(t, err) 187 assert.Equal(t, test.expectedResponse, result) 188 }) 189 } 190 } 191 192 func TestDs_GetActivationHistory(t *testing.T) { 193 tests := map[string]struct { 194 request GetActivationHistoryRequest 195 responseStatus int 196 responseBody string 197 expectedPath string 198 expectedResponse []ActivationHistoryEntry 199 withError error 200 }{ 201 "200 OK": { 202 request: GetActivationHistoryRequest{StreamID: 3}, 203 responseStatus: http.StatusOK, 204 responseBody: ` 205 [ 206 { 207 "streamId": 7050, 208 "streamVersionId": 2, 209 "createdBy": "user1", 210 "createdDate": "16-01-2020 11:07:12 GMT", 211 "isActive": false 212 }, 213 { 214 "streamId": 7050, 215 "streamVersionId": 2, 216 "createdBy": "user2", 217 "createdDate": "16-01-2020 09:31:02 GMT", 218 "isActive": true 219 } 220 ] 221 `, 222 expectedPath: "/datastream-config-api/v1/log/streams/3/activationHistory", 223 expectedResponse: []ActivationHistoryEntry{ 224 { 225 CreatedBy: "user1", 226 CreatedDate: "16-01-2020 11:07:12 GMT", 227 IsActive: false, 228 StreamID: 7050, 229 StreamVersionID: 2, 230 }, 231 { 232 CreatedBy: "user2", 233 CreatedDate: "16-01-2020 09:31:02 GMT", 234 IsActive: true, 235 StreamID: 7050, 236 StreamVersionID: 2, 237 }, 238 }, 239 }, 240 "validation error": { 241 request: GetActivationHistoryRequest{}, 242 withError: ErrStructValidation, 243 }, 244 "400 bad request": { 245 request: GetActivationHistoryRequest{StreamID: 123}, 246 responseStatus: http.StatusBadRequest, 247 responseBody: ` 248 { 249 "type": "bad-request", 250 "title": "Bad Request", 251 "detail": "", 252 "instance": "df22bc0f-ca8d-4bdb-afea-ffdeef819e22", 253 "statusCode": 400, 254 "errors": [ 255 { 256 "type": "bad-request", 257 "title": "Bad Request", 258 "detail": "Stream does not exist. Please provide valid stream." 259 } 260 ] 261 } 262 `, 263 expectedPath: "/datastream-config-api/v1/log/streams/123/activationHistory", 264 withError: &Error{ 265 Type: "bad-request", 266 Title: "Bad Request", 267 Instance: "df22bc0f-ca8d-4bdb-afea-ffdeef819e22", 268 StatusCode: http.StatusBadRequest, 269 Errors: []RequestErrors{ 270 { 271 Type: "bad-request", 272 Title: "Bad Request", 273 Detail: "Stream does not exist. Please provide valid stream.", 274 }, 275 }, 276 }, 277 }, 278 } 279 280 for name, test := range tests { 281 t.Run(name, func(t *testing.T) { 282 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 283 assert.Equal(t, test.expectedPath, r.URL.String()) 284 assert.Equal(t, http.MethodGet, r.Method) 285 w.WriteHeader(test.responseStatus) 286 _, err := w.Write([]byte(test.responseBody)) 287 assert.NoError(t, err) 288 })) 289 client := mockAPIClient(t, mockServer) 290 result, err := client.GetActivationHistory(context.Background(), test.request) 291 if test.withError != nil { 292 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 293 return 294 } 295 require.NoError(t, err) 296 assert.Equal(t, test.expectedResponse, result) 297 }) 298 } 299 }