github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/whitesource/sytemMock.go (about)

     1  //go:build !release
     2  // +build !release
     3  
     4  package whitesource
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  	"time"
    10  )
    11  
    12  // SystemMock stores a number of WhiteSource objects and, based on that, mocks the behavior of System.
    13  type SystemMock struct {
    14  	ProductName         string
    15  	Products            []Product
    16  	Projects            []Project
    17  	Alerts              []Alert
    18  	IgnoredAlerts       []Alert
    19  	AlertType           string
    20  	AlertError          error
    21  	Libraries           []Library
    22  	RiskReport          []byte
    23  	VulnerabilityReport []byte
    24  }
    25  
    26  func (m *SystemMock) GetProjectIgnoredAlertsByType(projectToken string, alertType string) ([]Alert, error) {
    27  	return m.IgnoredAlerts, nil
    28  }
    29  
    30  // GetProductByName mimics retrieving a Product by name. It returns an error of no Product is stored in the mock.
    31  func (m *SystemMock) GetProductByName(productName string) (Product, error) {
    32  	for _, product := range m.Products {
    33  		if product.Name == productName {
    34  			return product, nil
    35  		}
    36  	}
    37  	return Product{}, fmt.Errorf("no product with name '%s' found in Whitesource", productName)
    38  }
    39  
    40  // CreateProduct appends a new Product to the system mock and returns its token ("mock-product-token-<index>").
    41  func (m *SystemMock) CreateProduct(productName string) (string, error) {
    42  	now := time.Now().Format(DateTimeLayout)
    43  	productIndex := len(m.Products)
    44  	product := Product{
    45  		Name:           productName,
    46  		Token:          "mock-product-token-" + strconv.Itoa(productIndex),
    47  		CreationDate:   now,
    48  		LastUpdateDate: now,
    49  	}
    50  	m.Products = append(m.Products, product)
    51  	return product.Token, nil
    52  }
    53  
    54  // SetProductAssignments checks if the system mock contains a product with the given token and returns
    55  // an error depending on that, but otherwise does nothing with the provided arguments.
    56  func (m *SystemMock) SetProductAssignments(productToken string, _, _, _ *Assignment) error {
    57  	for _, product := range m.Products {
    58  		if product.Token == productToken {
    59  			return nil
    60  		}
    61  	}
    62  	return fmt.Errorf("no product with that token")
    63  }
    64  
    65  // GetProjectsMetaInfo returns the list of Projects stored in the mock or an error if token is unknown.
    66  func (m *SystemMock) GetProjectsMetaInfo(productToken string) ([]Project, error) {
    67  	for _, product := range m.Products {
    68  		if product.Token == productToken {
    69  			return m.Projects, nil
    70  		}
    71  	}
    72  	return nil, fmt.Errorf("no product with that token")
    73  }
    74  
    75  // GetProjectToken checks the Projects stored in the mock and returns a valid token, or an empty token and no error.
    76  func (m *SystemMock) GetProjectToken(productToken, projectName string) (string, error) {
    77  	for _, project := range m.Projects {
    78  		if project.Name == projectName {
    79  			return project.Token, nil
    80  		}
    81  	}
    82  	return "", nil
    83  }
    84  
    85  // GetProjectByToken checks the Projects stored in the mock and returns the one with the given token or an error.
    86  func (m *SystemMock) GetProjectByToken(projectToken string) (Project, error) {
    87  	for _, project := range m.Projects {
    88  		if project.Token == projectToken {
    89  			return project, nil
    90  		}
    91  	}
    92  	return Project{}, fmt.Errorf("no project with token '%s' found in Whitesource", projectToken)
    93  }
    94  
    95  // GetProjectRiskReport mocks retrieving a risc report.
    96  func (m *SystemMock) GetProjectRiskReport(projectToken string) ([]byte, error) {
    97  	return m.RiskReport, nil
    98  }
    99  
   100  // GetProjectVulnerabilityReport mocks retrieving a vulnerability report.
   101  // Behavior depends on what is stored in the mock.
   102  func (m *SystemMock) GetProjectVulnerabilityReport(projectToken string, format string) ([]byte, error) {
   103  	_, err := m.GetProjectByToken(projectToken)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	if m.VulnerabilityReport == nil {
   108  		return nil, fmt.Errorf("no report available")
   109  	}
   110  	return m.VulnerabilityReport, nil
   111  }
   112  
   113  // GetProjectAlerts returns the alerts stored in the SystemMock.
   114  func (m *SystemMock) GetProjectAlerts(projectToken string) ([]Alert, error) {
   115  	return m.Alerts, nil
   116  }
   117  
   118  // GetProjectAlertsByType returns the alerts stored in the SystemMock and records the type.
   119  func (m *SystemMock) GetProjectAlertsByType(projectToken, alertType string) ([]Alert, error) {
   120  	if m.AlertError != nil {
   121  		return m.Alerts, m.AlertError
   122  	}
   123  	m.AlertType = alertType
   124  	return m.Alerts, nil
   125  }
   126  
   127  // GetProjectLibraryLocations returns the libraries stored in the SystemMock.
   128  func (m *SystemMock) GetProjectLibraryLocations(projectToken string) ([]Library, error) {
   129  	return m.Libraries, nil
   130  }
   131  
   132  // GetProjectHierarchy returns the libraries stored in the SystemMock.
   133  func (m *SystemMock) GetProjectHierarchy(projectToken string, inHouse bool) ([]Library, error) {
   134  	return m.Libraries, nil
   135  }
   136  
   137  // NewSystemMockWithProjectName returns a pointer to a new instance of SystemMock using a project with a defined name.
   138  func NewSystemMockWithProjectName(lastUpdateDate, projectName string) *SystemMock {
   139  	mockLibrary := Library{
   140  		Name:     "mock-library",
   141  		Filename: "mock-library-file",
   142  		Version:  "mock-library-version",
   143  	}
   144  	return &SystemMock{
   145  		ProductName: "mock-product",
   146  		Products: []Product{
   147  			{
   148  				Name:           "mock-product",
   149  				Token:          "mock-product-token",
   150  				CreationDate:   "last-thursday",
   151  				LastUpdateDate: lastUpdateDate,
   152  			},
   153  		},
   154  		Projects: []Project{
   155  			{
   156  				ID:             42,
   157  				Name:           projectName,
   158  				PluginName:     "mock-plugin-name",
   159  				Token:          "mock-project-token",
   160  				UploadedBy:     "MrBean",
   161  				CreationDate:   "last-thursday",
   162  				LastUpdateDate: lastUpdateDate,
   163  			},
   164  		},
   165  		Alerts: []Alert{
   166  			{
   167  				Vulnerability: Vulnerability{
   168  					Name:  "something severe",
   169  					Score: 5,
   170  				},
   171  				Library:      mockLibrary,
   172  				Project:      projectName,
   173  				CreationDate: "last-thursday",
   174  			},
   175  		},
   176  		Libraries:           []Library{mockLibrary},
   177  		RiskReport:          []byte("mock-risk-report"),
   178  		VulnerabilityReport: []byte("mock-vulnerability-report"),
   179  	}
   180  }
   181  
   182  // NewSystemMock returns a pointer to a new instance of SystemMock.
   183  func NewSystemMock(lastUpdateDate string) *SystemMock {
   184  	const projectName = "mock-project - 1"
   185  	return NewSystemMockWithProjectName(lastUpdateDate, projectName)
   186  }