/** Tests if all of an owner's properties can be updated. */
  @Test
  public void updateOwnerTest() throws CompanyServiceValidationException {
    final String NEWFIRSTNAME = "newfirst";
    final String NEWLASTNAME = "newlast";
    final int NUM = 2;

    // persist a company with NUM owners
    Company company = persist(construct(1, NUM));

    // set new values
    for (int i = 0; i < company.getOwners().size(); i++) {
      Owner o = company.getOwners().get(i);
      o.setFirstName(NEWFIRSTNAME);
      o.setLastName(NEWLASTNAME);
    }

    // update the company (and owners)
    Company updated = companyService.save(company);
    assertEquals(company.getOwners().size(), updated.getOwners().size());

    // check
    for (int i = 0; i < updated.getOwners().size(); i++) {
      Owner uOwner = updated.getOwners().get(i);
      Owner cOwner = company.getOwners().get(i);

      assertTrue(uOwner.getFirstName().equals(cOwner.getFirstName()));
      assertTrue(uOwner.getLastName().equals(cOwner.getLastName()));
    }
  }
  /**
   * Persists some companies with owners and verifies if the owners of the companies are listed or
   * not.
   */
  @Test
  public void listOwnersTest() {
    final int NUM = 3;

    // persist NUM companies with NUM owners
    List<Company> persisted = persist(NUM, NUM);
    assertTrue(companyService.list().size() == NUM);

    // check the size
    for (Company c : persisted) {
      assertTrue(c.getOwners().size() == NUM);
    }
  }
  /** Tests if an owner can be removed from the company or not. */
  @Test
  public void deleteOwnerTest() throws CompanyServiceValidationException {
    final int NUM = 2;

    // persist 1 company with NUM owners
    Company company = persist(construct(1, NUM));

    // remove the last owner from the list
    company.getOwners().remove(NUM - 1);

    // save and check
    Company updated = companyService.save(company);
    assertEquals(updated.getOwners().size(), company.getOwners().size());
  }
  /** Tests the persistence of owners when a new company gets saved. */
  @Test
  public void createOwnersTest() throws CompanyServiceValidationException {
    final int NUM = 2;

    // save a new company with NUM owners and get it's id
    Long id = companyService.save(construct(1, NUM)).getId();

    // find by id
    Company found = companyRepository.findOne(id);

    // asserts
    assertNotNull(found);
    assertTrue(found.getOwners().size() == NUM);
  }
  /** Tests if owner's properties gets saved properly. */
  @Test
  public void ownerDetailsTest() {
    final int NUM = 3;
    // save a company with NUM owners
    Company persisted = persist(construct(1, NUM));
    Company found = companyService.details(persisted.getId());
    assertTrue(persisted.getOwners().size() == found.getOwners().size());

    // compare the previously returned and the found owners' properties
    for (int i = 0; i < NUM; i++) {
      Owner persistedOwner = persisted.getOwners().get(i);
      Owner foundOwner = found.getOwners().get(i);

      // asserts
      assertTrue(persistedOwner.getFirstName().equals(foundOwner.getFirstName()));
      assertTrue(persistedOwner.getLastName().equals(foundOwner.getLastName()));
    }
  }
  /** Tests if exception is thrown when we try to save without mandatory properties */
  @Test
  public void createInvalidTest() throws CompanyServiceValidationException {
    Company company = new Company();
    Owner o = new Owner();
    o.setCompany(company);

    // test missing companyid
    try {
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), MISSING_FIELD);
      assertEquals(e.getInfo(), COMPANYID.toString());
    }

    // test missing address
    try {
      company.setCompanyId("1");
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), MISSING_FIELD);
      assertEquals(e.getInfo(), ADDRESS.toString());
    }

    // test missing city
    try {
      company.setAddress(PREFIX_ADDRESS);
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), MISSING_FIELD);
      assertEquals(e.getInfo(), CITY.toString());
    }

    // test missing country
    try {
      company.setCity(PREFIX_CITY);
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), MISSING_FIELD);
      assertEquals(e.getInfo(), COUNTRY.toString());
    }

    // test missing name
    try {
      company.setCountry(PREFIX_COUNTRY);
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), MISSING_FIELD);
      assertEquals(e.getInfo(), NAME.toString());
    }

    // test if at least 1 owner is added
    try {
      company.setName(PREFIX_NAME);
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), MISSING_OWNER);
    }

    // test missing owner firstname
    try {
      company.getOwners().add(o);
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), MISSING_FIELD);
      assertEquals(e.getInfo(), OWNER_FIRSTNAME.toString());
    }

    // test missing owner lastname
    try {
      o.setFirstName(PREFIX_FIRSTNAME);
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), MISSING_FIELD);
      assertEquals(e.getInfo(), OWNER_LASTNAME.toString());
    }

    // just save
    o.setLastName(PREFIX_LASTNAME);
    companyService.save(company);
    final String INVALID = "invalid";

    // test invalid phone
    try {
      company.setPhone(INVALID);
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), INVALID_PHONE_NUMBER);
    }

    // test invalid email
    try {
      company.setPhone(null);
      company.setEmail(INVALID);
      companyService.save(company);
    } catch (CompanyServiceValidationException e) {
      assertEquals(e.getError(), INVALID_EMAIL);
    }
  }
 /** Tests if a persisted company can be deleted by id. */
 @Test
 public void deleteTest() {
   Company company = persist();
   companyService.delete(company.getId());
   assertNull(companyRepository.findOne(company.getId()));
 }
  /** Tests if all of a company's properties can be updated. */
  @Test
  public void updateTest() throws CompanyServiceValidationException {
    final String NEWADDRESS = "newaddress";
    final String NEWCOUNTRY = "newcountry";
    final String NEWCITY = "newcity";
    final String NEWNAME = "newname";
    final String NEWEMAIL = "newmail" + SUFFIX_EMAIL;
    final String NEWPHONE = "+361111111111";

    // persist a new company, then set new values
    Company company = persist();
    company.setAddress(NEWADDRESS);
    company.setCountry(NEWCOUNTRY);
    company.setCity(NEWCITY);
    company.setName(NEWNAME);
    company.setEmail(NEWEMAIL);
    company.setPhone(NEWPHONE);

    // merge the company with new values
    Company updated = companyService.save(company);

    // asserts
    assertTrue(updated.getAddress().equals(company.getAddress()));
    assertTrue(updated.getCity().equals(company.getCity()));
    assertTrue(updated.getCompanyId().equals(company.getCompanyId()));
    assertTrue(updated.getCountry().equals(company.getCountry()));
    assertTrue(updated.getName().equals(company.getName()));
    assertTrue(updated.getEmail().equals(company.getEmail()));
    assertTrue(updated.getPhone().equals(company.getPhone()));
  }
  /** Test if a companies' properties gets saved. */
  @Test
  public void detailsTest() {
    // persist and get details
    Company persisted = persist(construct());
    Company company = companyService.details(persisted.getId());

    // asserts
    assertTrue(persisted.getAddress().equals(company.getAddress()));
    assertTrue(persisted.getCity().equals(company.getCity()));
    assertTrue(persisted.getCompanyId().equals(company.getCompanyId()));
    assertTrue(persisted.getCountry().equals(company.getCountry()));
    assertTrue(persisted.getName().equals(company.getName()));
    assertTrue(persisted.getEmail().equals(company.getEmail()));
    assertTrue(persisted.getPhone().equals(company.getPhone()));
  }