<commit_before>package de.zalando.swagger

import java.io.File

import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserException
import org.scalatest.{FunSpec, MustMatchers}

class ParseVendorExtensionsTest extends FunSpec with MustMatchers {

  val ok = new File("compiler/src/test/resources/extensions/extensions.ok.yaml")
  val nok = new File("compiler/src/test/resources/extensions/extensions.nok.yaml")
  val hypermediaOk = new File("compiler/src/test/resources/extensions/hypermedia.ok.yaml")
  val hypermediaNOk1 = new File("compiler/src/test/resources/extensions/hypermedia.nok1.yaml")
  val hypermediaNOk2 = new File("compiler/src/test/resources/extensions/hypermedia.nok2.yaml")

  describe("The swagger parser") {
    it("should read valid vendor extensions") {
      implicit val (uri, swagger) = StrictYamlParser.parse(ok)
      swagger.info.vendorExtensions contains "x-info-extension" mustBe true
      swagger.paths("/").vendorExtensions contains "x-path-extension" mustBe true
      swagger.paths("/").get.vendorExtensions contains "x-operation-extension" mustBe true
      swagger.paths("/").get.responses("200").vendorExtensions contains "x-response-extension" mustBe true
      swagger.tags.head.vendorExtensions contains "x-tag-extension" mustBe true
      swagger.securityDefinitions("internalApiKey").vendorExtensions contains "x-security-extension" mustBe true
    }
    it("should reject invalid vendor extensions") {
      intercept[JsonMappingException] {
        StrictYamlParser.parse(nok)
      }.getClass mustBe classOf[JsonMappingException]
    }
    it("should read hypermedia definitions") {
      implicit val (uri, swagger) = StrictYamlParser.parse(hypermediaOk)
      val expected = Map("resource created" ->
        Map("resource updated" -> Map("condition" -> "some rule to show the transition"), "subresource added" -> null),
        "resource updated" -> Map("subresource added" -> Map("condition" -> ""),
          "self" -> Map("condition" -> "non-empty rule")), "resource deleted" -> Map("self" -> null),
        "subresource added" -> Map("resource updated" -> null, "self" -> null, "resource deleted" -> null))
      swagger.transitions.nonEmpty mustBe true
      swagger.transitions mustEqual expected
      swagger.paths("/").get.responses("200").targetState mustEqual Some("resource created")
      swagger.paths("/").get.responses("default").targetState mustEqual None
    }
    it("should reject hypermedia definitions without well-formed definition") {
      val exception = intercept[JsonMappingException] {
        StrictYamlParser.parse(hypermediaNOk1)
      }
      exception.getMessage mustEqual "Malformed transition definitions"
    }
    it("should reject hypermedia definitions with incorrect initial state") {
      intercept[ParserException] {
        StrictYamlParser.parse(hypermediaNOk2)
      }.getClass mustBe classOf[ParserException]
    }
  }
}<commit_msg>#140, refactor: Better error message for specification parsing errors<commit_after>package de.zalando.swagger

import java.io.File

import com.fasterxml.jackson.core.JsonParseException
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserException
import org.scalatest.{FunSpec, MustMatchers}

class ParseVendorExtensionsTest extends FunSpec with MustMatchers {

  val ok = new File("compiler/src/test/resources/extensions/extensions.ok.yaml")
  val nok = new File("compiler/src/test/resources/extensions/extensions.nok.yaml")
  val hypermediaOk = new File("compiler/src/test/resources/extensions/hypermedia.ok.yaml")
  val hypermediaNOk1 = new File("compiler/src/test/resources/extensions/hypermedia.nok1.yaml")
  val hypermediaNOk2 = new File("compiler/src/test/resources/extensions/hypermedia.nok2.yaml")

  describe("The swagger parser") {
    it("should read valid vendor extensions") {
      implicit val (uri, swagger) = StrictYamlParser.parse(ok)
      swagger.info.vendorExtensions contains "x-info-extension" mustBe true
      swagger.paths("/").vendorExtensions contains "x-path-extension" mustBe true
      swagger.paths("/").get.vendorExtensions contains "x-operation-extension" mustBe true
      swagger.paths("/").get.responses("200").vendorExtensions contains "x-response-extension" mustBe true
      swagger.tags.head.vendorExtensions contains "x-tag-extension" mustBe true
      swagger.securityDefinitions("internalApiKey").vendorExtensions contains "x-security-extension" mustBe true
    }
    it("should reject invalid vendor extensions") {
      intercept[JsonParseException] {
        StrictYamlParser.parse(nok)
      }.getClass mustBe classOf[JsonParseException]
    }
    it("should read hypermedia definitions") {
      implicit val (uri, swagger) = StrictYamlParser.parse(hypermediaOk)
      val expected = Map("resource created" ->
        Map("resource updated" -> Map("condition" -> "some rule to show the transition"), "subresource added" -> null),
        "resource updated" -> Map("subresource added" -> Map("condition" -> ""),
          "self" -> Map("condition" -> "non-empty rule")), "resource deleted" -> Map("self" -> null),
        "subresource added" -> Map("resource updated" -> null, "self" -> null, "resource deleted" -> null))
      swagger.transitions.nonEmpty mustBe true
      swagger.transitions mustEqual expected
      swagger.paths("/").get.responses("200").targetState mustEqual Some("resource created")
      swagger.paths("/").get.responses("default").targetState mustEqual None
    }
    it("should reject hypermedia definitions without well-formed definition") {
      val exception = intercept[JsonParseException] {
        StrictYamlParser.parse(hypermediaNOk1)
      }
      exception.getMessage mustEqual "Malformed transition definitions"
    }
    it("should reject hypermedia definitions with incorrect initial state") {
      intercept[ParserException] {
        StrictYamlParser.parse(hypermediaNOk2)
      }.getClass mustBe classOf[ParserException]
    }
  }
}
{'fraction_non_alphanumeric': 0.11723233316937202, 'fraction_numerical': 0.004426955238563699, 'mean_word_length': 4.191489361702128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7591979', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1658, 'n_words': 370}