Skip to content
This repository was archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ikikko committed Jan 2, 2015
1 parent ec16ec6 commit 0cca538
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies {
testCompile 'org.spockframework:spock-core:0.7-groovy-1.8',
'cglib:cglib-nodep:2.2.2',
'org.objenesis:objenesis:1.3'
jenkinsTest 'org.jenkins-ci.plugins:ant:1.2@jar',
'org.jenkins-ci.plugins:mailer:1.1@jar'
}

jenkinsPlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public String parse() {
sb.append(line).append('\n');
}

JSONObject jsonObject = JSONObject.fromObject(sb.toString());
return jsonObject.getJSONObject("post").getString("message");
return parseJson(sb.toString());

} catch (IOException e) {
throw new IllegalStateException(e);
}
}

String parseJson(String json) {
JSONObject jsonObject = JSONObject.fromObject(json);
return jsonObject.getJSONObject("post").getString("message");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

public class BuildExecutor extends WebhookExecutor {

// TODO nakamura : テスト追加
// TODO nakamura : パラメータ付きビルドの対応
// TODO nakamura : 認証付きの場合の確認

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jenkinsci.plugins.typetalk.webhookaction

import org.jenkinsci.plugins.typetalk.webhookaction.executorimpl.BuildExecutor
import org.jenkinsci.plugins.typetalk.webhookaction.executorimpl.UndefinedExecutor
import spock.lang.Specification
import spock.lang.Unroll

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class WebhookExecutorFactorySpec extends Specification {

@Unroll
def "create with message : #message"() {
setup:
def req = Mock(HttpServletRequest)
def res = Mock(HttpServletResponse)

when:
def executor = WebhookExecutorFactory.create(req, res, message)

then:
executor.class == result

where:
message || result
"@jenkins+ dummy" || UndefinedExecutor
"@jenkins+ build typetalk-plugin" || BuildExecutor
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.jenkinsci.plugins.typetalk.webhookaction

import spock.lang.Specification

class WebhookParserSpec extends Specification {

def parseJson() {
setup:
def parser = new WebhookParser()
def json = """
{
"topic": {
"id": 9526,
"name": "Typetalk Hack Tokyo Dec 2014",
"suggestion": "Typetalk Hack Tokyo Dec 2014",
"lastPostedAt": "2014-12-16T11:17:44Z",
"createdAt": "2014-12-16T08:32:53Z",
"updatedAt": "2014-12-16T08:32:53Z"
},
"post": {
"id": 754399,
"topicId": 9526,
"replyTo": null,
"message": "@ikikkobot+ build hello-typetalk-plugin",
"account": {
"id": 10000,
"name": "ikikkotest",
"fullName": "ikikkotest",
"suggestion": "ikikkotest",
"imageUrl": "https://typetalk.in/accounts/10/profile_image.png?t=1413099125640",
"createdAt": "2012-03-07T05:13:52Z",
"updatedAt": "2014-12-16T09:12:55Z"
},
"mention": null,
"attachments": [],
"likes": [],
"talks": [],
"links": [],
"createdAt": "2014-12-16T11:17:44Z",
"updatedAt": "2014-12-16T11:17:44Z"
}
}
"""

expect:
parser.parseJson(json) == "@ikikkobot+ build hello-typetalk-plugin"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.jenkinsci.plugins.typetalk.webhookaction.executorimpl

import org.junit.Rule
import org.jvnet.hudson.test.JenkinsRule
import spock.lang.Specification
import spock.lang.Unroll

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class BuildExecutorSpec extends Specification {

@Rule JenkinsRule j = new JenkinsRule()

@Unroll
def "execute : #job"() {
setup:
j.createFreeStyleProject("hello-typetalk-plugin")

def req = Mock(HttpServletRequest)
def res = Mock(HttpServletResponse)
def writer = new StringWriter()
res.writer >> new PrintWriter(writer)
def executor = new BuildExecutor(req, res, job)

when:
executor.execute()

then:
writer.toString() == result

where:
job || result
"hello-typetalk-plugin" || "'hello-typetalk-plugin' has been scheduled\n"
"dummy" || "'dummy' is not found\n"
}
}

0 comments on commit 0cca538

Please sign in to comment.