@Test @Transactional public void testRemoveContactFromEventMultipleGroups() 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); event = eventService.findById(event.getId()); contactService.attendEvent(newContact, event); newContact = contactService.findById(newContact.getId()); contactService.unattendEvent(newContact, event); event = eventService.findById(event.getId()); assertFalse(event.getAttendees().contains(newContact)); newContact = contactService.findById(newContact.getId()); assertFalse(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 @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)); }
@RequestMapping("/contacts") public DataPageDto find(ContactFilterDto filterDto) { ContactFilter filter = convert(filterDto); DataPageDto<ContactDto> page = new DataPageDto<>(); List<ContactDto> contactDtos = contactService.findContacts(filter); page.setData(contactDtos); page.setTotalCount(contactService.countContacts(filter)); return page; }
@RequestMapping(value = "/contacts/email/{email}", method = GET) public String getContactByEmail(@PathVariable String email) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true); ContactDto contact = contactService.getByEmail(email); return mapper.writeValueAsString(contact); }
@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()); }
@After public void tearDown() { groupService.deleteAll(); organizationService.deleteAll(); committeeService.deleteAll(); eventService.deleteAll(); contactService.deleteAll(); }
@Test public void shouldPassContactFormToEmailServiceWhenNoErrors() throws Exception { ContactForm contactForm = contactForm(); contactService.processContactForm(contactForm, bindingResult); verify(contactMailService).sendEmail(contactForm); }
@Test public void shouldReturnTwoFieldErrors() throws Exception { givenThatFieldErrorsArePresent(); ContactResult contactResult = contactService.processContactForm(contactForm(), bindingResult); assertThat(contactResult.getErrors()).hasSize(2); }
@Test public void shouldHaveErrorsWhenBindingResultHasFieldErrors() throws Exception { givenThatFieldErrorsArePresent(); ContactResult contactResult = contactService.processContactForm(contactForm(), bindingResult); assertThat(contactResult.isHasErrors()).isTrue(); }
@Test public void shouldHaveNoErrorsWhenBindingResultHasNoFieldErrors() throws Exception { given(bindingResult.hasFieldErrors()).willReturn(false); ContactResult contactResult = contactService.processContactForm(contactForm(), bindingResult); assertThat(contactResult.isHasErrors()).isFalse(); }
@Test public void shouldReturnFailureResult() throws Exception { givenThatFieldErrorsArePresent(); ContactResult contactResult = contactService.processContactForm(contactForm(), bindingResult); assertThat(contactResult.isSuccess()).isFalse(); }
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); }
@RequestMapping( value = "/contacts/{contactId}/social_networks/{socialNetworkId}", method = DELETE) @PreAuthorize("hasPermission(#contactId, 'sample.Contact', 'DELETE')") public void deleteSocialNetworkAccount( @PathVariable Long contactId, @PathVariable Long socialNetworkId) { contactService.deleteSocialNetworkAccount(socialNetworkId); }
@Test public void shouldReturnSuccessfulResult() throws Exception { given(bindingResult.hasFieldErrors()).willReturn(false); ContactResult contactResult = contactService.processContactForm(contactForm(), bindingResult); assertThat(contactResult.isSuccess()).isTrue(); }
@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()); }
@RequestMapping(value = "/contacts/{contactId}/educations/{educationId}", method = DELETE) @PreAuthorize("hasPermission(#contactId, 'sample.Contact', 'DELETE')") public void deleteEducationInfo(@PathVariable Long contactId, @PathVariable Long educationId) { logger.debug( "deleting info about universities education for contact {}, education id {}", contactId, educationId); contactService.deleteUniversityEducation(educationId); }
@Test public void shouldReturnAppropriateMessagesForValidationErrors() throws Exception { givenThatFieldErrorsArePresent(); ContactResult contactResult = contactService.processContactForm(contactForm(), bindingResult); assertThat(contactResult.getErrors()) .contains("In your face.", "You gone did something wrong."); }
@Test public void shouldNotPassContactFormToEmailServiceWhenErrors() throws Exception { ContactForm contactForm = contactForm(); givenThatFieldErrorsArePresent(); contactService.processContactForm(contactForm, bindingResult); verify(contactMailService, never()).sendEmail(contactForm); }
@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 public void shouldHandleMailServerExceptionAndReturnUnsuccessfulWithErrorMessage() throws Exception { doThrow(new RuntimeException()).when(contactMailService).sendEmail(any(ContactForm.class)); ContactResult contactResult = contactService.processContactForm(contactForm(), bindingResult); assertThat(contactResult.isHasErrors()).isTrue(); assertThat(contactResult.isSuccess()).isFalse(); assertThat(contactResult.getErrors()) .containsOnly("Uh oh! Something went wrong, please try again."); }
@Before public void setup() throws ConstraintViolation, NullDomainReference { createContacts(); committee = new Committee(); committee.setName("Committee Name"); committeeService.create(committee); contactService.addContactToCommittee(first, committee); contactService.addContactToCommittee(second, committee); organization = new Organization(); organization.setName("Organization Name"); organizationService.create(organization); contactService.addContactToOrganization(first, organization); contactService.addContactToOrganization(second, organization); event = new Event(); event.setName("Event Name"); event.setDateHeld("2015-01-01"); eventService.create(event); contactService.attendEvent(first, event); contactService.attendEvent(second, event); group = new Group(); group.setGroupName("New AlinskyGroup"); }
private void generateContacts() { Contact contact1 = new Contact(); contact1.setFirstName("Al"); contact1.setLastName("Bundy"); contact1.setAddress(new Address(9764, "Jeopardy Ln", "Chicago", "IL", 60290)); addressService.saveAddress(contact1.getAddress()); contactService.saveContact(contact1); Contact contact2 = new Contact(); contact2.setFirstName("Ned"); contact2.setLastName("Flanders"); contact2.setAddress(new Address(740, "Evergreen Terrace", "Springfield", "OR", 97477)); addressService.saveAddress(contact2.getAddress()); contactService.saveContact(contact2); Contact contact3 = new Contact(); contact3.setFirstName("Peter"); contact3.setLastName("Griffin"); contact3.setAddress(new Address(31, "Spooner St", "Quahog", "RI", 12908)); addressService.saveAddress(contact3.getAddress()); contactService.saveContact(contact3); }
@Test public void testRemoveContactFromAggregation() throws Exception { groupService.create(group); groupService.addAggregation(committee, group); group = groupService.findById(group.getId()); assertEquals( committee.getMembers().size(), group.getAggregations().iterator().next().getAggregationMembers().size()); contactService.removeContactFromCommittee(first, committee); committee = committeeService.findById(committee.getId()); group = groupService.findById(group.getId()); assertEquals( committee.getMembers().size(), group.getAggregations().iterator().next().getAggregationMembers().size()); }
@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)); }
@Test public void testCreateGroupFromMultipleAggregations() throws Exception { String id = groupService.create(group); groupService.addAggregation(committee, group); group = groupService.findById(group.getId()); groupService.addAggregation(organization, group); group = groupService.findById(group.getId()); groupService.addAggregation(event, group); group = groupService.findById(group.getId()); contactService.addToGroup(topLevel, group); Group fromDb = groupService.findById(id); assertEquals(group.getGroupName(), fromDb.getGroupName()); assertEquals(3, group.getAggregations().size()); Set<Contact> allAggregationMembers = new HashSet<>(); for (Aggregation aggregation : group.getAggregations()) { for (Contact c : aggregation.getAggregationMembers()) { allAggregationMembers.add(c); } } assertEquals(2, allAggregationMembers.size()); assertEquals(1, group.getTopLevelMembers().size()); }
@RequestMapping(value = "/contacts/{contactId}", method = DELETE) @PreAuthorize("hasPermission(#contactId, 'sample.Contact', 'DELETE')") public void delete(@PathVariable Long contactId) { super.deleteAcl(contactId); contactService.deleteById(contactId); }
@RequestMapping(value = "/contacts", method = POST) public Long create(@RequestBody ContactDto dto) { Long contactId = contactService.saveContact(dto); super.createAcl(contactId); return contactId; }
@PreAuthorize("hasPermission(#dto.getId(), 'sample.Contact', 'WRITE')") @RequestMapping(value = "/contacts", method = PUT) public void update(@RequestBody ContactDto dto) { contactService.updateContact(dto); }
@RequestMapping(value = "/contacts/nationalities", method = GET) public List<NationalityDto> getNationalities() { logger.debug("GETTING NATIONALITIES"); return contactService.getNationalities(); }