1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-05 15:15:44 +02:00
sap-jenkins-library/test/groovy/com/sap/piper/integration/WhitesourceOrgAdminRepositoryTest.groovy

282 lines
9.6 KiB
Groovy
Raw Normal View History

2019-03-04 22:55:43 +01:00
package com.sap.piper.integration
2019-03-13 09:58:47 +01:00
import hudson.AbortException
2019-03-04 22:55:43 +01:00
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
2019-03-04 22:55:43 +01:00
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsEnvironmentRule
2019-03-13 09:58:47 +01:00
import util.JenkinsErrorRule
2019-03-04 22:55:43 +01:00
import util.JenkinsLoggingRule
import util.LibraryLoadingTestExecutionListener
import util.Rules
import static org.assertj.core.api.Assertions.assertThat
2019-03-13 09:58:47 +01:00
import static org.hamcrest.Matchers.containsString
2019-03-04 22:55:43 +01:00
import static org.hamcrest.Matchers.is
2019-03-13 09:58:47 +01:00
import static org.hamcrest.Matchers.isA
2019-03-04 22:55:43 +01:00
class WhitesourceOrgAdminRepositoryTest extends BasePiperTest {
private ExpectedException expectedException = ExpectedException.none()
2019-03-13 09:58:47 +01:00
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
2019-03-04 22:55:43 +01:00
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(expectedException)
2019-03-13 09:58:47 +01:00
.around(loggingRule)
2019-03-04 22:55:43 +01:00
WhitesourceOrgAdminRepository repository
@Before
void init() throws Exception {
repository = new WhitesourceOrgAdminRepository(nullScript, [whitesource: [serviceUrl: "http://some.host.whitesource.com/api/"], verbose: true])
2019-03-04 22:55:43 +01:00
LibraryLoadingTestExecutionListener.prepareObjectInterceptors(repository)
}
@After
void tearDown() {
printCallStack()
nullScript.env = [:]
}
2019-03-13 11:38:44 +01:00
@Test
void testMissingConfig() {
expectedException.expect(AbortException)
expectedException.expectMessage("Parameter 'whitesource.serviceUrl' must be provided as part of the configuration.")
new WhitesourceOrgAdminRepository(nullScript, [:])
2019-03-13 11:38:44 +01:00
}
@Test
void testAccessor() {
new WhitesourceOrgAdminRepository(nullScript, [whitesourceAccessor: "com.sap.piper.integration.WhitesourceRepository", whitesource: [serviceUrl: "http://test.com"]])
2019-03-13 11:38:44 +01:00
}
2019-03-04 22:55:43 +01:00
@Test
void testResolveProductMeta() {
def whitesourceMetaResponse = [
productVitals: [
[
token: '410389ae-0269-4719-9cbf-fb5e299c8415',
name : 'NW'
],
[
token: '2892f1db-4361-4e83-a89d-d28a262d65b9',
name : 'XS UAA'
],
[
token: '1111111-1111-1111-1111-111111111111',
name : 'Correct Name Cloud'
]
]
]
repository.config.putAll([whitesource: [productName: "Correct Name Cloud"]])
2019-03-04 22:55:43 +01:00
def result = repository.findProductMeta(whitesourceMetaResponse)
assertThat(result).isEqualTo([
token: '1111111-1111-1111-1111-111111111111',
name : 'Correct Name Cloud'
])
}
2019-03-06 14:16:28 +01:00
@Test
void testHttpWhitesourceInternalCallUserKey() {
2019-03-15 13:11:34 +01:00
def config = [whitesource: [ serviceUrl: "http://some.host.whitesource.com/api/", orgAdminUserKey: "4711"], verbose: false]
2019-03-11 10:46:22 +01:00
repository.config.putAll(config)
2019-03-06 14:16:28 +01:00
def requestBody = ["someJson" : [ "someObject" : "abcdef" ]]
def requestParams
helper.registerAllowedMethod('httpRequest', [Map], { p ->
requestParams = p
})
repository.httpWhitesource(requestBody)
assertThat(requestParams, is(
[
url : config.serviceUrl,
httpMode : 'POST',
acceptType : 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
requestBody: requestBody,
quiet : true,
userKey : config.orgAdminUserKey
]
))
}
2019-03-13 09:58:47 +01:00
@Test
void testHttpWhitesourceInternalCallUserKeyVerboseProxy() {
2019-03-15 13:11:34 +01:00
def config = [whitesource: [ serviceUrl: "http://some.host.whitesource.com/api/", orgAdminUserKey: "4711"], verbose: true]
2019-03-13 09:58:47 +01:00
nullScript.env['HTTP_PROXY'] = "http://test.sap.com:8080"
repository.config.putAll(config)
def requestBody = ["someJson" : [ "someObject" : "abcdef" ]]
def requestParams
helper.registerAllowedMethod('httpRequest', [Map], { p ->
requestParams = p
})
repository.httpWhitesource(requestBody)
assertThat(requestParams, is(
[
url : config.serviceUrl,
httpMode : 'POST',
acceptType : 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
requestBody: requestBody,
quiet : false,
userKey : config.orgAdminUserKey,
httpProxy : "http://test.sap.com:8080"
]
))
assertThat(loggingRule.log, containsString("Sending http request with parameters"))
assertThat(loggingRule.log, containsString("Received response"))
}
@Test
void testCreateProduct() {
def config = [
2019-03-15 13:11:34 +01:00
whitesource: [
serviceUrl: "http://some.host.whitesource.com/api/",
verbose: false,
orgAdminUserKey: "4711",
orgToken: "abcd1234",
productName: "testProduct",
emailAddressesOfInitialProductAdmins: ['some@somewhere.com', 'some2@somewhere.com']
]
2019-03-13 09:58:47 +01:00
]
repository.config.putAll(config)
def requestBody1 = [
requestType: "getOrganizationProductVitals",
orgToken: config.orgToken,
userKey: "4711"
]
def requestBody2 = [
"requestType" : "setProductAssignments",
"productToken" : "54785",
"productMembership" : ["userAssignments":[], "groupAssignments":[]],
"productAdmins" : ["userAssignments":[[ "email": "some@somewhere.com" ], ["email": "some2@somewhere.com"]]],
"alertsEmailReceivers" : ["userAssignments":[]],
"userKey": "4711"
]
def requestParams = []
helper.registerAllowedMethod('httpRequest', [Map], { p ->
requestParams.add(p)
return [ content : "{ \"productToken\" : \"54785\" }" ]
})
repository.createProduct()
assertThat(requestParams[0], is(
[
url : config.serviceUrl,
httpMode : 'POST',
acceptType : 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
requestBody: requestBody1,
quiet : false,
userKey : config.orgAdminUserKey,
httpProxy : "http://test.sap.com:8080"
]
))
assertThat(requestParams[1], is(
[
url : config.serviceUrl,
httpMode : 'POST',
acceptType : 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
requestBody: requestBody2,
quiet : false,
userKey : config.orgAdminUserKey,
httpProxy : "http://test.sap.com:8080"
]
))
}
@Test
void testIssueHttpRequestError() {
2019-03-15 13:11:34 +01:00
def config = [whitesource: [ serviceUrl: "http://some.host.whitesource.com/api/", orgAdminUserKey: "4711"], verbose: false]
2019-03-13 09:58:47 +01:00
repository.config.putAll(config)
def requestBody = ["someJson" : [ "someObject" : "abcdef" ]]
def requestParams
helper.registerAllowedMethod('httpRequest', [Map], { p ->
requestParams = p
2019-03-13 11:38:44 +01:00
return [content: "{ \"errorCode\" : \"4546\", \"errorMessage\" : \"some text\" } }"]
2019-03-13 09:58:47 +01:00
})
def errorCaught = false
try {
repository.issueHttpRequest(requestBody)
} catch (e) {
errorCaught = true
assertThat(e, isA(AbortException.class))
assertThat(e.getMessage(), equals("[WhiteSource] Request failed with error message 'some text' (4546)."))
}
assertThat(errorCaught, is(true))
assertThat(requestParams, is(
[
url : config.serviceUrl,
httpMode : 'POST',
acceptType : 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
requestBody: requestBody,
quiet : true,
userKey : config.orgAdminUserKey
]
))
}
@Test
void testFetchProductMetaInfo() {
2019-03-15 13:11:34 +01:00
def config = [whitesource: [ serviceUrl: "http://some.host.whitesource.com/api/", orgAdminUserKey: "4711", orgToken: "12345", productName: "testProduct"], verbose: true]
2019-03-13 09:58:47 +01:00
nullScript.env['HTTP_PROXY'] = "http://test.sap.com:8080"
repository.config.putAll(config)
def requestBody = [
requestType: "getOrganizationProductVitals",
orgToken: config.orgToken,
userKey: "4711"
]
def requestParams
helper.registerAllowedMethod('httpRequest', [Map], { p ->
requestParams = p
return [ content: "{ \"productVitals\" : [ { \"name\": \"testProduct\"} ] }"]
})
def result = repository.fetchProductMetaInfo()
assertThat(requestParams, is(
[
url : config.serviceUrl,
httpMode : 'POST',
acceptType : 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
requestBody: requestBody,
quiet : false,
userKey : config.orgAdminUserKey,
httpProxy : "http://test.sap.com:8080"
]
))
assertThat(result, is([ name: "testProduct"]))
assertThat(loggingRule.log, containsString("Sending http request with parameters"))
assertThat(loggingRule.log, containsString("Received response"))
}
2019-03-04 22:55:43 +01:00
}