diff --git a/internal/testlib/server.go b/internal/testlib/server.go index 58f1a477..d13e7691 100644 --- a/internal/testlib/server.go +++ b/internal/testlib/server.go @@ -181,4 +181,4 @@ func AddPutErrorToMux(uri, body string, statusCode int) { statusCode = http.StatusInternalServerError } addResponse(uri, body, http.MethodPut, statusCode) -} \ No newline at end of file +} diff --git a/pkg/services/applications.go b/pkg/services/applications.go index 515de069..fdb4c167 100644 --- a/pkg/services/applications.go +++ b/pkg/services/applications.go @@ -12,28 +12,33 @@ import ( "github.com/itential/ipctl/pkg/logger" ) +// ApplicationOperationResponse represents the response structure for application operations type ApplicationOperationResponse struct { Status string `json:"status"` Message string `json:"message"` } +// Application represents an application configuration in the Itential platform type Application struct { - Name string `json:"name"` - Type string `json:"type"` - Model string `json:"model"` - Properties map[string]interface{} `json:"properties"` - IsEncrypted bool `json:"isEncrypted"` - LoggerProperties map[string]interface{} `json:"loggerProps"` + Name string `json:"name"` // Name of the application + Type string `json:"type"` // Type of the application + Model string `json:"model"` // Model used by the application + Properties map[string]interface{} `json:"properties"` // Application-specific properties + IsEncrypted bool `json:"isEncrypted"` // Whether the application uses encryption + LoggerProperties map[string]interface{} `json:"loggerProps"` // Logger configuration properties } +// ApplicationService provides methods to manage applications in the Itential platform type ApplicationService struct { client *ServiceClient } +// NewApplicationService creates a new ApplicationService instance with the provided client func NewApplicationService(c client.Client) *ApplicationService { return &ApplicationService{client: NewServiceClient(c)} } +// GetAll retrieves all applications from the Itential platform func (svc *ApplicationService) GetAll() ([]Application, error) { logger.Trace() @@ -65,6 +70,7 @@ func (svc *ApplicationService) GetAll() ([]Application, error) { return values, nil } +// Get retrieves a specific application by name from the Itential platform func (svc *ApplicationService) Get(name string) (*Application, error) { logger.Trace() @@ -86,6 +92,7 @@ func (svc *ApplicationService) Get(name string) (*Application, error) { return res.Data, nil } +// Create creates a new application in the Itential platform func (svc *ApplicationService) Create(in Application) (*Application, error) { logger.Trace() @@ -110,6 +117,7 @@ func (svc *ApplicationService) Create(in Application) (*Application, error) { return res.Data, nil } +// Start starts the specified application by name func (svc *ApplicationService) Start(name string) error { logger.Trace() @@ -130,6 +138,7 @@ func (svc *ApplicationService) Start(name string) error { return nil } +// Stop stops the specified application by name func (svc *ApplicationService) Stop(name string) error { logger.Trace() @@ -150,6 +159,7 @@ func (svc *ApplicationService) Stop(name string) error { return nil } +// Restart restarts the specified application by name func (svc *ApplicationService) Restart(name string) error { logger.Trace() diff --git a/pkg/services/applications_test.go b/pkg/services/applications_test.go index 269dcec0..770d07fa 100644 --- a/pkg/services/applications_test.go +++ b/pkg/services/applications_test.go @@ -5,6 +5,7 @@ package services import ( + "net/http" "path/filepath" "reflect" "testing" @@ -14,8 +15,12 @@ import ( ) var ( - applicationsGetAllSuccess = "applications/getall.success.json" - applicationsGetSuccess = "applications/get.success.json" + applicationsGetAllSuccess = "applications/getall.success.json" + applicationsGetSuccess = "applications/get.success.json" + applicationsCreateSuccess = "applications/create.success.json" + applicationsStartSuccess = "applications/start.success.json" + applicationsStopSuccess = "applications/stop.success.json" + applicationsRestartSuccess = "applications/restart.success.json" ) func setupApplicationService() *ApplicationService { @@ -62,3 +67,105 @@ func TestApplicationsGet(t *testing.T) { assert.Equal(t, "WorkFlowEngine", res.Name) } } + +func TestNewApplicationService(t *testing.T) { + client := testlib.Setup() + defer testlib.Teardown() + + svc := NewApplicationService(client) + + assert.NotNil(t, svc) + assert.NotNil(t, svc.client) + assert.Equal(t, reflect.TypeOf((*ApplicationService)(nil)), reflect.TypeOf(svc)) +} + +func TestApplicationsCreate(t *testing.T) { + svc := setupApplicationService() + defer testlib.Teardown() + + app := Application{ + Name: "TestApplication", + Type: "service", + Model: "App", + Properties: map[string]interface{}{ + "version": "1.0.0", + "description": "Test application", + }, + IsEncrypted: false, + LoggerProperties: map[string]interface{}{ + "level": "info", + }, + } + + for _, ele := range fixtureSuites { + response := testlib.Fixture( + filepath.Join(fixtureRoot, ele, applicationsCreateSuccess), + ) + + testlib.AddPostResponseToMux("/applications", response, http.StatusOK) + + res, err := svc.Create(app) + + assert.Nil(t, err) + assert.NotNil(t, res) + assert.Equal(t, reflect.TypeOf((*Application)(nil)), reflect.TypeOf(res)) + assert.Equal(t, "TestApplication", res.Name) + } +} + +func TestApplicationsStart(t *testing.T) { + svc := setupApplicationService() + defer testlib.Teardown() + + appName := "TestApplication" + + for _, ele := range fixtureSuites { + response := testlib.Fixture( + filepath.Join(fixtureRoot, ele, applicationsStartSuccess), + ) + + testlib.AddPutResponseToMux("/applications/TestApplication/start", response, 0) + + err := svc.Start(appName) + + assert.Nil(t, err) + } +} + +func TestApplicationsStop(t *testing.T) { + svc := setupApplicationService() + defer testlib.Teardown() + + appName := "TestApplication" + + for _, ele := range fixtureSuites { + response := testlib.Fixture( + filepath.Join(fixtureRoot, ele, applicationsStopSuccess), + ) + + testlib.AddPutResponseToMux("/applications/TestApplication/stop", response, 0) + + err := svc.Stop(appName) + + assert.Nil(t, err) + } +} + +func TestApplicationsRestart(t *testing.T) { + svc := setupApplicationService() + defer testlib.Teardown() + + appName := "TestApplication" + + for _, ele := range fixtureSuites { + response := testlib.Fixture( + filepath.Join(fixtureRoot, ele, applicationsRestartSuccess), + ) + + testlib.AddPutResponseToMux("/applications/TestApplication/restart", response, 0) + + err := svc.Restart(appName) + + assert.Nil(t, err) + } +} diff --git a/pkg/services/testdata/2023.2/applications/create.success.json b/pkg/services/testdata/2023.2/applications/create.success.json new file mode 100644 index 00000000..7ad72872 --- /dev/null +++ b/pkg/services/testdata/2023.2/applications/create.success.json @@ -0,0 +1,17 @@ +{ + "status": "success", + "message": "Successfully created application", + "data": { + "name": "TestApplication", + "type": "service", + "model": "App", + "properties": { + "version": "1.0.0", + "description": "Test application" + }, + "isEncrypted": false, + "loggerProps": { + "level": "info" + } + } +} \ No newline at end of file diff --git a/pkg/services/testdata/2023.2/applications/restart.success.json b/pkg/services/testdata/2023.2/applications/restart.success.json new file mode 100644 index 00000000..2a761683 --- /dev/null +++ b/pkg/services/testdata/2023.2/applications/restart.success.json @@ -0,0 +1,4 @@ +{ + "status": "success", + "message": "Application restarted successfully" +} \ No newline at end of file diff --git a/pkg/services/testdata/2023.2/applications/start.success.json b/pkg/services/testdata/2023.2/applications/start.success.json new file mode 100644 index 00000000..971acc7a --- /dev/null +++ b/pkg/services/testdata/2023.2/applications/start.success.json @@ -0,0 +1,4 @@ +{ + "status": "success", + "message": "Application started successfully" +} \ No newline at end of file diff --git a/pkg/services/testdata/2023.2/applications/stop.success.json b/pkg/services/testdata/2023.2/applications/stop.success.json new file mode 100644 index 00000000..51275987 --- /dev/null +++ b/pkg/services/testdata/2023.2/applications/stop.success.json @@ -0,0 +1,4 @@ +{ + "status": "success", + "message": "Application stopped successfully" +} \ No newline at end of file