3
3
namespace Microsoft . CST . OpenSource . Tests . ProjectManagerTests
4
4
{
5
5
using Microsoft . CST . OpenSource . Extensions ;
6
+ using Microsoft . CST . OpenSource . Model . Enums ;
6
7
using Model ;
7
8
using Moq ;
8
- using Octokit ;
9
9
using oss ;
10
10
using PackageActions ;
11
11
using PackageManagers ;
12
12
using PackageUrl ;
13
13
using RichardSzalay . MockHttp ;
14
+ using System ;
14
15
using System . Collections . Generic ;
15
16
using System . Collections . Immutable ;
16
17
using System . Linq ;
17
18
using System . Net ;
18
19
using System . Net . Http ;
20
+ using System . Text ;
19
21
using System . Threading . Tasks ;
20
22
using VisualStudio . TestTools . UnitTesting ;
21
23
22
24
[ TestClass ]
23
25
public class MavenProjectManagerTests
24
26
{
27
+ public const string DEFAULT_MAVEN_ENDPOINT = "https://repo1.maven.org/maven2" ;
28
+ public const string GOOGLE_MAVEN_ENDPOINT = "https://maven.google.com" ;
29
+
25
30
private readonly Mock < MavenProjectManager > _projectManager ;
26
31
private readonly IHttpClientFactory _httpFactory ;
27
32
28
33
private readonly IDictionary < string , string > _packages = new Dictionary < string , string > ( )
29
34
{
30
- { "https://repo1.maven.org/maven2/ant/ant/1.6/" , Resources . maven_ant_1_6_html } ,
31
35
{ "https://repo1.maven.org/maven2/ant/ant/" , Resources . maven_ant_all_html } ,
32
36
{ "https://repo1.maven.org/maven2/ant/ant/maven-metadata.xml" , Resources . maven_ant_metadata_xml } ,
37
+ { "https://repo1.maven.org/maven2/ant/ant/1.6/" , Resources . maven_ant_1_6_html } ,
33
38
{ "https://repo1.maven.org/maven2/ant/ant/1.6/ant-1.6.pom" , Resources . maven_ant_1_6_pom } ,
34
- { "https://repo1.maven.org/maven2/com/microsoft/fluentui/fluentui_listitem/0.0.8/ " , Resources . maven_microsoft_fluentui_listitem_0_0_8_html } ,
35
- { "https://repo1.maven.org/maven2/com/microsoft/fluentui/fluentui_listitem/" , Resources . maven_microsoft_fluentui_listitem_all_html } ,
36
- { "https://repo1.maven.org/maven2/com/microsoft/fluentui/fluentui_listitem/maven-metadata.xml " , Resources . maven_microsoft_fluentui_listitem_metadata_xml } ,
39
+ { "https://repo1.maven.org/maven2/com/microsoft/fluentui/fluentui_listitem/" , Resources . maven_fluentui_listitem_all_html } ,
40
+ { "https://repo1.maven.org/maven2/com/microsoft/fluentui/fluentui_listitem/maven-metadata.xml " , Resources . maven_fluentui_listitem_metadata_xml } ,
41
+ { "https://repo1.maven.org/maven2/com/microsoft/fluentui/fluentui_listitem/0.0.8/ " , Resources . maven_fluentui_listitem_0_0_8_html } ,
37
42
{ "https://repo1.maven.org/maven2/com/microsoft/fluentui/fluentui_listitem/0.0.8/fluentui_listitem-0.0.8.pom" , Resources . maven_fluentui_listitem_0_0_8_pom } ,
43
+ { "https://maven.google.com/android/arch/core/core/maven-metadata.xml" , Resources . maven_core_metadata_xml } ,
44
+ { "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2.aar" , Resources . maven_core_1_0_0_alpha2_aar } ,
45
+ { "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2.pom" , Resources . maven_core_1_0_0_alpha2_pom } ,
46
+ { "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2-sources.jar" , Resources . maven_core_1_0_0_alpha2_sources_jar } ,
47
+ { "https://maven.google.com/com/google/cose/cose/maven-metadata.xml" , Resources . maven_cose_metadata_xml } ,
48
+ { "https://maven.google.com/com/google/cose/cose/20230908/cose-20230908.pom" , Resources . maven_cose_20230908_pom } ,
49
+ { "https://maven.google.com/com/google/cose/cose/20230908/cose-20230908.jar" , Resources . maven_cose_20230908_jar } ,
50
+ { "https://maven.google.com/com/google/cose/cose/20230908/cose-20230908-javadoc.jar" , Resources . maven_cose_20230908_javadoc_jar } ,
51
+ { "https://maven.google.com/com/google/cose/cose/20230908/artifact-metadata.json" , Resources . maven_cose_20230908_artifact_metadata_json } ,
38
52
} . ToImmutableDictionary ( ) ;
39
53
40
54
public MavenProjectManagerTests ( )
@@ -47,6 +61,16 @@ public MavenProjectManagerTests()
47
61
{
48
62
MockHttpFetchResponse ( HttpStatusCode . OK , url , json , mockHttp ) ;
49
63
}
64
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2-client.jar" ) . Respond ( HttpStatusCode . NotFound ) ;
65
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2.ear" ) . Respond ( HttpStatusCode . NotFound ) ;
66
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2-javadoc.jar" ) . Respond ( HttpStatusCode . NotFound ) ;
67
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2.rar" ) . Respond ( HttpStatusCode . NotFound ) ;
68
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2-tests.jar" ) . Respond ( HttpStatusCode . NotFound ) ;
69
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2-tests-sources.jar" ) . Respond ( HttpStatusCode . NotFound ) ;
70
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2.war" ) . Respond ( HttpStatusCode . NotFound ) ;
71
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/core-1.0.0-alpha2.jar" ) . Respond ( HttpStatusCode . NotFound ) ;
72
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/android/arch/core/core/" ) . Respond ( HttpStatusCode . NotFound ) ;
73
+ mockHttp . When ( HttpMethod . Get , "https://maven.google.com/com/google/cose/cose/" ) . Respond ( HttpStatusCode . NotFound ) ;
50
74
51
75
mockFactory . Setup ( _ => _ . CreateClient ( It . IsAny < string > ( ) ) ) . Returns ( mockHttp . ToHttpClient ( ) ) ;
52
76
_httpFactory = mockFactory . Object ;
@@ -56,35 +80,67 @@ public MavenProjectManagerTests()
56
80
57
81
[ DataTestMethod ]
58
82
[ DataRow ( "pkg:maven/ant/[email protected] ?repository_url=https://repo1.maven.org/maven2" , "https://repo1.maven.org/maven2/ant/ant/1.6/" ) ]
59
- public async Task GetArtifactDownloadUrisSucceeds_Async ( string purlString , string expectedUriPrefix )
83
+ public async Task MavenCentral_GetArtifactDownloadUrisSucceeds_Async ( string purlString , string expectedUriPrefix )
60
84
{
61
85
PackageURL purl = new ( purlString ) ;
62
- List < ArtifactUri < MavenProjectManager . MavenArtifactType > > uris = await _projectManager . Object . GetArtifactDownloadUrisAsync ( purl ) . ToListAsync ( ) ;
86
+ List < ArtifactUri < MavenArtifactType > > uris = await _projectManager . Object . GetArtifactDownloadUrisAsync ( purl ) . ToListAsync ( ) ;
63
87
64
- Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenProjectManager . MavenArtifactType . Jar
65
- && artifact . Uri == new System . Uri ( expectedUriPrefix + $ "{ purl . Name } -{ purl . Version } .jar") ) ) ;
66
- Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenProjectManager . MavenArtifactType . SourcesJar
88
+ Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenArtifactType . Jar
89
+ && artifact . Uri == new System . Uri ( expectedUriPrefix + $ "{ purl . Name } -{ purl . Version } .jar") ) ) ;
90
+ Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenArtifactType . SourcesJar
67
91
&& artifact . Uri == new System . Uri ( expectedUriPrefix + $ "{ purl . Name } -{ purl . Version } -sources.jar") ) ) ;
68
- Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenProjectManager . MavenArtifactType . Pom
92
+ Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenArtifactType . Pom
93
+ && artifact . Uri == new System . Uri ( expectedUriPrefix + $ "{ purl . Name } -{ purl . Version } .pom") ) ) ;
94
+ }
95
+
96
+ [ DataTestMethod ]
97
+ [ DataRow ( "pkg:maven/android.arch.core/[email protected] ?repository_url=https://maven.google.com" , "https://maven.google.com/android/arch/core/core/1.0.0-alpha2/" ) ]
98
+ public async Task GoogleMaven_Core_GetArtifactDownloadUrisSucceeds_Async ( string purlString , string expectedUriPrefix )
99
+ {
100
+ PackageURL purl = new ( purlString ) ;
101
+ List < ArtifactUri < MavenArtifactType > > uris = await _projectManager . Object . GetArtifactDownloadUrisAsync ( purl ) . ToListAsync ( ) ;
102
+
103
+ Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenArtifactType . Pom
104
+ && artifact . Uri == new System . Uri ( expectedUriPrefix + $ "{ purl . Name } -{ purl . Version } .pom") ) ) ;
105
+ Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenArtifactType . Aar
106
+ && artifact . Uri == new System . Uri ( expectedUriPrefix + $ "{ purl . Name } -{ purl . Version } .aar") ) ) ;
107
+ }
108
+
109
+ [ DataTestMethod ]
110
+ [ DataRow ( "pkg:maven/com.google.cose/cose@20230908?repository_url=https://maven.google.com" , "https://maven.google.com/com/google/cose/cose/20230908/" ) ]
111
+ public async Task GoogleMaven_Cose_GetArtifactDownloadUrisSucceeds_Async ( string purlString , string expectedUriPrefix )
112
+ {
113
+ PackageURL purl = new ( purlString ) ;
114
+ List < ArtifactUri < MavenArtifactType > > uris = await _projectManager . Object . GetArtifactDownloadUrisAsync ( purl ) . ToListAsync ( ) ;
115
+
116
+ Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenArtifactType . Jar
117
+ && artifact . Uri == new System . Uri ( expectedUriPrefix + $ "{ purl . Name } -{ purl . Version } .jar") ) ) ;
118
+ Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenArtifactType . JavadocJar
119
+ && artifact . Uri == new System . Uri ( expectedUriPrefix + $ "{ purl . Name } -{ purl . Version } -javadoc.jar") ) ) ;
120
+ Assert . IsNotNull ( uris . SingleOrDefault ( artifact => artifact . Type == MavenArtifactType . Pom
69
121
&& artifact . Uri == new System . Uri ( expectedUriPrefix + $ "{ purl . Name } -{ purl . Version } .pom") ) ) ;
70
122
}
71
123
72
124
[ DataTestMethod ]
73
125
[ DataRow ( "pkg:maven/ant/[email protected] ?repository_url=https://repo1.maven.org/maven2" ) ] // Normal package
126
+ [ DataRow ( "pkg:maven/android.arch.core/[email protected] ?repository_url=https://maven.google.com" ) ]
127
+ [ DataRow ( "pkg:maven/com.google.cose/cose@20230908?repository_url=https://maven.google.com" ) ]
74
128
public async Task MetadataSucceeds ( string purlString )
75
129
{
76
130
PackageURL purl = new ( purlString ) ;
77
131
PackageMetadata ? metadata = await _projectManager . Object . GetPackageMetadataAsync ( purl , useCache : false ) ;
78
-
132
+
79
133
Assert . IsNotNull ( metadata ) ;
80
134
Assert . AreEqual ( purl . GetFullName ( ) , metadata . Name ) ;
81
135
Assert . AreEqual ( purl . Version , metadata . PackageVersion ) ;
82
136
Assert . IsNotNull ( metadata . UploadTime ) ;
83
137
}
84
138
85
139
[ DataTestMethod ]
86
- [ DataRow ( "pkg:maven/ant/[email protected] " , 13 , "1.7.0" ) ]
87
- [ DataRow ( "pkg:maven/com.microsoft.fluentui/[email protected] " , 21 , "0.1.6" ) ]
140
+ [ DataRow ( "pkg:maven/ant/[email protected] " , 12 , "1.6.5" ) ]
141
+ [ DataRow ( "pkg:maven/com.microsoft.fluentui/[email protected] " , 31 , "0.3.4" ) ]
142
+ [ DataRow ( "pkg:maven/android.arch.core/[email protected] ?repository_url=https://maven.google.com" , 3 , "1.0.0-alpha3" ) ]
143
+ [ DataRow ( "pkg:maven/com.google.cose/cose@20230908?repository_url=https://maven.google.com" , 1 , "20230908" ) ]
88
144
public async Task EnumerateVersionsSucceeds ( string purlString , int count , string latestVersion )
89
145
{
90
146
PackageURL purl = new ( purlString ) ;
@@ -93,10 +149,24 @@ public async Task EnumerateVersionsSucceeds(string purlString, int count, string
93
149
Assert . AreEqual ( count , versions . Count ) ;
94
150
Assert . AreEqual ( latestVersion , versions . First ( ) ) ;
95
151
}
96
-
152
+
97
153
[ DataTestMethod ]
98
154
[ DataRow ( "pkg:maven/ant/[email protected] " ) ]
99
155
[ DataRow ( "pkg:maven/com.microsoft.fluentui/[email protected] " ) ]
156
+ [ DataRow ( "pkg:maven/android.arch.core/[email protected] ?repository_url=https://maven.google.com" ) ]
157
+ [ DataRow ( "pkg:maven/com.google.cose/cose@20230908?repository_url=https://maven.google.com" ) ]
158
+ public async Task PackageExistsAsyncSucceeds ( string purlString )
159
+ {
160
+ PackageURL purl = new ( purlString ) ;
161
+
162
+ Assert . IsTrue ( await _projectManager . Object . PackageExistsAsync ( purl , useCache : false ) ) ;
163
+ }
164
+
165
+ [ DataTestMethod ]
166
+ [ DataRow ( "pkg:maven/ant/[email protected] " ) ]
167
+ [ DataRow ( "pkg:maven/com.microsoft.fluentui/[email protected] " ) ]
168
+ [ DataRow ( "pkg:maven/android.arch.core/[email protected] ?repository_url=https://maven.google.com" ) ]
169
+ [ DataRow ( "pkg:maven/com.google.cose/cose@20230908?repository_url=https://maven.google.com" ) ]
100
170
public async Task PackageVersionExistsAsyncSucceeds ( string purlString )
101
171
{
102
172
PackageURL purl = new ( purlString ) ;
@@ -110,9 +180,29 @@ private static void MockHttpFetchResponse(
110
180
string content ,
111
181
MockHttpMessageHandler httpMock )
112
182
{
113
- httpMock
114
- . When ( HttpMethod . Get , url )
115
- . Respond ( statusCode , "application/json" , content ) ;
183
+ if ( url . EndsWith ( ".pom" ) )
184
+ {
185
+ string lastModified = DateTime . Parse ( "9/8/2023 4:21:38 PM" ) . ToString ( "R" ) ;
186
+
187
+ httpMock
188
+ . When ( HttpMethod . Get , url )
189
+ . Respond ( req =>
190
+ {
191
+ var response = new HttpResponseMessage
192
+ {
193
+ StatusCode = statusCode ,
194
+ Content = new StringContent ( content , Encoding . UTF8 , "application/json" )
195
+ } ;
196
+ response . Content . Headers . Add ( "Last-Modified" , lastModified ) ;
197
+ return response ;
198
+ } ) ;
199
+ }
200
+ else
201
+ {
202
+ httpMock
203
+ . When ( HttpMethod . Get , url )
204
+ . Respond ( statusCode , "application/json" , content ) ;
205
+ }
116
206
}
117
207
}
118
208
}
0 commit comments