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

TMS: implement response processing based on its HTTP response code for authentication and uploadFileToNode (#1335)

* Expose error message if status is not 200, extend junit tests

* Address feedback

Co-authored-by: Marcus Holl <marcus.holl@sap.com>
This commit is contained in:
artembannikov 2020-04-06 11:29:18 +02:00 committed by GitHub
parent 4b93dc5db7
commit 26feb014df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 26 deletions

View File

@ -51,15 +51,12 @@ class TransportManagementService implements Serializable {
}
def response = sendApiRequest(parameters)
if (config.verbose) {
echo("Received response with status ${response.status} from authentication request.")
if (response.status != 200) {
prepareAndThrowException(response, "OAuth Token retrieval failed (HTTP status code '${response.status}').")
}
echo("OAuth Token retrieved successfully.")
return jsonUtils.jsonStringToGroovyObject(response.content).access_token
}
@ -147,15 +144,16 @@ class TransportManagementService implements Serializable {
}
def response = sendApiRequest(parameters)
if (config.verbose) {
echo("Received response '${response.content}' with status ${response.status}.")
if (response.status != 200) {
prepareAndThrowException(response, "Node upload failed (HTTP status code '${response.status}').")
}
echo("Node upload successful.")
def successMessage = "Node upload successful."
if (config.verbose) {
successMessage += " Response content '${response.content}'."
}
echo(successMessage)
return jsonUtils.jsonStringToGroovyObject(response.content)
}
private sendApiRequest(parameters) {
@ -164,12 +162,19 @@ class TransportManagementService implements Serializable {
quiet : !config.verbose,
consoleLogResponseBody: false, // must be false, otherwise this reveals the api-token in the auth-request
ignoreSslErrors : true,
validResponseCodes : "100:399"
validResponseCodes : "100:599"
]
return script.httpRequest(defaultParameters + parameters)
}
private prepareAndThrowException(response, errorMessage) {
if (response.status >= 400) {
errorMessage += " Response content '${response.content}'."
}
script.error "[${getClass().getSimpleName()}] ${errorMessage}"
}
private echo(message) {
script.echo "[${getClass().getSimpleName()}] ${message}"
}

View File

@ -34,7 +34,7 @@ class TransportManagementServiceTest extends BasePiperTest {
Map requestParams
helper.registerAllowedMethod('httpRequest', [Map.class], { m ->
requestParams = m
return [content: '{ "access_token": "myOAuthToken" }']
return [content: '{ "access_token": "myOAuthToken" }', status: 200]
})
def uaaUrl = 'http://dummy.com/oauth'
@ -54,9 +54,7 @@ class TransportManagementServiceTest extends BasePiperTest {
@Test
void retrieveOAuthToken__inVerboseMode__yieldsMoreEchos() {
Map requestParams
helper.registerAllowedMethod('httpRequest', [Map.class], { m ->
requestParams = m
helper.registerAllowedMethod('httpRequest', [Map.class], {
return [content: '{ "access_token": "myOAuthToken" }', status: 200]
})
@ -65,11 +63,52 @@ class TransportManagementServiceTest extends BasePiperTest {
def clientSecret = 'mySecret'
def tms = new TransportManagementService(nullScript, [verbose: true])
tms.authentication(uaaUrl, clientId, clientSecret)
def token = tms.authentication(uaaUrl, clientId, clientSecret)
assertThat(loggingRule.log, containsString("[TransportManagementService] OAuth Token retrieval started."))
assertThat(loggingRule.log, containsString("[TransportManagementService] UAA-URL: '${uaaUrl}', ClientId: '${clientId}'"))
assertThat(loggingRule.log, containsString("Received response with status 200 from authentication request."))
assertThat(loggingRule.log, containsString("[TransportManagementService] OAuth Token retrieved successfully."))
assertThat(token, is('myOAuthToken'))
}
@Test
void retrieveOAuthToken__failure() {
def uaaUrl = 'http://dummy.com/oauth'
def clientId = 'myId'
def clientSecret = 'mySecret'
def responseStatusCode = 400
def responseContent = 'Here an error message is expected (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN NON-VERBOSE MODE)'
thrown.expect(AbortException)
thrown.expectMessage("[TransportManagementService] OAuth Token retrieval failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.")
loggingRule.expect("[TransportManagementService] OAuth Token retrieval started.")
helper.registerAllowedMethod('httpRequest', [Map.class], {
return [content: responseContent, status: responseStatusCode]
})
def tms = new TransportManagementService(nullScript, [verbose: false])
tms.authentication(uaaUrl, clientId, clientSecret)
}
@Test
void retrieveOAuthToken__failure__status__400__inVerboseMode() {
def uaaUrl = 'http://dummy.com/oauth'
def clientId = 'myId'
def clientSecret = 'mySecret'
def responseStatusCode = 400
def responseContent = 'Here an error message is expected (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN VERBOSE MODE)'
thrown.expect(AbortException)
thrown.expectMessage("[TransportManagementService] OAuth Token retrieval failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.")
loggingRule.expect("[TransportManagementService] OAuth Token retrieval started.")
loggingRule.expect("[TransportManagementService] UAA-URL: '${uaaUrl}', ClientId: '${clientId}'")
helper.registerAllowedMethod('httpRequest', [Map.class], {
return [content: responseContent, status: responseStatusCode]
})
def tms = new TransportManagementService(nullScript, [verbose: true])
tms.authentication(uaaUrl, clientId, clientSecret)
}
@Test
@ -172,7 +211,7 @@ class TransportManagementServiceTest extends BasePiperTest {
Map requestParams
helper.registerAllowedMethod('httpRequest', [Map.class], { m ->
requestParams = m
return [content: '{ "upload": "success" }']
return [content: '{ "upload": "success" }', status: 200]
})
def url = 'http://dummy.com/oauth'
@ -197,18 +236,17 @@ class TransportManagementServiceTest extends BasePiperTest {
@Test
void uploadFileToNode__inVerboseMode__yieldsMoreEchos() {
Map requestParams
helper.registerAllowedMethod('httpRequest', [Map.class], { m ->
requestParams = m
return [content: '{ "upload": "success" }']
})
def url = 'http://dummy.com/oauth'
def token = 'myToken'
def nodeName = 'myNode'
def fileId = 1234
def description = "My description."
def namedUser = 'myUser'
def responseContent = '{ "upload": "success" }'
helper.registerAllowedMethod('httpRequest', [Map.class], {
return [content: responseContent, status: 200]
})
def tms = new TransportManagementService(nullScript, [verbose: true])
tms.uploadFileToNode(url, token, nodeName, fileId, description, namedUser)
@ -216,7 +254,53 @@ class TransportManagementServiceTest extends BasePiperTest {
assertThat(loggingRule.log, containsString("[TransportManagementService] Node upload started."))
assertThat(loggingRule.log, containsString("[TransportManagementService] URL: '${url}', NodeName: '${nodeName}', FileId: '${fileId}'"))
assertThat(loggingRule.log, containsString("\"upload\": \"success\""))
assertThat(loggingRule.log, containsString("[TransportManagementService] Node upload successful."))
assertThat(loggingRule.log, containsString("[TransportManagementService] Node upload successful. Response content '${responseContent}'."))
}
@Test
void uploadFileToNode__failure() {
def url = 'http://dummy.com/oauth'
def token = 'myToken'
def nodeName = 'myNode'
def fileId = 1234
def description = "My description."
def namedUser = 'myUser'
def responseStatusCode = 400
def responseContent = '{ "errorType": "TsInternalServerErrorException", "message": "The application has encountered an unexpected error (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN NON-VERBOSE MODE)." }'
thrown.expect(AbortException)
thrown.expectMessage("[TransportManagementService] Node upload failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.")
loggingRule.expect("[TransportManagementService] Node upload started.")
helper.registerAllowedMethod('httpRequest', [Map.class], {
return [content: responseContent, status: responseStatusCode]
})
def tms = new TransportManagementService(nullScript, [verbose: false])
tms.uploadFileToNode(url, token, nodeName, fileId, description, namedUser)
}
@Test
void uploadFileToNode__failure__status__400__inVerboseMode() {
def url = 'http://dummy.com/oauth'
def token = 'myToken'
def nodeName = 'myNode'
def fileId = 1234
def description = "My description."
def namedUser = 'myUser'
def responseStatusCode = 400
def responseContent = '{ "errorType": "TsInternalServerErrorException", "message": "The application has encountered an unexpected error (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN VERBOSE MODE)." }'
thrown.expect(AbortException)
thrown.expectMessage("[TransportManagementService] Node upload failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.")
loggingRule.expect("[TransportManagementService] Node upload started.")
loggingRule.expect("[TransportManagementService] URL: '${url}', NodeName: '${nodeName}', FileId: '${fileId}'")
helper.registerAllowedMethod('httpRequest', [Map.class], {
return [content: responseContent, status: responseStatusCode]
})
def tms = new TransportManagementService(nullScript, [verbose: true])
tms.uploadFileToNode(url, token, nodeName, fileId, description, namedUser)
}
}