|
| 1 | +import com.sun.net.httpserver.HttpExchange |
| 2 | +import com.sun.net.httpserver.HttpHandler |
| 3 | +import com.sun.net.httpserver.HttpServer |
| 4 | +import groovy.json.JsonBuilder |
| 5 | +import groovy.json.JsonSlurper |
| 6 | +import spock.lang.Specification |
| 7 | +import org.jfrog.artifactory.client.ArtifactoryClientBuilder |
| 8 | + |
| 9 | +class AtlassianTest extends Specification { |
| 10 | + def 'atlassian plugin test'() { |
| 11 | + setup: |
| 12 | + def baseurl = 'http://localhost:8088/artifactory' |
| 13 | + def auth = "Basic ${("admin:password").bytes.encodeBase64().toString()}" |
| 14 | + def artifactory = ArtifactoryClientBuilder.create().setUrl(baseurl) |
| 15 | + .setUsername('admin').setPassword('password').build() |
| 16 | + def mockServer = new MockServer(port: 7990) |
| 17 | + mockServer.start() |
| 18 | + |
| 19 | + when: |
| 20 | + def conn = new URL(baseurl + '/api/build').openConnection() |
| 21 | + conn.setDoOutput(true) |
| 22 | + conn.setRequestMethod('PUT') |
| 23 | + conn.setRequestProperty('Content-Type', 'application/json') |
| 24 | + conn.setRequestProperty('Authorization', auth) |
| 25 | + def jsonPayload = new JsonBuilder([version: '1.0.1', name: 'atlassian-test-build', number: '1', type: 'GENERIC', agent: [name: "Jenkins", version: "1.0.0"], buildAgent: [name: "Maven", version: "1.0.0"], started: '2020-03-05T21:47:17.229+0000', durationMillis: 2292103, vcsUrl: "http://localhost:7990/foo/bar", vcsRevision: 'b2869957593381150d3d3fb854674396b69e51c7']) |
| 26 | + conn.outputStream.withCloseable { output -> |
| 27 | + output.write(jsonPayload.toString().bytes) |
| 28 | + } |
| 29 | + assert conn.responseCode == 204 |
| 30 | + mockServer.waitForMessage() |
| 31 | + |
| 32 | + then: |
| 33 | + mockServer.messageReceived == true |
| 34 | + mockServer.validMessageReceived == true |
| 35 | + |
| 36 | + cleanup: |
| 37 | + mockServer?.stop() |
| 38 | + } |
| 39 | + |
| 40 | + class MockServer { |
| 41 | + HttpServer server |
| 42 | + int port = 7990 |
| 43 | + boolean messageReceived |
| 44 | + boolean validMessageReceived |
| 45 | + |
| 46 | + def start() { |
| 47 | + InetSocketAddress address = new InetSocketAddress(port); |
| 48 | + server = HttpServer.create(address, 0); |
| 49 | + HttpHandler requestValidatorHandler = new HttpHandler() { |
| 50 | + @Override |
| 51 | + void handle(HttpExchange exchange) throws IOException { |
| 52 | + println "Mock server receive a new request" |
| 53 | + messageReceived = true |
| 54 | + def content = exchange.requestBody.text |
| 55 | + println "Message content: $content" |
| 56 | + validMessageReceived = isMessageContentValid(content) |
| 57 | + exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0) |
| 58 | + exchange.close() |
| 59 | + } |
| 60 | + |
| 61 | + boolean isMessageContentValid(content) { |
| 62 | + def jsonContent = new JsonSlurper().parseText(content) |
| 63 | + assert jsonContent.server == 'http://localhost:8088/artifactory' |
| 64 | + assert jsonContent.state == 'SUCCESSFUL' |
| 65 | + assert jsonContent.key == 'atlassian-test-build' |
| 66 | + assert jsonContent.resultKey == 'atlassian-test-build:1' |
| 67 | + assert jsonContent.name == 'atlassian-test-build' |
| 68 | + assert jsonContent.url == "http://localhost:8088/ui/builds/atlassian-test-build/1/1583444837229" |
| 69 | + assert jsonContent.description == "atlassian-test-build build 1 was successfully published to Artifactory at 2020-03-05T21:47:17.229+0000 by Jenkins/1.0.0 using tool Maven/1.0.0" |
| 70 | + assert jsonContent.duration == 2292.103 |
| 71 | + return true |
| 72 | + } |
| 73 | + } |
| 74 | + server.createContext("/rest/build-status/1.0/commits/b2869957593381150d3d3fb854674396b69e51c7", requestValidatorHandler) |
| 75 | + server.start() |
| 76 | + println "Mock server started" |
| 77 | + } |
| 78 | + |
| 79 | + def stop() { |
| 80 | + server?.stop(0) |
| 81 | + println "Mock server stopped" |
| 82 | + } |
| 83 | + |
| 84 | + def waitForMessage() { |
| 85 | + println "Waiting up to 2 minutes for notification receiving..." |
| 86 | + def initialTime = System.currentTimeMillis() |
| 87 | + while (System.currentTimeMillis() - initialTime < 120000) { |
| 88 | + sleep(5000) |
| 89 | + if (this.messageReceived) { |
| 90 | + sleep (5000) |
| 91 | + break |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments