2020-03-07 00:10:10 +02:00
|
|
|
package nexus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddArtifact(t *testing.T) {
|
|
|
|
t.Run("Test valid artifact", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
|
|
|
|
|
|
|
err := nexusUpload.AddArtifact(ArtifactDescription{
|
|
|
|
Classifier: "",
|
|
|
|
Type: "pom",
|
|
|
|
File: "pom.xml",
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err, "Expected to add valid artifact")
|
|
|
|
assert.True(t, len(nexusUpload.artifacts) == 1)
|
|
|
|
|
|
|
|
assert.True(t, nexusUpload.artifacts[0].Classifier == "")
|
|
|
|
assert.True(t, nexusUpload.artifacts[0].Type == "pom")
|
|
|
|
assert.True(t, nexusUpload.artifacts[0].File == "pom.xml")
|
|
|
|
})
|
|
|
|
t.Run("Test missing type", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
|
|
|
|
|
|
|
err := nexusUpload.AddArtifact(ArtifactDescription{
|
|
|
|
Classifier: "",
|
|
|
|
Type: "",
|
|
|
|
File: "pom.xml",
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Error(t, err, "Expected to fail adding invalid artifact")
|
|
|
|
assert.True(t, len(nexusUpload.artifacts) == 0)
|
|
|
|
})
|
|
|
|
t.Run("Test missing file", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
|
|
|
|
|
|
|
err := nexusUpload.AddArtifact(ArtifactDescription{
|
|
|
|
Classifier: "",
|
|
|
|
Type: "pom",
|
|
|
|
File: "",
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Error(t, err, "Expected to fail adding invalid artifact")
|
|
|
|
assert.True(t, len(nexusUpload.artifacts) == 0)
|
|
|
|
})
|
|
|
|
t.Run("Test adding duplicate artifact is ignored", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
|
|
|
|
|
|
|
_ = nexusUpload.AddArtifact(ArtifactDescription{
|
|
|
|
Classifier: "",
|
|
|
|
Type: "pom",
|
|
|
|
File: "pom.xml",
|
|
|
|
})
|
|
|
|
err := nexusUpload.AddArtifact(ArtifactDescription{
|
|
|
|
Classifier: "",
|
|
|
|
Type: "pom",
|
|
|
|
File: "pom.xml",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err, "Expected to succeed adding duplicate artifact")
|
|
|
|
assert.True(t, len(nexusUpload.artifacts) == 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetArtifacts(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
|
|
|
|
|
|
|
err := nexusUpload.AddArtifact(ArtifactDescription{
|
|
|
|
Classifier: "",
|
|
|
|
Type: "pom",
|
|
|
|
File: "pom.xml",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err, "Expected to succeed adding valid artifact")
|
|
|
|
|
|
|
|
artifacts := nexusUpload.GetArtifacts()
|
|
|
|
// Overwrite array entry in the returned array...
|
|
|
|
artifacts[0] = ArtifactDescription{
|
|
|
|
Classifier: "",
|
2020-03-20 19:20:52 +02:00
|
|
|
Type: "jar",
|
|
|
|
File: "app.jar",
|
2020-03-07 00:10:10 +02:00
|
|
|
}
|
|
|
|
// ... but expect the entry in nexusUpload object to be unchanged
|
2020-03-20 19:20:52 +02:00
|
|
|
assert.Equal(t, "pom", nexusUpload.artifacts[0].Type)
|
|
|
|
assert.Equal(t, "pom.xml", nexusUpload.artifacts[0].File)
|
2020-03-07 00:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBaseURL(t *testing.T) {
|
2020-03-20 19:20:52 +02:00
|
|
|
// Invalid parameters to getBaseURL() already tested via SetRepoURL() tests
|
2020-03-07 00:10:10 +02:00
|
|
|
t.Run("Test base URL for nexus2 is sensible", func(t *testing.T) {
|
2020-03-20 19:20:52 +02:00
|
|
|
baseURL, err := getBaseURL("localhost:8081/nexus", "nexus2", "maven-releases")
|
2020-03-07 00:10:10 +02:00
|
|
|
assert.NoError(t, err, "Expected getBaseURL() to succeed")
|
2020-03-20 19:20:52 +02:00
|
|
|
assert.Equal(t, "localhost:8081/nexus/content/repositories/maven-releases/", baseURL)
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
t.Run("Test base URL for nexus3 is sensible", func(t *testing.T) {
|
2020-03-20 19:20:52 +02:00
|
|
|
baseURL, err := getBaseURL("localhost:8081", "nexus3", "maven-releases")
|
2020-03-07 00:10:10 +02:00
|
|
|
assert.NoError(t, err, "Expected getBaseURL() to succeed")
|
2020-03-20 19:20:52 +02:00
|
|
|
assert.Equal(t, "localhost:8081/repository/maven-releases/", baseURL)
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetBaseURL(t *testing.T) {
|
|
|
|
t.Run("Test no host provided", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
2020-04-11 12:56:44 +02:00
|
|
|
err := nexusUpload.SetRepoURL("", "nexus3", "maven-releases", "npm-repo")
|
2020-03-20 19:20:52 +02:00
|
|
|
assert.Error(t, err, "Expected SetRepoURL() to fail (no host)")
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
t.Run("Test host wrongly includes protocol http://", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
2020-04-11 12:56:44 +02:00
|
|
|
err := nexusUpload.SetRepoURL("htTp://localhost:8081", "nexus3", "maven-releases", "npm-repo")
|
2020-03-31 15:16:18 +02:00
|
|
|
if assert.NoError(t, err, "Expected SetRepoURL() to work") {
|
2020-04-11 12:56:44 +02:00
|
|
|
assert.Equal(t, "localhost:8081/repository/maven-releases/", nexusUpload.mavenRepoURL)
|
2020-03-31 15:16:18 +02:00
|
|
|
}
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
t.Run("Test host wrongly includes protocol https://", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
2020-04-11 12:56:44 +02:00
|
|
|
err := nexusUpload.SetRepoURL("htTpS://localhost:8081", "nexus3", "maven-releases", "npm-repo")
|
2020-03-31 15:16:18 +02:00
|
|
|
if assert.NoError(t, err, "Expected SetRepoURL() to work") {
|
2020-04-11 12:56:44 +02:00
|
|
|
assert.Equal(t, "localhost:8081/repository/maven-releases/", nexusUpload.mavenRepoURL)
|
2020-03-31 15:16:18 +02:00
|
|
|
}
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
t.Run("Test invalid version provided", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
2020-04-11 12:56:44 +02:00
|
|
|
err := nexusUpload.SetRepoURL("localhost:8081", "3", "maven-releases", "npm-repo")
|
2020-03-20 19:20:52 +02:00
|
|
|
assert.Error(t, err, "Expected SetRepoURL() to fail (invalid nexus version)")
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
t.Run("Test no nexus version provided", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
2020-04-11 12:56:44 +02:00
|
|
|
err := nexusUpload.SetRepoURL("localhost:8081", "nexus1", "maven-releases", "npm-repo")
|
2020-03-20 19:20:52 +02:00
|
|
|
assert.Error(t, err, "Expected SetRepoURL() to fail (unsupported nexus version)")
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
t.Run("Test unsupported nexus version provided", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
2020-04-11 12:56:44 +02:00
|
|
|
err := nexusUpload.SetRepoURL("localhost:8081", "nexus1", "maven-releases", "npm-repo")
|
2020-03-20 19:20:52 +02:00
|
|
|
assert.Error(t, err, "Expected SetRepoURL() to fail (unsupported nexus version)")
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-20 19:20:52 +02:00
|
|
|
func TestSetInfo(t *testing.T) {
|
2020-03-07 00:10:10 +02:00
|
|
|
t.Run("Test invalid artifact version", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
2020-03-20 19:20:52 +02:00
|
|
|
err := nexusUpload.SetInfo("my.group", "artifact.id", "")
|
|
|
|
assert.Error(t, err, "Expected SetInfo() to fail (empty version)")
|
|
|
|
assert.Equal(t, "", nexusUpload.groupID)
|
|
|
|
assert.Equal(t, "", nexusUpload.artifactID)
|
|
|
|
assert.Equal(t, "", nexusUpload.version)
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
t.Run("Test valid artifact version", func(t *testing.T) {
|
|
|
|
nexusUpload := Upload{}
|
2020-03-20 19:20:52 +02:00
|
|
|
err := nexusUpload.SetInfo("my.group", "artifact.id", "1.0.0-SNAPSHOT")
|
|
|
|
assert.NoError(t, err, "Expected SetInfo() to succeed")
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
2020-03-20 19:20:52 +02:00
|
|
|
t.Run("Test empty artifactID", func(t *testing.T) {
|
2020-03-07 00:10:10 +02:00
|
|
|
nexusUpload := Upload{}
|
2020-03-20 19:20:52 +02:00
|
|
|
err := nexusUpload.SetInfo("my.group", "", "1.0")
|
|
|
|
assert.Error(t, err, "Expected to fail setting empty artifactID")
|
|
|
|
assert.Equal(t, "", nexusUpload.groupID)
|
|
|
|
assert.Equal(t, "", nexusUpload.artifactID)
|
|
|
|
assert.Equal(t, "", nexusUpload.version)
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
2020-03-20 19:20:52 +02:00
|
|
|
t.Run("Test empty groupID", func(t *testing.T) {
|
2020-03-07 00:10:10 +02:00
|
|
|
nexusUpload := Upload{}
|
2020-03-20 19:20:52 +02:00
|
|
|
err := nexusUpload.SetInfo("", "id", "1.0")
|
|
|
|
assert.Error(t, err, "Expected to fail setting empty groupID")
|
|
|
|
assert.Equal(t, "", nexusUpload.groupID)
|
|
|
|
assert.Equal(t, "", nexusUpload.artifactID)
|
|
|
|
assert.Equal(t, "", nexusUpload.version)
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
2020-03-20 19:20:52 +02:00
|
|
|
t.Run("Test invalid ID", func(t *testing.T) {
|
2020-03-07 00:10:10 +02:00
|
|
|
nexusUpload := Upload{}
|
2020-03-20 19:20:52 +02:00
|
|
|
err := nexusUpload.SetInfo("my.group", "artifact/id", "1.0.0-SNAPSHOT")
|
|
|
|
assert.Error(t, err, "Expected to fail adding invalid artifact")
|
|
|
|
assert.Equal(t, "", nexusUpload.groupID)
|
|
|
|
assert.Equal(t, "", nexusUpload.artifactID)
|
|
|
|
assert.Equal(t, "", nexusUpload.version)
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-20 19:20:52 +02:00
|
|
|
func TestClear(t *testing.T) {
|
2020-03-07 00:10:10 +02:00
|
|
|
nexusUpload := Upload{}
|
|
|
|
|
2020-03-20 19:20:52 +02:00
|
|
|
err := nexusUpload.AddArtifact(ArtifactDescription{
|
|
|
|
Classifier: "",
|
|
|
|
Type: "pom",
|
|
|
|
File: "pom.xml",
|
2020-03-07 00:10:10 +02:00
|
|
|
})
|
2020-03-20 19:20:52 +02:00
|
|
|
assert.NoError(t, err, "Expected to succeed adding valid artifact")
|
|
|
|
assert.Equal(t, 1, len(nexusUpload.GetArtifacts()))
|
2020-03-07 00:10:10 +02:00
|
|
|
|
2020-03-20 19:20:52 +02:00
|
|
|
nexusUpload.Clear()
|
2020-03-07 00:10:10 +02:00
|
|
|
|
2020-03-20 19:20:52 +02:00
|
|
|
assert.Equal(t, 0, len(nexusUpload.GetArtifacts()))
|
2020-03-07 00:10:10 +02:00
|
|
|
}
|