• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperledger / identus-cloud-agent / 10807284826

11 Sep 2024 07:46AM CUT coverage: 48.663% (+0.1%) from 48.554%
10807284826

Pull #1332

patlo-iog
test: make test work again

Signed-off-by: Pat Losoponkul <pat.losoponkul@iohk.io>
Pull Request #1332: feat: presentation_submission validation logic [WIP]

92 of 134 new or added lines in 5 files covered. (68.66%)

248 existing lines in 65 files now uncovered.

7478 of 15367 relevant lines covered (48.66%)

0.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.26
/shared/json/src/main/scala/org/hyperledger/identus/shared/json/JsonPath.scala
1
package org.hyperledger.identus.shared.json
2

3
import com.jayway.jsonpath.{InvalidPathException, JsonPath as JaywayJsonPath, PathNotFoundException}
4
import com.jayway.jsonpath.spi.json.JacksonJsonProvider
5
import org.hyperledger.identus.shared.models.{Failure, StatusCode}
6
import zio.*
7
import zio.json.*
8
import zio.json.ast.Json
9

10
import scala.util.Try
11

12
sealed trait JsonPathError extends Failure {
13
  override def namespace: String = "JsonPathError"
×
14
}
15

16
object JsonPathError {
17
  final case class InvalidPathInput(msg: String) extends JsonPathError {
18
    override def statusCode: StatusCode = StatusCode.BadRequest
×
19
    override def userFacingMessage: String = s"The JsonPath input is not valid: $msg"
×
20
  }
21

22
  final case class PathNotFound(path: String) extends JsonPathError {
23
    override def statusCode: StatusCode = StatusCode.BadRequest
×
24
    override def userFacingMessage: String = s"The json path '$path' cannot be found in a json"
×
25
  }
26

27
  final case class UnexpectedCompilePathError(path: String, e: Throwable) extends JsonPathError {
NEW
28
    override def statusCode: StatusCode = StatusCode.InternalServerError
×
NEW
29
    override def userFacingMessage: String = s"An unhandled error occurred while compiling the JsonPath for $path"
×
30
  }
31

32
  final case class UnexpectedReadPathError(path: String, e: Throwable) extends JsonPathError {
NEW
33
    override def statusCode: StatusCode = StatusCode.InternalServerError
×
NEW
34
    override def userFacingMessage: String = s"An unhandled error occurred while reading the JsonPath for $path"
×
35
  }
36
}
37

38
opaque type JsonPath = JaywayJsonPath
39

40
object JsonPath {
41
  def compileUnsafe(path: String): JsonPath = JaywayJsonPath.compile(path)
1✔
42

43
  def compile(path: String): Either[JsonPathError, JsonPath] =
1✔
44
    Try(compileUnsafe(path)).toEither.left
1✔
45
      .map {
1✔
46
        case e: IllegalArgumentException => JsonPathError.InvalidPathInput(e.getMessage())
1✔
47
        case e: InvalidPathException     => JsonPathError.InvalidPathInput(e.getMessage())
1✔
NEW
48
        case e                           => JsonPathError.UnexpectedCompilePathError(path, e)
×
49
      }
50

51
  extension (jsonPath: JsonPath) {
52
    def read(json: Json): Either[JsonPathError, Json] = {
1✔
53
      val jsonProvider = JacksonJsonProvider()
1✔
54
      val document = JaywayJsonPath.parse(json.toString())
1✔
55
      for {
1✔
56
        queriedObj <- Try(document.read[java.lang.Object](jsonPath)).toEither.left.map {
1✔
57
          case e: PathNotFoundException => JsonPathError.PathNotFound(jsonPath.getPath())
1✔
NEW
58
          case e                        => JsonPathError.UnexpectedReadPathError(jsonPath.getPath(), e)
×
59
        }
60
        queriedJsonStr = jsonProvider.toJson(queriedObj)
1✔
61
        queriedJson <- queriedJsonStr
1✔
62
          .fromJson[Json]
63
          .left
1✔
64
          .map(e => JsonPathError.UnexpectedReadPathError(jsonPath.getPath(), Exception(e)))
1✔
65
      } yield queriedJson
66
    }
67
  }
68
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc