1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/com/sap/piper/DescriptorUtilsTest.groovy

232 lines
7.3 KiB
Groovy
Raw Normal View History

2019-03-13 00:22:27 +02:00
package com.sap.piper
2019-03-13 12:06:47 +02:00
import hudson.AbortException
2019-03-13 00:22:27 +02:00
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
2019-03-13 12:06:47 +02:00
import util.JenkinsEnvironmentRule
import util.JenkinsErrorRule
2019-03-13 00:22:27 +02:00
import util.JenkinsLoggingRule
import util.JenkinsSetupRule
import util.LibraryLoadingTestExecutionListener
2019-03-13 12:38:44 +02:00
import static org.hamcrest.Matchers.is
2019-03-13 00:22:27 +02:00
import static org.junit.Assert.assertEquals
2019-03-13 12:06:47 +02:00
import static org.junit.Assert.assertThat
import static org.hamcrest.core.Is.*
2019-03-13 00:22:27 +02:00
class DescriptorUtilsTest extends BasePiperTest {
@Rule
2019-03-13 12:06:47 +02:00
public JenkinsErrorRule errorRule = new JenkinsErrorRule(this)
@Rule
public JenkinsEnvironmentRule environmentRule = new JenkinsEnvironmentRule(this)
@Rule
public JenkinsSetupRule setUpRule = new JenkinsSetupRule(this)
@Rule
2019-03-13 00:22:27 +02:00
public JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
@Rule
public RuleChain ruleChain =
RuleChain.outerRule(setUpRule)
2019-03-13 12:06:47 +02:00
.around(errorRule)
.around(environmentRule)
2019-03-13 00:22:27 +02:00
.around(loggingRule)
DescriptorUtils descriptorUtils
@Before
void init() throws Exception {
descriptorUtils = new DescriptorUtils()
LibraryLoadingTestExecutionListener.prepareObjectInterceptors(descriptorUtils)
}
2019-03-13 12:06:47 +02:00
@Test
void testGetNpmGAVSapArtifact() {
helper.registerAllowedMethod("readJSON", [Map.class], {
searchConfig ->
def packageJsonFile = new File("test/resources/DescriptorUtils/npm/${searchConfig.file}")
return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text)
2019-03-13 12:06:47 +02:00
})
def gav = descriptorUtils.getNpmGAV('package2.json')
assertEquals(gav.group, '')
assertEquals(gav.artifact, 'some-test')
assertEquals(gav.version, '1.2.3')
}
2019-03-13 00:22:27 +02:00
@Test
void testGetNpmGAV() {
helper.registerAllowedMethod("readJSON", [Map.class], {
searchConfig ->
def packageJsonFile = new File("test/resources/DescriptorUtils/npm/${searchConfig.file}")
return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text)
2019-03-13 00:22:27 +02:00
})
def gav = descriptorUtils.getNpmGAV('package.json')
assertEquals(gav.group, '@sap')
assertEquals(gav.artifact, 'hdi-deploy')
assertEquals(gav.version, '2.3.0')
}
2019-03-13 12:06:47 +02:00
@Test
void testGetNpmGAVSapArtifactError() {
helper.registerAllowedMethod("readJSON", [Map.class], {
searchConfig ->
def packageJsonFile = new File("test/resources/DescriptorUtils/npm/${searchConfig.file}")
return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text)
2019-03-13 12:06:47 +02:00
})
2019-03-13 12:38:44 +02:00
def errorCaught = false
2019-03-13 12:06:47 +02:00
try {
descriptorUtils.getNpmGAV('package3.json')
} catch (e) {
2019-03-13 12:38:44 +02:00
errorCaught = true
2019-03-13 12:06:47 +02:00
assertThat(e, isA(AbortException.class))
assertThat(e.getMessage(), is("Unable to parse package name '@someerror'"))
}
2019-03-13 12:38:44 +02:00
assertThat(errorCaught, is(true))
2019-03-13 12:06:47 +02:00
}
2019-03-13 00:36:55 +02:00
@Test
void testGetSbtGAV() {
helper.registerAllowedMethod("readJSON", [Map.class], {
searchConfig ->
def packageJsonFile = new File("test/resources/DescriptorUtils/sbt/${searchConfig.file}")
return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text)
2019-03-13 00:36:55 +02:00
})
def gav = descriptorUtils.getSbtGAV('sbtDescriptor.json')
assertEquals(gav.group, 'sap')
assertEquals(gav.artifact, 'hdi-deploy')
assertEquals(gav.packaging, 'test')
assertEquals(gav.version, '2.3.0')
}
2019-03-13 00:22:27 +02:00
@Test
void testGetDlangGAV() {
helper.registerAllowedMethod("readJSON", [Map.class], {
searchConfig ->
def packageJsonFile = new File("test/resources/DescriptorUtils/dlang/${searchConfig.file}")
return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text)
2019-03-13 00:22:27 +02:00
})
def gav = descriptorUtils.getDlangGAV('dub.json')
assertEquals(gav.group, 'com.sap.dlang')
assertEquals(gav.artifact, 'hdi-deploy')
assertEquals(gav.version, '2.3.0')
}
@Test
void testGetPipGAV() {
2019-03-22 12:55:50 +02:00
helper.registerAllowedMethod("readFile", [Map.class], {
2019-03-13 00:22:27 +02:00
map ->
2019-03-22 12:55:50 +02:00
def descriptorFile = new File("test/resources/utilsTest/${map.file}")
2019-03-13 00:22:27 +02:00
return descriptorFile.text
})
def gav = descriptorUtils.getPipGAV('setup.py')
assertEquals('', gav.group)
assertEquals('py_connect', gav.artifact)
assertEquals('1.0', gav.version)
}
2019-03-13 11:41:11 +02:00
@Test
void testGetPipGAVFromVersionTxt() {
2019-03-22 12:55:50 +02:00
helper.registerAllowedMethod("readFile", [Map.class], {
2019-03-13 11:41:11 +02:00
map ->
2019-03-22 12:55:50 +02:00
def descriptorFile = new File("test/resources/DescriptorUtils/pip/${map.file}")
2019-03-13 11:41:11 +02:00
return descriptorFile.text
})
def gav = descriptorUtils.getPipGAV('setup.py')
assertEquals('', gav.group)
assertEquals('some-test', gav.artifact)
assertEquals('1.0.0-SNAPSHOT', gav.version)
}
2019-03-13 00:22:27 +02:00
@Test
void testGetMavenGAVComplete() {
helper.registerAllowedMethod("readMavenPom", [Map.class], {
searchConfig ->
return new Object(){
def groupId = 'test.group', artifactId = 'test-artifact', version = '1.2.4', packaging = 'jar'
}
})
def gav = descriptorUtils.getMavenGAV('pom.xml')
assertEquals(gav.group, 'test.group')
assertEquals(gav.artifact, 'test-artifact')
assertEquals(gav.version, '1.2.4')
assertEquals(gav.packaging, 'jar')
}
@Test
void testGetMavenGAVPartial() {
def parameters = []
helper.registerAllowedMethod("readMavenPom", [Map.class], {
searchConfig ->
return new Object(){
def groupId = null, artifactId = null, version = null, packaging = 'jar'
}
})
helper.registerAllowedMethod("sh", [Map.class], {
mvnHelpCommand ->
def scriptCommand = mvnHelpCommand['script']
parameters.add(scriptCommand)
if(scriptCommand.contains('project.groupId'))
return 'test.group'
if(scriptCommand.contains('project.artifactId'))
return 'test-artifact'
if(scriptCommand.contains('project.version'))
return '1.2.4'
})
def gav = descriptorUtils.getMavenGAV('pom.xml')
assertEquals(gav.group, 'test.group')
assertEquals(gav.artifact, 'test-artifact')
assertEquals(gav.version, '1.2.4')
assertEquals(gav.packaging, 'jar')
}
2019-03-22 12:55:50 +02:00
@Test
void testGetGoGAV() {
helper.registerAllowedMethod("readFile", [Map.class], {
map ->
def path = 'test/resources/DescriptorUtils/go/' + map.file.substring(map.file.lastIndexOf(File.separator) + 1, map.file.length())
def descriptorFile = new File(path)
if(descriptorFile.exists())
return descriptorFile.text
else
return null
})
def gav = descriptorUtils.getGoGAV('./myProject/glide.yaml')
assertEquals('', gav.group)
assertEquals('myProject', gav.artifact)
assertEquals('1.2.3', gav.version)
}
2019-03-13 00:22:27 +02:00
}