Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ GitOps Domain Snapshot Orchestrator for Switcher API
Windows: `$env:GO_ENV="test"; go run ./src/cmd/app/main.go`<br>
Unit: `GO_ENV=test go run ./src/cmd/app/main.go`
- Testing `go test -coverpkg=./... -v`
- Coverage `go test -coverprofile="coverage.out" ./... && go tool cover -html="coverage.out"`
- Building `go build -o ./bin/app ./src/cmd/app/main.go`
1 change: 1 addition & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sonar.sources=src
sonar.exclusions=**/*_test.go
sonar.tests=src
sonar.test.inclusions=**/*_test.go
sonar.coverage.exclusions=src/server/**/*,src/db/**/*,src/cmd/**/*,src/repository/**/*

sonar.language=go
sonar.go.coverage.reportPaths=coverage.out
8 changes: 4 additions & 4 deletions src/controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func NewAccountController(repo repository.AccountRepository) *AccountController
func (controller *AccountController) RegisterRoutes(r *mux.Router) http.Handler {
const routesDomainVar = "/{domainId}"

r.HandleFunc(controller.RouteAccountPath, controller.CreateAccountHandler).Methods(http.MethodPost)
r.HandleFunc(controller.RouteAccountPath+routesDomainVar, controller.FetchAccountHandler).Methods(http.MethodGet)
r.HandleFunc(controller.RouteAccountPath+routesDomainVar, controller.UpdateAccountHandler).Methods(http.MethodPut)
r.HandleFunc(controller.RouteAccountPath+routesDomainVar, controller.DeleteAccountHandler).Methods(http.MethodDelete)
r.NewRoute().Path(controller.RouteAccountPath + routesDomainVar).Name("GetAccount").HandlerFunc(controller.FetchAccountHandler).Methods(http.MethodGet)
r.NewRoute().Path(controller.RouteAccountPath).Name("CreateAccount").HandlerFunc(controller.CreateAccountHandler).Methods(http.MethodPost)
r.NewRoute().Path(controller.RouteAccountPath + routesDomainVar).Name("UpdateAccount").HandlerFunc(controller.UpdateAccountHandler).Methods(http.MethodPut)
r.NewRoute().Path(controller.RouteAccountPath + routesDomainVar).Name("DeleteAccount").HandlerFunc(controller.DeleteAccountHandler).Methods(http.MethodDelete)

return r
}
Expand Down
16 changes: 16 additions & 0 deletions src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,26 @@ import (
"net/http/httptest"
"testing"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/switcherapi/switcher-gitops/src/model"
)

func TestAccountRegisterRoutes(t *testing.T) {
// Create a router
r := mux.NewRouter()

// Test
accountController.RegisterRoutes(r)

// Assert
assert.NotNil(t, r)
assert.NotNil(t, r.GetRoute("GetAccount"))
assert.NotNil(t, r.GetRoute("CreateAccount"))
assert.NotNil(t, r.GetRoute("UpdateAccount"))
assert.NotNil(t, r.GetRoute("DeleteAccount"))
}

func TestCreateAccountHandler(t *testing.T) {
// Create a request and response recorder
w, r := givenAccountRequest(accountV1)
Expand Down
2 changes: 1 addition & 1 deletion src/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewApiController() *ApiController {
}

func (controller *ApiController) RegisterRoutes(r *mux.Router) http.Handler {
r.HandleFunc(controller.RouteCheckApiPath, controller.CheckApiHandler).Methods(http.MethodGet)
r.NewRoute().Path(controller.RouteCheckApiPath).Name("CheckApi").HandlerFunc(controller.CheckApiHandler).Methods(http.MethodGet)

return r
}
Expand Down
9 changes: 9 additions & 0 deletions src/controller/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import (
"net/http/httptest"
"testing"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)

func TestApiRegisterRoutes(t *testing.T) {
r := mux.NewRouter()
apiController.RegisterRoutes(r)

assert.NotNil(t, r)
assert.NotNil(t, r.GetRoute("CheckApi"))
}

func TestCheckApiHandler(t *testing.T) {
w, r := givenApiRequest()
apiController.CheckApiHandler(w, r)
Expand Down
12 changes: 12 additions & 0 deletions src/utils/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ func TestResponseJSON(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "Some message", response["message"])
}

func TestResponseJSONError(t *testing.T) {
w := httptest.NewRecorder()
data := make(chan int)
status := http.StatusOK

ResponseJSON(w, data, status)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, "", w.Body.String())
}