@Test
  @Transactional
  public void testAddContactToEventMultipleGroups() throws Exception {

    groupService.create(group);
    groupService.addAggregation(event, group);

    Group secondGroup = new Group();
    secondGroup.setGroupName("Second Group");
    groupService.create(secondGroup);
    groupService.addAggregation(event, secondGroup);

    Contact newContact = new Contact();
    newContact.setFirstName("Fresh Contact");
    newContact.setEmail("Fresh email");
    contactService.create(newContact);

    contactService.attendEvent(newContact, event);

    event = eventService.findById(event.getId());
    assertTrue(event.getAttendees().contains(newContact));

    newContact = contactService.findById(newContact.getId());
    assertTrue(newContact.getAttendedEvents().contains(event));
  }
  @Test
  @Transactional
  public void testAddContactToOrganizationMultipleGroups() throws Exception {

    groupService.create(group);
    groupService.addAggregation(organization, group);

    Group secondGroup = new Group();
    secondGroup.setGroupName("Second Group");
    groupService.create(secondGroup);
    groupService.addAggregation(organization, secondGroup);

    Contact newContact = new Contact();
    newContact.setFirstName("Fresh Contact");
    newContact.setEmail("Fresh email");
    contactService.create(newContact);

    contactService.addContactToOrganization(newContact, organization);

    newContact = contactService.findById(newContact.getId());
    assertTrue(newContact.getOrganizations().contains(organization));

    group = groupService.findById(group.getId());
    assertTrue(group.getAggregations().contains(organization));

    secondGroup = groupService.findById(secondGroup.getId());
    assertTrue(secondGroup.getAggregations().contains(organization));

    organization = organizationService.findById(organization.getId());
    assertTrue(organization.getMembers().contains(newContact));
  }
  /**
   * Test of handleEvent method, of class Conversation. Tests both local and remote side
   *
   * @throws UnknownHostException
   * @throws InterruptedException
   * @throws IOException
   */
  @Test
  public void testHandleEvent() throws UnknownHostException, IOException, InterruptedException {
    // clear the converstion created in setUp()
    EventPool.getAppPool().removeListener(conversation);

    // get the local app-pool and let it start
    EventPool localPool = EventPool.getAppPool();
    Thread.sleep(100);

    // create a remote eventpool and connect it to the local one
    EventPool remotePool = new EventPool();
    Connection remoteConnection = new Connection(InetAddress.getLocalHost(), remotePool, null);
    Thread.sleep(100);

    // create a contact out of the local user (as seen from the other side)
    Account localAccount = ChatApplication.getInstance().getAccount();
    Contact localContact = new Contact(localAccount.getUsername(), InetAddress.getLocalHost());
    Contact remoteContact = new Contact("Jaspervdj", InetAddress.getLocalHost());

    // add both of these contacts to our list, so we can look them up later
    ContactList contactList = localAccount.getContactList();
    contactList.addContact(localContact);
    contactList.addContact(remoteContact);

    // conversation manager will contain the conversation with the remote user, as
    // well as the conversation with the local one
    ConversationManager conversationManager =
        ChatApplication.getInstance().getConversationManager();
    Conversation localConversation = conversationManager.startConversation(remoteContact, false);
    Conversation remoteConversation = conversationManager.startConversation(localContact, false);

    // register the remoteConversation specifically with the remote pool
    remotePool.addListener(remoteConversation, new ConversationEventFilter(remoteConversation));
    // remove remoteListener from localPool
    localPool.removeListener(remoteConversation);

    // now everything is setup, and a user can type a message
    ChatMessage chatMessage =
        new ChatMessage(localContact.getUsername(), "Dag Javache, jij jij remoteUser!");
    ConversationEvent localEvent = new NewChatMessageEvent(localConversation, chatMessage);

    // raise the event on the local pool, should get sent to remotePool too
    localPool.raiseNetworkEvent(localEvent);
    Thread.sleep(100);

    // let's check the results!
    assertEquals(1, remoteConversation.getChatMessages(10).length);
    assertEquals(
        "Dag Javache, jij jij remoteUser!", remoteConversation.getChatMessages(1)[0].getText());

    assertEquals(1, localConversation.getChatMessages(10).length);
    assertEquals(
        "Dag Javache, jij jij remoteUser!", localConversation.getChatMessages(1)[0].getText());
  }
  @Test
  public void testRemoveContactFromGroup() throws Exception {
    groupService.create(group);
    groupService.addAggregation(committee, group);
    contactService.addToGroup(topLevel, group);

    assertEquals(1, group.getTopLevelMembers().size());

    contactService.removeFromGroup(topLevel, group);

    Group fromDb = groupService.findById(group.getId());
    assertEquals(0, fromDb.getTopLevelMembers().size());

    Contact contactFromDb = contactService.findById(topLevel.getId());
    assertEquals(0, contactFromDb.getGroups().size());
  }
  @Test
  public void testDeleteGroup() throws Exception {
    groupService.create(group);
    groupService.addAggregation(committee, group);

    group = groupService.findById(group.getId());
    contactService.addToGroup(topLevel, group);

    groupService.delete(group);

    Group groupFromDb = groupService.findById(group.getId());
    assertNull(groupFromDb);
    Aggregation fromDb = committeeService.findById(committee.getId());
    assertNotNull(fromDb);
    assertEquals(0, fromDb.getGroups().size());
    assertEquals(committee.getAggregationMembers().size(), fromDb.getAggregationMembers().size());

    Contact topLevelFromDb = contactService.findById(topLevel.getId());
    assertNotNull(topLevelFromDb);
    assertEquals(0, topLevelFromDb.getGroups().size());
  }
  @Test
  @Transactional
  public void testAddContactToGroupAndGroupConstituent() throws Exception {

    groupService.create(group);
    groupService.addAggregation(committee, group);

    Contact contact = new Contact();
    contact.setFirstName("Test Contact");
    contact.setEmail("*****@*****.**");
    contactService.create(contact);

    contactService.addContactToCommittee(contact, committee);
    contactService.addToGroup(contact, group);

    contact = contactService.findById(contact.getId());
    assertTrue(contact.getGroups().contains(group));
    assertTrue(contact.getCommittees().contains(committee));

    committee = committeeService.findById(committee.getId());
    assertTrue(committee.getMembers().contains(contact));

    group = groupService.findById(group.getId());
    assertTrue(group.getTopLevelMembers().contains(contact));
  }
  @Test
  @Transactional
  public void testAddContactToMultipleGroupsMultipleConstituents() throws Exception {

    groupService.create(group);
    groupService.addAggregation(committee, group);
    groupService.addAggregation(event, group);

    Group secondGroup = new Group();
    secondGroup.setGroupName("Second Group");
    groupService.create(secondGroup);
    groupService.addAggregation(committee, secondGroup);
    groupService.addAggregation(event, secondGroup);

    Contact contact = new Contact();
    contact.setFirstName("Test Contact");
    contact.setEmail("*****@*****.**");
    contactService.create(contact);

    contactService.addContactToCommittee(contact, committee);
    contactService.attendEvent(contact, event);
    contactService.addToGroup(contact, group);
    contactService.addToGroup(contact, secondGroup);

    contact = contactService.findById(contact.getId());
    group = groupService.findById(group.getId());
    secondGroup = groupService.findById(secondGroup.getId());
    event = eventService.findById(event.getId());
    committee = committeeService.findById(committee.getId());

    assertTrue(contact.getGroups().contains(group));
    assertTrue(contact.getGroups().contains(secondGroup));
    assertTrue(contact.getCommittees().contains(committee));
    assertTrue(contact.getAttendedEvents().contains(event));

    assertTrue(event.getAttendees().contains(contact));
    assertTrue(event.getGroups().contains(group));
    assertTrue(event.getGroups().contains(secondGroup));

    assertTrue(committee.getMembers().contains(contact));
    assertTrue(committee.getGroups().contains(group));
    assertTrue(committee.getGroups().contains(secondGroup));

    assertTrue(group.getTopLevelMembers().contains(contact));
    assertTrue(group.getAggregations().contains(committee));
    assertTrue(group.getAggregations().contains(event));

    assertTrue(secondGroup.getTopLevelMembers().contains(contact));
    assertTrue(secondGroup.getAggregations().contains(committee));
    assertTrue(secondGroup.getAggregations().contains(event));
  }
  private void createContacts() throws ConstraintViolation {
    first = new Contact();
    first.setFirstName("First");
    first.setEmail("*****@*****.**");

    second = new Contact();
    second.setFirstName("Second");
    second.setEmail("*****@*****.**");

    topLevel = new Contact();
    topLevel.setFirstName("Top Level");
    topLevel.setEmail("*****@*****.**");

    contactService.create(first);
    contactService.create(second);
    contactService.create(topLevel);
  }
  /** Test for fetching zobjects when there is an object that matches the query */
  @Test
  public void getInvoice() throws Exception {

    // Setup Product details
    String productId = getTestProduct();
    String productRatePlanId = getTestProductRatePlan(productId);
    String productRateplanChargeId = getTestProductRatePlanCharge(productRatePlanId);

    assertNotNull(productId);
    assertNotNull(productRatePlanId);
    assertNotNull(productRateplanChargeId);

    SubscribeRequest subscribeReq = new SubscribeRequest();

    // subscribeReq.setAccount(testAccount());
    String uniqueString = UUID.randomUUID().toString();

    Contact contact = new Contact();
    contact.setFirstName(uniqueString);
    contact.setLastName(uniqueString);

    Account account = new Account();
    account.setName(uniqueString);
    account.setBillCycleDay(1);
    account.setCurrency("USD");
    account.setAllowInvoiceEdit(false);
    account.setAutoPay(false);
    account.setStatus("Draft");
    account.setPaymentTerm("Due Upon Receipt");
    account.setBatch("Batch1");

    PaymentMethod paymentMethod = new PaymentMethod();
    paymentMethod.setType("CreditCard");
    paymentMethod.setCreditCardNumber("5105105105105100");
    paymentMethod.setCreditCardType("Visa");
    paymentMethod.setCreditCardExpirationYear(2026);
    paymentMethod.setCreditCardExpirationMonth(5);
    paymentMethod.setCreditCardHolderName("Unit Test");

    // Generate Start and stop days for subscription
    XMLGregorianCalendar effectiveStartDate = null;
    XMLGregorianCalendar effectiveEndDate = null;
    try {
      GregorianCalendar calStart = new GregorianCalendar();
      // calStart.setTime(now);
      calStart.add(Calendar.DATE, -1);

      GregorianCalendar calEnd = new GregorianCalendar();
      // calEnd.setTime(now);
      calEnd.add(Calendar.DATE, 1);

      effectiveStartDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(calStart);
      effectiveEndDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(calStart);

    } catch (DatatypeConfigurationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    Subscription subscription = new Subscription();
    subscription.setContractAcceptanceDate(effectiveStartDate);
    subscription.setContractEffectiveDate(effectiveStartDate);
    subscription.setInitialTerm(12);
    subscription.setRenewalTerm(12);

    RatePlan ratePlan = new RatePlan();
    ratePlan.setProductRatePlanId(productRatePlanId);
    RatePlanData ratePlanData = new RatePlanData();
    ratePlanData.setRatePlan(ratePlan);

    SubscriptionData subscriptionData = new SubscriptionData();
    subscriptionData.setSubscription(subscription);
    subscriptionData.getRatePlanData().add(ratePlanData);

    subscribeReq.setAccount(account);
    subscribeReq.setBillToContact(contact);
    subscribeReq.setSoldToContact(contact);
    subscribeReq.setPaymentMethod(paymentMethod);
    subscribeReq.setSubscriptionData(subscriptionData);

    SubscribeResult subscribeResult =
        module.subscribe(Collections.singletonList(subscribeReq)).get(0);
    assertTrue(subscribeResult.isSuccess());
    assertEquals(0, subscribeResult.getErrors().size());

    Map<String, Object> result = module.getInvoice(subscribeResult.getInvoiceId());

    System.out.println("Result = " + result);

    assertEquals("Posted", result.get("status"));
    // assertEquals("amount",result.get("amount"));
    assertNotSame(0, ((ArrayList) result.get("invoiceitems")).size());

    assertNotNull(result.get("billTo"));
    assertNotNull(result.get("soldTo"));

    DeleteResult deleteResultAccount = null;
    DeleteResult deleteResultProduct = null;
    try {
      deleteResultAccount =
          module
              .delete(
                  ZObjectType.Account, Collections.singletonList(subscribeResult.getAccountId()))
              .get(0);
      deleteResultProduct =
          module.delete(ZObjectType.Product, Collections.singletonList(productId)).get(0);
    } catch (Exception e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
    assertTrue(deleteResultAccount.isSuccess());
    assertTrue(deleteResultProduct.isSuccess());
  }
  @Test
  public void testAddMultipleContacts() throws Exception {
    groupService.create(group);
    groupService.addAggregation(committee, group);

    group = groupService.findById(group.getId());
    contactService.addToGroup(topLevel, group);

    Contact anotherContact = new Contact();
    anotherContact.setFirstName("Another");
    anotherContact.setEmail("*****@*****.**");
    contactService.create(anotherContact);

    contactService.addToGroup(anotherContact, group);
    group = groupService.findById(group.getId());
    assertEquals(2, group.getTopLevelMembers().size());

    anotherContact = contactService.findById(anotherContact.getId());
    topLevel = contactService.findById(topLevel.getId());

    assertEquals(1, anotherContact.getGroups().size());
    assertEquals(1, topLevel.getGroups().size());

    groupService.delete(group);

    anotherContact = contactService.findById(anotherContact.getId());
    topLevel = contactService.findById(topLevel.getId());

    assertEquals(0, anotherContact.getGroups().size());
    assertEquals(0, topLevel.getGroups().size());
  }
 @Test
 public void newContact_instantiatesCorrectly() {
   Contact newContact = new Contact("Jane Doe");
   assertEquals("Jane Doe", newContact.getName());
 }