1+ package nebula.plugin.publishing
2+
3+ import org.mockserver.integration.ClientAndServer
4+ import org.mockserver.model.HttpRequest.request
5+ import org.mockserver.model.HttpResponse.response
6+
7+ fun expectPublicationWithChecksums (mockServer : ClientAndServer , path : String ): List <(ClientAndServer ) -> Unit > {
8+ val verifications = mutableListOf< (ClientAndServer ) -> Unit > ()
9+ val paths = listOf (
10+ path,
11+ " $path .md5" ,
12+ " $path .sha1" ,
13+ " $path .sha256" ,
14+ " $path .sha512"
15+ )
16+ paths.forEach {
17+ val request = request(it).withMethod(" PUT" )
18+ mockServer
19+ .`when `(request)
20+ .respond { response().withStatusCode(200 ) }
21+ verifications.add { it.verify(request) }
22+ }
23+ return verifications
24+ }
25+
26+ fun expectSignedPublicationWithChecksums (mockServer : ClientAndServer , path : String ): List <(ClientAndServer ) -> Unit > {
27+ return expectPublicationWithChecksums(mockServer, path) +
28+ expectPublicationWithChecksums(mockServer, " $path .asc" )
29+ }
30+
31+ class Publication (
32+ val mockServer : ClientAndServer ,
33+ val repo : String ,
34+ val groupName : String ,
35+ val moduleName : String ,
36+ val version : String ,
37+ ) {
38+ private val groupPath = groupName.replace(" ." , " /" )
39+ private val modulePath = " $repo /$groupPath /${moduleName} "
40+ private val fullPath = " $modulePath /${version} "
41+ private val verifications = mutableListOf< (ClientAndServer ) -> Unit > ()
42+ fun withArtifact (classifier : String , extension : String ) {
43+ verifications.addAll(
44+ expectSignedPublicationWithChecksums(
45+ mockServer,
46+ " /$fullPath /$moduleName -${version} -$classifier .$extension "
47+ )
48+ )
49+ }
50+
51+ fun withGradleModuleMetadata () {
52+ verifications.addAll(
53+ expectSignedPublicationWithChecksums(mockServer, " /$fullPath /$moduleName -${version} .module" )
54+ )
55+ }
56+
57+ fun withArtifact (extension : String ) {
58+ verifications.addAll(
59+ expectSignedPublicationWithChecksums(mockServer, " /$fullPath /$moduleName -${version} .$extension " )
60+ )
61+ }
62+
63+ fun verifications (): List <(ClientAndServer ) -> Unit > {
64+ return verifications
65+ }
66+ }
67+
68+ fun ClientAndServer.expectPublication (
69+ repo : String ,
70+ groupName : String ,
71+ moduleName : String ,
72+ version : String ,
73+ additional : Publication .() -> Unit = {}
74+ ): VerificationsContainer {
75+ val groupPath = groupName.replace(" ." , " /" )
76+ val modulePath = " $repo /$groupPath /${moduleName} "
77+ val fullPath = " $modulePath /${version} "
78+ val allVersionsXml = listOf (" 0.0.0" ).joinToString(" /n" ) { " <version>$it </version>" }
79+ `when `(request(" /$modulePath /maven-metadata.xml" ))
80+ .respond {
81+ response().withBody(
82+ """
83+ <?xml version="1.0" encoding="UTF-8"?>
84+ <metadata>
85+ <groupId>${groupName} </groupId>
86+ <artifactId>${moduleName} </artifactId>
87+ <versioning>
88+ <latest>0.0.0</latest>
89+ <release>0.0.0</release>
90+ <versions>
91+ $allVersionsXml
92+ </versions>
93+ <lastUpdated>20210816163607</lastUpdated>
94+ </versioning>
95+ </metadata>
96+ """
97+ ).withStatusCode(200 )
98+ }
99+ val verifications = mutableListOf< (ClientAndServer ) -> Unit > ()
100+ verifications.addAll(
101+ expectSignedPublicationWithChecksums(this , " /$fullPath /${moduleName} -${version} .pom" )
102+ )
103+ verifications.addAll(
104+ expectPublicationWithChecksums(this , " /$modulePath /maven-metadata.xml" )
105+ )
106+
107+ verifications.addAll(
108+ Publication (this , repo, groupName, moduleName, version).apply { additional() }.verifications()
109+ )
110+ return VerificationsContainer (verifications)
111+ }
112+
113+ class VerificationsContainer (val verifications : List <(ClientAndServer ) -> Unit >) {
114+ fun verify (mockServer : ClientAndServer ) {
115+ verifications.forEach { verification ->
116+ verification(mockServer)
117+ }
118+ }
119+ }
0 commit comments