github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/com/sap/piper/integration/CloudFoundryTest.groovy (about) 1 package com.sap.piper.integration 2 3 import hudson.AbortException 4 import org.junit.Rule 5 import org.junit.Test 6 import org.junit.Ignore 7 import org.junit.rules.ExpectedException 8 import org.junit.rules.RuleChain 9 import util.* 10 11 import groovy.json.JsonSlurper 12 import static org.hamcrest.Matchers.* 13 import static org.junit.Assert.assertThat 14 import util.JenkinsCredentialsRule 15 16 class CloudFoundryTest extends BasePiperTest { 17 public ExpectedException exception = ExpectedException.none() 18 private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this) 19 private JenkinsCredentialsRule jenkinsCredentialsRule = new JenkinsCredentialsRule(this) 20 21 @Rule 22 public RuleChain rules = Rules 23 .getCommonRules(this) 24 .around(exception) 25 .around(new JenkinsErrorRule(this)) 26 .around(new JenkinsReadJsonRule(this)) 27 .around(shellRule) 28 .around(jenkinsCredentialsRule) 29 30 @Test 31 void getAuthEndPoint_test() { 32 helper.registerAllowedMethod('httpRequest', [Map.class] , { 33 return [content: '{ "authorization_endpoint": "myAuthEndPoint" }', status: 200] 34 }) 35 36 String apiEndPoint = 'http://dummy.sap.com' 37 boolean verbose = true 38 39 def cf = new CloudFoundry(nullScript) 40 def endPoint = cf.getAuthEndPoint(apiEndPoint, verbose) 41 42 assertThat(endPoint, is('myAuthEndPoint')) 43 } 44 45 @Test 46 void getBearerToken_test(){ 47 String authorizationEndpoint = 'http://dummy.sap.com' 48 String credentialsId = 'credentialsId' 49 boolean verbose = true 50 51 jenkinsCredentialsRule.withCredentials(credentialsId, 'myuser', 'topsecret') 52 53 helper.registerAllowedMethod('httpRequest', [Map.class] , { 54 return [content: '{ "access_token": "myAccessToken" }', status: 200] 55 }) 56 57 def cf = new CloudFoundry(nullScript) 58 def token = cf.getBearerToken(authorizationEndpoint, credentialsId, verbose) 59 60 assertThat(token.toString(), is('Bearer myAccessToken')) 61 } 62 63 @Test 64 void getAppRefUrl_test(){ 65 String apiEndpoint = 'http://dummy.sap.com' 66 String org = 'myOrg' 67 String space = 'mySpace' 68 String bearerToken = 'myAccessToken' 69 String appName = 'myAppName' 70 boolean verbose = true 71 72 helper.registerAllowedMethod('httpRequest', [Map.class] , { 73 return [content: '{ "resources":[{"guid":"myGuid", "links":{"self":{"href":"myAppUrl"}}}] }', status: 200] 74 }) 75 76 def cf = new CloudFoundry(nullScript) 77 def appUrl = cf.getAppRefUrl(apiEndpoint, org, space, bearerToken, appName, verbose) 78 79 assertThat(appUrl.toString(), is('myAppUrl')) 80 } 81 82 @Test 83 void getOrgGuid_test(){ 84 String apiEndpoint = 'http://dummy.sap.com' 85 String org = 'myOrg' 86 String bearerToken = 'myToken' 87 boolean verbose = true 88 89 helper.registerAllowedMethod('httpRequest', [Map.class] , { 90 return [content: '{ "resources":[{"guid":"myOrgGuid"}] }', status: 200] 91 }) 92 93 def cf = new CloudFoundry(nullScript) 94 def orgGuid = cf.getOrgGuid(apiEndpoint, org, bearerToken, verbose) 95 96 assertThat(orgGuid.toString(), is('myOrgGuid')) 97 } 98 99 @Test 100 void getSpaceGuid_test(){ 101 String apiEndpoint ='http://dummy.sap.com' 102 String orgGuid = 'myOrgGuid' 103 String space = 'mySpace' 104 String bearerToken = 'myToken' 105 boolean verbose = true 106 107 helper.registerAllowedMethod('httpRequest', [Map.class] , { 108 return [content: '{ "resources":[{"guid":"mySpaceGuid"}] }', status: 200] 109 }) 110 111 def cf = new CloudFoundry(nullScript) 112 def spaceGuid = cf.getSpaceGuid(apiEndpoint, orgGuid, space, bearerToken, verbose) 113 114 assertThat(spaceGuid.toString(), is('mySpaceGuid')) 115 } 116 117 @Test 118 void getAppEnvironment_test(){ 119 String apiEndpoint = 'http://dummy.sap.com' 120 String org = 'myOrg' 121 String space = 'mySpace' 122 String credentialsId = 'credentialsId' 123 String appName = 'myAppName' 124 boolean verbose = true 125 126 jenkinsCredentialsRule.withCredentials(credentialsId, 'myuser', 'topsecret') 127 128 helper.registerAllowedMethod('httpRequest', [Map.class] , { 129 return [content: '{ "authorization_endpoint": "myAuthEndPoint", "access_token": "myAccessToken", "resources":[{"guid":"myGuid", "links":{"self":{"href":"myAppUrl"}}}] }', status: 200] 130 }) 131 132 def cf = new CloudFoundry(nullScript) 133 def appEnv = cf.getAppEnvironment(apiEndpoint, org, space, credentialsId, appName, verbose) 134 135 assertThat(appEnv.toString(), is('{authorization_endpoint=myAuthEndPoint, access_token=myAccessToken, resources=[{guid=myGuid, links={self={href=myAppUrl}}}]}')) 136 } 137 138 @Test 139 void getXsuaaCredentials_test(){ 140 String apiEndpoint = 'http://dummy.sap.com' 141 String org = 'myOrg' 142 String space = 'mySpace' 143 String credentialsId = 'credentialsId' 144 String appName = 'myAppName' 145 boolean verbose = true 146 147 jenkinsCredentialsRule.withCredentials(credentialsId, 'myuser', 'topsecret') 148 149 helper.registerAllowedMethod('httpRequest', [Map.class] , { 150 return [content: '{ "system_env_json":{"VCAP_SERVICES":{"xsuaa":[{"credentials":"myCredentials"}]}}, "authorization_endpoint": "myAuthEndPoint", "access_token": "myAccessToken", "resources":[{"guid":"myGuid", "links":{"self":{"href":"myAppUrl"}}}] }', status: 200] 151 }) 152 153 def cf = new CloudFoundry(nullScript) 154 def xsuaaCred = cf.getXsuaaCredentials(apiEndpoint, org, space, credentialsId, appName, verbose) 155 156 assertThat(xsuaaCred.toString(), is('myCredentials')) 157 } 158 }