-
Notifications
You must be signed in to change notification settings - Fork 5
Add Application Type CRUD API handlers #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yeswanth2420
wants to merge
12
commits into
develop
Choose a base branch
from
dynamic_apptype
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
304650d
Dynamic Apptype handlers
yeswanth2420 d8f96bb
Added logic for CRUD operations
yeswanth2420 9754f24
Dynamic Applicationtype code changes
yeswanth2420 c00e739
Update Go module and dependencies to latest versions
yeswanth2420 b5600f8
removed comment line
yeswanth2420 2935ff9
updated default apptypes
yeswanth2420 93e8614
Enhance application type creation validation and update default type …
yeswanth2420 be303ad
Update application type API path to use hyphenated format
yeswanth2420 c669711
Merge branch 'develop' into dynamic_apptype
yeswanth2420 f990acd
Copilot changes resolved
yeswanth2420 7d139fd
Add validation for application type ID in handlers and corresponding …
yeswanth2420 68ea9df
Merge branch 'develop' into dynamic_apptype
yeswanth2420 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,208 @@ | ||||||||||
| package applicationtype | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "encoding/json" | ||||||||||
| "fmt" | ||||||||||
| "net/http" | ||||||||||
| "time" | ||||||||||
|
|
||||||||||
| "github.com/gorilla/mux" | ||||||||||
| "github.com/rdkcentral/xconfadmin/adminapi/auth" | ||||||||||
| xhttp "github.com/rdkcentral/xconfadmin/http" | ||||||||||
| xapptype "github.com/rdkcentral/xconfadmin/shared/applicationtype" | ||||||||||
| xwhttp "github.com/rdkcentral/xconfwebconfig/http" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| func CreateApplicationTypeHandler(w http.ResponseWriter, r *http.Request) { | ||||||||||
| _, err := auth.CanWrite(r, auth.CHANGE_ENTITY) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusForbidden, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| var appType xapptype.ApplicationType | ||||||||||
| xw, ok := w.(*xwhttp.XResponseWriter) | ||||||||||
| if !ok { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, "responsewriter cast error") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| body := xw.Body() | ||||||||||
| err = json.Unmarshal([]byte(body), &appType) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| isDefault := xapptype.IsDefaultAppType(appType.Name) | ||||||||||
| if isDefault { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, "Cannot create default application type") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| exists, _ := xapptype.ApplicationTypeNameExists(appType.Name) | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| if exists { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusConflict, "Application type already exists") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| createdAppType, err := CreateApplicationType(r, &appType) | ||||||||||
| if err != nil { | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| data, err := json.Marshal(createdAppType) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| xhttp.WriteXconfResponse(w, http.StatusCreated, data) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func GetAllApplicationTypeHandler(w http.ResponseWriter, r *http.Request) { | ||||||||||
| _, err := auth.CanRead(r, auth.CHANGE_ENTITY) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusForbidden, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| appTypes, err := xapptype.GetAllApplicationTypeAsList() | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| data, err := json.Marshal(appTypes) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| xhttp.WriteXconfResponse(w, http.StatusOK, data) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func GetApplicationTypeHandler(w http.ResponseWriter, r *http.Request) { | ||||||||||
| _, err := auth.CanRead(r, auth.CHANGE_ENTITY) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusForbidden, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| id := mux.Vars(r)["id"] | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| if id == "" { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, "Application type ID is required") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| appType, err := xapptype.GetOneApplicationType(id) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| if appType == nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusNotFound, "Application type not found") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| data, err := json.Marshal(appType) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| xhttp.WriteXconfResponse(w, http.StatusOK, data) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func DeleteApplicationTypeHandler(w http.ResponseWriter, r *http.Request) { | ||||||||||
| _, err := auth.CanWrite(r, auth.CHANGE_ENTITY) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusForbidden, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| id := mux.Vars(r)["id"] | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| if id == "" { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, "Application type ID is required") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| appType, err := xapptype.GetOneApplicationType(id) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| if appType == nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusNotFound, "Application type not found") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| if xapptype.IsDefaultAppType(appType.Name) { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, "Default application types cannot be deleted") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| err = xapptype.DeleteOneApplicationType(id) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| response := map[string]string{ | ||||||||||
| "message": fmt.Sprintf("Application type '%s' deleted successfully", appType.Name), | ||||||||||
| } | ||||||||||
| data, err := json.Marshal(response) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| xhttp.WriteXconfResponse(w, http.StatusOK, data) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func UpdateApplicationTypeHandler(w http.ResponseWriter, r *http.Request) { | ||||||||||
| _, err := auth.CanWrite(r, auth.CHANGE_ENTITY) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusForbidden, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| id := mux.Vars(r)["id"] | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| if id == "" { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, "Application type ID is required") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| xw, ok := w.(*xwhttp.XResponseWriter) | ||||||||||
| if !ok { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, "responsewriter cast error") | ||||||||||
| return | ||||||||||
| } | ||||||||||
| body := xw.Body() | ||||||||||
| var updateRequest xapptype.ApplicationType | ||||||||||
| err = json.Unmarshal([]byte(body), &updateRequest) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
|
|
||||||||||
| err = ValidateApplicationType(&updateRequest) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| existingAppType, err := xapptype.GetOneApplicationType(id) | ||||||||||
| if err != nil { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||||||||||
| return | ||||||||||
| } | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| if existingAppType.Name != updateRequest.Name { | ||||||||||
| nameExists, _ := xapptype.ApplicationTypeNameExists(updateRequest.Name) | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| if nameExists { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusConflict, | ||||||||||
| fmt.Sprintf("Application type with name '%s' already exists", updateRequest.Name)) | ||||||||||
| return | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| if xapptype.IsDefaultAppType(existingAppType.Name) { | ||||||||||
| xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, "Default application types cannot be updated") | ||||||||||
| return | ||||||||||
| } | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| existingAppType.ID = id | ||||||||||
| existingAppType.Name = updateRequest.Name | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| if updateRequest.Description != "" { | ||||||||||
|
yeswanth2420 marked this conversation as resolved.
|
||||||||||
| existingAppType.Description = updateRequest.Description | ||||||||||
| } | ||||||||||
|
Comment on lines
+192
to
+194
|
||||||||||
| if updateRequest.Description != "" { | |
| existingAppType.Description = updateRequest.Description | |
| } | |
| existingAppType.Description = updateRequest.Description |
109 changes: 109 additions & 0 deletions
109
adminapi/applicationtype/application_type_handler_test.go
This file contains hidden or 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,109 @@ | ||
| package applicationtype | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gorilla/mux" | ||
| xwhttp "github.com/rdkcentral/xconfwebconfig/http" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestCreateApplicationTypeHandler(t *testing.T) { | ||
| req := httptest.NewRequest(http.MethodPost, "/api/application-types", nil) | ||
| rec := httptest.NewRecorder() | ||
|
|
||
| CreateApplicationTypeHandler(rec, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
| w := xwhttp.NewXResponseWriter(rec) | ||
|
|
||
| invalidJson := `{invalid json}` | ||
| w.SetBody(invalidJson) | ||
| CreateApplicationTypeHandler(w, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
|
|
||
| // valid Json | ||
| validJson := `{"name": "testAppType"}` | ||
| w.SetBody(validJson) | ||
| CreateApplicationTypeHandler(w, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
|
|
||
| w.SetBody(validJson) | ||
| CreateApplicationTypeHandler(w, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
|
|
||
| validJson = `{"name": "stb"}` | ||
| w.SetBody(validJson) | ||
| CreateApplicationTypeHandler(w, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
| } | ||
|
|
||
| func TestGetAllApplicationTypeHandler(t *testing.T) { | ||
| req := httptest.NewRequest(http.MethodGet, "/api/application-types", nil) | ||
| rec := httptest.NewRecorder() | ||
| GetAllApplicationTypeHandler(rec, req) | ||
| assert.True(t, rec.Code == http.StatusOK || rec.Code == http.StatusInternalServerError) | ||
| } | ||
|
|
||
| func TestGetApplicationTypeHandler(t *testing.T) { | ||
| req := httptest.NewRequest(http.MethodGet, "/api/application-types/{id}", nil) | ||
| req = mux.SetURLVars(req, map[string]string{"id": "nonexistentID"}) | ||
| rec := httptest.NewRecorder() | ||
| GetApplicationTypeHandler(rec, req) | ||
| assert.Equal(t, http.StatusInternalServerError, rec.Code) | ||
|
|
||
| req = mux.SetURLVars(req, map[string]string{"id": ""}) | ||
| rec = httptest.NewRecorder() | ||
| GetApplicationTypeHandler(rec, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
| } | ||
|
|
||
| func TestUpdateApplicationTypeHandler(t *testing.T) { | ||
| req := httptest.NewRequest(http.MethodPut, "/api/application-types/123", nil) | ||
| req = mux.SetURLVars(req, map[string]string{"id": "123"}) | ||
| rec := httptest.NewRecorder() | ||
|
|
||
| UpdateApplicationTypeHandler(rec, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
| assert.Contains(t, rec.Body.String(), "responsewriter cast error") | ||
|
|
||
| rec = httptest.NewRecorder() | ||
| w := xwhttp.NewXResponseWriter(rec) | ||
| invalidJson := `{invalid json}` | ||
| w.SetBody(invalidJson) | ||
| UpdateApplicationTypeHandler(w, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
|
|
||
| rec = httptest.NewRecorder() | ||
| w = xwhttp.NewXResponseWriter(rec) | ||
| validJson := `{"name": "updatedApp"}` | ||
| w.SetBody(validJson) | ||
| UpdateApplicationTypeHandler(w, req) | ||
| assert.True(t, rec.Code == http.StatusOK || rec.Code == http.StatusBadRequest || rec.Code == http.StatusInternalServerError) | ||
|
|
||
| rec = httptest.NewRecorder() | ||
| w = xwhttp.NewXResponseWriter(rec) | ||
| validJson = `{"name": "updatedAppType"}` | ||
| w.SetBody(validJson) | ||
| UpdateApplicationTypeHandler(w, req) | ||
| assert.True(t, rec.Code == http.StatusOK || rec.Code == http.StatusBadRequest || rec.Code == http.StatusInternalServerError) | ||
|
|
||
| req = mux.SetURLVars(req, map[string]string{"id": ""}) | ||
| rec = httptest.NewRecorder() | ||
| UpdateApplicationTypeHandler(rec, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
| } | ||
|
|
||
| func TestDeleteApplicationTypeHandler(t *testing.T) { | ||
| req := httptest.NewRequest(http.MethodDelete, "/api/application-types/{id}", nil) | ||
| req = mux.SetURLVars(req, map[string]string{"id": "nonexistentID"}) | ||
| rec := httptest.NewRecorder() | ||
| DeleteApplicationTypeHandler(rec, req) | ||
| assert.Equal(t, http.StatusInternalServerError, rec.Code) | ||
|
|
||
| req = mux.SetURLVars(req, map[string]string{"id": ""}) | ||
| rec = httptest.NewRecorder() | ||
| DeleteApplicationTypeHandler(rec, req) | ||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.