Skip to content

Commit

Permalink
Tests for Json2ModelParser
Browse files Browse the repository at this point in the history
  • Loading branch information
Ragin-LundF committed Apr 30, 2020
1 parent c01d784 commit 74612df
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 2 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ sourceSets {
srcDirs = ['src']
}
}
test {
groovy {
srcDirs = ['test/groovy']
}
resources {
srcDirs = ['test/resources']
}
}
}

dependencies {
Expand Down
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Thu Apr 30 19:42:56 CEST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
164 changes: 164 additions & 0 deletions test/groovy/Json2ModelParserTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import groovy.test.GroovyTestCase
import model.JobsModel
import model.MultibranchModel
import model.PipelineJobModel
import org.junit.jupiter.api.Test
import parser.Json2ModelParser

class Json2ModelParserTest extends GroovyTestCase {
@Test
void testParseJobJsonToModel() {
// read model
JobsModel jenkinsJobModel = readJobsModelWithJson2ModelParser()

// assert basic structure
assertNotNull(jenkinsJobModel.multiBranchJobs)
assertNotNull(jenkinsJobModel.pipelineJobs)
assertEquals(2, jenkinsJobModel.multiBranchJobs.size())
assertEquals(1, jenkinsJobModel.pipelineJobs.size())

// validate multibranchModel
assertEqualsMultiBranchJobModel(
"firstMultiBranchJob",
"First job",
"mainView",
"Jenkinsfile",
"firstMultiBranchJob",
"https://github.com/myProjects/firstMultiBranchJob.git",
"* * * * *",
"myGitCredentialsId",
jenkinsJobModel.multiBranchJobs.get(0) as MultibranchModel
)
assertEqualsMultiBranchJobModel(
"secondMultiBranchJob",
"Second job",
null,
"Jenkinsfile",
"secondMultiBranchJob",
"https://github.com/myProjects/secondMultiBranchJob.git",
"* * * * *",
"myGitCredentialsId",
jenkinsJobModel.multiBranchJobs.get(1) as MultibranchModel
)

// validate pipelineJob
assertEqualsPipelineJobModel(
"myPipelineJob",
"This is a test job",
"pipelineView",
"Jenkinsfile.groovy",
"2 H * * *",
"618f1dae-9475-41c3-9d17-381ff3c8684e",
"master",
"myPipelineJobId",
"https://github.com/myProjects/myPipelineJobProject.git",
"* * * * *",
"myGitCredentialsId",
jenkinsJobModel.pipelineJobs.get(0) as PipelineJobModel
)
}

@Test
void testCreateViewModelMap() {
// read jobs
JobsModel jobsModel = readJobsModelWithJson2ModelParser()
assertNotNull(jobsModel)

// create view map
Map<String, List<String>> viewMap = Json2ModelParser.createViewModelMap(jobsModel)
assertNotNull(viewMap)
assertEquals(2, viewMap.size())
assertTrue(viewMap.find {it.key == "mainView"}.value.contains("firstMultiBranchJob"))
assertTrue(viewMap.find {it.key == "pipelineView"}.value.contains("myPipelineJob"))
}

/**
* Check the MultibranchModel
*
* @param jobName expected name of the multibranch job
* @param jobDescription expected description of the multibranch job
* @param view expected view of the multibranch job
* @param pipelineScriptPath expected pipeline scriptPath of the multibranch job
* @param gitRepositoryId expected repositoryId of the Git object of the multibranch job
* @param gitRepositoryUrl expected repositoryUrl of the Git object of the multibranch job
* @param gitRepositoryTrigger expected repositoryTrigger of the Git object of the multibranch job
* @param gitCredentialsId expected credentialsId of the Git object of the multibranch job
* @param multibranchModel filled instance of MultiBranchModel which should be compared (actual)
*/
private void assertEqualsMultiBranchJobModel(
String jobName,
String jobDescription,
String view,
String pipelineScriptPath,
String gitRepositoryId,
String gitRepositoryUrl,
String gitRepositoryTrigger,
String gitCredentialsId,
MultibranchModel multibranchModel
) {
assertEquals(jobName, multibranchModel.jobName)
assertEquals(jobDescription, multibranchModel.jobDescription)
assertEquals(view, multibranchModel.view)
assertEquals(pipelineScriptPath, multibranchModel.pipelineScriptPath)
assertNotNull(multibranchModel.git)
assertEquals(gitRepositoryId, multibranchModel.git.repositoryId)
assertEquals(gitRepositoryUrl, multibranchModel.git.repositoryUrl)
assertEquals(gitRepositoryTrigger, multibranchModel.git.repositoryTrigger)
assertEquals(gitCredentialsId, multibranchModel.git.credentialsId)
}
/**
* Check the MultibranchModel
*
* @param jobName expected name of the multibranch job
* @param jobDescription expected description of the multibranch job
* @param view expected view of the multibranch job
* @param pipelineScriptPath expected pipeline scriptPath of the multibranch job
* @param cronTrigger expected cronTrigger of the multibranch job
* @param remoteTriggerUuid expected remoteTriggerUuid of the multibranch job
* @param remoteBranchName expected remoteBranchName of the multibranch job
* @param gitRepositoryId expected repositoryId of the Git object of the multibranch job
* @param gitRepositoryUrl expected repositoryUrl of the Git object of the multibranch job
* @param gitRepositoryTrigger expected repositoryTrigger of the Git object of the multibranch job
* @param gitCredentialsId expected credentialsId of the Git object of the multibranch job
* @param pipelineJobModel filled instance of MultiBranchModel which should be compared (actual)
*/
private void assertEqualsPipelineJobModel(
String jobName,
String jobDescription,
String view,
String pipelineScriptPath,
String cronTrigger,
String remoteTriggerUuid,
String remoteBranchName,
String gitRepositoryId,
String gitRepositoryUrl,
String gitRepositoryTrigger,
String gitCredentialsId,
PipelineJobModel pipelineJobModel
) {
assertEquals(jobName, pipelineJobModel.jobName)
assertEquals(jobDescription, pipelineJobModel.jobDescription)
assertEquals(view, pipelineJobModel.view)
assertEquals(pipelineScriptPath, pipelineJobModel.pipelineScriptPath)
assertEquals(cronTrigger, pipelineJobModel.cronTrigger)
assertEquals(remoteTriggerUuid, pipelineJobModel.remoteTriggerUuid)
assertEquals(remoteBranchName, pipelineJobModel.remoteBranchName)
assertNotNull(pipelineJobModel.git)
assertEquals(gitRepositoryId, pipelineJobModel.git.repositoryId)
assertEquals(gitRepositoryUrl, pipelineJobModel.git.repositoryUrl)
assertEquals(gitRepositoryTrigger, pipelineJobModel.git.repositoryTrigger)
assertEquals(gitCredentialsId, pipelineJobModel.git.credentialsId)
}

/**
* Read jobs with Json2ModelParser
*
* @return parsed JobsModel object from jenkins-jobdsl-jobs.json
*/
private JobsModel readJobsModelWithJson2ModelParser() {
JobsModel jobsModel = Json2ModelParser.parseJobJsonToModel(getClass().getResource("jenkins-jobdsl-jobs.json").toURI().path)
assertNotNull(jobsModel)

return jobsModel
}
}
44 changes: 44 additions & 0 deletions test/resources/jenkins-jobdsl-jobs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"multiBranchJobs": [
{
"jobName": "firstMultiBranchJob",
"jobDescription": "First job",
"view": "mainView",
"pipelineScriptPath": "Jenkinsfile",
"git": {
"repositoryId": "firstMultiBranchJob",
"repositoryUrl": "https://github.com/myProjects/firstMultiBranchJob.git",
"repositoryTrigger": "* * * * *",
"credentialsId": "myGitCredentialsId"
}
},
{
"jobName": "secondMultiBranchJob",
"jobDescription": "Second job",
"pipelineScriptPath": "Jenkinsfile",
"git": {
"repositoryId": "secondMultiBranchJob",
"repositoryUrl": "https://github.com/myProjects/secondMultiBranchJob.git",
"repositoryTrigger": "* * * * *",
"credentialsId": "myGitCredentialsId"
}
}
],
"pipelineJobs": [
{
"jobName": "myPipelineJob",
"jobDescription": "This is a test job",
"pipelineScriptPath": "Jenkinsfile.groovy",
"cronTrigger": "2 H * * *",
"view": "pipelineView",
"remoteTriggerUuid": "618f1dae-9475-41c3-9d17-381ff3c8684e",
"remoteBranchName": "master",
"git": {
"repositoryId": "myPipelineJobId",
"repositoryUrl": "https://github.com/myProjects/myPipelineJobProject.git",
"repositoryTrigger": "* * * * *",
"credentialsId": "myGitCredentialsId"
}
}
]
}

0 comments on commit 74612df

Please sign in to comment.