Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.2.3</version>
</project>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>2.0.1</version>
</project>
Binary file not shown.

This file was deleted.

67 changes: 39 additions & 28 deletions internal/test/integration/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ import (
)

const (
testMavenRepoName = "junit-fixture"
testMavenDistributionBasePath = "junit-fixture"
testMavenRepoName = "foo-fixture"
testMavenDistributionBasePath = "foo-fixture"
testMavenRemoteURL = "http://maven_fixture/"
testMavenContentOrigin = "http://localhost:8088"
testMavenGroupID = "junit"
testMavenArtifactID = "junit"
testMavenVersion = "4.13.2"
testMavenPomPath = "junit/junit/4.13.2/junit-4.13.2.pom"
testMavenGroupID = "foo"
testMavenArtifactID = "bar"
testMavenVersion123 = "1.2.3"
testMavenVersion201 = "2.0.1"
)

var testMavenArtifactPaths = []string{
"foo/bar/1.2.3/foo.bar.1.2.3.pom",
"foo/bar/1.2.3/foo.bar.1.2.3.rhlw-00001.jar",
"foo/bar/1.2.3/foo.bar.1.2.3.rhlw-00002.jar",
"foo/bar/2.0.1/foo.bar.2.0.1.pom",
"foo/bar/2.0.1/foo.bar.2.0.1.rhlw-00001.jar",
}

