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

     1  //go:build unit
     2  // +build unit
     3  
     4  package nexus
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestAddArtifact(t *testing.T) {
    13  	t.Run("Test valid artifact", func(t *testing.T) {
    14  		nexusUpload := Upload{}
    15  
    16  		err := nexusUpload.AddArtifact(ArtifactDescription{
    17  			Classifier: "",
    18  			Type:       "pom",
    19  			File:       "pom.xml",
    20  		})
    21  
    22  		assert.NoError(t, err, "Expected to add valid artifact")
    23  		assert.True(t, len(nexusUpload.artifacts) == 1)
    24  
    25  		assert.True(t, nexusUpload.artifacts[0].Classifier == "")
    26  		assert.True(t, nexusUpload.artifacts[0].Type == "pom")
    27  		assert.True(t, nexusUpload.artifacts[0].File == "pom.xml")
    28  	})
    29  	t.Run("Test missing type", func(t *testing.T) {
    30  		nexusUpload := Upload{}
    31  
    32  		err := nexusUpload.AddArtifact(ArtifactDescription{
    33  			Classifier: "",
    34  			Type:       "",
    35  			File:       "pom.xml",
    36  		})
    37  
    38  		assert.Error(t, err, "Expected to fail adding invalid artifact")
    39  		assert.True(t, len(nexusUpload.artifacts) == 0)
    40  	})
    41  	t.Run("Test missing file", func(t *testing.T) {
    42  		nexusUpload := Upload{}
    43  
    44  		err := nexusUpload.AddArtifact(ArtifactDescription{
    45  			Classifier: "",
    46  			Type:       "pom",
    47  			File:       "",
    48  		})
    49  
    50  		assert.Error(t, err, "Expected to fail adding invalid artifact")
    51  		assert.True(t, len(nexusUpload.artifacts) == 0)
    52  	})
    53  	t.Run("Test adding duplicate artifact is ignored", func(t *testing.T) {
    54  		nexusUpload := Upload{}
    55  
    56  		_ = nexusUpload.AddArtifact(ArtifactDescription{
    57  			Classifier: "",
    58  			Type:       "pom",
    59  			File:       "pom.xml",
    60  		})
    61  		err := nexusUpload.AddArtifact(ArtifactDescription{
    62  			Classifier: "",
    63  			Type:       "pom",
    64  			File:       "pom.xml",
    65  		})
    66  		assert.NoError(t, err, "Expected to succeed adding duplicate artifact")
    67  		assert.True(t, len(nexusUpload.artifacts) == 1)
    68  	})
    69  }
    70  
    71  func TestGetArtifacts(t *testing.T) {
    72  	nexusUpload := Upload{}
    73  
    74  	err := nexusUpload.AddArtifact(ArtifactDescription{
    75  		Classifier: "",
    76  		Type:       "pom",
    77  		File:       "pom.xml",
    78  	})
    79  	assert.NoError(t, err, "Expected to succeed adding valid artifact")
    80  
    81  	artifacts := nexusUpload.GetArtifacts()
    82  	// Overwrite array entry in the returned array...
    83  	artifacts[0] = ArtifactDescription{
    84  		Classifier: "",
    85  		Type:       "jar",
    86  		File:       "app.jar",
    87  	}
    88  	// ... but expect the entry in nexusUpload object to be unchanged
    89  	assert.Equal(t, "pom", nexusUpload.artifacts[0].Type)
    90  	assert.Equal(t, "pom.xml", nexusUpload.artifacts[0].File)
    91  }
    92  
    93  func TestGetBaseURL(t *testing.T) {
    94  	// Invalid parameters to getBaseURL() already tested via SetRepoURL() tests
    95  	t.Run("Test base URL for nexus2 is sensible", func(t *testing.T) {
    96  		baseURL, err := getBaseURL("localhost:8081/nexus", "nexus2", "maven-releases")
    97  		assert.NoError(t, err, "Expected getBaseURL() to succeed")
    98  		assert.Equal(t, "localhost:8081/nexus/content/repositories/maven-releases/", baseURL)
    99  	})
   100  	t.Run("Test base URL for nexus3 is sensible", func(t *testing.T) {
   101  		baseURL, err := getBaseURL("localhost:8081", "nexus3", "maven-releases")
   102  		assert.NoError(t, err, "Expected getBaseURL() to succeed")
   103  		assert.Equal(t, "localhost:8081/repository/maven-releases/", baseURL)
   104  	})
   105  }
   106  
   107  func TestSetBaseURL(t *testing.T) {
   108  	t.Run("Test no host provided", func(t *testing.T) {
   109  		nexusUpload := Upload{}
   110  		err := nexusUpload.SetRepoURL("", "nexus3", "maven-releases", "npm-repo")
   111  		assert.Error(t, err, "Expected SetRepoURL() to fail (no host)")
   112  	})
   113  	t.Run("Test host wrongly includes protocol http://", func(t *testing.T) {
   114  		nexusUpload := Upload{}
   115  		err := nexusUpload.SetRepoURL("htTp://localhost:8081", "nexus3", "maven-releases", "npm-repo")
   116  		if assert.NoError(t, err, "Expected SetRepoURL() to work") {
   117  			assert.Equal(t, "localhost:8081/repository/maven-releases/", nexusUpload.mavenRepoURL)
   118  		}
   119  	})
   120  	t.Run("Test host wrongly includes protocol https://", func(t *testing.T) {
   121  		nexusUpload := Upload{}
   122  		err := nexusUpload.SetRepoURL("htTpS://localhost:8081", "nexus3", "maven-releases", "npm-repo")
   123  		if assert.NoError(t, err, "Expected SetRepoURL() to work") {
   124  			assert.Equal(t, "localhost:8081/repository/maven-releases/", nexusUpload.mavenRepoURL)
   125  		}
   126  	})
   127  	t.Run("Test invalid version provided", func(t *testing.T) {
   128  		nexusUpload := Upload{}
   129  		err := nexusUpload.SetRepoURL("localhost:8081", "3", "maven-releases", "npm-repo")
   130  		assert.Error(t, err, "Expected SetRepoURL() to fail (invalid nexus version)")
   131  	})
   132  	t.Run("Test no nexus version provided", func(t *testing.T) {
   133  		nexusUpload := Upload{}
   134  		err := nexusUpload.SetRepoURL("localhost:8081", "nexus1", "maven-releases", "npm-repo")
   135  		assert.Error(t, err, "Expected SetRepoURL() to fail (unsupported nexus version)")
   136  	})
   137  	t.Run("Test unsupported nexus version provided", func(t *testing.T) {
   138  		nexusUpload := Upload{}
   139  		err := nexusUpload.SetRepoURL("localhost:8081", "nexus1", "maven-releases", "npm-repo")
   140  		assert.Error(t, err, "Expected SetRepoURL() to fail (unsupported nexus version)")
   141  	})
   142  }
   143  
   144  func TestSetInfo(t *testing.T) {
   145  	t.Run("Test invalid artifact version", func(t *testing.T) {
   146  		nexusUpload := Upload{}
   147  		err := nexusUpload.SetInfo("my.group", "artifact.id", "")
   148  		assert.Error(t, err, "Expected SetInfo() to fail (empty version)")
   149  		assert.Equal(t, "", nexusUpload.groupID)
   150  		assert.Equal(t, "", nexusUpload.artifactID)
   151  		assert.Equal(t, "", nexusUpload.version)
   152  	})
   153  	t.Run("Test valid artifact version", func(t *testing.T) {
   154  		nexusUpload := Upload{}
   155  		err := nexusUpload.SetInfo("my.group", "artifact.id", "1.0.0-SNAPSHOT")
   156  		assert.NoError(t, err, "Expected SetInfo() to succeed")
   157  	})
   158  	t.Run("Test empty artifactID", func(t *testing.T) {
   159  		nexusUpload := Upload{}
   160  		err := nexusUpload.SetInfo("my.group", "", "1.0")
   161  		assert.Error(t, err, "Expected to fail setting empty artifactID")
   162  		assert.Equal(t, "", nexusUpload.groupID)
   163  		assert.Equal(t, "", nexusUpload.artifactID)
   164  		assert.Equal(t, "", nexusUpload.version)
   165  	})
   166  	t.Run("Test empty groupID", func(t *testing.T) {
   167  		nexusUpload := Upload{}
   168  		err := nexusUpload.SetInfo("", "id", "1.0")
   169  		assert.Error(t, err, "Expected to fail setting empty groupID")
   170  		assert.Equal(t, "", nexusUpload.groupID)
   171  		assert.Equal(t, "", nexusUpload.artifactID)
   172  		assert.Equal(t, "", nexusUpload.version)
   173  	})
   174  	t.Run("Test invalid ID", func(t *testing.T) {
   175  		nexusUpload := Upload{}
   176  		err := nexusUpload.SetInfo("my.group", "artifact/id", "1.0.0-SNAPSHOT")
   177  		assert.Error(t, err, "Expected to fail adding invalid artifact")
   178  		assert.Equal(t, "", nexusUpload.groupID)
   179  		assert.Equal(t, "", nexusUpload.artifactID)
   180  		assert.Equal(t, "", nexusUpload.version)
   181  	})
   182  }
   183  
   184  func TestClear(t *testing.T) {
   185  	nexusUpload := Upload{}
   186  
   187  	err := nexusUpload.AddArtifact(ArtifactDescription{
   188  		Classifier: "",
   189  		Type:       "pom",
   190  		File:       "pom.xml",
   191  	})
   192  	assert.NoError(t, err, "Expected to succeed adding valid artifact")
   193  	assert.Equal(t, 1, len(nexusUpload.GetArtifacts()))
   194  
   195  	nexusUpload.Clear()
   196  
   197  	assert.Equal(t, 0, len(nexusUpload.GetArtifacts()))
   198  }