<commit_before>/*
 * Copyright 2019 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package controllers.supervision

import models.supervision._
import org.jsoup.Jsoup
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.mockito.Matchers.{eq => eqTo, _}
import play.api.i18n.Messages
import play.api.mvc.Result
import play.api.test.Helpers._
import utils.{AmlsSpec, AuthorisedFixture, DependencyMocks}

import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

class ProfessionalBodyMemberControllerSpec extends AmlsSpec with MockitoSugar {

  trait Fixture extends AuthorisedFixture with DependencyMocks {self =>
    val request = addToken(authRequest)

    val controller = new ProfessionalBodyMemberController (
      dataCacheConnector = mockCacheConnector,
      authConnector = self.authConnector
    )

    mockCacheSave[Supervision]

  }

  "ProfessionalBodyMemberController" must {

    "update the the answer field in supervision when the answer is yes" in new Fixture {

      val supervision = Some(
        Supervision (
          anotherBody = Some(AnotherBodyNo),
          professionalBodyMember = Some(ProfessionalBodyMemberNo),
          professionalBodies = None,
          professionalBody = Some(ProfessionalBodyNo),
          hasChanged = true,
          hasAccepted = true
        )
      )

      val newSupervision = controller.updateSupervisionFromIncomingData(ProfessionalBodyMemberYes, supervision)

      Await.result(newSupervision, 1 seconds).professionalBodyMember.get mustEqual(ProfessionalBodyMemberYes)
    }

    "reset professionalBodies to null if the answer to the question is No" in new Fixture {

      val supervision = Some(
        Supervision (
          anotherBody = Some(AnotherBodyNo),
          professionalBodyMember = Some(ProfessionalBodyMemberYes),
          professionalBodies = Some(ProfessionalBodies(Set(AccountingTechnicians))),
          professionalBody = Some(ProfessionalBodyNo),
          hasChanged = true,
          hasAccepted = true
        )
      )

      val newSupervision = controller.updateSupervisionFromIncomingData(ProfessionalBodyMemberNo, supervision)

      Await.result(newSupervision, 1 seconds).professionalBodies mustEqual(None)
    }

    "load the page Is your business a member of a professional body?" in new Fixture  {

      mockCacheFetch[Supervision](None)

      val result = controller.get()(request)
      status(result) must be(OK)
      contentAsString(result) must include(Messages("supervision.memberofprofessionalbody.title"))

    }

    "load the page Is your business a member of a professional body? with pre-populate data" in new Fixture  {

      mockCacheFetch[Supervision](Some(Supervision(
        professionalBodyMember = Some(ProfessionalBodyMemberYes)
      )))

      val result = controller.get()(request)
      status(result) must be(OK)

      val document = Jsoup.parse(contentAsString(result))
      document.select("input[value=true]").hasAttr("checked") must be(true)

    }

    "on post with valid data" must {

      "redirect to WhichProfessionalBodyController" when {

        "the answer is yes to the question and no previous professional bodies has been registered in the cache" in new Fixture {
          val supervision = Some (
            Supervision (
              professionalBodyMember = Some(ProfessionalBodyMemberYes),
              professionalBodies = None
            )
          )

          val result: Future[Result] = Future(controller.redirectTo(supervision, any()))

          status(result) must be(SEE_OTHER)
          redirectLocation(result) must be(Some(routes.WhichProfessionalBodyController.get().url))
        }

        "isAMember field is true" when {
          "edit is false" in new Fixture {

            val newRequest = request.withFormUrlEncodedBody(
              "isAMember" -> "true"
            )

            mockCacheFetch[Supervision](None)

            val result = controller.post()(newRequest)
            status(result) must be(SEE_OTHER)
            redirectLocation(result) must be(Some(routes.WhichProfessionalBodyController.get().url))
          }
          "edit is true" when {
            "professionalBodies is not defined" in new Fixture {

              val newRequest = request.withFormUrlEncodedBody(
                "isAMember" -> "true"
              )

              mockCacheFetch[Supervision](None)

              val result = controller.post(true)(newRequest)
              status(result) must be(SEE_OTHER)
              redirectLocation(result) must be(Some(routes.WhichProfessionalBodyController.get(true).url))
            }
          }
        }
      }

      "redirect to PenalisedByProfessionalController" when {

        "the answer is no to the question and no previous professional bodies has been registered in the cache" in new Fixture {
          val supervision = Some (
            Supervision (
              professionalBodyMember = Some(ProfessionalBodyMemberNo)
            )
          )

          val result: Future[Result] = Future(controller.redirectTo(supervision, edit = false))

          status(result) must be(SEE_OTHER)
          redirectLocation(result) must be(Some(routes.PenalisedByProfessionalController.get().url))
        }

        "isMember is false" in new Fixture {

          val newRequest = request.withFormUrlEncodedBody(
            "isAMember" -> "false"
          )

          mockCacheFetch[Supervision](None)

          val result = controller.post()(newRequest)
          status(result) must be(SEE_OTHER)
          redirectLocation(result) must be(Some(routes.PenalisedByProfessionalController.get().url))
        }
      }

      "redirect to SummaryController" when {

        "the answer is yes and professional bodies is already known" in new Fixture {
          val supervision = Some (<commit_msg>AMLS-5060 | update to test descriptions<commit_after>/*
 * Copyright 2019 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package controllers.supervision

import models.supervision._
import org.jsoup.Jsoup
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.mockito.Matchers.{eq => eqTo, _}
import play.api.i18n.Messages
import play.api.mvc.Result
import play.api.test.Helpers._
import utils.{AmlsSpec, AuthorisedFixture, DependencyMocks}

import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

class ProfessionalBodyMemberControllerSpec extends AmlsSpec with MockitoSugar {

  trait Fixture extends AuthorisedFixture with DependencyMocks {self =>
    val request = addToken(authRequest)

    val controller = new ProfessionalBodyMemberController (
      dataCacheConnector = mockCacheConnector,
      authConnector = self.authConnector
    )

    mockCacheSave[Supervision]

  }

  "ProfessionalBodyMemberController" must {

    "update the the answer field in supervision when the answer is yes" in new Fixture {

      val supervision = Some(
        Supervision (
          anotherBody = Some(AnotherBodyNo),
          professionalBodyMember = Some(ProfessionalBodyMemberNo),
          professionalBodies = None,
          professionalBody = Some(ProfessionalBodyNo),
          hasChanged = true,
          hasAccepted = true
        )
      )

      val newSupervision = controller.updateSupervisionFromIncomingData(ProfessionalBodyMemberYes, supervision)

      Await.result(newSupervision, 1 seconds).professionalBodyMember.get mustEqual(ProfessionalBodyMemberYes)
    }

    "reset professionalBodies to null if the answer to the question is No" in new Fixture {

      val supervision = Some(
        Supervision (
          anotherBody = Some(AnotherBodyNo),
          professionalBodyMember = Some(ProfessionalBodyMemberYes),
          professionalBodies = Some(ProfessionalBodies(Set(AccountingTechnicians))),
          professionalBody = Some(ProfessionalBodyNo),
          hasChanged = true,
          hasAccepted = true
        )
      )

      val newSupervision = controller.updateSupervisionFromIncomingData(ProfessionalBodyMemberNo, supervision)

      Await.result(newSupervision, 1 seconds).professionalBodies mustEqual(None)
    }

    "load the page Is your business a member of a professional body?" in new Fixture  {

      mockCacheFetch[Supervision](None)

      val result = controller.get()(request)
      status(result) must be(OK)
      contentAsString(result) must include(Messages("supervision.memberofprofessionalbody.title"))

    }

    "load the page Is your business a member of a professional body? with pre-populate data" in new Fixture  {

      mockCacheFetch[Supervision](Some(Supervision(
        professionalBodyMember = Some(ProfessionalBodyMemberYes)
      )))

      val result = controller.get()(request)
      status(result) must be(OK)

      val document = Jsoup.parse(contentAsString(result))
      document.select("input[value=true]").hasAttr("checked") must be(true)

    }

    "on post with valid data" must {

      "redirect to WhichProfessionalBodyController" when {

        "the answer to the question is yes and no previous professional bodies exist in the cache" in new Fixture {
          val supervision = Some (
            Supervision (
              professionalBodyMember = Some(ProfessionalBodyMemberYes),
              professionalBodies = None
            )
          )

          val result: Future[Result] = Future(controller.redirectTo(supervision, any()))

          status(result) must be(SEE_OTHER)
          redirectLocation(result) must be(Some(routes.WhichProfessionalBodyController.get().url))
        }

        "isAMember field is true" when {
          "edit is false" in new Fixture {

            val newRequest = request.withFormUrlEncodedBody(
              "isAMember" -> "true"
            )

            mockCacheFetch[Supervision](None)

            val result = controller.post()(newRequest)
            status(result) must be(SEE_OTHER)
            redirectLocation(result) must be(Some(routes.WhichProfessionalBodyController.get().url))
          }
          "edit is true" when {
            "professionalBodies is not defined" in new Fixture {

              val newRequest = request.withFormUrlEncodedBody(
                "isAMember" -> "true"
              )

              mockCacheFetch[Supervision](None)

              val result = controller.post(true)(newRequest)
              status(result) must be(SEE_OTHER)
              redirectLocation(result) must be(Some(routes.WhichProfessionalBodyController.get(true).url))
            }
          }
        }
      }

      "redirect to PenalisedByProfessionalController" when {

        "the answer to the question is no and no previous professional bodies exist in the cache" in new Fixture {
          val supervision = Some (
            Supervision (
              professionalBodyMember = Some(ProfessionalBodyMemberNo)
            )
          )

          val result: Future[Result] = Future(controller.redirectTo(supervision, edit = false))

          status(result) must be(SEE_OTHER)
          redirectLocation(result) must be(Some(routes.PenalisedByProfessionalController.get().url))
        }

        "isMember is false" in new Fixture {

          val newRequest = request.withFormUrlEncodedBody(
            "isAMember" -> "false"
          )

          mockCacheFetch[Supervision](None)

          val result = controller.post()(newRequest)
          status(result) must be(SEE_OTHER)
          redirectLocation(result) must be(Some(routes.PenalisedByProfessionalController.get().url))
        }
      }

      "redirect to SummaryController" when {

        "the answer is yes and professional bodies is already known" in new Fixture {
          val supervision = Some (
{'fraction_non_alphanumeric': 0.07634176434299815, 'fraction_numerical': 0.00215916101172116, 'mean_word_length': 2.8222811671087533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7591495', 'n_tokens_mistral': 3500, 'n_tokens_neox': 3226, 'n_words': 926}