diff --git a/build.gradle b/build.gradle index 91e0c67..83ed978 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,14 @@ sourceSets { srcDirs = ['src'] } } + test { + groovy { + srcDirs = ['test/groovy'] + } + resources { + srcDirs = ['test/resources'] + } + } } dependencies { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a4b4429..ac68817 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/test/groovy/Json2ModelParserTest.groovy b/test/groovy/Json2ModelParserTest.groovy new file mode 100644 index 0000000..c583ec4 --- /dev/null +++ b/test/groovy/Json2ModelParserTest.groovy @@ -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> 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 + } +} diff --git a/test/resources/jenkins-jobdsl-jobs.json b/test/resources/jenkins-jobdsl-jobs.json new file mode 100644 index 0000000..d82effb --- /dev/null +++ b/test/resources/jenkins-jobdsl-jobs.json @@ -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" + } + } + ] +} \ No newline at end of file