Skip to content

Commit

Permalink
Add unit test for SecureStoreService
Browse files Browse the repository at this point in the history
  • Loading branch information
engswee committed May 9, 2024
1 parent 59ad519 commit f4653ae
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
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
}
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'
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<dependency>
<groupId>com.equaliseit</groupId>
<artifactId>sap-cpi-mocks</artifactId>
<version>1.0.0</version>
<version>1.0.2</version>
</dependency>
</dependencies>

Expand Down

0 comments on commit f4653ae

Please sign in to comment.