change in the error msg for the getModelInfo 65/14265/15
authorrajdeep11 <rajdeep.sin@samsung.com>
Mon, 17 Mar 2025 09:25:28 +0000 (14:55 +0530)
committerRajdeep Singh <rajdeep.sin@samsung.com>
Thu, 15 May 2025 10:15:12 +0000 (10:15 +0000)
Change-Id: I34f97618788e747ea417f83d08e5ab552d2c619e
Signed-off-by: rajdeep11 <rajdeep.sin@samsung.com>
apis/mmes_apis.go
apis_test/mmes_apis_test.go

index 2807bcc..0ec3ea8 100644 (file)
@@ -128,8 +128,11 @@ func (m *MmeApiHandler) GetModelInfo(cont *gin.Context) {
 
        for key := range queryParams {
                if !allowedParams[key] {
-                       cont.JSON(http.StatusBadRequest, gin.H{
-                               "error": "Only modelName and modelVersion are allowed",
+                       logging.ERROR("error:", "Only allowed params are modelname and modelversion")
+                       cont.JSON(http.StatusBadRequest, models.ProblemDetail{
+                               Status: http.StatusBadRequest,
+                               Title: "Bad Request",
+                               Detail: fmt.Sprintf("Only allowed params are modelname and modelversion"),
                        })
                        return
                }
@@ -159,9 +162,10 @@ func (m *MmeApiHandler) GetModelInfo(cont *gin.Context) {
                        if err != nil {
                                statusCode := http.StatusInternalServerError
                                logging.ERROR("Error occurred, send status code: ", statusCode)
-                               cont.JSON(statusCode, gin.H{
-                                       "code":    statusCode,
-                                       "message": "Unexpected Error in server, you can't get model information list",
+                               cont.JSON(statusCode, models.ProblemDetail{
+                                       Status: http.StatusInternalServerError,
+                                       Title: "Internal Server Error",
+                                       Detail: fmt.Sprintf("Can't fetch the models due to , %s", err.Error()),
                                })
                                return
                        }
@@ -175,19 +179,20 @@ func (m *MmeApiHandler) GetModelInfo(cont *gin.Context) {
                        if err != nil {
                                statusCode := http.StatusInternalServerError
                                logging.ERROR("Error occurred, send status code: ", statusCode)
-                               cont.JSON(statusCode, gin.H{
-                                       "code":    statusCode,
-                                       "message": "Unexpected Error in server, you can't get model information list",
+                               cont.JSON(statusCode, models.ProblemDetail{
+                                       Status: http.StatusInternalServerError,
+                                       Title:  "Internal Server Error",
+                                       Detail: fmt.Sprintf("Can't fetch all the models due to , %s", err.Error()),
                                })
                                return
                        }
                        if modelInfo.ModelId.ModelName != modelName && modelInfo.ModelId.ModelVersion != modelVersion {
                                statusCode := http.StatusNotFound
-                               errMessage := fmt.Sprintf("Record not found with modelName: %s and modelVersion: %s", modelName, modelVersion)
                                logging.ERROR("Record not found, send status code: ", statusCode)
-                               cont.JSON(statusCode, gin.H{
-                                       "code":    statusCode,
-                                       "message": errMessage,
+                               cont.JSON(statusCode, models.ProblemDetail{
+                                       Status: http.StatusInternalServerError,
+                                       Title:  "Internal Server Error",
+                                       Detail: fmt.Sprintf("Record not found with modelName: %s and modelVersion: %s", modelName, modelVersion),
                                })
                                return
                        }
index 2c6a268..728928d 100644 (file)
@@ -133,6 +133,27 @@ func (i *iDBMock) Delete(id string) (int64, error) {
        return 1, nil
 }
 
+func (i *iDBMock) GetModelInfoByName(modelName string) ([]models.ModelRelatedInformation, error) {
+       args := i.Called()
+       if _, ok := args.Get(1).(error); !ok {
+               return args.Get(0).([]models.ModelRelatedInformation), nil
+       } else {
+               var emptyModelInfo []models.ModelRelatedInformation
+               return emptyModelInfo, args.Error(1)
+       }
+}
+
+func (i *iDBMock) GetModelInfoByNameAndVer(modelName string, modelVersion string) (*models.ModelRelatedInformation, error) {
+       args := i.Called()
+
+       if _, ok := args.Get(1).(error); !ok {
+               return args.Get(0).(*models.ModelRelatedInformation), nil
+       } else {
+               var emptyModelInfo *models.ModelRelatedInformation
+               return emptyModelInfo, args.Error(1)
+       }
+}
+
 func TestRegisterModel(t *testing.T) {
        os.Setenv("LOG_FILE_NAME", "testing")
        iDBMockInst := new(iDBMock)
@@ -272,3 +293,154 @@ func TestWhenFailGetModelInfoList(t *testing.T) {
 
        assert.Equal(t, 500, responseRecorder.Code)
 }
+
+func TestGetModelInfoParamsInvalid(t *testing.T) {
+       // Setting ENV
+       os.Setenv("LOG_FILE_NAME", "testing")
+
+       // Setting Mock
+       iDBMockInst := new(iDBMock)
+
+       handler := apis.NewMmeApiHandler(nil, iDBMockInst)
+       router := routers.InitRouter(handler)
+       responseRecorder := httptest.NewRecorder()
+
+       req, _ := http.NewRequest("GET", "/ai-ml-model-discovery/v1/models?model-me=qoe2", nil)
+
+       router.ServeHTTP(responseRecorder, req)
+
+       body, _ := io.ReadAll(responseRecorder.Body)
+       fmt.Println(responseRecorder)
+       assert.Equal(t, 400, responseRecorder.Code)
+       assert.Equal(t, `{"status":400,"title":"Bad Request","detail":"Only allowed params are modelname and modelversion"}`, string(body))
+}
+
+func TestGetModelInfoByNameSuccess(t *testing.T) {
+       // Setting ENV
+       os.Setenv("LOG_FILE_NAME", "testing")
+
+       // Setting Mock
+       iDBMockInst := new(iDBMock)
+       iDBMockInst.On("GetModelInfoByName").Return([]models.ModelRelatedInformation{
+               {
+                       Id: "1234",
+                       ModelId: models.ModelID{
+                               ModelName:    "test",
+                               ModelVersion: "v1.0",
+                       },
+                       Description: "this is test modelINfo",
+                       ModelInformation: models.ModelInformation{
+                               Metadata: models.Metadata{
+                                       Author: "someone",
+                               },
+                               InputDataType:  "pdcpBytesDl,pdcpBytesUl,kpi",
+                               OutputDataType: "c,d",
+                       },
+               },
+       }, nil)
+       handler := apis.NewMmeApiHandler(nil, iDBMockInst)
+       router := routers.InitRouter(handler)
+       responseRecorder := httptest.NewRecorder()
+
+       req, _ := http.NewRequest("GET", "/ai-ml-model-discovery/v1/models?model-name=qoe1", nil)
+
+       router.ServeHTTP(responseRecorder, req)
+
+       response := responseRecorder.Result()
+       body, _ := io.ReadAll(response.Body)
+
+       var modelInfos []models.ModelRelatedInformation
+       logging.INFO("modelinfo", "list:", modelInfos)
+       json.Unmarshal(body, &modelInfos)
+
+       assert.Equal(t, 200, responseRecorder.Code)
+       assert.Equal(t, "1234", modelInfos[0].Id)
+}
+
+func TestGetModelInfoByNameFail(t *testing.T) {
+       // Setting ENV
+       os.Setenv("LOG_FILE_NAME", "testing")
+
+       // Setting Mock
+       iDBMockInst := new(iDBMock)
+       iDBMockInst.On("GetModelInfoByName").Return([]models.ModelRelatedInformation{}, fmt.Errorf("db not available"))
+
+       handler := apis.NewMmeApiHandler(nil, iDBMockInst)
+       router := routers.InitRouter(handler)
+       responseRecorder := httptest.NewRecorder()
+
+       req, _ := http.NewRequest("GET", "/ai-ml-model-discovery/v1/models?model-name=qoe1", nil)
+       router.ServeHTTP(responseRecorder, req)
+
+       body, _ := io.ReadAll(responseRecorder.Body)
+
+       assert.Equal(t, 500, responseRecorder.Code)
+       assert.Equal(t, `{"status":500,"title":"Internal Server Error","detail":"Can't fetch the models due to , db not available"}`, string(body))
+}
+
+func TestGetModelInfoByNameAndVersionSuccess(t *testing.T) {
+       // Setting ENV
+       os.Setenv("LOG_FILE_NAME", "testing")
+
+       iDBMockInst := new(iDBMock)
+
+       modelInfo := models.ModelRelatedInformation{
+               Id: "1234",
+               ModelId: models.ModelID{
+                       ModelName:    "test",
+                       ModelVersion: "v1.0",
+               },
+               Description: "this is test modelINfo",
+               ModelInformation: models.ModelInformation{
+                       Metadata: models.Metadata{
+                               Author: "someone",
+                       },
+                       InputDataType:  "pdcpBytesDl,pdcpBytesUl,kpi",
+                       OutputDataType: "c,d",
+               },
+       }
+
+       iDBMockInst.On("GetModelInfoByNameAndVer").Return(&modelInfo, nil)
+
+       handler := apis.NewMmeApiHandler(nil, iDBMockInst)
+       router := routers.InitRouter(handler)
+       responseRecorder := httptest.NewRecorder()
+
+       req, _ := http.NewRequest("GET", "/ai-ml-model-discovery/v1/models?model-name=test&model-version=v1.0", nil)
+
+       router.ServeHTTP(responseRecorder, req)
+
+       response := responseRecorder.Result()
+       body, _ := io.ReadAll(response.Body)
+
+       var modelInfos []models.ModelRelatedInformation
+       logging.INFO("modelinfo", "list:", modelInfos)
+       json.Unmarshal(body, &modelInfos)
+
+       assert.Equal(t, 200, responseRecorder.Code)
+       assert.Equal(t, "1234", modelInfos[0].Id)
+}
+
+func TestGetModelInfoByNameAndVersionFail(t *testing.T) {
+       // Setting ENV
+       os.Setenv("LOG_FILE_NAME", "testing")
+
+       iDBMockInst := new(iDBMock)
+       modelInfo := models.ModelRelatedInformation{}
+       iDBMockInst.On("GetModelInfoByNameAndVer").Return(&modelInfo, fmt.Errorf("db not available"))
+
+       handler := apis.NewMmeApiHandler(nil, iDBMockInst)
+       router := routers.InitRouter(handler)
+       responseRecorder := httptest.NewRecorder()
+
+       req, _ := http.NewRequest("GET", "/ai-ml-model-discovery/v1/models?model-name=test&model-version=v1.0", nil)
+
+       router.ServeHTTP(responseRecorder, req)
+
+       response := responseRecorder.Result()
+       fmt.Println(responseRecorder)
+       body, _ := io.ReadAll(response.Body)
+
+       assert.Equal(t, 500, responseRecorder.Code)
+       assert.Equal(t, `{"status":500,"title":"Internal Server Error","detail":"Can't fetch all the models due to , db not available"}`, string(body))
+}