github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/com/sap/piper/DescriptorUtilsTest.groovy (about) 1 package com.sap.piper 2 3 import hudson.AbortException 4 import org.junit.Before 5 import org.junit.Rule 6 import org.junit.Test 7 import org.junit.rules.RuleChain 8 import util.BasePiperTest 9 import util.JenkinsEnvironmentRule 10 import util.JenkinsErrorRule 11 import util.JenkinsLoggingRule 12 import util.JenkinsSetupRule 13 import util.LibraryLoadingTestExecutionListener 14 import util.Rules 15 16 import static org.hamcrest.Matchers.is 17 import static org.junit.Assert.assertEquals 18 import static org.junit.Assert.assertThat 19 import static org.hamcrest.core.Is.* 20 21 class DescriptorUtilsTest extends BasePiperTest { 22 23 @Rule 24 public JenkinsErrorRule errorRule = new JenkinsErrorRule(this) 25 @Rule 26 public JenkinsEnvironmentRule environmentRule = new JenkinsEnvironmentRule(this) 27 @Rule 28 public JenkinsSetupRule setUpRule = new JenkinsSetupRule(this) 29 @Rule 30 public JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) 31 32 @Rule 33 public RuleChain ruleChain = Rules.getCommonRules(this) 34 .around(loggingRule) 35 36 DescriptorUtils descriptorUtils 37 38 @Before 39 void init() throws Exception { 40 descriptorUtils = new DescriptorUtils() 41 LibraryLoadingTestExecutionListener.prepareObjectInterceptors(descriptorUtils) 42 } 43 44 @Test 45 void testGetNpmGAVSapArtifact() { 46 47 helper.registerAllowedMethod("readJSON", [Map.class], { 48 searchConfig -> 49 def packageJsonFile = new File("test/resources/DescriptorUtils/npm/${searchConfig.file}") 50 return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text) 51 }) 52 53 def gav = descriptorUtils.getNpmGAV('package2.json') 54 55 assertEquals(gav.group, '') 56 assertEquals(gav.artifact, 'some-test') 57 assertEquals(gav.version, '1.2.3') 58 } 59 60 @Test 61 void testGetNpmGAV() { 62 63 helper.registerAllowedMethod("readJSON", [Map.class], { 64 searchConfig -> 65 def packageJsonFile = new File("test/resources/DescriptorUtils/npm/${searchConfig.file}") 66 return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text) 67 }) 68 69 def gav = descriptorUtils.getNpmGAV('package.json') 70 71 assertEquals(gav.group, '@sap') 72 assertEquals(gav.artifact, 'hdi-deploy') 73 assertEquals(gav.version, '2.3.0') 74 } 75 76 @Test 77 void testGetNpmGAVSapArtifactError() { 78 79 helper.registerAllowedMethod("readJSON", [Map.class], { 80 searchConfig -> 81 def packageJsonFile = new File("test/resources/DescriptorUtils/npm/${searchConfig.file}") 82 return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text) 83 }) 84 85 def errorCaught = false 86 try { 87 descriptorUtils.getNpmGAV('package3.json') 88 } catch (e) { 89 errorCaught = true 90 assertThat(e, isA(AbortException.class)) 91 assertThat(e.getMessage(), is("Unable to parse package name '@someerror'")) 92 } 93 assertThat(errorCaught, is(true)) 94 } 95 96 @Test 97 void testGetSbtGAV() { 98 99 helper.registerAllowedMethod("readJSON", [Map.class], { 100 searchConfig -> 101 def packageJsonFile = new File("test/resources/DescriptorUtils/sbt/${searchConfig.file}") 102 return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text) 103 }) 104 105 def gav = descriptorUtils.getSbtGAV('sbtDescriptor.json') 106 107 assertEquals(gav.group, 'sap') 108 assertEquals(gav.artifact, 'hdi-deploy') 109 assertEquals(gav.packaging, 'test') 110 assertEquals(gav.version, '2.3.0') 111 } 112 113 @Test 114 void testGetDubGAV() { 115 116 helper.registerAllowedMethod("readJSON", [Map.class], { 117 searchConfig -> 118 def packageJsonFile = new File("test/resources/DescriptorUtils/dub/${searchConfig.file}") 119 return new JsonUtils().jsonStringToGroovyObject(packageJsonFile.text) 120 }) 121 122 def gav = descriptorUtils.getDubGAV('dub.json') 123 124 assertEquals(gav.group, 'com.sap.dlang') 125 assertEquals(gav.artifact, 'hdi-deploy') 126 assertEquals(gav.version, '2.3.0') 127 } 128 129 @Test 130 void testGetPipGAV() { 131 132 helper.registerAllowedMethod("readFile", [Map.class], { 133 map -> 134 def descriptorFile = new File("test/resources/utilsTest/${map.file}") 135 return descriptorFile.text 136 }) 137 138 def gav = descriptorUtils.getPipGAV('setup.py') 139 140 assertEquals('', gav.group) 141 assertEquals('py_connect', gav.artifact) 142 assertEquals('1.0', gav.version) 143 } 144 145 @Test 146 void testGetPipGAVFromVersionTxt() { 147 148 helper.registerAllowedMethod("readFile", [Map.class], { 149 map -> 150 def descriptorFile = new File("test/resources/DescriptorUtils/pip/${map.file}") 151 return descriptorFile.text 152 }) 153 154 def gav = descriptorUtils.getPipGAV('setup.py') 155 156 assertEquals('', gav.group) 157 assertEquals('some-test', gav.artifact) 158 assertEquals('1.0.0-SNAPSHOT', gav.version) 159 } 160 161 @Test 162 void testGetMavenGAVComplete() { 163 164 helper.registerAllowedMethod("readMavenPom", [Map.class], { 165 searchConfig -> 166 return new Object(){ 167 def groupId = 'test.group', artifactId = 'test-artifact', version = '1.2.4', packaging = 'jar' 168 } 169 }) 170 171 def gav = descriptorUtils.getMavenGAV('pom.xml') 172 173 assertEquals(gav.group, 'test.group') 174 assertEquals(gav.artifact, 'test-artifact') 175 assertEquals(gav.version, '1.2.4') 176 assertEquals(gav.packaging, 'jar') 177 } 178 179 @Test 180 void testGetMavenGAVPartial() { 181 def parameters = [] 182 183 helper.registerAllowedMethod("readMavenPom", [Map.class], { 184 searchConfig -> 185 return new Object(){ 186 def groupId = null, artifactId = null, version = null, packaging = 'jar' 187 } 188 }) 189 190 helper.registerAllowedMethod("sh", [Map.class], { 191 mvnHelpCommand -> 192 def scriptCommand = mvnHelpCommand['script'] 193 parameters.add(scriptCommand) 194 if(scriptCommand.contains('project.groupId')) 195 return 'test.group' 196 if(scriptCommand.contains('project.artifactId')) 197 return 'test-artifact' 198 if(scriptCommand.contains('project.version')) 199 return '1.2.4' 200 }) 201 202 def gav = descriptorUtils.getMavenGAV('pom.xml') 203 204 assertEquals(gav.group, 'test.group') 205 assertEquals(gav.artifact, 'test-artifact') 206 assertEquals(gav.version, '1.2.4') 207 assertEquals(gav.packaging, 'jar') 208 } 209 210 @Test 211 void testGetGoGAV() { 212 213 helper.registerAllowedMethod("readFile", [Map.class], { 214 map -> 215 def path = 'test/resources/DescriptorUtils/go/' + map.file.substring(map.file.lastIndexOf('/') + 1, map.file.length()) 216 def descriptorFile = new File(path) 217 if(descriptorFile.exists()) 218 return descriptorFile.text 219 else 220 return null 221 }) 222 223 def gav = descriptorUtils.getGoGAV('./myProject/Gopkg.toml', new URI('https://github.com/test/golang')) 224 225 assertEquals('', gav.group) 226 assertEquals('github.com/test/golang.myProject', gav.artifact) 227 assertEquals('1.2.3', gav.version) 228 } 229 }