@Test
  public void testDeleteCertification() throws CredentialAlreadyExistsException {
    profile.addCredential(certification);
    action.setProfile(profile);
    action.setCertification(certification);
    assertEquals(1, profile.getCredentials().size());

    action.deleteCertification();
    assertEquals(0, profile.getCredentials().size());
    verify(mockProfileService).save(profile, FormTypeEnum.CV);
  }
 @Before
 @Override
 public void setUp() throws Exception {
   super.setUp();
   action.setServletRequest(getMockRequest());
   subInvestigator = profile.addSubInvestigator(person);
   subInvestigator.setId(ValueGenerator.getUniqueLong());
   action.setProfile(profile);
   action.setPersonAssociationId(subInvestigator.getId());
   action.setPersonAssociation(subInvestigator);
   when(mockPersonAssociationService.getSubInvestigatorById(subInvestigator.getId()))
       .thenReturn(subInvestigator);
 }
public class RemoveSubinvestigatorActionTest extends AbstractWebTest {

  @Rule public ExpectedException thrown = ExpectedException.none();
  @Inject private PersonAssociationService mockPersonAssociationService;
  @Inject private InvestigatorProfileService mockProfileService;
  @Inject private ProtocolRegistrationService mockRegistrationService;
  @Inject private RemoveSubinvestigatorAction action;
  private InvestigatorProfile profile = InvestigatorProfileFactory.getInstance().create();
  private InvestigatorProfile subInvestigatorProfile =
      InvestigatorProfileFactory.getInstance().create();
  private Person person = subInvestigatorProfile.getPerson();
  private SubInvestigator subInvestigator;

  @Before
  @Override
  public void setUp() throws Exception {
    super.setUp();
    action.setServletRequest(getMockRequest());
    subInvestigator = profile.addSubInvestigator(person);
    subInvestigator.setId(ValueGenerator.getUniqueLong());
    action.setProfile(profile);
    action.setPersonAssociationId(subInvestigator.getId());
    action.setPersonAssociation(subInvestigator);
    when(mockPersonAssociationService.getSubInvestigatorById(subInvestigator.getId()))
        .thenReturn(subInvestigator);
  }

  @Test
  public void testPrepare() {
    action.setPersonAssociation(null);
    action.prepare();
    assertSame(subInvestigator, action.getPersonAssociation());
  }

  @Test
  public void testEnter_SubInvestigator() {
    assertEquals(ActionSupport.SUCCESS, action.enter());
  }

  @Test
  public void testEnter_NoProfile() {
    action.setProfile(null);
    thrown.expect(NullPointerException.class);
    thrown.expectMessage(
        "User Profile does not exist! You must create a user profile before editing an person.");
    action.enter();
  }

  @Test
  public void testEnter_NoSubinvestigatorId() {
    action.setPersonAssociation(null);
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("No Person Association selected for removal.");
    action.enter();
  }

  @Test
  public void testRemovePersonAssociation() {
    assertEquals(FirebirdUIConstants.RETURN_CLOSE_DIALOG, action.removePersonAssociation());
    verify(mockProfileService).deleteAssociatedSubInvestigator(profile, subInvestigator);
  }

  @Test
  public void testGetSubinvestigatorRegistrationsJson() throws JSONException {
    SubInvestigatorRegistration inProgressReg =
        createSubInvestigatorRegistration(RegistrationStatus.IN_PROGRESS);
    SubInvestigatorRegistration returnedReg =
        createSubInvestigatorRegistration(RegistrationStatus.RETURNED);
    SubInvestigatorRegistration submittedReg =
        createSubInvestigatorRegistration(RegistrationStatus.SUBMITTED);
    when(mockRegistrationService.getSubinvestigatorRegistrations(profile, person))
        .thenReturn(Lists.newArrayList(inProgressReg, returnedReg, submittedReg));
    String json = action.getSubinvestigatorRegistrationsJson();
    checkRegistrationInJson(inProgressReg, json);
    checkRegistrationInJson(returnedReg, json);
    checkRegistrationInJson(submittedReg, json);
  }

  private SubInvestigatorRegistration createSubInvestigatorRegistration(RegistrationStatus status) {
    InvestigatorRegistration investigatorRegistration =
        RegistrationFactory.getInstanceWithId().createInvestigatorRegistration(profile);
    SubInvestigatorRegistration registration =
        RegistrationFactory.getInstanceWithId()
            .createSubinvestigatorRegistration(investigatorRegistration, subInvestigatorProfile);
    investigatorRegistration.setStatus(status);
    return registration;
  }

  private void checkRegistrationInJson(SubInvestigatorRegistration registration, String json) {
    assertTrue(json.contains(registration.getId().toString()));
    assertTrue(json.contains(registration.getProtocol().getProtocolTitle()));
    InvestigatorRegistration primaryRegistration = registration.getPrimaryRegistration();
    assertTrue(json.contains(primaryRegistration.getStatus().getDisplay()));
    if (primaryRegistration.isLockedForInvestigator()
        || primaryRegistration.getStatus() == RegistrationStatus.RETURNED) {
      assertTrue(json.contains(action.getText("label.no")));
    } else {
      assertTrue(json.contains(action.getText("label.yes")));
    }
  }
}