-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for SecureStoreService
- Loading branch information
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...e Demo/Groovy XML Transformation/src/main/resources/script/SetAuthorizationPayload.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import com.sap.gateway.ip.core.customdev.util.Message | ||
import com.sap.it.api.ITApiFactory | ||
import com.sap.it.api.securestore.SecureStoreService | ||
import com.sap.it.api.securestore.UserCredential | ||
import groovy.json.JsonOutput | ||
|
||
Message processData(Message message) { | ||
def clientCredentialsSecureMaterialName = message.getProperty('ClientCredentialsSecureMaterialName') | ||
def secureStorageService = ITApiFactory.getService(SecureStoreService, null) | ||
UserCredential userCredential = secureStorageService.getUserCredential(clientCredentialsSecureMaterialName) | ||
|
||
def clientId = userCredential.getUsername() | ||
def clientSecret = userCredential.getPassword().toString() | ||
|
||
Map output = ['clientId': clientId, 'clientSecret': clientSecret] | ||
|
||
message.setBody(JsonOutput.toJson(output)) | ||
message.setHeader('Content-Type', 'application/json') | ||
return message | ||
} |
54 changes: 54 additions & 0 deletions
54
FlashPipe Demo/Groovy XML Transformation/src/test/groovy/SetAuthorizationPayloadSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import com.sap.gateway.ip.core.customdev.util.Message | ||
import com.sap.it.api.securestore.SecureStoreService | ||
import com.sap.it.api.securestore.UserCredential | ||
import com.sap.it.api.securestore.exception.SecureStoreException | ||
import groovy.json.JsonSlurper | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class SetAuthorizationPayloadSpec extends Specification { | ||
@Shared | ||
Script script | ||
|
||
Message msg | ||
|
||
def setupSpec() { | ||
GroovyShell shell = new GroovyShell() | ||
script = shell.parse(this.getClass().getResource('/script/SetAuthorizationPayload.groovy').toURI()) | ||
} | ||
|
||
def setup() { | ||
msg = new Message() | ||
} | ||
|
||
def 'Scenario 1 - Credential exists'() { | ||
given: | ||
msg.setProperty('ClientCredentialsSecureMaterialName', 'Sample-Client-Credentials') | ||
SecureStoreService secureStoreService = SecureStoreService.getInstance() | ||
secureStoreService.addCredential('Sample-Client-Credentials', new UserCredential('dummyId', 'dummySecret')) | ||
|
||
when: | ||
'Script is executed' | ||
script.processData(msg) | ||
|
||
then: | ||
def root = new JsonSlurper().parse(msg.getBody(Reader)) | ||
verifyAll { | ||
root.clientId == 'dummyId' | ||
root.clientSecret == 'dummySecret' | ||
} | ||
} | ||
|
||
def 'Scenario 2 - Credential missing'() { | ||
given: | ||
msg.setProperty('ClientCredentialsSecureMaterialName', 'Missing-Client-Credentials') | ||
|
||
when: | ||
'Script is executed' | ||
script.processData(msg) | ||
|
||
then: | ||
def err = thrown(SecureStoreException) | ||
err.message == 'Could not fetch the credential for alias Missing-Client-Credentials' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters