<commit_before>/*
 * Copyright 2017 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.changeofficer

import connectors.DataCacheConnector
import models.responsiblepeople._
import org.joda.time.LocalDate
import org.mockito.Matchers.{eq => meq, _}
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import play.api.i18n.Messages
import play.api.inject.bind
import play.api.inject.guice.GuiceInjectorBuilder
import play.api.test.Helpers._
import uk.gov.hmrc.play.frontend.auth.connectors.AuthConnector
import utils.{AuthorisedFixture, GenericTestHelper, StatusConstants}

import scala.concurrent.Future

class RemoveResponsiblePersonControllerSpec extends GenericTestHelper with MockitoSugar {

  trait TestFixture extends AuthorisedFixture { self =>
    val request = addToken(self.authRequest)

    val dataCacheConnector = mock[DataCacheConnector]

    val injector = new GuiceInjectorBuilder()
      .overrides(bind[AuthConnector].to(self.authConnector))
      .overrides(bind[DataCacheConnector].to(dataCacheConnector))
      .build()

    lazy val controller = injector.instanceOf[RemoveResponsiblePersonController]

    val nominatedOfficer = ResponsiblePeople(
      personName = Some(PersonName("firstName", None, "lastName",None, None)),
      positions = Some(Positions(Set(NominatedOfficer),None))
    )

    val otherResponsiblePerson = ResponsiblePeople(
      personName = Some(PersonName("otherFirstName", None, "otherLastName",None, None)),
      positions = Some(Positions(Set(Director),None))
    )

    when(dataCacheConnector.fetch[Seq[ResponsiblePeople]](any())(any(), any(), any()))
      .thenReturn(Future.successful(Some(Seq(nominatedOfficer, otherResponsiblePerson))))
  }

  "The RemoveResponsiblePersonController" when {

    "get is called" must {
      "display the view" in new TestFixture {
        val result = controller.get()(request)

        status(result) mustBe OK
        contentAsString(result) must include(Messages("changeofficer.removeresponsibleperson.title"))
      }
    }

    "post is called" must {
      "Redirect to NewOfficerController" in new TestFixture {
        val result = controller.post()(request.withFormUrlEncodedBody(
          "date.day" -> "10",
          "date.month" -> "11",
          "date.year" -> "2001"
        ))

        val responsiblePerson = ResponsiblePeople(personName = Some(PersonName("First", Some("Middle"), "Last", None, None)))

        status(result) mustBe SEE_OTHER

        when{
          controller.dataCacheConnector.fetch[Seq[ResponsiblePeople]](any())(any(),any(),any())
        } thenReturn Future.successful(Some(
          Seq(responsiblePerson)
        ))

        verify(controller.dataCacheConnector).save[Seq[ResponsiblePeople]](any(), meq(Seq(
          responsiblePerson.copy(
            status = Some(StatusConstants.Deleted),
            endDate = Some(ResponsiblePersonEndDate(new LocalDate(2001, 11, 10))),
            hasChanged = true)
          )))(any(), any(), any())

        redirectLocation(result) mustBe Some(controllers.changeofficer.routes.NewOfficerController.get().url)
      }
      "return BAD_REQUEST for invalid form" in new TestFixture {
        val result = controller.post()(request.withFormUrlEncodedBody(
          "date.day" -> "a",
          "date.month" -> "b",
          "date.year" -> "c"
        ))

        status(result) mustBe BAD_REQUEST
      }
    }

  }

}<commit_msg>Save the removal of nominated officer<commit_after>/*
 * Copyright 2017 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.changeofficer

import connectors.DataCacheConnector
import models.responsiblepeople._
import org.joda.time.LocalDate
import org.mockito.Matchers.{eq => meq, _}
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import play.api.i18n.Messages
import play.api.inject.bind
import play.api.inject.guice.GuiceInjectorBuilder
import play.api.test.Helpers._
import uk.gov.hmrc.http.cache.client.CacheMap
import uk.gov.hmrc.play.frontend.auth.connectors.AuthConnector
import utils.{AuthorisedFixture, GenericTestHelper, StatusConstants}

import scala.concurrent.Future

class RemoveResponsiblePersonControllerSpec extends GenericTestHelper with MockitoSugar {

  trait TestFixture extends AuthorisedFixture { self =>
    val request = addToken(self.authRequest)

    val dataCacheConnector = mock[DataCacheConnector]

    val injector = new GuiceInjectorBuilder()
      .overrides(bind[AuthConnector].to(self.authConnector))
      .overrides(bind[DataCacheConnector].to(dataCacheConnector))
      .build()

    lazy val controller = injector.instanceOf[RemoveResponsiblePersonController]

    val nominatedOfficer = ResponsiblePeople(
      personName = Some(PersonName("firstName", None, "lastName",None, None)),
      positions = Some(Positions(Set(NominatedOfficer),None))
    )

    val otherResponsiblePerson = ResponsiblePeople(
      personName = Some(PersonName("otherFirstName", None, "otherLastName",None, None)),
      positions = Some(Positions(Set(Director),None))
    )

    when(dataCacheConnector.fetch[Seq[ResponsiblePeople]](any())(any(), any(), any()))
      .thenReturn(Future.successful(Some(Seq(nominatedOfficer, otherResponsiblePerson))))
  }

  "The RemoveResponsiblePersonController" when {

    "get is called" must {
      "display the view" in new TestFixture {
        val result = controller.get()(request)

        status(result) mustBe OK
        contentAsString(result) must include(Messages("changeofficer.removeresponsibleperson.title"))
      }
    }

    "post is called" must {
      "Redirect to NewOfficerController" in new TestFixture {

        when {
          controller.dataCacheConnector.save[Seq[ResponsiblePeople]](any(), any())(any(), any(), any())
        } thenReturn Future.successful(CacheMap("", Map.empty))

        val result = controller.post()(request.withFormUrlEncodedBody(
          "date.day" -> "10",
          "date.month" -> "11",
          "date.year" -> "2001"
        ))

        status(result) mustBe SEE_OTHER
        redirectLocation(result) mustBe Some(controllers.changeofficer.routes.NewOfficerController.get().url)

        verify(controller.dataCacheConnector).save[Seq[ResponsiblePeople]](any(), meq(Seq(
          nominatedOfficer.copy(
            status = Some(StatusConstants.Deleted),
            endDate = Some(ResponsiblePersonEndDate(new LocalDate(2001, 11, 10))),
            hasChanged = true),
          otherResponsiblePerson
        )))(any(), any(), any())

      }
      "return BAD_REQUEST for invalid form" in new TestFixture {
        val result = controller.post()(request.withFormUrlEncodedBody(
          "date.day" -> "a",
          "date.month" -> "b",
          "date.year" -> "c"
        ))

        status(result) mustBe BAD_REQUEST
      }
    }

  }

}
{'fraction_non_alphanumeric': 0.1080398838823678, 'fraction_numerical': 0.0065631705162186044, 'mean_word_length': 3.6150262085032034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '5833895', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2229, 'n_words': 529}