Files
houston-be/service/monitoringService/monitoring_service_client_test.go
Sriram Bhargav e7d60fa70b TP-49969 | Adding helper in monitoring service client to fetch impacted customers (#309)
* TP-49969 | Adding helper in monitoring service client to fetch impacted customers

* TP-49969 | Fixing file extension issue
2023-12-12 10:27:04 +05:30

360 lines
14 KiB
Go

package monitoringService
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/gojuno/minimock/v3"
"github.com/google/uuid"
"github.com/spf13/viper"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"houston/common/util"
"houston/logger"
"houston/mocks"
"houston/model"
service "houston/service/response"
"io"
"net/http"
"testing"
)
type MonitoringServiceSuite struct {
suite.Suite
}
func (suite *MonitoringServiceSuite) SetupSuite() {
logger.InitLogger()
viper.Set("MONITORING_SERVICE_BASEURL", "http://localhost:8082")
viper.Set("DEFAULT_MONITORING_SERVICE_TIMEOUT", "5s")
viper.Set("MONITORING_SERVICE_GET_GRAFANA_IMAGES_PATH", "/incident/v1/grafanaScreenshot")
viper.Set("MONITORING_SERVICE_GET_IMPACTED_CUSTOMERS_CSV_PATH", "/incident/v1/impactedCustomers")
viper.Set("MAX_FILE_SIZE", 10)
viper.Set("DEFAULT_HTTP_REQUEST_TIMEOUT", "5s")
}
func (suite *MonitoringServiceSuite) Test_GetGrafanaImages_500Case() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
incidents := []string{"incident1", "incident2"}
// Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("MONITORING_SERVICE_BASEURL") + viper.GetString("MONITORING_SERVICE_GET_GRAFANA_IMAGES_PATH")
getGrafanaImagesRequestBody := model.GetGrafanaImagesRequest{
Incidents: incidents,
}
payload, _ := json.Marshal(getGrafanaImagesRequestBody)
requestHeaders := map[string]string{
"Content-Type": util.ContentTypeJSON,
"CORRELATION_ID": requestId,
}
response := &http.Response{
StatusCode: 500,
Body: nil,
}
restClient.PostWithTimeoutMock.When(fullURL, *bytes.NewBuffer(payload), requestHeaders, defaultTimeOut,
nil).Then(response, nil)
responseChannel := make(chan service.MonitoringServiceClientResponse)
defer close(responseChannel)
go NewMonitoringServiceActionsImpl(restClient).GetGrafanaImages(incidents, requestId, responseChannel)
select {
case monitoringServiceResponse := <-responseChannel:
logger.Info("monitoringServiceResponse", zap.Any("monitoringServiceResponse", monitoringServiceResponse))
suite.Empty(monitoringServiceResponse.DirectoryPath)
suite.Equal(monitoringServiceResponse.Error, errors.New("error while generating grafana images"))
}
}
func (suite *MonitoringServiceSuite) Test_GetGrafanaImages_ErrorResponseCase() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
incidents := []string{"incident1", "incident2"}
// Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("MONITORING_SERVICE_BASEURL") + viper.GetString("MONITORING_SERVICE_GET_GRAFANA_IMAGES_PATH")
getGrafanaImagesRequestBody := model.GetGrafanaImagesRequest{
Incidents: incidents,
}
payload, _ := json.Marshal(getGrafanaImagesRequestBody)
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)
go NewMonitoringServiceActionsImpl(restClient).GetGrafanaImages(incidents, requestId, responseChannel)
select {
case monitoringServiceResponse := <-responseChannel:
logger.Info("monitoringServiceResponse", zap.Any("monitoringServiceResponse", monitoringServiceResponse))
suite.Empty(monitoringServiceResponse.DirectoryPath)
suite.Equal(monitoringServiceResponse.Error, errors.New("error while getting grafana images"))
}
}
func (suite *MonitoringServiceSuite) Test_GetGrafanaImages_EmptyResponseBodyCase() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
incidents := []string{"incident1", "incident2"}
// Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("MONITORING_SERVICE_BASEURL") + viper.GetString("MONITORING_SERVICE_GET_GRAFANA_IMAGES_PATH")
getGrafanaImagesRequestBody := model.GetGrafanaImagesRequest{
Incidents: incidents,
}
payload, _ := json.Marshal(getGrafanaImagesRequestBody)
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)
go NewMonitoringServiceActionsImpl(restClient).GetGrafanaImages(incidents, requestId, responseChannel)
select {
case monitoringServiceResponse := <-responseChannel:
suite.Empty(monitoringServiceResponse.DirectoryPath)
suite.Equal(monitoringServiceResponse.Error, errors.New("empty response body received from monitoring service"))
}
}
func (suite *MonitoringServiceSuite) Test_GetImpactedCustomersCSV_MonitoringService_500Case() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
incidents := []string{"incident1", "incident2"}
//Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("MONITORING_SERVICE_BASEURL") + viper.GetString("MONITORING_SERVICE_GET_IMPACTED_CUSTOMERS_CSV_PATH")
getImpactedCustomersCSVRequestBody := model.GetGrafanaImagesRequest{
Incidents: incidents,
}
response := &http.Response{
StatusCode: 500,
}
payload, _ := json.Marshal(getImpactedCustomersCSVRequestBody)
requestHeaders := map[string]string{
"Content-Type": "application/json",
"CORRELATION_ID": requestId,
}
restClient.PostWithTimeoutMock.When(fullURL, *bytes.NewBuffer(payload), requestHeaders, defaultTimeOut, nil).Then(response, nil)
responseChannel := make(chan service.MonitoringServiceClientResponse)
defer close(responseChannel)
go NewMonitoringServiceActionsImpl(restClient).GetImpactedCustomersCSV(incidents, requestId, responseChannel)
select {
case monitoringServiceResponse := <-responseChannel:
suite.Empty(monitoringServiceResponse.DirectoryPath)
suite.Equal(errors.New("error while generating impacted customers csv"), monitoringServiceResponse.Error)
}
}
func (suite *MonitoringServiceSuite) Test_GetImpactedCustomersCSV_MonitoringService_EmptyBodyCase() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
incidents := []string{"incident1", "incident2"}
//Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("MONITORING_SERVICE_BASEURL") + viper.GetString("MONITORING_SERVICE_GET_IMPACTED_CUSTOMERS_CSV_PATH")
getImpactedCustomersCSVRequestBody := model.GetGrafanaImagesRequest{
Incidents: incidents,
}
response := &http.Response{
StatusCode: 200,
Body: nil,
}
payload, _ := json.Marshal(getImpactedCustomersCSVRequestBody)
requestHeaders := map[string]string{
"Content-Type": "application/json",
"CORRELATION_ID": requestId,
}
restClient.PostWithTimeoutMock.When(fullURL, *bytes.NewBuffer(payload), requestHeaders, defaultTimeOut, nil).Then(response, nil)
responseChannel := make(chan service.MonitoringServiceClientResponse)
defer close(responseChannel)
go NewMonitoringServiceActionsImpl(restClient).GetImpactedCustomersCSV(incidents, requestId, responseChannel)
select {
case monitoringServiceResponse := <-responseChannel:
suite.Empty(monitoringServiceResponse.DirectoryPath)
suite.Equal(errors.New("empty response body received from monitoring service"), monitoringServiceResponse.Error)
}
}
func (suite *MonitoringServiceSuite) Test_GetImpactedCustomersCSV_MonitoringService_NilResponseCase() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
incidents := []string{"incident1", "incident2"}
//Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("MONITORING_SERVICE_BASEURL") + viper.GetString("MONITORING_SERVICE_GET_IMPACTED_CUSTOMERS_CSV_PATH")
getImpactedCustomersCSVRequestBody := model.GetGrafanaImagesRequest{
Incidents: incidents,
}
payload, _ := json.Marshal(getImpactedCustomersCSVRequestBody)
requestHeaders := map[string]string{
"Content-Type": "application/json",
"CORRELATION_ID": requestId,
}
restClient.PostWithTimeoutMock.When(fullURL, *bytes.NewBuffer(payload), requestHeaders, defaultTimeOut,
nil).Then(nil, nil)
responseChannel := make(chan service.MonitoringServiceClientResponse)
defer close(responseChannel)
go NewMonitoringServiceActionsImpl(restClient).GetImpactedCustomersCSV(incidents, requestId, responseChannel)
select {
case monitoringServiceResponse := <-responseChannel:
suite.Empty(monitoringServiceResponse.DirectoryPath)
suite.Equal(errors.New("no response received from monitoring service"), monitoringServiceResponse.Error)
}
}
func (suite *MonitoringServiceSuite) Test_GetImpactedCustomersCSV_MonitoringService_ErrorResponseCase() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
incidents := []string{"incident1", "incident2"}
//Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("MONITORING_SERVICE_BASEURL") + viper.GetString("MONITORING_SERVICE_GET_IMPACTED_CUSTOMERS_CSV_PATH")
getImpactedCustomersCSVRequestBody := model.GetGrafanaImagesRequest{
Incidents: incidents,
}
payload, _ := json.Marshal(getImpactedCustomersCSVRequestBody)
requestHeaders := map[string]string{
"Content-Type": "application/json",
"CORRELATION_ID": requestId,
}
restClient.PostWithTimeoutMock.When(fullURL, *bytes.NewBuffer(payload), requestHeaders, defaultTimeOut,
nil).Then(&http.Response{}, errors.New("request timed out"))
responseChannel := make(chan service.MonitoringServiceClientResponse)
defer close(responseChannel)
go NewMonitoringServiceActionsImpl(restClient).GetImpactedCustomersCSV(incidents, requestId, responseChannel)
select {
case monitoringServiceResponse := <-responseChannel:
suite.Empty(monitoringServiceResponse.DirectoryPath)
suite.Equal(errors.New("request timed out"), monitoringServiceResponse.Error)
}
}
func (suite *MonitoringServiceSuite) Test_GetImpactedCustomersCSV_MonitoringService_InvalidDataCase() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
incidents := []string{"incident1", "incident2"}
//Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("MONITORING_SERVICE_BASEURL") + viper.GetString("MONITORING_SERVICE_GET_IMPACTED_CUSTOMERS_CSV_PATH")
getImpactedCustomersCSVRequestBody := model.GetGrafanaImagesRequest{
Incidents: incidents,
}
response := &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte("invalid data"))),
}
payload, _ := json.Marshal(getImpactedCustomersCSVRequestBody)
requestHeaders := map[string]string{
"Content-Type": "application/json",
"CORRELATION_ID": requestId,
}
restClient.PostWithTimeoutMock.When(fullURL, *bytes.NewBuffer(payload), requestHeaders, defaultTimeOut, nil).Then(response, nil)
responseChannel := make(chan service.MonitoringServiceClientResponse)
defer close(responseChannel)
go NewMonitoringServiceActionsImpl(restClient).GetImpactedCustomersCSV(incidents, requestId, responseChannel)
select {
case monitoringServiceResponse := <-responseChannel:
suite.Empty(monitoringServiceResponse.DirectoryPath)
suite.Equal(errors.New("error while unmarshalling response body"), monitoringServiceResponse.Error)
}
}
func (suite *MonitoringServiceSuite) Test_GetImpactedCustomersCSV_MonitoringService_DownloadErrorCase() {
controller := minimock.NewController(suite.T())
suite.T().Cleanup(controller.Finish)
requestId := uuid.NewString()
restClient := mocks.NewHttpRestClientMock(controller)
incidents := []string{"incident1", "incident2"}
//Mocking HTTP calls
defaultTimeOut := viper.GetDuration("DEFAULT_MONITORING_SERVICE_TIMEOUT")
fullURL := viper.GetString("MONITORING_SERVICE_BASEURL") + viper.GetString("MONITORING_SERVICE_GET_IMPACTED_CUSTOMERS_CSV_PATH")
getImpactedCustomersCSVRequestBody := model.GetGrafanaImagesRequest{
Incidents: incidents,
}
fileDownloadURLOne := "http://localhost:8080/1"
fileDownloadURLTwo := "http://localhost:8080/2"
monitoringServiceResponse := fmt.Sprintf("[\"%s\",\"%s\"]", fileDownloadURLOne, fileDownloadURLTwo)
response := &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewBufferString(monitoringServiceResponse)),
}
payload, _ := json.Marshal(getImpactedCustomersCSVRequestBody)
requestHeaders := map[string]string{
"Content-Type": "application/json",
"CORRELATION_ID": requestId,
}
restClient.PostWithTimeoutMock.When(fullURL, *bytes.NewBuffer(payload), requestHeaders, defaultTimeOut, nil).Then(response, nil)
restClient.GetMock.When(fileDownloadURLOne, nil, nil, true).Then(nil,
errors.New("error while downloading file"))
restClient.GetMock.When(fileDownloadURLTwo, nil, nil, true).Then(nil,
errors.New("error while downloading file"))
responseChannel := make(chan service.MonitoringServiceClientResponse)
defer close(responseChannel)
go NewMonitoringServiceActionsImpl(restClient).GetImpactedCustomersCSV(incidents, requestId, responseChannel)
select {
case monitoringServiceResponse := <-responseChannel:
suite.Empty(monitoringServiceResponse.DirectoryPath)
suite.Equal(errors.New("error while downloading and saving csv files"), monitoringServiceResponse.Error)
}
}
func TestDriveService(t *testing.T) {
suite.Run(t, new(MonitoringServiceSuite))
}