Files
houston-be/pkg/monitoringService/ee_monitoring_service_client_test.go
2024-11-29 19:12:22 +05:30

121 lines
3.7 KiB
Go

package monitoringService
import (
"bytes"
"encoding/json"
"errors"
"github.com/gojuno/minimock/v3"
"github.com/google/uuid"
"github.com/spf13/viper"
"github.com/stretchr/testify/suite"
"houston/contracts"
"houston/logger"
"houston/mocks"
service "houston/service/response"
"net/http"
"testing"
)
type EEMonitoringServiceSuite struct {
suite.Suite
}
func (suite *EEMonitoringServiceSuite) SetupSuite() {
logger.InitLogger()
viper.Set("EE_MONITORING_SERVICE_BASEURL", "http://localhost:8082")
viper.Set("DEFAULT_EE_MONITORING_SERVICE_TIMEOUT", "5s")
viper.Set("EE_MONITORING_SERVICE_WEBHOOK_API_PATH", "/monitoring-webhook/trigger")
viper.Set("DEFAULT_HTTP_REQUEST_TIMEOUT", "5s")
}
func (suite *EEMonitoringServiceSuite) Test_TriggerWebhook_ErrorResponseCase() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
// Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_EE_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("EE_MONITORING_SERVICE_BASEURL") + viper.GetString("EE_MONITORING_SERVICE_WEBHOOK_API_PATH")
payload, _ := json.Marshal(getRequestBody())
requestHeaders := map[string]string{
"Content-Type": "application/json",
"X-Correlation-Id": requestId,
"correlationId": requestId,
}
restClient.PostWithTimeoutMock.When(fullURL, *bytes.NewBuffer(payload), requestHeaders, defaultTimeOut,
nil).Then(nil, errors.New("error while getting grafana images"))
responseChannel := make(chan service.MonitoringServiceClientResponse)
defer close(responseChannel)
err := NewEEMonitoringServiceClient(restClient).TriggerSeverityUpdateWebhook(
1, "test", requestId, getCurrentSeverity(), "Houston-testing", "slack-channel",
)
suite.Error(err)
}
func (suite *EEMonitoringServiceSuite) Test_TriggerWebhook_SuccessCase() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
// Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_EE_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("EE_MONITORING_SERVICE_BASEURL") + viper.GetString("EE_MONITORING_SERVICE_WEBHOOK_API_PATH")
payload, _ := json.Marshal(getRequestBody())
requestHeaders := map[string]string{
"Content-Type": "application/json",
"X-Correlation-Id": requestId,
"correlationId": requestId,
}
response := &http.Response{
StatusCode: 200,
Body: nil,
}
restClient.PostWithTimeoutMock.When(fullURL, *bytes.NewBuffer(payload), requestHeaders, defaultTimeOut,
nil).Then(response, nil)
responseChannel := make(chan service.MonitoringServiceClientResponse)
defer close(responseChannel)
err := NewEEMonitoringServiceClient(restClient).TriggerSeverityUpdateWebhook(
1, "test", requestId, getCurrentSeverity(), "Houston-testing", "slack-channel",
)
suite.Nil(err)
}
func getRequestBody() *contracts.SeverityUpdateWebhookRequest {
incidentDetails := contracts.IncidentDetails{
IncidentType: "HOUSTON",
IncidentID: 1,
}
metaData := contracts.SeverityUpdateRequestMetaData{
CurrentSeverity: getCurrentSeverity(),
UpdatedBy: "test",
ResponderTeam: "Houston-testing",
SlackChannelId: "slack-channel",
}
return &contracts.SeverityUpdateWebhookRequest{
Requester: "HOUSTON",
RequestType: "HOUSTON_SEVERITY_UPDATE",
IncidentDetails: incidentDetails,
RequestMetaData: metaData,
}
}
func getCurrentSeverity() contracts.SeverityInfo {
return contracts.SeverityInfo{Name: "Sev-0", ID: 1}
}
func TestEEMonitoringServiceClient(t *testing.T) {
suite.Run(t, new(EEMonitoringServiceSuite))
}