@Test
  public void testUpdateLookupParameter() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    Entry<String, String> entry =
        protocolFundingSourceService.getLookupParameters(FundingSourceType.SPONSOR);
    Assert.assertNotNull(entry);
    String fieldConversions = entry.getValue();
    StringBuilder builder = new StringBuilder();
    builder.append("sponsorCode:" + PROTOCOL_FUNDING_SOURCE_NUMBER + Constants.COMMA);
    builder.append("sponsorName:" + PROTOCOL_FUNDING_SOURCE_NAME);

    Assert.assertThat(entry.getValue(), JUnitMatchers.containsString(builder.toString()));
    String parameter =
        KRADConstants.METHOD_TO_CALL_BOPARM_LEFT_DEL
            + KRADConstants.METHOD_TO_CALL_BOPARM_RIGHT_DEL
            + KRADConstants.METHOD_TO_CALL_PARM1_LEFT_DEL
            + KRADConstants.METHOD_TO_CALL_PARM1_RIGHT_DEL;
    String updatedParam =
        protocolFundingSourceService.updateLookupParameter(
            parameter, Sponsor.class.getName(), fieldConversions);
    Assert.assertThat(
        updatedParam,
        JUnitMatchers.containsString(
            "(!!" + Sponsor.class.getName() + "!!)(((" + builder.toString() + ")))"));
  }
  @Test
  public void testCalculateOtherFundingEmptyId() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());

    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                OTHER_SOURCE_TYPE_ID, EMPTY_NUMBER, null);
    assertNull(fundingSource);
  }
 @Test
 public void testCalculateUnitFundingSourceBadId() throws Exception {
   protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
   protocolFundingSourceService.setUnitService(getUnitService());
   protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
   ProtocolFundingSource fundingSource =
       (ProtocolFundingSource)
           protocolFundingSourceService.updateProtocolFundingSource(
               UNIT_SOURCE_TYPE_ID, UNIT_NUMBER_BAD, null);
   assertNotNull(fundingSource);
   assertNull(fundingSource.getFundingSourceName());
 }
 @Test
 public void testCalculateSponsorFunding() throws Exception {
   protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
   protocolFundingSourceService.setSponsorService(getSponsorService());
   protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
   ProtocolFundingSource fundingSource =
       (ProtocolFundingSource)
           protocolFundingSourceService.updateProtocolFundingSource(
               SPONSOR_SOURCE_TYPE_ID, SPONSOR_NUMBER_AIR_FORCE, null);
   assertNotNull(fundingSource);
   assertTrue(fundingSource.getFundingSourceName().equalsIgnoreCase(sponsorNameAirForce));
 }
  @Test
  public void testIsValidIdForTypeOther() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
    ProtocolFundingSource fundingSource =
        new ProtocolFundingSource(OTHER_SOURCE_TYPE_ID, FundingSourceType.OTHER, "otherName", null);
    assertTrue(protocolFundingSourceService.isValidIdForType(fundingSource));

    fundingSource =
        new ProtocolFundingSource(
            OTHER_SOURCE_TYPE_ID, FundingSourceType.OTHER, EMPTY_NUMBER, null);
    assertTrue(protocolFundingSourceService.isValidIdForType(fundingSource));
  }
  @Test
  public void testIsValidIdForTypeUnit() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
    protocolFundingSourceService.setUnitService(getUnitService());

    ProtocolFundingSource fundingSource =
        new ProtocolFundingSource(UNIT_NUMBER_GOOD, FundingSourceType.UNIT, null, null);
    assertTrue(protocolFundingSourceService.isValidIdForType(fundingSource));

    fundingSource = new ProtocolFundingSource(UNIT_NUMBER_BAD, FundingSourceType.UNIT, null, null);
    assertFalse(protocolFundingSourceService.isValidIdForType(fundingSource));
  }
 @Test
 public void testCalculateUnitFunding() throws Exception {
   protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
   protocolFundingSourceService.setUnitService(getUnitService());
   protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
   ProtocolFundingSource fundingSource =
       (ProtocolFundingSource)
           protocolFundingSourceService.updateProtocolFundingSource(
               UNIT_SOURCE_TYPE_ID, UNIT_NUMBER_GOOD, null);
   assertNotNull(fundingSource);
   assertNotNull(fundingSource.getFundingSourceName());
   assertTrue(fundingSource.getFundingSourceName().equalsIgnoreCase(UNIT_NAME_GOOD));
 }
  @Test
  public void testCalculateDevProposalFundingNegativeEmptyID() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setParameterService(getParameterService());
    protocolFundingSourceService.setBusinessObjectService(getBusinessObjectService());
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());

    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                DEVELOPMENT_PROP_SOURCE_TYPE_ID, EMPTY_NUMBER, null);
    assertNull(fundingSource);
  }
  @Test
  public void testCalculateUnitFundingSourceEmptyId() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    final UnitService unitService = context.mock(UnitService.class);

    protocolFundingSourceService.setUnitService(unitService);
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                UNIT_SOURCE_TYPE_ID, EMPTY_NUMBER, null);
    assertNull(fundingSource);
  }
  @Test
  public void testIsValidIdForTypeSponsor() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
    protocolFundingSourceService.setSponsorService(getSponsorService());

    ProtocolFundingSource fundingSource =
        new ProtocolFundingSource(SPONSOR_NUMBER_AIR_FORCE, FundingSourceType.SPONSOR, null, null);
    assertTrue(protocolFundingSourceService.isValidIdForType(fundingSource));

    fundingSource =
        new ProtocolFundingSource(SPONSOR_NUMBER_BAD, FundingSourceType.SPONSOR, null, null);
    assertFalse(protocolFundingSourceService.isValidIdForType(fundingSource));
  }
  @Test
  public void testCalculateOtherFunding() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());

    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                OTHER_SOURCE_TYPE_ID, OTHER_NUMBER_GOOD, OTHER_NAME_GOOD);
    assertNotNull(fundingSource);
    assertNotNull(fundingSource.getFundingSourceName());
    assertTrue(fundingSource.getFundingSourceName().equalsIgnoreCase(OTHER_NAME_GOOD));
    assertTrue(StringUtils.isEmpty(fundingSource.getFundingSourceTitle()));
  }
  @Test
  public void testIsValidIdForTypeAward() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
    protocolFundingSourceService.setAwardService(getAwardService());
    protocolFundingSourceService.setParameterService(getParameterService());
    ProtocolFundingSource fundingSource =
        new ProtocolFundingSource(AWARD_NUMBER_GOOD, FundingSourceType.AWARD, null, null);
    assertTrue(protocolFundingSourceService.isValidIdForType(fundingSource));

    fundingSource =
        new ProtocolFundingSource(AWARD_NUMBER_BAD, FundingSourceType.AWARD, null, null);
    assertFalse(protocolFundingSourceService.isValidIdForType(fundingSource));
  }
  @Test
  public void testCalculateAwardFundingBadIdBadNumber() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setAwardService(getAwardService());
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
    protocolFundingSourceService.setParameterService(getParameterService());

    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                AWARD_SOURCE_TYPE_ID, AWARD_NUMBER_BAD, null);
    assertNotNull(fundingSource);
    assertTrue(StringUtils.isEmpty(fundingSource.getFundingSourceName()));
    assertTrue(StringUtils.isEmpty(fundingSource.getFundingSourceTitle()));
  }
  @Test
  public void testCalculateDevProposalFundingBadID() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setParameterService(getParameterService());
    protocolFundingSourceService.setBusinessObjectService(getBusinessObjectService());
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());

    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                DEVELOPMENT_PROP_SOURCE_TYPE_ID, DEV_PROPOSAL_NUMBER_BAD, null);
    assertNotNull(fundingSource);
    assertTrue(StringUtils.isEmpty(fundingSource.getFundingSourceName()));
    assertTrue(StringUtils.isEmpty(fundingSource.getFundingSourceTitle()));
  }
  @Test
  public void testCalculateAwardFundingBadIdGoodNumber() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setAwardService(getAwardService());
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
    protocolFundingSourceService.setParameterService(getParameterService());

    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                AWARD_SOURCE_TYPE_ID, AWARD_NUMBER_GOOD, null);
    assertNotNull(fundingSource);
    assertNotNull(fundingSource.getFundingSourceName());
    assertTrue(fundingSource.getFundingSourceName().equalsIgnoreCase(sponsorNameAirForce));
    assertTrue(fundingSource.getFundingSourceTitle().equalsIgnoreCase(AWARD_TITLE_GOOD));
  }
  @Test
  public void testCalculateDevProposalFunding() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setParameterService(getParameterService());
    protocolFundingSourceService.setBusinessObjectService(getBusinessObjectService());
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());

    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                DEVELOPMENT_PROP_SOURCE_TYPE_ID, DEV_PROPOSAL_NUMBER_GOOD, null);
    assertNotNull(fundingSource);
    assertNotNull(fundingSource.getFundingSourceName());
    assertTrue(fundingSource.getFundingSourceName().equalsIgnoreCase(sponsorNameAirForce));
    assertTrue(fundingSource.getFundingSourceTitle().equalsIgnoreCase(DEV_PROPOSAL_TITLE_GOOD));
  }
  @Test
  public void testCalculateInstProposalFundingBadIdBadNumber() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setParameterService(getParameterService());
    protocolFundingSourceService.setInstitutionalProposalService(getInstProposalService());
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
    protocolFundingSourceService.setBusinessObjectService(getBusinessObjectService());

    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                INSTITUTE_PROP_SOURCE_TYPE_ID, INST_PROPOSAL_NUMBER_BAD, null);
    assertNotNull(fundingSource);
    assertTrue(StringUtils.isEmpty(fundingSource.getFundingSourceName()));
    assertTrue(StringUtils.isEmpty(fundingSource.getFundingSourceTitle()));
  }
  @Test
  public void testCalculateInstProposalFundingBadIdGoodNumber() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setParameterService(getParameterService());
    protocolFundingSourceService.setInstitutionalProposalService(getInstProposalService());
    protocolFundingSourceService.setFundingSourceTypeService(getFundingSourceTypeService());
    protocolFundingSourceService.setBusinessObjectService(getBusinessObjectService());

    ProtocolFundingSource fundingSource =
        (ProtocolFundingSource)
            protocolFundingSourceService.updateProtocolFundingSource(
                INSTITUTE_PROP_SOURCE_TYPE_ID, INST_PROPOSAL_NUMBER_GOOD, null);
    assertNotNull(fundingSource);
    assertNotNull(fundingSource.getFundingSourceName());
    assertTrue(fundingSource.getFundingSourceName().equalsIgnoreCase(sponsorNameAirForce));
    assertTrue(fundingSource.getFundingSourceTitle().equalsIgnoreCase(INST_PROPOSAL_TITLE_GOOD));
  }
  @Test
  public void testIsLookupableFundingSource() throws Exception {
    String badFundingTypeCode = "-99";
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setParameterService(getParameterService());

    assertFalse(protocolFundingSourceService.isLookupable(FundingSourceType.OTHER));
    assertFalse(protocolFundingSourceService.isLookupable(badFundingTypeCode));

    assertTrue(protocolFundingSourceService.isLookupable(FundingSourceType.INSTITUTIONAL_PROPOSAL));
    assertTrue(protocolFundingSourceService.isLookupable(FundingSourceType.UNIT));
    assertTrue(protocolFundingSourceService.isLookupable(FundingSourceType.SPONSOR));
    assertTrue(protocolFundingSourceService.isLookupable(FundingSourceType.AWARD));
    assertTrue(protocolFundingSourceService.isLookupable(FundingSourceType.PROPOSAL_DEVELOPMENT));
  }
  @Test
  public void testUpdateSourceNameEditable() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    protocolFundingSourceService.setParameterService(getParameterService());

    assertFalse(protocolFundingSourceService.isEditable(FundingSourceType.INSTITUTIONAL_PROPOSAL));
    assertTrue(protocolFundingSourceService.isEditable(FundingSourceType.OTHER));

    assertFalse(protocolFundingSourceService.isEditable(FundingSourceType.AWARD));
    assertFalse(protocolFundingSourceService.isEditable(FundingSourceType.PROPOSAL_DEVELOPMENT));
    assertFalse(protocolFundingSourceService.isEditable(FundingSourceType.SPONSOR));
    assertFalse(protocolFundingSourceService.isEditable(FundingSourceType.UNIT));
  }
  @Test
  public void testGetLookupParameters() throws Exception {
    protocolFundingSourceService = new ProtocolFundingSourceServiceImpl();
    StringBuilder builder = new StringBuilder();

    Entry<String, String> entry =
        protocolFundingSourceService.getLookupParameters(FundingSourceType.SPONSOR);
    Assert.assertNotNull(entry);
    builder.append("sponsorCode:" + PROTOCOL_FUNDING_SOURCE_NUMBER + Constants.COMMA);
    builder.append("sponsorName:" + PROTOCOL_FUNDING_SOURCE_NAME);
    Assert.assertThat(entry.getValue(), JUnitMatchers.containsString(builder.toString()));
    builder.delete(0, builder.length());

    entry = protocolFundingSourceService.getLookupParameters(FundingSourceType.UNIT);
    Assert.assertNotNull(entry);
    builder.append("unitNumber:" + PROTOCOL_FUNDING_SOURCE_NUMBER + Constants.COMMA);
    builder.append("unitName:" + PROTOCOL_FUNDING_SOURCE_NAME);
    Assert.assertThat(entry.getValue(), JUnitMatchers.containsString(builder.toString()));
    builder.delete(0, builder.length());

    entry =
        protocolFundingSourceService.getLookupParameters(FundingSourceType.PROPOSAL_DEVELOPMENT);
    Assert.assertNotNull(entry);
    builder.append("proposalNumber:" + PROTOCOL_FUNDING_SOURCE_NUMBER + Constants.COMMA);
    builder.append("sponsor.sponsorName:" + PROTOCOL_FUNDING_SOURCE_NAME + Constants.COMMA);
    builder.append("title:" + PROTOCOL_FUNDING_SOURCE_TITLE);
    Assert.assertThat(entry.getValue(), JUnitMatchers.containsString(builder.toString()));
    builder.delete(0, builder.length());

    entry =
        protocolFundingSourceService.getLookupParameters(FundingSourceType.INSTITUTIONAL_PROPOSAL);
    Assert.assertNotNull(entry);
    builder.append("proposalId:" + PROTOCOL_FUNDING_SOURCE + Constants.COMMA);
    builder.append("proposalNumber:" + PROTOCOL_FUNDING_SOURCE_NUMBER + Constants.COMMA);
    builder.append("sponsor.sponsorName:" + PROTOCOL_FUNDING_SOURCE_NAME + Constants.COMMA);
    builder.append("title:" + PROTOCOL_FUNDING_SOURCE_TITLE);
    Assert.assertThat(entry.getValue(), JUnitMatchers.containsString(builder.toString()));
    builder.delete(0, builder.length());

    entry = protocolFundingSourceService.getLookupParameters(FundingSourceType.AWARD);
    Assert.assertNotNull(entry);
    builder.append("awardId:" + PROTOCOL_FUNDING_SOURCE + Constants.COMMA);
    builder.append("awardNumber:" + PROTOCOL_FUNDING_SOURCE_NUMBER + Constants.COMMA);
    builder.append("sponsor.sponsorName:" + PROTOCOL_FUNDING_SOURCE_NAME + Constants.COMMA);
    builder.append("title:" + PROTOCOL_FUNDING_SOURCE_TITLE);
    Assert.assertThat(entry.getValue(), JUnitMatchers.containsString(builder.toString()));

    try {
      entry = protocolFundingSourceService.getLookupParameters(FundingSourceType.OTHER);
      fail("IllegalArgumentException was not thrown for invalid test case using OTHER");
    } catch (IllegalArgumentException e) {
      // yup
    }
  }