/** 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())); } }
/** 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); } }