diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 138c2fd..6279664 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,14 +4,15 @@ spring.h2.console.enabled=true # ---Turn Statistics on spring.jpa.properties.hibernate.generate_statistics=true logging.level.org.hibernate.stat=debug +logging.level.org.hibernate.type=debug # ---Show queries -spring.jpa.show-sql=true +# spring.jpa.show-sql=true +# spring.jpa.properties.hibernate.format_sql=true # ---In memory DB spring.datasource.url=jdbc:h2:mem:contactsdb spring.data.jpa.repositories.bootstrap-mode=default -spring.jpa.properties.hibernate.format_sql=true -# ---Initialize Hibernate before loading from data.sql -spring.jpa.defer-datasource-initialization=true -logging.level.org.hibernate.type=debug +# ---Uncomment the next properties to load demo data from file +# spring.jpa.defer-datasource-initialization=true +# spring.sql.init.data-locations=classpath:demo.sql # ---For development only # server.error.include-message=always diff --git a/src/main/resources/data.sql b/src/main/resources/demo.sql similarity index 100% rename from src/main/resources/data.sql rename to src/main/resources/demo.sql diff --git a/src/test/java/com/singlestone/contacts/controller/ContactControllerTest.java b/src/test/java/com/singlestone/contacts/controller/ContactControllerTest.java index 457cae7..0e41777 100644 --- a/src/test/java/com/singlestone/contacts/controller/ContactControllerTest.java +++ b/src/test/java/com/singlestone/contacts/controller/ContactControllerTest.java @@ -1,6 +1,7 @@ package com.singlestone.contacts.controller; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -10,8 +11,10 @@ import java.util.List; import com.fasterxml.jackson.databind.ObjectMapper; import com.singlestone.contacts.model.Address; +import com.singlestone.contacts.model.Contact; import com.singlestone.contacts.model.Name; import com.singlestone.contacts.model.Phone.Type; +import com.singlestone.contacts.model.dto.CallListDTO; import com.singlestone.contacts.model.dto.ContactDTO; import com.singlestone.contacts.model.dto.PhoneDTO; import com.singlestone.contacts.service.ContactService; @@ -96,23 +99,104 @@ public class ContactControllerTest { } @Test - void testGetContact() { + void testGetAllContactsNoContactsAvailable() throws Exception { + List contacts = new ArrayList<>(); + when(contactService.getAllContacts()).thenReturn(contacts); + mockMvc.perform(get("/api/v1/contacts"). + accept(MediaType.APPLICATION_JSON). + content("")). + andExpect(status().isNoContent()); + verify(contactService, times(1)).getAllContacts(); + } + @Test + void testGetContact() throws Exception { + ContactDTO contactToGet = createTestContactDTO(); + when(contactService.getContact(anyLong())).thenReturn(contactToGet); + mockMvc.perform(get("/api/v1/contacts/1"). + accept(MediaType.APPLICATION_JSON). + content("")). + andExpect(status().isOk()); + verify(contactService, times(1)).getContact(anyLong()); + } + + @Test + void testGetContactDoesNotExist() throws Exception { + when(contactService.getContact(anyLong())).thenReturn(null); + mockMvc.perform(get("/api/v1/contacts/1"). + accept(MediaType.APPLICATION_JSON). + content("")). + andExpect(status().isNotFound()); + verify(contactService, times(1)).getContact(anyLong()); } @Test - void testUpdateContact() { + void testUpdateContact() throws Exception { + ContactDTO contactToUpdate = createTestContactDTO(); + when(contactService.updateContact(anyLong(), any())).thenReturn(contactToUpdate); + mockMvc.perform(put("/api/v1/contacts/1"). + contentType(MediaType.APPLICATION_JSON). + content(asJsonString(contactToUpdate))). + andExpect(status().isOk()); + verify(contactService, times(1)).updateContact(anyLong(), any()); + } + @Test + void testUpdateContactDoesNotExist() throws Exception { + ContactDTO contact = createTestContactDTO(); + when(contactService.updateContact(anyLong(), any())).thenReturn(null); + mockMvc.perform(put("/api/v1/contacts/1"). + contentType(MediaType.APPLICATION_JSON). + content(asJsonString(contact))). + andExpect(status().isNotFound()); + verify(contactService, times(1)).updateContact(anyLong(), any()); } @Test - void testDeleteContact() { + void testDeleteContact() throws Exception { + ContactDTO contactToDelete = createTestContactDTO(); + when(contactService.deleteContact(anyLong())).thenReturn(contactToDelete); + mockMvc.perform(delete("/api/v1/contacts/1"). + accept(MediaType.APPLICATION_JSON). + content("")). + andExpect(status().isNoContent()); + verify(contactService, times(1)).deleteContact(anyLong()); + } + @Test + void testDeleteContactDoesNotExist() throws Exception { + when(contactService.deleteContact(anyLong())).thenReturn(null); + mockMvc.perform(delete("/api/v1/contacts/1"). + accept(MediaType.APPLICATION_JSON). + content("")). + andExpect(status().isNotFound()); + verify(contactService, times(1)).deleteContact(anyLong()); } @Test - void testGetCallList() { + void testGetCallList() throws Exception { + Contact contact = new Contact(createTestContactDTO()); + contact.getPhone().get(0).setContact(contact); + CallListDTO contactToreturn = new CallListDTO(contact.getPhone().get(0)); + List contacts = new ArrayList<>(); + contacts.add(contactToreturn); + when(contactService.getCallList()).thenReturn(contacts); + mockMvc.perform(get("/api/v1/contacts/call-list"). + accept(MediaType.APPLICATION_JSON). + content("")). + andExpect(status().isOk()); + verify(contactService, times(1)).getCallList(); + } + @Test + void testGetCallListIsEmpty() throws Exception { + List contacts = new ArrayList<>(); + when(contactService.getCallList()).thenReturn(contacts); + mockMvc.perform(get("/api/v1/contacts/call-list"). + accept(MediaType.APPLICATION_JSON). + content("")). + andExpect(status().isNoContent()); + verify(contactService, times(1)).getCallList(); } public static String asJsonString(final Object obj){ diff --git a/src/test/java/com/singlestone/contacts/resources/application.properties b/src/test/java/com/singlestone/contacts/resources/application.properties deleted file mode 100644 index 138c2fd..0000000 --- a/src/test/java/com/singlestone/contacts/resources/application.properties +++ /dev/null @@ -1,17 +0,0 @@ -# ---Comments without preceeding '---' are settings to enable for development -# ---Enable H2 console -spring.h2.console.enabled=true -# ---Turn Statistics on -spring.jpa.properties.hibernate.generate_statistics=true -logging.level.org.hibernate.stat=debug -# ---Show queries -spring.jpa.show-sql=true -# ---In memory DB -spring.datasource.url=jdbc:h2:mem:contactsdb -spring.data.jpa.repositories.bootstrap-mode=default -spring.jpa.properties.hibernate.format_sql=true -# ---Initialize Hibernate before loading from data.sql -spring.jpa.defer-datasource-initialization=true -logging.level.org.hibernate.type=debug -# ---For development only -# server.error.include-message=always diff --git a/src/test/java/com/singlestone/contacts/resources/data.sql b/src/test/java/com/singlestone/contacts/resources/data.sql deleted file mode 100644 index 1e8ebdb..0000000 --- a/src/test/java/com/singlestone/contacts/resources/data.sql +++ /dev/null @@ -1,25 +0,0 @@ --- Contacts -insert into CONTACTS (street, city, state, zip, email, middle, first, last) -values('street1', 'city1', 'state1', 'zip1', 'e1@mail.com', 'a', 'a', 'a'); -insert into CONTACTS (street, city, state, zip, email, middle, first, last) -values('street2', 'city2', 'state2', 'zip2', 'e2@mail.com', 'a', 'b', 'a'); -insert into CONTACTS (street, city, state, zip, email, middle, first, last) -values('street3', 'city3', 'state3', 'zip3', 'e3@mail.com', 'a', 'b', 'b'); -insert into CONTACTS (street, city, state, zip, email, middle, first, last) -values('street4', 'city4', 'state4', 'zip4', 'e4@mail.com', 'a', 'a', 'b'); - --- Phones -insert into PHONES (number, type, contact_id) -values('111-111-1111', 'HOME', 1); -insert into PHONES (number, type, contact_id) -values('222-222-2222', 'WORK', 1); -insert into PHONES (number, type, contact_id) -values('333-333-3333', 'WORK', 2); -insert into PHONES (number, type, contact_id) -values('444-444-4444', 'HOME', 2); -insert into PHONES (number, type, contact_id) -values('555-555-5555', 'MOBILE', 3); -insert into PHONES (number, type, contact_id) -values('777-777-7777', 'WORK', 4); -insert into PHONES (number, type, contact_id) -values('888-888-8888', 'HOME', 4);