Skip to content

Commit

Permalink
Niganesh/align with error spec 1 (#47)
Browse files Browse the repository at this point in the history
Co-authored-by: Nithya Ganesh <[email protected]>
  • Loading branch information
nithyaganeshng and Nithya Ganesh authored Jun 30, 2023
1 parent 5978f33 commit 904aef3
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import com.microsoft.walletlibrary.requests.RequestHandlerFactory
import com.microsoft.walletlibrary.requests.RequestResolverFactory
import com.microsoft.walletlibrary.requests.VerifiedIdRequest
import com.microsoft.walletlibrary.requests.input.VerifiedIdRequestInput
import com.microsoft.walletlibrary.util.UnspecifiedVerifiedIdException
import com.microsoft.walletlibrary.util.VerifiedIdExceptions
import com.microsoft.walletlibrary.util.VerifiedIdResult
import com.microsoft.walletlibrary.util.WalletLibraryLogger
import com.microsoft.walletlibrary.verifiedid.VerifiedId
import kotlinx.serialization.decodeFromString
Expand All @@ -33,25 +36,25 @@ class VerifiedIdClient(
val requestResolver = requestResolverFactory.getResolver(verifiedIdRequestInput)
val rawRequest = requestResolver.resolve(verifiedIdRequestInput)
val requestHandler = requestHandlerFactory.getHandler(requestResolver)
Result.success(requestHandler.handleRequest(rawRequest))
VerifiedIdResult.success(requestHandler.handleRequest(rawRequest))
} catch (exception: Exception) {
Result.failure(exception)
UnspecifiedVerifiedIdException("Unspecified Exception", VerifiedIdExceptions.UNSPECIFIED_EXCEPTION.value, exception).toVerifiedIdResult()
}
}

