github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/com/sap/piper/MtaUtilsTest.groovy (about) 1 package com.sap.piper 2 3 import org.junit.Before 4 import org.junit.ClassRule 5 import org.junit.Rule 6 import org.junit.Test 7 import org.junit.rules.ExpectedException 8 import org.junit.rules.RuleChain 9 import org.junit.rules.TemporaryFolder 10 import org.yaml.snakeyaml.Yaml 11 12 import groovy.json.JsonSlurper 13 import hudson.AbortException 14 import util.BasePiperTest 15 import util.JenkinsReadYamlRule 16 import util.Rules 17 18 19 class MtaUtilsTest extends BasePiperTest { 20 private static srcPackageJson = 'test/resources/MtaUtils/package.json' 21 private static mtaTemplate = 'resources/template_mta.yaml' 22 private static data 23 private static String generatedFile 24 private static String targetMtaDescriptor 25 private File badJson 26 private mtaUtils 27 28 private ExpectedException thrown= ExpectedException.none() 29 30 @ClassRule 31 public static TemporaryFolder tmp = new TemporaryFolder() 32 33 @Rule 34 public RuleChain ruleChain = Rules 35 .getCommonRules(this) 36 .around(new JenkinsReadYamlRule(this)) 37 .around(thrown) 38 39 @Before 40 void init() { 41 targetMtaDescriptor = "${tmp.getRoot()}/generated_mta.yml" 42 mtaUtils = new MtaUtils(nullScript) 43 44 this.helper.registerAllowedMethod('readJSON', [Map], { Map parameters -> 45 return new JsonSlurper().parse(new File(parameters.file)) 46 }) 47 48 this.helper.registerAllowedMethod('libraryResource', [Map], { Map parameters -> 49 new Yaml().load(new File(mtaTemplate).text) 50 }) 51 52 this.helper.registerAllowedMethod('readYaml', [], { 53 return new Yaml().load(new FileReader(mtaTemplate)) 54 }) 55 56 this.helper.registerAllowedMethod('writeYaml', [Map], { Map parameters -> 57 generatedFile = parameters.file 58 data = parameters.data 59 }) 60 61 this.helper.registerAllowedMethod('fileExists', [String.class], { true }) 62 } 63 64 @Test 65 void testStraightForward(){ 66 mtaUtils.generateMtaDescriptorFromPackageJson(srcPackageJson, targetMtaDescriptor, 'testAppName') 67 assert data.ID == 'com.mycompany.northwind' 68 assert data.version == '1.0.3' 69 assert data.modules.name[0] == 'testAppName' 70 assert data.modules.parameters.version[0] == '1.0.3-${timestamp}' 71 assert data.modules.parameters.name[0] == 'testAppName' 72 } 73 74 @Test 75 void testSrcPackageJsonEmpty() { 76 thrown.expect(IllegalArgumentException) 77 thrown.expectMessage("The parameter 'srcPackageJson' can not be null or empty.") 78 mtaUtils.generateMtaDescriptorFromPackageJson('', targetMtaDescriptor, 'testApplicationName') 79 } 80 81 @Test 82 void testSrcPackageJsonNull() { 83 thrown.expect(IllegalArgumentException) 84 thrown.expectMessage("The parameter 'srcPackageJson' can not be null or empty.") 85 mtaUtils.generateMtaDescriptorFromPackageJson(null, targetMtaDescriptor, 'testApplicationName') 86 } 87 88 @Test 89 void testTargetMtaDescriptorEmpty() { 90 thrown.expect(IllegalArgumentException) 91 thrown.expectMessage("The parameter 'targetMtaDescriptor' can not be null or empty.") 92 mtaUtils.generateMtaDescriptorFromPackageJson(srcPackageJson, '', 'testApplicationName') 93 } 94 95 @Test 96 void testTargetMtaDescriptorNull() { 97 thrown.expect(IllegalArgumentException) 98 thrown.expectMessage("The parameter 'targetMtaDescriptor' can not be null or empty.") 99 mtaUtils.generateMtaDescriptorFromPackageJson(srcPackageJson, null, 'testApplicationName') 100 } 101 102 @Test 103 void testApplicationNameEmpty() { 104 thrown.expect(IllegalArgumentException) 105 thrown.expectMessage("The parameter 'applicationName' can not be null or empty.") 106 mtaUtils.generateMtaDescriptorFromPackageJson(srcPackageJson, targetMtaDescriptor, '') 107 } 108 109 @Test 110 void testApplicationNameNull() { 111 thrown.expect(IllegalArgumentException) 112 thrown.expectMessage("The parameter 'applicationName' can not be null or empty.") 113 mtaUtils.generateMtaDescriptorFromPackageJson(srcPackageJson, targetMtaDescriptor, null) 114 } 115 116 @Test 117 void testMissingNameInJson() { 118 badJson = tmp.newFile('missingName.json') 119 badJson.text = missingNameInJson() 120 badJson.dump() 121 122 thrown.expect(AbortException) 123 thrown.expectMessage("'name' not set in the given package.json.") 124 125 mtaUtils.generateMtaDescriptorFromPackageJson(badJson.absolutePath, targetMtaDescriptor, 'testApplicationName') 126 } 127 128 @Test 129 void testEmptyNameInJson() { 130 badJson = tmp.newFile('emptyName.json') 131 badJson.text = emptyNameInJson() 132 badJson.dump() 133 134 thrown.expect(AbortException) 135 thrown.expectMessage("'name' not set in the given package.json.") 136 137 mtaUtils.generateMtaDescriptorFromPackageJson(badJson.absolutePath, targetMtaDescriptor, 'testApplicationName') 138 } 139 140 @Test 141 void testMissingVersionInJson() { 142 badJson = tmp.newFile('missingVersion.json') 143 badJson.text = missingVersionInJson() 144 badJson.dump() 145 146 thrown.expect(AbortException) 147 thrown.expectMessage("'version' not set in the given package.json.") 148 149 mtaUtils.generateMtaDescriptorFromPackageJson(badJson.absolutePath, targetMtaDescriptor, 'testApplicationName') 150 } 151 152 @Test 153 void testEmptyVersionInJson() { 154 badJson = tmp.newFile('emptyVersion.json') 155 badJson.text = emptyVersionInJson() 156 badJson.dump() 157 158 thrown.expect(AbortException) 159 thrown.expectMessage("'version' not set in the given package.json.") 160 161 mtaUtils.generateMtaDescriptorFromPackageJson(badJson.absolutePath, targetMtaDescriptor, 'testApplicationName') 162 } 163 164 @Test 165 void testFileGenerated() { 166 mtaUtils.generateMtaDescriptorFromPackageJson(srcPackageJson, targetMtaDescriptor, 'testApplicationName') 167 assert generatedFile.equals(targetMtaDescriptor) 168 } 169 170 171 private missingNameInJson() { 172 return ''' 173 { 174 "version": "1.0.3", 175 "description": "Webshop application for test purposes", 176 "private": true, 177 "devDependencies": { 178 "grunt": "1.0.1", 179 "@sap/grunt-sapui5-bestpractice-build": "^1.3.17" 180 } 181 } 182 ''' 183 } 184 185 private emptyNameInJson() { 186 return ''' 187 { 188 "name": "", 189 "version": "1.0.3", 190 "description": "Webshop application for test purposes", 191 "private": true, 192 "devDependencies": { 193 "grunt": "1.0.1", 194 "@sap/grunt-sapui5-bestpractice-build": "^1.3.17" 195 } 196 } 197 ''' 198 } 199 private missingVersionInJson() { 200 return ''' 201 { 202 "name": "com.mycompany.northwind", 203 "description": "Webshop application for test purposes", 204 "private": true, 205 "devDependencies": { 206 "grunt": "1.0.1", 207 "@sap/grunt-sapui5-bestpractice-build": "^1.3.17" 208 } 209 } 210 ''' 211 } 212 213 private emptyVersionInJson() { 214 return ''' 215 { 216 "name": "com.mycompany.northwind", 217 "version": "", 218 "description": "Webshop application for test purposes", 219 "private": true, 220 "devDependencies": { 221 "grunt": "1.0.1", 222 "@sap/grunt-sapui5-bestpractice-build": "^1.3.17" 223 } 224 } 225 ''' 226 } 227 }