Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bin/xconfadmin-linux-amd64 -f config/xconfadmin.conf
Test the server is running:

```bash
curl http://localhost:9001/api/v1/version
curl http://localhost:9001/api/version
Comment thread
yeswanth2420 marked this conversation as resolved.
```

Expected response:
Expand Down
21 changes: 17 additions & 4 deletions adminapi/adminapi_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
queries "github.com/rdkcentral/xconfadmin/adminapi/queries"
common "github.com/rdkcentral/xconfadmin/common"
xhttp "github.com/rdkcentral/xconfadmin/http"
xapptype "github.com/rdkcentral/xconfadmin/shared/applicationtype"

log "github.com/sirupsen/logrus"
)
Expand All @@ -52,11 +53,9 @@ func WebServerInjection(ws *xhttp.WebconfigServer, xc *dataapi.XconfConfigs) {
common.WakeupPoolTagName = "t_canary_wakeup"
} else {
common.AuthProvider = ws.XW_XconfServer.ServerConfig.GetString("xconfwebconfig.xconf.authprovider")
Comment thread
yeswanth2420 marked this conversation as resolved.
applicationTypeString := ws.XW_XconfServer.ServerConfig.GetString("xconfwebconfig.xconf.application_types")
if applicationTypeString == "" {
applicationTypeString = "stb"
if len(common.ApplicationTypes) == 0 {
common.ApplicationTypes = []string{"stb"}
}
common.ApplicationTypes = strings.Split(applicationTypeString, ",")
common.CacheUpdateWindowSize = ws.XW_XconfServer.ServerConfig.GetInt64("xconfwebconfig.xconf.cache_update_window_size")
common.SatOn = ws.XW_XconfServer.ServerConfig.GetBoolean("xconfwebconfig.sat.SAT_ON")
common.AllowedNumberOfFeatures = int(ws.XW_XconfServer.ServerConfig.GetInt32("xconfwebconfig.xconf.allowedNumberOfFeatures", 100))
Expand Down Expand Up @@ -115,8 +114,22 @@ func WebServerInjection(ws *xhttp.WebconfigServer, xc *dataapi.XconfConfigs) {
func initDB() {
queries.CreateFirmwareRuleTemplates() // Initialize FirmwareRule templates
initAppSettings() // Initialize Application settings
loadApplicationTypes() // Load Application Types from DB
}

func loadApplicationTypes() {
appTypes, err := xapptype.GetAllApplicationTypeAsList()
if err != nil {
log.Errorf("Error loading application types from DB: %v", err)
return
}
var appTypeNames []string
for _, appType := range appTypes {
appTypeNames = append(appTypeNames, appType.Name)
}
common.ApplicationTypes = appTypeNames
log.Info("Loaded application types from DB")
}
func initAppSettings() {
settings, err := common.GetAppSettings()
if err != nil {
Expand Down
208 changes: 208 additions & 0 deletions adminapi/applicationtype/application_type_handler.go
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)
Comment thread
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 {
Comment thread
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"]
Comment thread
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"]
Comment thread
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
}
Comment thread
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"]
Comment thread
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
}
Comment thread
yeswanth2420 marked this conversation as resolved.
Comment thread
yeswanth2420 marked this conversation as resolved.
if existingAppType.Name != updateRequest.Name {
nameExists, _ := xapptype.ApplicationTypeNameExists(updateRequest.Name)
Comment thread
yeswanth2420 marked this conversation as resolved.
Comment thread
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
}
}
Comment thread
yeswanth2420 marked this conversation as resolved.
if xapptype.IsDefaultAppType(existingAppType.Name) {
xhttp.WriteAdminErrorResponse(w, http.StatusBadRequest, "Default application types cannot be updated")
return
}
Comment thread
yeswanth2420 marked this conversation as resolved.
existingAppType.ID = id
existingAppType.Name = updateRequest.Name
Comment thread
yeswanth2420 marked this conversation as resolved.
if updateRequest.Description != "" {
Comment thread
yeswanth2420 marked this conversation as resolved.
existingAppType.Description = updateRequest.Description
}
Comment on lines +192 to +194
Copy link

Copilot AI Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The update handler only updates the Description field if the request contains a non-empty description (line 180-182). However, this prevents clearing an existing description. Consider allowing explicit clearing of the description field, or document this as intentional behavior.

Suggested change
if updateRequest.Description != "" {
existingAppType.Description = updateRequest.Description
}
existingAppType.Description = updateRequest.Description

Copilot uses AI. Check for mistakes.
existingAppType.UpdatedAt = time.Now().Unix()

err = xapptype.SetOneApplicationType(existingAppType)
if err != nil {
xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
data, err := json.Marshal(existingAppType)
if err != nil {
xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
xhttp.WriteXconfResponse(w, http.StatusOK, data)
}
109 changes: 109 additions & 0 deletions adminapi/applicationtype/application_type_handler_test.go
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)
}
Loading
Loading