Skip to content

Commit

Permalink
Merge pull request #57 from Onyxmoon/shoppinglist-service-tests
Browse files Browse the repository at this point in the history
Shoppinglist service tests
  • Loading branch information
jarali authored Oct 31, 2023
2 parents c78ec7b + 0bc1cb0 commit a676a03
Show file tree
Hide file tree
Showing 10 changed files with 1,279 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Price Whisper
![Coverage](https://img.shields.io/badge/Coverage-79.2%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-81.0%25-brightgreen)
[![Run tests (product-service)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-product-service.yml/badge.svg)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-product-service.yml)
[![Run tests (shoppinglist-service)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-shoppinglist-service.yml/badge.svg)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-shoppinglist-service.yml)
[![Run tests (user-service)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-user-service.yml/badge.svg)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-user-service.yml)
Expand Down
10 changes: 5 additions & 5 deletions src/shoppinglist-service/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func New(shoppingListController userShoppingList.Controller, shoppingListEntryCo
r.POST("/api/v1/shoppinglist/:userId", shoppingListController.PostList)
r.DELETE("/api/v1/shoppinglist/:listId", shoppingListController.DeleteList)

r.GET("/api/v1/shoppinglistEntry/:listId", shoppingListEntryController.GetEntries)
r.GET("/api/v1/shoppinglistEntry/:listId/:productId", shoppingListEntryController.GetEntry)
r.PUT("/api/v1/shoppinglistEntry/:listId/:productId", shoppingListEntryController.PutEntry)
r.POST("/api/v1/shoppinglistEntry/:listId/:productId", shoppingListEntryController.PostEntry)
r.DELETE("/api/v1/shoppinglistEntry/:listId/:productId", shoppingListEntryController.DeleteEntry)
r.GET("/api/v1/shoppinglistentries/:listId", shoppingListEntryController.GetEntries)
r.GET("/api/v1/shoppinglistentries/:listId/:productId", shoppingListEntryController.GetEntry)
r.PUT("/api/v1/shoppinglistentries/:listId/:productId", shoppingListEntryController.PutEntry)
r.POST("/api/v1/shoppinglistentries/:listId/:productId", shoppingListEntryController.PostEntry)
r.DELETE("/api/v1/shoppinglistentries/:listId/:productId", shoppingListEntryController.DeleteEntry)

return &Router{r}
}
Expand Down
229 changes: 229 additions & 0 deletions src/shoppinglist-service/api/router/router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
package router

import (
"hsfl.de/group6/hsfl-master-ai-cloud-engineering/shoppinglist-service/userShoppingList"
shoppingListModel "hsfl.de/group6/hsfl-master-ai-cloud-engineering/shoppinglist-service/userShoppingList/model"
"hsfl.de/group6/hsfl-master-ai-cloud-engineering/shoppinglist-service/userShoppingListEntry"
entryModel "hsfl.de/group6/hsfl-master-ai-cloud-engineering/shoppinglist-service/userShoppingListEntry/model"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestRouter(t *testing.T) {
listRepo := setupMockListRepository()
entryRepo := setupMockEntryRepository()
shoppingListController := userShoppingList.NewDefaultController(listRepo)
shoppingListEntryController := userShoppingListEntry.NewDefaultController(entryRepo)
router := New(shoppingListController, shoppingListEntryController)

t.Run("should return 404 NOT FOUND if path is unknown", func(t *testing.T) {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/unknown/route", nil)

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusNotFound {
t.Errorf("Expected status code %d, got %d", http.StatusNotFound, w.Code)
}
})

t.Run("shoppinglist routes", func(t *testing.T) {
t.Run("GET /api/v1/shoppinglist/:userId should call GetLists", func(t *testing.T) {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/v1/shoppinglist/1", nil)

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
}
})

t.Run("GET /api/v1/shoppinglist/:listId/:userId should call GetList", func(t *testing.T) {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/v1/shoppinglist/1/2", nil)

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
}
})

t.Run("PUT /api/v1/shoppinglist/:listId/:userId should call PutList", func(t *testing.T) {
// given
w := httptest.NewRecorder()
jsonRequest := `{"name": "Updated List Name"}`
r := httptest.NewRequest("PUT", "/api/v1/shoppinglist/1/2", strings.NewReader(jsonRequest))

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
}
})

t.Run("POST /api/v1/shoppinglist/:userId should call PostList", func(t *testing.T) {
// given
w := httptest.NewRecorder()
jsonRequest := `{"name": "New Shopping List"}`
r := httptest.NewRequest("POST", "/api/v1/shoppinglist/3", strings.NewReader(jsonRequest))

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusCreated {
t.Errorf("Expected status code %d, got %d", http.StatusCreated, w.Code)
}
})

t.Run("DELETE /api/v1/shoppinglist/:listId should call DeleteList", func(t *testing.T) {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest("DELETE", "/api/v1/shoppinglist/1", nil)

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
}
})
})

