@Test
  public void shouldCreateOwnershipCase() {
    String allUsersGrpId = "grp1";
    Group allUsersGroup = new Group();
    allUsersGroup.setId(allUsersGrpId);
    model.put(CommcareGateway.OWNER_ID_KEY, allUsersGrpId);

    doReturn(allUsersGroup).when(mockAllGroups).getGroupByName(CommcareGateway.ALL_USERS_GROUP);
    doReturn(getExpectedOwnershipCaseXml())
        .when(spyCommcareGateway)
        .getXmlFromObject(OWNERSHIP_CASE_REGISTER_FORM_TEMPLATE_PATH, model);

    spyCommcareGateway.createOwnershipCase();
    verify(mockHttpClientService).post(someUrl, getExpectedOwnershipCaseXml());
  }
  @Test
  public void shouldUpdateGroupIfExists() throws Exception {

    String[] newUsers = {"1", "2", "3"};
    String groupName = "All_Users";
    Group group = new Group();
    String[] existingUsers = {"4", "5"};
    group.setUsers(existingUsers);
    group.setName(groupName);
    doReturn(group).when(mockAllGroups).getGroupByName(groupName);
    spyCommcareGateway.createOrUpdateGroup(groupName, newUsers);
    ArgumentCaptor<Group> groupCaptor = ArgumentCaptor.forClass(Group.class);
    verify(mockAllGroups).update(groupCaptor.capture());

    assertThat(groupCaptor.getValue().getUsers(), is(ArrayUtils.addAll(existingUsers, newUsers)));
    assertThat(groupCaptor.getValue().getName(), is(groupName));
  }
  @Test
  public void shouldSubmitUpdateOwnerFormForGroupOwnershipRequest() {
    CaseOwnershipInformation caseOwnershipInformation =
        getCaseOwnershipInformation(
            UUID.randomUUID().toString(), null, "f98589102c60fcc2e0f3c422bb361ebd", "cg1", null);
    String currentOwnerId = caseOwnershipInformation.getUserId();
    model.put(CommcareGateway.CASE_OWNERSHIP_FORM_KEY, caseOwnershipInformation);
    doReturn(getExpectedBeneficiaryCaseXml())
        .when(spyCommcareGateway)
        .getXmlFromObject(eq(OWNER_UPDATE_FORM_TEMPLATE_PATH), eq(model));
    String someGroup = "someGroup";
    Group group = new Group();
    group.setId("somegroupId");
    doReturn(group).when(mockAllGroups).getGroupByName(someGroup);
    ArgumentCaptor<CaseOwnershipInformation> captor =
        ArgumentCaptor.forClass(CaseOwnershipInformation.class);

    spyCommcareGateway.addGroupOwnership(caseOwnershipInformation, someGroup);

    verify(spyCommcareGateway).postOwnerUpdate(captor.capture());
    verify(mockHttpClientService).post(someUrl, getExpectedBeneficiaryCaseXml());

    assertThat(captor.getValue().getOwnerId(), is(currentOwnerId + "," + group.getId()));
  }