fun encode(verifiedId: VerifiedId): Result<String> {
fun encode(verifiedId: VerifiedId): VerifiedIdResult<String> {
return try {
Result.success(serializer.encodeToString(verifiedId))
VerifiedIdResult.success(serializer.encodeToString(verifiedId))
} catch (exception: Exception) {
Result.failure(exception)
UnspecifiedVerifiedIdException("Unspecified Exception", VerifiedIdExceptions.UNSPECIFIED_EXCEPTION.value, exception).toVerifiedIdResult()
}
}

fun decodeVerifiedId(encodedVerifiedId: String): Result<VerifiedId> {
return try {
Result.success(serializer.decodeFromString(encodedVerifiedId))
VerifiedIdResult.success(serializer.decodeFromString(encodedVerifiedId))
} catch (exception: Exception) {
Result.failure(exception)
UnspecifiedVerifiedIdException("Unspecified Exception", VerifiedIdExceptions.UNSPECIFIED_EXCEPTION.value, exception).toVerifiedIdResult()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

package com.microsoft.walletlibrary.requests.requirements

import com.microsoft.walletlibrary.util.PinRequirementNotFulfilledException
import com.microsoft.walletlibrary.util.RequirementNotMetException
import com.microsoft.walletlibrary.util.VerifiedIdExceptions
import com.microsoft.walletlibrary.util.VerifiedIdResult

/**
* Represents information that describes pin required in order to complete a VerifiedID request.
Expand All @@ -25,10 +27,10 @@ class PinRequirement(
internal var pin: String? = null
): Requirement {
// Validates the requirement and throws an exception if the requirement is invalid or not fulfilled.
override fun validate(): Result<Unit> {
override fun validate(): VerifiedIdResult<Unit> {
if (pin == null)
return Result.failure(PinRequirementNotFulfilledException("PinRequirement has not been fulfilled."))
return Result.success(Unit)
return RequirementNotMetException("Pin has not been set.", VerifiedIdExceptions.REQUIREMENT_NOT_MET_EXCEPTION.value).toVerifiedIdResult()
return VerifiedIdResult.success(Unit)
}

// Fulfills the requirement in the request with specified value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,4 @@ class VerifiedIdRequirementMissingIdException(message: String = "", cause: Throw

class VerifiedIdRequirementIdConflictException(message: String = "", cause: Throwable? = null, retryable: Boolean = false): RequirementValidationException(message, cause, retryable)

class IdInVerifiedIdRequirementDoesNotMatchRequestException(message: String = "", cause: Throwable? = null, retryable: Boolean = false): RequirementValidationException(message, cause, retryable)

class PinRequirementNotFulfilledException(message: String = "", cause: Throwable? = null, retryable: Boolean = false): RequirementValidationException(message, cause, retryable)
class IdInVerifiedIdRequirementDoesNotMatchRequestException(message: String = "", cause: Throwable? = null, retryable: Boolean = false): RequirementValidationException(message, cause, retryable)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.microsoft.walletlibrary.util

sealed class VerifiedIdException(message: String, val code: String, val correlationId: String? = null): Exception(message) {
fun <T>toVerifiedIdResult(): VerifiedIdResult<T> {
return VerifiedIdResult.failure(this)
}
}
class NetworkingException(
message: String,
code: String,
correlationId: String? = null,
statusCode: String? = null,
innerError: Exception? = null,
retryable: Boolean = false
) : VerifiedIdException(message, code, correlationId)

class RequirementNotMetException(message: String, code: String, correlationId: String? = null) :
VerifiedIdException(message, code, correlationId)

class UnspecifiedVerifiedIdException(message: String, code: String, val innerError: Exception? = null, correlationId: String? = null) :
VerifiedIdException(message, code, correlationId)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.microsoft.walletlibrary.util

enum class VerifiedIdExceptions(val value: String) {
NETWORKING_EXCEPTION("networking_error"),
REQUIREMENT_NOT_MET_EXCEPTION("requirement_not_met"),
UNSPECIFIED_EXCEPTION("unspecified_error")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.microsoft.walletlibrary.util

typealias VerifiedIdResult<T> = Result<T>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import com.microsoft.walletlibrary.util.HandlerMissingException
import com.microsoft.walletlibrary.util.ResolverMissingException
import com.microsoft.walletlibrary.util.UnSupportedProtocolException
import com.microsoft.walletlibrary.util.UnSupportedVerifiedIdRequestInputException
import com.microsoft.walletlibrary.util.UnspecifiedVerifiedIdException
import com.microsoft.walletlibrary.util.VerifiedIdResult
import com.microsoft.walletlibrary.util.WalletLibraryLogger
import com.microsoft.walletlibrary.util.defaultTestSerializer
import com.microsoft.walletlibrary.verifiedid.VerifiableCredential
Expand Down Expand Up @@ -79,7 +81,7 @@ class VerifiedIdClientTest {
val verifiedIdRequest = verifiedIdClient.createRequest(verifiedIdRequestURL)

// Assert
assertThat(verifiedIdRequest).isInstanceOf(Result::class.java)
assertThat(verifiedIdRequest).isInstanceOf(VerifiedIdResult::class.java)
assertThat(verifiedIdRequest.isSuccess).isTrue
assertThat(verifiedIdRequest.getOrNull()).isNotNull
assertThat(verifiedIdRequest.getOrNull()).isInstanceOf(VerifiedIdPresentationRequest::class.java)
Expand All @@ -105,12 +107,17 @@ class VerifiedIdClientTest {
runBlocking {
// Act
val actualResult = verifiedIdClient.createRequest(verifiedIdRequestURL)
val actualException = actualResult.exceptionOrNull()

// Assert
assertThat(actualResult).isInstanceOf(Result::class.java)
assertThat(actualResult).isInstanceOf(VerifiedIdResult::class.java)
assertThat(actualResult.isFailure).isTrue
assertThat(actualResult.exceptionOrNull()).isNotNull
assertThat(actualResult.exceptionOrNull()).isInstanceOf(ResolverMissingException::class.java)
assertThat(actualException).isNotNull
assertThat(actualException).isInstanceOf(UnspecifiedVerifiedIdException::class.java)
assertThat((actualException as UnspecifiedVerifiedIdException).code).isEqualTo("unspecified_error")
assertThat(actualException).hasMessage("Unspecified Exception")
assertThat(actualException.correlationId).isNull()
assertThat(actualException.innerError).isInstanceOf(ResolverMissingException::class.java)
}
}

Expand All @@ -134,12 +141,17 @@ class VerifiedIdClientTest {
runBlocking {
// Act
val actualResult = verifiedIdClient.createRequest(verifiedIdRequestURL)
val actualException = actualResult.exceptionOrNull()

// Assert
assertThat(actualResult).isInstanceOf(Result::class.java)
assertThat(actualResult).isInstanceOf(VerifiedIdResult::class.java)
assertThat(actualResult.isFailure).isTrue
assertThat(actualResult.exceptionOrNull()).isNotNull
assertThat(actualResult.exceptionOrNull()).isInstanceOf(HandlerMissingException::class.java)
assertThat(actualException).isNotNull
assertThat(actualException).isInstanceOf(UnspecifiedVerifiedIdException::class.java)
assertThat((actualException as UnspecifiedVerifiedIdException).code).isEqualTo("unspecified_error")
assertThat(actualException).hasMessage("Unspecified Exception")
assertThat(actualException.correlationId).isNull()
assertThat(actualException.innerError).isInstanceOf(HandlerMissingException::class.java)
}
}

Expand All @@ -166,12 +178,17 @@ class VerifiedIdClientTest {
runBlocking {
// Act
val actualResult = verifiedIdClient.createRequest(verifiedIdRequestURL)
val actualException = actualResult.exceptionOrNull()

// Assert
assertThat(actualResult).isInstanceOf(Result::class.java)
assertThat(actualResult).isInstanceOf(VerifiedIdResult::class.java)
assertThat(actualResult.isFailure).isTrue
assertThat(actualResult.exceptionOrNull()).isNotNull
assertThat(actualResult.exceptionOrNull()).isInstanceOf(UnSupportedProtocolException::class.java)
assertThat(actualException).isNotNull
assertThat(actualException).isInstanceOf(UnspecifiedVerifiedIdException::class.java)
assertThat((actualException as UnspecifiedVerifiedIdException).code).isEqualTo("unspecified_error")
assertThat(actualException).hasMessage("Unspecified Exception")
assertThat(actualException.correlationId).isNull()
assertThat(actualException.innerError).isInstanceOf(UnSupportedProtocolException::class.java)
}
}

Expand All @@ -198,12 +215,17 @@ class VerifiedIdClientTest {
runBlocking {
// Act
val actualResult = verifiedIdClient.createRequest(verifiedIdRequestURL)
val actualException = actualResult.exceptionOrNull()

// Assert
assertThat(actualResult).isInstanceOf(Result::class.java)
assertThat(actualResult).isInstanceOf(VerifiedIdResult::class.java)
assertThat(actualResult.isFailure).isTrue
assertThat(actualResult.exceptionOrNull()).isNotNull
assertThat(actualResult.exceptionOrNull()).isInstanceOf(UnSupportedVerifiedIdRequestInputException::class.java)
assertThat(actualException).isNotNull
assertThat(actualException).isInstanceOf(UnspecifiedVerifiedIdException::class.java)
assertThat((actualException as UnspecifiedVerifiedIdException).code).isEqualTo("unspecified_error")
assertThat(actualException).hasMessage("Unspecified Exception")
assertThat(actualException.correlationId).isNull()
assertThat(actualException.innerError).isInstanceOf(UnSupportedVerifiedIdRequestInputException::class.java)
}
}

Expand Down Expand Up @@ -249,7 +271,7 @@ class VerifiedIdClientTest {
val actualEncodedVc = verifiedIdClient.encode(vc)

// Assert
assertThat(actualEncodedVc).isInstanceOf(Result::class.java)
assertThat(actualEncodedVc).isInstanceOf(VerifiedIdResult::class.java)
assertThat(actualEncodedVc.isSuccess).isTrue
assertThat(actualEncodedVc.getOrNull()).isNotNull
assertThat(actualEncodedVc.getOrNull()).isEqualTo(expectedEncoding)
Expand Down Expand Up @@ -295,19 +317,20 @@ class VerifiedIdClientTest {

// Act
val actualDecodedVc = verifiedIdClient.decodeVerifiedId(encodedVc)
val actualVc = actualDecodedVc.getOrNull()

// Assert
assertThat(actualDecodedVc).isInstanceOf(Result::class.java)
assertThat(actualDecodedVc).isInstanceOf(VerifiedIdResult::class.java)
assertThat(actualDecodedVc.isSuccess).isTrue
assertThat(actualDecodedVc.getOrNull()).isNotNull
assertThat(actualDecodedVc.getOrNull()).isInstanceOf(VerifiableCredential::class.java)
assertThat((actualDecodedVc.getOrNull() as VerifiableCredential).getClaims().size).isEqualTo(1)
assertThat((actualDecodedVc.getOrNull() as VerifiableCredential).getClaims().first().id).isEqualTo("name 1")
assertThat((actualDecodedVc.getOrNull() as VerifiableCredential).getClaims().first().value).isEqualTo("\"value1\"")
assertThat((actualDecodedVc.getOrNull() as VerifiableCredential).style).isInstanceOf(BasicVerifiedIdStyle::class.java)
assertThat((actualDecodedVc.getOrNull() as VerifiableCredential).style.name).isEqualTo("Test VC")
assertThat(((actualDecodedVc.getOrNull() as VerifiableCredential).style as BasicVerifiedIdStyle).backgroundColor).isEqualTo("#000000")
assertThat(((actualDecodedVc.getOrNull() as VerifiableCredential).style as BasicVerifiedIdStyle).textColor).isEqualTo("#ffffff")
assertThat(((actualDecodedVc.getOrNull() as VerifiableCredential).style as BasicVerifiedIdStyle).issuer).isEqualTo("Test Issuer")
assertThat(actualVc).isNotNull
assertThat(actualVc).isInstanceOf(VerifiableCredential::class.java)
assertThat((actualVc as VerifiableCredential).getClaims().size).isEqualTo(1)
assertThat(actualVc.getClaims().first().id).isEqualTo("name 1")
assertThat(actualVc.getClaims().first().value).isEqualTo("\"value1\"")
assertThat(actualVc.style).isInstanceOf(BasicVerifiedIdStyle::class.java)
assertThat(actualVc.style.name).isEqualTo("Test VC")
assertThat((actualVc.style as BasicVerifiedIdStyle).backgroundColor).isEqualTo("#000000")
assertThat((actualVc.style as BasicVerifiedIdStyle).textColor).isEqualTo("#ffffff")
assertThat((actualVc.style as BasicVerifiedIdStyle).issuer).isEqualTo("Test Issuer")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import com.microsoft.did.sdk.credential.service.IssuanceRequest
import com.microsoft.did.sdk.credential.service.IssuanceResponse
import com.microsoft.did.sdk.credential.service.models.contracts.InputContract
import com.microsoft.did.sdk.credential.service.models.contracts.VerifiableCredentialContract
import com.microsoft.walletlibrary.requests.requirements.*
import com.microsoft.walletlibrary.requests.requirements.AccessTokenRequirement
import com.microsoft.walletlibrary.requests.requirements.GroupRequirement
import com.microsoft.walletlibrary.requests.requirements.GroupRequirementOperator
import com.microsoft.walletlibrary.requests.requirements.IdTokenRequirement
import com.microsoft.walletlibrary.requests.requirements.PinRequirement
import com.microsoft.walletlibrary.requests.requirements.RequestedClaim
import com.microsoft.walletlibrary.requests.requirements.SelfAttestedClaimRequirement
import com.microsoft.walletlibrary.util.IdTokenRequirementNotFulfilledException
import com.microsoft.walletlibrary.util.PinRequirementNotFulfilledException
import com.microsoft.walletlibrary.util.RequirementNotMetException
import com.microsoft.walletlibrary.util.SelfAttestedClaimRequirementNotFulfilledException
import io.mockk.every
import io.mockk.mockk
Expand Down Expand Up @@ -190,7 +196,7 @@ class IssuanceResponseMappingTest {
// Act and Assert
assertThatThrownBy{
issuanceResponse.addRequirements(pinRequirement)
}.isInstanceOf(PinRequirementNotFulfilledException::class.java)
}.isInstanceOf(RequirementNotMetException::class.java)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.microsoft.walletlibrary.requests.requirements

import com.microsoft.walletlibrary.util.PinRequirementNotFulfilledException
import com.microsoft.walletlibrary.util.RequirementNotMetException
import com.microsoft.walletlibrary.util.VerifiedIdResult
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

Expand Down Expand Up @@ -38,10 +39,13 @@ class PinRequirementTest {
val actualResult = pinRequirement.validate()

// Assert
assertThat(actualResult).isInstanceOf(Result::class.java)
assertThat(actualResult).isInstanceOf(VerifiedIdResult::class.java)
assertThat(actualResult.isFailure).isTrue
assertThat(actualResult.exceptionOrNull()).isNotNull
assertThat(actualResult.exceptionOrNull()).isInstanceOf(PinRequirementNotFulfilledException::class.java)
assertThat(actualResult.exceptionOrNull()).isInstanceOf(RequirementNotMetException::class.java)
assertThat((actualResult.exceptionOrNull() as RequirementNotMetException).code).isEqualTo("requirement_not_met")
assertThat(actualResult.exceptionOrNull() as RequirementNotMetException).hasMessage("Pin has not been set.")
assertThat((actualResult.exceptionOrNull() as RequirementNotMetException).correlationId).isNull()
}

@Test
Expand All @@ -54,7 +58,7 @@ class PinRequirementTest {
val actualResult = pinRequirement.validate()

// Assert
assertThat(actualResult).isInstanceOf(Result::class.java)
assertThat(actualResult).isInstanceOf(VerifiedIdResult::class.java)
assertThat(actualResult.isSuccess).isTrue
assertThat(pinRequirement.pin).isNotNull
assertThat(pinRequirement.pin).isEqualTo(expectedPin)
Expand Down
2 changes: 1 addition & 1 deletion walletlibrarydemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ dependencies {
implementation "androidx.room:room-runtime:$androidx_room_persistence_version"
implementation "androidx.room:room-ktx:$androidx_room_persistence_version"
kapt "androidx.room:room-compiler:$androidx_room_persistence_version"
implementation "com.microsoft.entra.verifiedid:walletlibrary:0.0.1-beta.3"
implementation "com.microsoft.entra.verifiedid:walletlibrary:1.0.0"
testImplementation "junit:junit:$junit_version"
androidTestImplementation "androidx.test.ext:junit:$androidx_test_junit_version"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidx_test_espresso_core_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.RecyclerView
import com.microsoft.walletlibrary.requests.requirements.*
import com.microsoft.walletlibrary.requests.requirements.IdTokenRequirement
import com.microsoft.walletlibrary.requests.requirements.PinRequirement
import com.microsoft.walletlibrary.requests.requirements.Requirement
import com.microsoft.walletlibrary.requests.requirements.SelfAttestedClaimRequirement
import com.microsoft.walletlibrary.requests.requirements.VerifiedIdRequirement
import com.microsoft.walletlibrarydemo.R
import com.microsoft.walletlibrarydemo.databinding.RequirementTextRowBinding
import com.microsoft.walletlibrarydemo.databinding.RequirementVerifiedclaimRowBinding
Expand Down

0 comments on commit 904aef3

Please sign in to comment.