t.Run("shoppinglist entries routes", func(t *testing.T) {
t.Run("GET /api/v1/shoppinglistentries/:listId should call GetEntries", func(t *testing.T) {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/v1/shoppinglistentries/1", nil)

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
}
})

t.Run("GET /api/v1/shoppinglistentries/:listId/:productId should call GetEntry", func(t *testing.T) {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/v1/shoppinglistentries/1/2", nil)

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
}
})

t.Run("PUT /api/v1/shoppinglistentries/:listId/:productId should call PutEntry", func(t *testing.T) {
// given
w := httptest.NewRecorder()
jsonRequest := `{"count": 2, "note": "Test entry", "checked": false}`
r := httptest.NewRequest("PUT", "/api/v1/shoppinglistentries/1/2", strings.NewReader(jsonRequest))

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
}
})

t.Run("POST /api/v1/shoppinglistentries/:listId/:productId should call PostEntry", func(t *testing.T) {
// given
w := httptest.NewRecorder()
jsonRequest := `{"count": 2, "note": "Test entry", "checked": false}`
r := httptest.NewRequest("POST", "/api/v1/shoppinglistentries/1/4", strings.NewReader(jsonRequest))

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusCreated {
t.Errorf("Expected status code %d, got %d", http.StatusCreated, w.Code)
}
})

t.Run("DELETE /api/v1/shoppinglistentries/:listId/:productId should call DeleteEntry", func(t *testing.T) {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest("DELETE", "/api/v1/shoppinglistentries/1/2", nil)

// when
router.ServeHTTP(w, r)

// then
if w.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
}
})
})

}

func setupMockEntryRepository() userShoppingListEntry.Repository {
repository := userShoppingListEntry.NewDemoRepository()
entries := setupDemoEntrySlice()
for _, entry := range entries {
repository.Create(entry)
}
return repository
}

func setupMockListRepository() userShoppingList.Repository {
repository := userShoppingList.NewDemoRepository()
lists := setupDemoListSlice()
for _, list := range lists {
repository.Create(list)
}
return repository
}

func setupDemoListSlice() []*shoppingListModel.UserShoppingList {
return []*shoppingListModel.UserShoppingList{
{
Id: 1,
UserId: 2,
Completed: false,
},
{
Id: 2,
UserId: 1,
Completed: true,
},
{
Id: 3,
UserId: 3,
Completed: true,
},
}
}
func setupDemoEntrySlice() []*entryModel.UserShoppingListEntry {
return []*entryModel.UserShoppingListEntry{
{ShoppingListId: 1, ProductId: 1, Count: 3, Note: "Sample entry 1", Checked: false},
{ShoppingListId: 1, ProductId: 2, Count: 2, Note: "Sample entry 2", Checked: true},
{ShoppingListId: 2, ProductId: 1, Count: 1, Note: "Sample entry 3", Checked: false},
{ShoppingListId: 2, ProductId: 2, Count: 4, Note: "Sample entry 3", Checked: false},
}
}
16 changes: 15 additions & 1 deletion src/shoppinglist-service/userShoppingList/default_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func NewDefaultController(userShoppingListRepository Repository) *defaultControl

func (controller defaultController) GetList(writer http.ResponseWriter, request *http.Request) {
listId, err := strconv.ParseUint(request.Context().Value("listId").(string), 10, 64)
if err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
return
}
userId, err := strconv.ParseUint(request.Context().Value("userId").(string), 10, 64)
if err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
Expand All @@ -41,6 +45,10 @@ func (controller defaultController) GetList(writer http.ResponseWriter, request

func (controller defaultController) PutList(writer http.ResponseWriter, request *http.Request) {
listId, err := strconv.ParseUint(request.Context().Value("listId").(string), 10, 64)
if err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
return
}
userId, err := strconv.ParseUint(request.Context().Value("userId").(string), 10, 64)
if err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
Expand All @@ -64,7 +72,13 @@ func (controller defaultController) PutList(writer http.ResponseWriter, request
}

func (controller defaultController) PostList(writer http.ResponseWriter, request *http.Request) {
userId, err := strconv.ParseUint(request.Context().Value("userId").(string), 10, 64)
userIdValue := request.Context().Value("userId")
if userIdValue == nil {
http.Error(writer, "User ID not provided", http.StatusBadRequest)
return
}

userId, err := strconv.ParseUint(userIdValue.(string), 10, 64)
if err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
return
Expand Down
Loading

0 comments on commit a676a03

Please sign in to comment.