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
2 changes: 1 addition & 1 deletion internal/testlib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ func AddPutErrorToMux(uri, body string, statusCode int) {
statusCode = http.StatusInternalServerError
}
addResponse(uri, body, http.MethodPut, statusCode)
}
}
22 changes: 16 additions & 6 deletions pkg/services/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand Down
111 changes: 109 additions & 2 deletions pkg/services/applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package services

import (
"net/http"
"path/filepath"
"reflect"
"testing"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}
17 changes: 17 additions & 0 deletions pkg/services/testdata/2023.2/applications/create.success.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "success",
"message": "Application restarted successfully"
}
4 changes: 4 additions & 0 deletions pkg/services/testdata/2023.2/applications/start.success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "success",
"message": "Application started successfully"
}
4 changes: 4 additions & 0 deletions pkg/services/testdata/2023.2/applications/stop.success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "success",
"message": "Application stopped successfully"
}