122 lines
3.6 KiB
Go
122 lines
3.6 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/common/util"
|
|
"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": util.ContentTypeJSON,
|
|
"CORRELATION_ID": 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, getOldSeverity(), getNewSeverity(),
|
|
)
|
|
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": util.ContentTypeJSON,
|
|
"CORRELATION_ID": 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, getOldSeverity(), getNewSeverity(),
|
|
)
|
|
|
|
suite.Nil(err)
|
|
}
|
|
|
|
func getRequestBody() *contracts.SeverityUpdateWebhookRequest {
|
|
incidentDetails := contracts.IncidentDetails{
|
|
IncidentType: "HOUSTON",
|
|
IncidentID: 1,
|
|
}
|
|
metaData := contracts.SeverityUpdateRequestMetaData{
|
|
OldSeverity: getOldSeverity(),
|
|
NewSeverity: getNewSeverity(),
|
|
UpdatedBy: "test",
|
|
}
|
|
return &contracts.SeverityUpdateWebhookRequest{
|
|
Requester: "HOUSTON",
|
|
RequestType: "HOUSTON_SEVERITY_UPDATE",
|
|
IncidentDetails: incidentDetails,
|
|
RequestMetaData: metaData,
|
|
}
|
|
}
|
|
|
|
func getOldSeverity() contracts.SeverityInfo {
|
|
return contracts.SeverityInfo{Name: "Sev-1", ID: 2}
|
|
}
|
|
func getNewSeverity() contracts.SeverityInfo {
|
|
return contracts.SeverityInfo{Name: "Sev-0", ID: 1}
|
|
}
|
|
|
|
func TestEEMonitoringServiceClient(t *testing.T) {
|
|
suite.Run(t, new(EEMonitoringServiceSuite))
|
|
}
|