@Before
 public void before() {
   mockMvc = MockMvcBuilders.standaloneSetup(this.legalRepresentativeController).build();
   PatientLegalRepresentativeAssociationDto patientLegalRepresentativeAssociationDto =
       mock(PatientLegalRepresentativeAssociationDto.class);
   PatientProfileDto legalRepDto = mock(PatientProfileDto.class);
   PatientProfileDto updatedPatientProfileDto = mock(PatientProfileDto.class);
   when(userContext.getCurrentUser()).thenReturn(authenticatedUser);
   when(authenticatedUser.getUsername()).thenReturn("albert.smith");
   when(patientService.findPatientProfileByUsername(anyString())).thenReturn(patientProfileDto);
   when(patientProfileDto.getId()).thenReturn((long) 10);
   when(patientLegalRepresentativeAssociationService.getPatientDtoFromLegalRepresentativeDto(
           any(LegalRepresentativeDto.class)))
       .thenReturn(legalRepDto);
   when(patientLegalRepresentativeAssociationService.getAssociationDtoFromLegalRepresentativeDto(
           any(LegalRepresentativeDto.class)))
       .thenReturn(patientLegalRepresentativeAssociationDto);
   when(patientService.savePatient(legalRepDto)).thenReturn(updatedPatientProfileDto);
 }
  /* (non-Javadoc)
   * @see gov.samhsa.consent2share.infrastructure.DtoToDomainEntityMapper#map(java.lang.Object)
   */
  @Override
  public Patient map(PatientProfileDto patientDto) {
    Patient patient = null;

    // Since username is not required, so need to check if it is null
    if (patientDto.getUsername() == null) {
      if (patientDto.getId() != null) {
        patient = patientRepository.findOne(patientDto.getId());
      } else {
        patient = new Patient();
      }
    } else {
      patient = patientRepository.findByUsername(patientDto.getUsername());
    }
    patient.setFirstName(patientDto.getFirstName());
    patient.setLastName(patientDto.getLastName());
    patient.setEmail(patientDto.getEmail());
    patient.setBirthDay(patientDto.getBirthDate());

    // Optional demographics fields
    if (StringUtils.hasText(patientDto.getSocialSecurityNumber())) {
      patient.setSocialSecurityNumber(patientDto.getSocialSecurityNumber());
    } else {
      patient.setSocialSecurityNumber(null);
    }

    if (StringUtils.hasText(patientDto.getMedicalRecordNumber())) {
      patient.setMedicalRecordNumber(patientDto.getMedicalRecordNumber());
    } else {
      patient.setMedicalRecordNumber(null);
    }

    if (StringUtils.hasText(patientDto.getEnterpriseIdentifier())) {
      patient.setEnterpriseIdentifier(patientDto.getEnterpriseIdentifier());
    } else {
      patient.setEnterpriseIdentifier(null);
    }

    if (StringUtils.hasText(patientDto.getTelephoneTelephone())) {
      Telephone telephone = new Telephone();
      telephone.setTelephone(patientDto.getTelephoneTelephone());
      // TODO: set telecom use code
      patient.setTelephone(telephone);
    } else {
      patient.setTelephone(null);
    }

    if (StringUtils.hasText(patientDto.getAddressStreetAddressLine())
        || StringUtils.hasText(patientDto.getAddressCity())
        || StringUtils.hasText(patientDto.getAddressStateCode())
        || StringUtils.hasText(patientDto.getAddressPostalCode())) {

      Address address = new Address();
      address.setStreetAddressLine(patientDto.getAddressStreetAddressLine());
      address.setCity(patientDto.getAddressCity());
      address.setStateCode(stateCodeRepository.findByCode(patientDto.getAddressStateCode()));
      address.setPostalCode(patientDto.getAddressPostalCode());
      address.setCountryCode(countryCodeRepository.findByCode(patientDto.getAddressCountryCode()));
      patient.setAddress(address);
    } else {
      patient.setAddress(null);
    }

    if (StringUtils.hasText(patientDto.getAdministrativeGenderCode())) {
      AdministrativeGenderCode administrativeGenderCode =
          administrativeGenderCodeRepository.findByCode(patientDto.getAdministrativeGenderCode());
      patient.setAdministrativeGenderCode(administrativeGenderCode);
    } else {
      patient.setAdministrativeGenderCode(null);
    }

    if (patientDto.getLanguageCode() != null
        && StringUtils.hasText(patientDto.getLanguageCode().getCode())) {
      patient.setLanguageCode(
          languageCodeRepository.findByCode(patientDto.getLanguageCode().getCode()));
    } else {
      patient.setLanguageCode(null);
    }

    if (patientDto.getMaritalStatusCode() != null
        && StringUtils.hasText(patientDto.getMaritalStatusCode().getCode())) {
      patient.setMaritalStatusCode(
          maritalStatusCodeRepository.findByCode(patientDto.getMaritalStatusCode().getCode()));
    } else {
      patient.setMaritalStatusCode(null);
    }

    if (patientDto.getRaceCode() != null
        && StringUtils.hasText(patientDto.getRaceCode().getCode())) {
      patient.setRaceCode(raceCodeRepository.findByCode(patientDto.getRaceCode().getCode()));
    } else {
      patient.setRaceCode(null);
    }

    if (patientDto.getReligiousAffiliationCode() != null
        && StringUtils.hasText(patientDto.getReligiousAffiliationCode().getCode())) {
      patient.setReligiousAffiliationCode(
          religiousAffiliationCodeRepository.findByCode(
              patientDto.getReligiousAffiliationCode().getCode()));
    } else {
      patient.setReligiousAffiliationCode(null);
    }

    //		if (patientDto.getIndividualProviders() != null
    //				&& patientDto.getIndividualProviders().size() > 0) {
    //			patient.setIndividualProviders(patientDto.getIndividualProviders());
    //		} else {
    //			patient.setIndividualProviders(null);
    //		}

    return patient;
  }