<commit_before>package controllers.disposal_of_vehicle

import helpers.UnitSpec
import helpers.disposal_of_vehicle.Helper._
import mappings.disposal_of_vehicle.SetupTradeDetails._
import pages.disposal_of_vehicle._
import play.api.test.Helpers._
import play.api.test.{FakeRequest, WithApplication}
import services.fakes.FakeAddressLookupService._

class SetUpTradeDetailsUnitSpec extends UnitSpec {

  "BeforeYouStart - Controller" should {

    "present" in new WithApplication {
      val request = FakeRequest().withSession()
      val result = setUpTradeDetails().present(request)
      whenReady(result) {
        r => r.header.status should equal(OK)
      }
    }

    "redirect to next page when the form is completed successfully" in new WithApplication {
      val request = buildCorrectlyPopulatedRequest()
      val result = setUpTradeDetails().submit(request)
      whenReady(result) {
        r => r.header.headers.get(LOCATION) should equal(Some(BusinessChooseYourAddressPage.address))
      }
    }

    "return a bad request if no details are entered" in new WithApplication {
      val request = buildCorrectlyPopulatedRequest(dealerName = "", dealerPostcode = "")
      val result = setUpTradeDetails().submit(request)
      whenReady(result) {
        r => r.header.status should equal(BAD_REQUEST)
      }
    }

    "replace max length error message for traderBusinessName with standard error message (US158)" in new WithApplication {
      val request = buildCorrectlyPopulatedRequest(dealerName = "a" * (traderNameMaxLength + 1))
      val result = setUpTradeDetails().submit(request)
      val count = countSubstring(contentAsString(result), "Must be between two and 30 characters and not contain invalid characters")
      count should equal(2)
    }

    "replace required and min length error messages for traderBusinessName with standard error message (US158)" in new WithApplication {
      val request = buildCorrectlyPopulatedRequest(dealerName = "")
      val result = setUpTradeDetails().submit(request)
      val count = countSubstring(contentAsString(result), "Must be between two and 30 characters and not contain invalid characters")
      count should equal(2) // The same message is displayed in 2 places - once in the validation-summary at the top of
      // the page and once above the field.
    }
  }

  private def buildCorrectlyPopulatedRequest(dealerName: String = traderBusinessNameValid.toString, dealerPostcode: String = postcodeValid) = {
    FakeRequest().withSession().withFormUrlEncodedBody(
      traderNameId -> dealerName,
      traderPostcodeId -> dealerPostcode)
  }

  private def setUpTradeDetails() =
    new SetUpTradeDetails()

}<commit_msg>US137 test for SetUpTradeDetails write cookie<commit_after>package controllers.disposal_of_vehicle

import helpers.UnitSpec
import helpers.disposal_of_vehicle.Helper._
import mappings.disposal_of_vehicle.SetupTradeDetails._
import pages.disposal_of_vehicle._
import play.api.test.Helpers._
import play.api.test.{FakeRequest, WithApplication}
import services.fakes.FakeAddressLookupService._
import play.api.mvc.Cookies
import helpers.disposal_of_vehicle.CookieFactoryForUnitSpecs

class SetUpTradeDetailsUnitSpec extends UnitSpec {

  "SetUpTradeDetails - Controller" should {

    "present" in new WithApplication {
      val request = FakeRequest().withSession()
      val result = setUpTradeDetails().present(request)
      whenReady(result) {
        r => r.header.status should equal(OK)
      }
    }

    "redirect to next page when the form is completed successfully" in new WithApplication {
      val request = buildCorrectlyPopulatedRequest()
      val result = setUpTradeDetails().submit(request)
      whenReady(result) {
        r => r.header.headers.get(LOCATION) should equal(Some(BusinessChooseYourAddressPage.address))
      }
    }

    "return a bad request if no details are entered" in new WithApplication {
      val request = buildCorrectlyPopulatedRequest(dealerName = "", dealerPostcode = "")
      val result = setUpTradeDetails().submit(request)
      whenReady(result) {
        r => r.header.status should equal(BAD_REQUEST)
      }
    }

    "replace max length error message for traderBusinessName with standard error message (US158)" in new WithApplication {
      val request = buildCorrectlyPopulatedRequest(dealerName = "a" * (traderNameMaxLength + 1))
      val result = setUpTradeDetails().submit(request)
      val count = countSubstring(contentAsString(result), "Must be between two and 30 characters and not contain invalid characters")
      count should equal(2)
    }

    "replace required and min length error messages for traderBusinessName with standard error message (US158)" in new WithApplication {
      val request = buildCorrectlyPopulatedRequest(dealerName = "")
      val result = setUpTradeDetails().submit(request)
      val count = countSubstring(contentAsString(result), "Must be between two and 30 characters and not contain invalid characters")
      count should equal(2) // The same message is displayed in 2 places - once in the validation-summary at the top of
      // the page and once above the field.
    }

    "write cookie when the form is completed successfully" in new WithApplication {
      val request = buildCorrectlyPopulatedRequest()
      val result = setUpTradeDetails().submit(request)
      whenReady(result) {
        r =>
          val cookies = r.header.headers.get(SET_COOKIE).toSeq.flatMap(Cookies.decode)
          val found = cookies.exists(cookie => cookie.equals(CookieFactoryForUnitSpecs.setupTradeDetails()))
          found should equal(true)
      }
    }
  }

  private def buildCorrectlyPopulatedRequest(dealerName: String = traderBusinessNameValid, dealerPostcode: String = postcodeValid) = {
    FakeRequest().withSession().withFormUrlEncodedBody(
      traderNameId -> dealerName,
      traderPostcodeId -> dealerPostcode)
  }

  private def setUpTradeDetails() =
    new SetUpTradeDetails()

}
{'fraction_non_alphanumeric': 0.07928047968021319, 'fraction_numerical': 0.005163224516988674, 'mean_word_length': 3.914075286415712, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2277367', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1570, 'n_words': 460}