<commit_before>/*
 * Copyright 2018 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 utils

import connectors.{AmlsConnector, DataCacheConnector}
import models.registrationdetails.RegistrationDetails
import org.scalatest.MustMatchers
import org.scalatest.mock.MockitoSugar
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.inject.guice.GuiceApplicationBuilder
import org.mockito.Mockito._
import org.mockito.Matchers.{eq => eqTo, _}
import cats.implicits._
import models.businesscustomer.ReviewDetails
import models.businessmatching.BusinessMatching
import org.scalatest.concurrent.ScalaFutures
import uk.gov.hmrc.play.frontend.auth.AuthContext
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import uk.gov.hmrc.http.HeaderCarrier

class BusinessNameSpec extends PlaySpec with MustMatchers with OneAppPerSuite with MockitoSugar with ScalaFutures {

  trait Fixture {
    implicit val amlsConnector = mock[AmlsConnector]
    implicit val cacheConnector = mock[DataCacheConnector]
    implicit val headerCarrier = HeaderCarrier()
    implicit val authContext = mock[AuthContext]

    val safeId = "X87FUDIKJJKJH87364"
  }

  "The BusinessName helper utility" must {
    "get the business name from amls" in new Fixture {
      when {
        amlsConnector.registrationDetails(eqTo(safeId))(any(), any(), any())
      } thenReturn Future.successful(RegistrationDetails("Test Business", isIndividual = false))

      whenReady(BusinessName.getName(safeId.some).value) { result =>
        result mustBe "Test Business".some
      }
    }

    "get the name from the data cache" when {
      "the safeId is not available" in new Fixture {

        val reviewDetails = mock[ReviewDetails]
        when(reviewDetails.businessName) thenReturn "Test Business from the cache"

        when {
          cacheConnector.fetch[BusinessMatching](eqTo(BusinessMatching.key))(any(), any(), any())
        } thenReturn Future.successful(BusinessMatching(reviewDetails = reviewDetails.some).some)

        whenReady(BusinessName.getName(None).value) { result =>
          result mustBe "Test Business from the cache".some
        }
      }
    }
  }
}<commit_msg>AMLS-4096 | Added unit test for new method in BusinessName<commit_after>/*
 * Copyright 2018 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 utils

import cats.implicits._
import connectors.{AmlsConnector, DataCacheConnector}
import models.ReadStatusResponse
import models.businesscustomer.ReviewDetails
import models.businessmatching.BusinessMatching
import models.registrationdetails.RegistrationDetails
import models.status.SubmissionReady
import org.mockito.Matchers.{eq => eqTo, _}
import org.mockito.Mockito._
import org.scalatest.MustMatchers
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.mock.MockitoSugar
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import services.StatusService
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.frontend.auth.AuthContext

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class BusinessNameSpec extends PlaySpec with MustMatchers with OneAppPerSuite with MockitoSugar with ScalaFutures {

  trait Fixture {
    implicit val amlsConnector = mock[AmlsConnector]
    implicit val cacheConnector = mock[DataCacheConnector]
    implicit val headerCarrier = HeaderCarrier()
    implicit val authContext = mock[AuthContext]
    implicit val statusResponse = mock[ReadStatusResponse]
    implicit val statusService = mock[StatusService]

    val safeId = "X87FUDIKJJKJH87364"
  }

  "The BusinessName helper utility" must {
    "get the business name from amls" in new Fixture {
      when {
        amlsConnector.registrationDetails(eqTo(safeId))(any(), any(), any())
      } thenReturn Future.successful(RegistrationDetails("Test Business", isIndividual = false))

      whenReady(BusinessName.getName(safeId.some).value) { result =>
        result mustBe "Test Business".some
      }
    }

    "get the name from the data cache" when {
      "the safeId is not available" in new Fixture {

        val reviewDetails = mock[ReviewDetails]
        when(reviewDetails.businessName) thenReturn "Test Business from the cache"

        when {
          cacheConnector.fetch[BusinessMatching](eqTo(BusinessMatching.key))(any(), any(), any())
        } thenReturn Future.successful(BusinessMatching(reviewDetails = reviewDetails.some).some)

        whenReady(BusinessName.getName(None).value) { result =>
          result mustBe "Test Business from the cache".some
        }
      }
    }

    "get the name from amls" when {
      "the safeId comes from API9" in new Fixture {

        when(statusResponse.safeId) thenReturn Some(safeId)

        when {
          statusService.getDetailedStatus(any(), any(), any())
        } thenReturn Future.successful((SubmissionReady, Some(statusResponse)))

        when {
          amlsConnector.registrationDetails(eqTo(safeId))(any(), any(), any())
        } thenReturn Future.successful(RegistrationDetails("Test Business", isIndividual = false))

        whenReady(BusinessName.getBusinessNameFromAmls().value) { result =>
          result mustBe "Test Business".some
        }
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.08388416745357255, 'fraction_numerical': 0.005508341202392194, 'mean_word_length': 3.9570982839313573, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '4649128', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1728, 'n_words': 509}