-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ Implemented /criteria/{criteria_id}/enqueue/v1 endpoint
- Loading branch information
1 parent
eaa05f1
commit 2faaf83
Showing
5 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package criteria | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"ahbcc/internal/log" | ||
) | ||
|
||
// EnqueueHandlerV1 HTTP Handler of the endpoint /criteria/enqueue/v1 | ||
func EnqueueHandlerV1(enqueueCriteria Enqueue) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
criteriaIDParam := r.PathValue("criteria_id") | ||
criteriaID, err := strconv.Atoi(criteriaIDParam) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
http.Error(w, InvalidURLParameter, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = enqueueCriteria(ctx, criteriaID) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
http.Error(w, FailedToEnqueueCriteria, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
log.Info(ctx, "Criteria successfully sent to enqueue") | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte("Criteria successfully sent to enqueue")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package criteria_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"ahbcc/cmd/api/search/criteria" | ||
) | ||
|
||
func TestEnqueueHandlerV1_success(t *testing.T) { | ||
mockEnqueueCriteria := criteria.MockEnqueue(nil) | ||
|
||
mockResponseWriter := httptest.NewRecorder() | ||
mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/criteria/{criteria_id}/enqueue/v1", http.NoBody) | ||
mockRequest.SetPathValue("criteria_id", "1") | ||
|
||
handlerV1 := criteria.EnqueueHandlerV1(mockEnqueueCriteria) | ||
|
||
handlerV1(mockResponseWriter, mockRequest) | ||
|
||
want := http.StatusOK | ||
got := mockResponseWriter.Result().StatusCode | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
|
||
func TestEnqueueHandlerV1_failedWhenTheURLParamIsEmpty(t *testing.T) { | ||
mockEnqueueCriteria := criteria.MockEnqueue(nil) | ||
|
||
mockResponseWriter := httptest.NewRecorder() | ||
mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/criteria/{criteria_id}/enqueue/v1", http.NoBody) | ||
|
||
handlerV1 := criteria.EnqueueHandlerV1(mockEnqueueCriteria) | ||
|
||
handlerV1(mockResponseWriter, mockRequest) | ||
|
||
want := http.StatusBadRequest | ||
got := mockResponseWriter.Result().StatusCode | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
|
||
func TestEnqueueHandlerV1_failedWhenTheURLParamCannotBeParsed(t *testing.T) { | ||
mockEnqueueCriteria := criteria.MockEnqueue(nil) | ||
|
||
mockResponseWriter := httptest.NewRecorder() | ||
mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/criteria/{criteria_id}/enqueue/v1", http.NoBody) | ||
mockRequest.SetPathValue("criteria_id", "error") | ||
|
||
handlerV1 := criteria.EnqueueHandlerV1(mockEnqueueCriteria) | ||
|
||
handlerV1(mockResponseWriter, mockRequest) | ||
|
||
want := http.StatusBadRequest | ||
got := mockResponseWriter.Result().StatusCode | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
|
||
func TestEnqueueHandlerV1_failsWhenEnqueueCriteriaThrowsError(t *testing.T) { | ||
mockEnqueueCriteria := criteria.MockEnqueue(errors.New("failed while executing enqueue criteria")) | ||
|
||
mockResponseWriter := httptest.NewRecorder() | ||
mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/criteria/{criteria_id}/enqueue/v1", http.NoBody) | ||
mockRequest.SetPathValue("criteria_id", "1") | ||
|
||
handlerV1 := criteria.EnqueueHandlerV1(mockEnqueueCriteria) | ||
|
||
handlerV1(mockResponseWriter, mockRequest) | ||
|
||
want := http.StatusInternalServerError | ||
got := mockResponseWriter.Result().StatusCode | ||
|
||
assert.Equal(t, want, got) | ||
} |