type MavenSuite struct {
suite.Suite
client *zestwrapper.MavenZest
Expand All @@ -46,8 +54,10 @@ func (m *MavenSuite) createTestRepository(t *testing.T) {
)
require.NoError(t, err)

err = m.client.FetchArtifact(testMavenContentOrigin, m.domainName, testMavenDistributionBasePath, testMavenPomPath)
require.NoError(t, err)
for _, artifactPath := range testMavenArtifactPaths {
err = m.client.FetchArtifact(testMavenContentOrigin, m.domainName, testMavenDistributionBasePath, artifactPath)
require.NoError(t, err)
}

addTask, err := m.client.AddCachedContent(repoHref, remoteHref)
require.NoError(t, err)
Expand Down Expand Up @@ -95,21 +105,21 @@ func (m *MavenSuite) TestMavenPackageList() {
pkg := response.Results[0]
assert.Equal(m.T(), testMavenGroupID, pkg.GroupID)
assert.Equal(m.T(), testMavenArtifactID, pkg.ArtifactID)
assert.Contains(m.T(), pkg.Versions, testMavenVersion)
assert.Contains(m.T(), pkg.Versions, testMavenVersion123)
assert.Contains(m.T(), pkg.Versions, testMavenVersion201)
require.NotEmpty(m.T(), pkg.LatestReleases)

foundVersion := false
foundVersions := make(map[string]bool)
for _, latest := range pkg.LatestReleases {
if latest.Version == testMavenVersion {
foundVersion = true
assert.NotEmpty(m.T(), latest.CreatedAt)
}
foundVersions[latest.Version] = true
assert.NotEmpty(m.T(), latest.CreatedAt)
}
assert.True(m.T(), foundVersion)
assert.True(m.T(), foundVersions[testMavenVersion123])
assert.True(m.T(), foundVersions[testMavenVersion201])
}

func (m *MavenSuite) TestMavenPackageListSearchFilter() {
response, err := m.tangy.MavenPackageList(context.Background(), m.repositoryHref, tangy.MavenPackageListFilters{Search: "junit"}, tangy.PageOptions{
response, err := m.tangy.MavenPackageList(context.Background(), m.repositoryHref, tangy.MavenPackageListFilters{Search: "foo"}, tangy.PageOptions{
Offset: 0,
Limit: 10,
})
Expand Down Expand Up @@ -159,7 +169,7 @@ func (m *MavenSuite) TestMavenBuildList() {
m.repositoryHref,
testMavenGroupID,
testMavenArtifactID,
testMavenVersion,
testMavenVersion123,
tangy.PageOptions{Offset: 0, Limit: 10},
)
require.NoError(m.T(), err)
Expand All @@ -170,7 +180,7 @@ func (m *MavenSuite) TestMavenBuildList() {
build := response.Results[0]
assert.Equal(m.T(), testMavenGroupID, build.GroupID)
assert.Equal(m.T(), testMavenArtifactID, build.ArtifactID)
assert.Equal(m.T(), testMavenVersion, build.Version)
assert.Equal(m.T(), testMavenVersion123, build.Version)
assert.True(m.T(), strings.HasSuffix(build.Filename, ".pom"), build.Filename)
assert.NotEmpty(m.T(), build.CreatedAt)
}
Expand All @@ -181,7 +191,7 @@ func (m *MavenSuite) TestMavenBuildListPagination() {
m.repositoryHref,
testMavenGroupID,
testMavenArtifactID,
testMavenVersion,
testMavenVersion123,
tangy.PageOptions{Offset: 0, Limit: 1},
)
require.NoError(m.T(), err)
Expand All @@ -193,7 +203,7 @@ func (m *MavenSuite) TestMavenBuildListPagination() {
m.repositoryHref,
testMavenGroupID,
testMavenArtifactID,
testMavenVersion,
testMavenVersion123,
tangy.PageOptions{Offset: 1, Limit: 10},
)
require.NoError(m.T(), err)
Expand All @@ -202,7 +212,7 @@ func (m *MavenSuite) TestMavenBuildListPagination() {
}

func (m *MavenSuite) TestMavenBuildListEmptyHref() {
response, err := m.tangy.MavenBuildList(context.Background(), "", testMavenGroupID, testMavenArtifactID, testMavenVersion, tangy.PageOptions{Limit: 10})
response, err := m.tangy.MavenBuildList(context.Background(), "", testMavenGroupID, testMavenArtifactID, testMavenVersion123, tangy.PageOptions{Limit: 10})
require.NoError(m.T(), err)
assert.Empty(m.T(), response.Results)
assert.Zero(m.T(), response.Total)
Expand All @@ -219,12 +229,11 @@ func (m *MavenSuite) TestMavenBuildListOptionalFilters() {
)
require.NoError(m.T(), err)
require.NotEmpty(m.T(), response.Results)
assert.Equal(m.T(), 1, response.Total)
assert.Equal(m.T(), 2, response.Total)

build := response.Results[0]
assert.Equal(m.T(), testMavenGroupID, build.GroupID)
assert.Equal(m.T(), testMavenArtifactID, build.ArtifactID)
assert.Equal(m.T(), testMavenVersion, build.Version)

filtered, err := m.tangy.MavenBuildList(
context.Background(),
Expand All @@ -235,8 +244,8 @@ func (m *MavenSuite) TestMavenBuildListOptionalFilters() {
tangy.PageOptions{Offset: 0, Limit: 10},
)
require.NoError(m.T(), err)
require.Len(m.T(), filtered.Results, 1)
assert.Equal(m.T(), 1, filtered.Total)
require.Len(m.T(), filtered.Results, 2)
assert.Equal(m.T(), 2, filtered.Total)

filtered, err = m.tangy.MavenBuildList(
context.Background(),
Expand All @@ -247,20 +256,22 @@ func (m *MavenSuite) TestMavenBuildListOptionalFilters() {
tangy.PageOptions{Offset: 0, Limit: 10},
)
require.NoError(m.T(), err)
require.Len(m.T(), filtered.Results, 1)
assert.Equal(m.T(), 1, filtered.Total)
require.Len(m.T(), filtered.Results, 2)
assert.Equal(m.T(), 2, filtered.Total)
}

func (m *MavenSuite) TestMavenRepositoryMetrics() {
metrics, err := m.tangy.MavenRepositoryMetrics(context.Background(), m.repositoryHref)
require.NoError(m.T(), err)
assert.Equal(m.T(), 1, metrics.PackageCount)
assert.Equal(m.T(), 1, metrics.BuildCount)
assert.Equal(m.T(), 3, metrics.BuildCount)
assert.Equal(m.T(), 2, metrics.VersionCount)
}

func (m *MavenSuite) TestMavenRepositoryMetricsEmptyHref() {
metrics, err := m.tangy.MavenRepositoryMetrics(context.Background(), "")
require.NoError(m.T(), err)
assert.Zero(m.T(), metrics.PackageCount)
assert.Zero(m.T(), metrics.BuildCount)
assert.Zero(m.T(), metrics.VersionCount)
}
22 changes: 20 additions & 2 deletions internal/test/integration/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const (
testPythonRepoName = "shelf-reader-fixture"
testPythonMultiVersionRepoName = "idna-multi-version-fixture"
testPythonMetricsRepoName = "idna-metrics-fixture"
testPythonMultiVersionPackage = "idna"
testPythonMultiVersionKeepCount = int64(2)
testPythonRepoURL = "https://pypi.org/"
Expand Down Expand Up @@ -320,17 +321,34 @@ func (p *PythonSuite) TestPythonBuildList() {
}

func (p *PythonSuite) TestPythonRepositoryMetrics() {
metrics, err := p.tangy.PythonRepositoryMetrics(context.Background(), p.repositoryHref)
repoHref, remoteHref, err := p.client.CreateRepository(
p.domainName,
testPythonMetricsRepoName,
testPythonRepoURL,
[]string{testPythonMultiVersionPackage},
testPythonMultiVersionKeepCount,
)
require.NoError(p.T(), err)

syncTask, err := p.client.SyncPythonRepository(repoHref, remoteHref)
require.NoError(p.T(), err)

_, err = p.client.PollTask(syncTask)
require.NoError(p.T(), err)

metrics, err := p.tangy.PythonRepositoryMetrics(context.Background(), repoHref)
require.NoError(p.T(), err)
assert.Equal(p.T(), 1, metrics.PackageCount)
assert.Equal(p.T(), 1, metrics.BuildCount)
assert.Equal(p.T(), 2, metrics.BuildCount)
assert.Equal(p.T(), 2, metrics.VersionCount)
}

func (p *PythonSuite) TestPythonRepositoryMetricsEmptyHref() {
metrics, err := p.tangy.PythonRepositoryMetrics(context.Background(), "")
require.NoError(p.T(), err)
assert.Zero(p.T(), metrics.PackageCount)
assert.Zero(p.T(), metrics.BuildCount)
assert.Zero(p.T(), metrics.VersionCount)
}

func (p *PythonSuite) TestPythonPackageGetNotFound() {
Expand Down
19 changes: 13 additions & 6 deletions pkg/tangy/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type MavenBuildListResponse struct {
type MavenRepositoryMetrics struct {
PackageCount int `json:"package_count"`
BuildCount int `json:"build_count"`
VersionCount int `json:"version_count"`
}

type mavenArtifactQueryResult struct {
Expand Down Expand Up @@ -409,8 +410,8 @@ func (t *tangyImpl) MavenBuildList(ctx context.Context, repositoryHref, groupID,
}, nil
}

// MavenRepositoryMetrics returns package and build counts for the latest version of a repository.
// Packages are distinct group_id/artifact_id pairs; builds are .pom artifacts.
// MavenRepositoryMetrics returns package, build, and version counts for the latest version of a repository.
// Packages are distinct group_id/artifact_id pairs from .pom artifacts; builds and versions are counted from .jar artifacts.
func (t *tangyImpl) MavenRepositoryMetrics(ctx context.Context, repositoryHref string) (MavenRepositoryMetrics, error) {
if repositoryHref == "" {
return MavenRepositoryMetrics{}, nil
Expand Down Expand Up @@ -444,19 +445,25 @@ func (t *tangyImpl) MavenRepositoryMetrics(ctx context.Context, repositoryHref s
}

pomFilter := ` AND rp.filename LIKE '%.pom'`
artifactFrom := `
jarFilter := ` AND rp.filename LIKE '%.jar'`
pomFrom := `
FROM maven_mavenartifact rp
` + innerUnion + pomFilter
jarFrom := `
FROM maven_mavenartifact rp
` + innerUnion + jarFilter

metricsQuery := `
SELECT
(SELECT COUNT(DISTINCT (rp.group_id, rp.artifact_id))
` + artifactFrom + `) AS package_count,
` + pomFrom + `) AS package_count,
(SELECT COUNT(*)
` + artifactFrom + `) AS build_count`
` + jarFrom + `) AS build_count,
(SELECT COUNT(DISTINCT (rp.group_id, rp.artifact_id, rp.version))
` + jarFrom + `) AS version_count`

var metrics MavenRepositoryMetrics
err = conn.QueryRow(ctx, metricsQuery, args).Scan(&metrics.PackageCount, &metrics.BuildCount)
err = conn.QueryRow(ctx, metricsQuery, args).Scan(&metrics.PackageCount, &metrics.BuildCount, &metrics.VersionCount)
if err != nil {
return MavenRepositoryMetrics{}, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/tangy/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func TestMockTangyMavenRepositoryMetrics(t *testing.T) {
expected := MavenRepositoryMetrics{
PackageCount: 1,
BuildCount: 1,
VersionCount: 1,
}

mockTangy.On("MavenRepositoryMetrics", ctx, repoHref).Return(expected, nil)
Expand Down
10 changes: 7 additions & 3 deletions pkg/tangy/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type PythonBuildListResponse struct {
type PythonRepositoryMetrics struct {
PackageCount int `json:"package_count"`
BuildCount int `json:"build_count"`
VersionCount int `json:"version_count"`
}

type PythonDistributionListItem struct {
Expand Down Expand Up @@ -520,7 +521,8 @@ func (t *tangyImpl) PythonBuildList(ctx context.Context, repositoryHref, nameNor
}, nil
}

// PythonRepositoryMetrics returns package and build counts for the latest version of a repository.
// PythonRepositoryMetrics returns package, build, and version counts for the latest version of a repository.
// Build count equals version count (distinct name_normalized + version pairs).
func (t *tangyImpl) PythonRepositoryMetrics(ctx context.Context, repositoryHref string) (PythonRepositoryMetrics, error) {
if repositoryHref == "" {
return PythonRepositoryMetrics{}, nil
Expand Down Expand Up @@ -566,14 +568,16 @@ func (t *tangyImpl) PythonRepositoryMetrics(ctx context.Context, repositoryHref
SELECT 1
` + contentFrom + `
GROUP BY rp.name_normalized, rp.version
) builds) AS build_count`
) versions) AS version_count`

var metrics PythonRepositoryMetrics
err = conn.QueryRow(ctx, metricsQuery, args).Scan(&metrics.PackageCount, &metrics.BuildCount)
err = conn.QueryRow(ctx, metricsQuery, args).Scan(&metrics.PackageCount, &metrics.VersionCount)
if err != nil {
return PythonRepositoryMetrics{}, err
}

metrics.BuildCount = metrics.VersionCount

return metrics, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/tangy/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ func TestMockTangyPythonRepositoryMetrics(t *testing.T) {
expected := PythonRepositoryMetrics{
PackageCount: 10,
BuildCount: 25,
VersionCount: 25,
}

mockTangy.On("PythonRepositoryMetrics", ctx, repoHref).Return(expected, nil)
Expand Down
Loading