From 172e2c8a331fac4bac0051f42720ff1f38702c6a Mon Sep 17 00:00:00 2001 From: Lauren Emick Date: Wed, 19 Aug 2020 11:50:55 -0700 Subject: [PATCH 1/5] uploaded --- usermodel/pom.xml | 25 +-- .../com/lambdaschool/usermodel/SeedData.java | 147 ++++++++++++++++++ 2 files changed, 163 insertions(+), 9 deletions(-) create mode 100755 usermodel/src/test/java/com/lambdaschool/usermodel/SeedData.java diff --git a/usermodel/pom.xml b/usermodel/pom.xml index eedeb7e..0ac2699 100644 --- a/usermodel/pom.xml +++ b/usermodel/pom.xml @@ -76,15 +76,22 @@ 2.9.2 - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + io.rest-assured + spring-mock-mvc + 3.3.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/usermodel/src/test/java/com/lambdaschool/usermodel/SeedData.java b/usermodel/src/test/java/com/lambdaschool/usermodel/SeedData.java new file mode 100755 index 0000000..33da82a --- /dev/null +++ b/usermodel/src/test/java/com/lambdaschool/usermodel/SeedData.java @@ -0,0 +1,147 @@ +package com.lambdaschool.usermodel; + +import com.github.javafaker.Faker; +import com.github.javafaker.service.FakeValuesService; +import com.github.javafaker.service.RandomService; +import com.lambdaschool.usermodel.models.Role; +import com.lambdaschool.usermodel.models.User; +import com.lambdaschool.usermodel.models.UserRoles; +import com.lambdaschool.usermodel.models.Useremail; +import com.lambdaschool.usermodel.services.RoleService; +import com.lambdaschool.usermodel.services.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Locale; + +/** + * SeedData puts both known and random data into the database. It implements CommandLineRunner. + *

+ * CoomandLineRunner: Spring Boot automatically runs the run method once and only once + * after the application context has been loaded. + */ +@Transactional +@Component +public class SeedData implements CommandLineRunner +{ + /** + * Connects the Role Service to this process + */ + @Autowired + RoleService roleService; + + /** + * Connects the user service to this process + */ + @Autowired + UserService userService; + + /** + * Generates test, seed data for our application + * First a set of known data is seeded into our database. + * Second a random set of data using Java Faker is seeded into our database. + * Note this process does not remove data from the database. So if data exists in the database + * prior to running this process, that data remains in the database. + * + * @param args The parameter is required by the parent interface but is not used in this process. + */ + @Transactional + @Override + public void run(String[] args) throws Exception + { + userService.deleteAll(); + roleService.deleteAll(); + Role r1 = new Role("admin"); + Role r2 = new Role("user"); + Role r3 = new Role("data"); + + r1 = roleService.save(r1); + r2 = roleService.save(r2); + r3 = roleService.save(r3); + + // admin, data, user + User u1 = new User("admin", + "password", + "admin@lambdaschool.local"); + u1.getRoles().add(new UserRoles(u1, r1)); + u1.getRoles().add(new UserRoles(u1, r2)); + u1.getRoles().add(new UserRoles(u1, r3)); + u1.getUseremails() + .add(new Useremail(u1, + "admin@email.local")); + u1.getUseremails() + .add(new Useremail(u1, + "admin@mymail.local")); + + userService.save(u1); + + // data, user + User u2 = new User("cinnamon", + "1234567", + "cinnamon@lambdaschool.local"); + u2.getRoles().add(new UserRoles(u2, r2)); + u2.getRoles().add(new UserRoles(u2, r3)); + u2.getUseremails() + .add(new Useremail(u2, + "cinnamon@mymail.local")); + u2.getUseremails() + .add(new Useremail(u2, + "hops@mymail.local")); + u2.getUseremails() + .add(new Useremail(u2, + "bunny@email.local")); + userService.save(u2); + + // user + User u3 = new User("barnbarn", + "ILuvM4th!", + "barnbarn@lambdaschool.local"); + u3.getRoles().add(new UserRoles(u3, r2)); + u3.getUseremails() + .add(new Useremail(u3, + "barnbarn@email.local")); + userService.save(u3); + + User u4 = new User("puttat", + "password", + "puttat@school.lambda"); + u4.getRoles().add(new UserRoles(u4, r2)); + userService.save(u4); + + User u5 = new User("misskitty", + "password", + "misskitty@school.lambda"); + u5.getRoles().add(new UserRoles(u5, r2)); + userService.save(u5); + + if (false) + { + // using JavaFaker create a bunch of regular users + // https://www.baeldung.com/java-faker + // https://www.baeldung.com/regular-expressions-java + + FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-US"), + new RandomService()); + Faker nameFaker = new Faker(new Locale("en-US")); + + for (int i = 0; i < 25; i++) + { + new User(); + User fakeUser; + + fakeUser = new User(nameFaker.name() + .username(), + "password", + nameFaker.internet() + .emailAddress()); + fakeUser.getRoles().add(new UserRoles(fakeUser, r2)); + fakeUser.getUseremails() + .add(new Useremail(fakeUser, + fakeValuesService.bothify("????##@gmail.com"))); + userService.save(fakeUser); + } + } + } +} \ No newline at end of file From 46dec165952d03f3030e1fe16c992519671bd547 Mon Sep 17 00:00:00 2001 From: Lauren Emick Date: Wed, 19 Aug 2020 17:22:21 -0700 Subject: [PATCH 2/5] 6 tests pass for userserviceimpl --- .../com/lambdaschool/usermodel/SeedData.java | 10 +- .../usermodel/UserModelApplication.java | 47 ++------ .../services/UserServiceImplTest.java | 113 ++++++++++++++++++ .../src/test/resources/application.properties | 25 ++++ 4 files changed, 153 insertions(+), 42 deletions(-) create mode 100644 usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java create mode 100644 usermodel/src/test/resources/application.properties diff --git a/usermodel/src/test/java/com/lambdaschool/usermodel/SeedData.java b/usermodel/src/test/java/com/lambdaschool/usermodel/SeedData.java index 33da82a..ea3b626 100755 --- a/usermodel/src/test/java/com/lambdaschool/usermodel/SeedData.java +++ b/usermodel/src/test/java/com/lambdaschool/usermodel/SeedData.java @@ -62,7 +62,7 @@ public void run(String[] args) throws Exception r3 = roleService.save(r3); // admin, data, user - User u1 = new User("admin", + User u1 = new User("Test admin", "password", "admin@lambdaschool.local"); u1.getRoles().add(new UserRoles(u1, r1)); @@ -78,7 +78,7 @@ public void run(String[] args) throws Exception userService.save(u1); // data, user - User u2 = new User("cinnamon", + User u2 = new User("Test cinnamon", "1234567", "cinnamon@lambdaschool.local"); u2.getRoles().add(new UserRoles(u2, r2)); @@ -95,7 +95,7 @@ public void run(String[] args) throws Exception userService.save(u2); // user - User u3 = new User("barnbarn", + User u3 = new User("Test barnbarn", "ILuvM4th!", "barnbarn@lambdaschool.local"); u3.getRoles().add(new UserRoles(u3, r2)); @@ -104,13 +104,13 @@ public void run(String[] args) throws Exception "barnbarn@email.local")); userService.save(u3); - User u4 = new User("puttat", + User u4 = new User("Test puttat", "password", "puttat@school.lambda"); u4.getRoles().add(new UserRoles(u4, r2)); userService.save(u4); - User u5 = new User("misskitty", + User u5 = new User("Test misskitty", "password", "misskitty@school.lambda"); u5.getRoles().add(new UserRoles(u5, r2)); diff --git a/usermodel/src/test/java/com/lambdaschool/usermodel/UserModelApplication.java b/usermodel/src/test/java/com/lambdaschool/usermodel/UserModelApplication.java index ac24e33..3243624 100644 --- a/usermodel/src/test/java/com/lambdaschool/usermodel/UserModelApplication.java +++ b/usermodel/src/test/java/com/lambdaschool/usermodel/UserModelApplication.java @@ -1,52 +1,25 @@ package com.lambdaschool.usermodel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; -import org.springframework.core.env.Environment; -import org.springframework.web.servlet.DispatcherServlet; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; /** - * Starting class for testing + * Main class to start the application. */ -@EnableWebMvc -// @EnableJpaAuditing +//@EnableJpaAuditing @SpringBootApplication public class UserModelApplication { - private static final Logger logger = LoggerFactory.getLogger(com.lambdaschool.usermodel.UserModelApplication.class); - - private static boolean stop = false; - - @Autowired - private static Environment env; - - private static void checkEnvironmentVariable(String envvar) - { - if (System.getenv(envvar) == null) - { - logger.error("Environment Variable " + envvar + " missing"); - stop = true; - } - } + /** + * Main method to start the application. + * + * @param args Not used in this application. + */ public static void main(String[] args) { - checkEnvironmentVariable("OAUTHCLIENTID"); - checkEnvironmentVariable("OAUTHCLIENTSECRET"); - - if (!stop) - { - ApplicationContext ctx = SpringApplication.run(com.lambdaschool.usermodel.UserModelApplication.class, - args); - - DispatcherServlet dispatcherServlet = (DispatcherServlet) ctx.getBean("dispatcherServlet"); - dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); - } + SpringApplication.run(UserModelApplication.class, + args); } - } diff --git a/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java b/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java new file mode 100644 index 0000000..31fd96a --- /dev/null +++ b/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java @@ -0,0 +1,113 @@ +package com.lambdaschool.usermodel.services; + +import com.lambdaschool.usermodel.UserModelApplication; +import com.lambdaschool.usermodel.models.User; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.persistence.EntityNotFoundException; +import java.util.List; + +import static org.junit.Assert.*; + +// Service Impl using DB +@RunWith(SpringRunner.class) // tell junit we're doing a spring app +@SpringBootTest(classes = UserModelApplication.class)// where is our public static void main +public class UserServiceImplTest +{ + @Autowired + private UserService userService; + + @Before + public void setUp() throws Exception + { + MockitoAnnotations.initMocks(this); // initialize mockito (systems will be using) to work with this class + +// List myList = userService.findAll(); +// for (User u : myList) +// { +// System.out.println(u.getUserid() + "" + u.getUsername()); +// } + } + + @After + public void tearDown() throws Exception + { + } + + @Test + public void findUserById() + { + assertEquals("test cinnamon", userService.findUserById(7).getUsername()); + } + + @Test(expected = EntityNotFoundException.class) + public void findUserByIdNotFound() + { + assertEquals("test cinnamon", userService.findUserById(100).getUsername()); + } + + @Test + public void findByNameContaining() + { + assertEquals(1, userService.findByNameContaining("cin").size()); + } + + @Test + public void findAll() + { + assertEquals(5, userService.findAll().size()); + } + + @Test + public void delete() + { + } + + @Test + public void findByName() + { + assertEquals("test cinnamon", userService.findByName("test cinnamon").getUsername()); + } + + @Test(expected = EntityNotFoundException.class) + public void findByNameNotFound() + { + assertEquals("test frank", userService.findByName("test frank").getUsername()); + } + + @Test + public void saveadd() + { + // create user obj + String user2username = "Test Lauren"; + User u2 = new User(user2username, + "password", + "lauren@school.lambda"); + + // create user + + } + + @Test + public void saveput() + { + } + + @Test + public void update() + { + } + + @Test + public void deleteAll() + { + } +} \ No newline at end of file diff --git a/usermodel/src/test/resources/application.properties b/usermodel/src/test/resources/application.properties new file mode 100644 index 0000000..9758fe0 --- /dev/null +++ b/usermodel/src/test/resources/application.properties @@ -0,0 +1,25 @@ +# Configurations useful for working with H2 +spring.h2.console.enabled=true +spring.h2.console.path=/h2-console +# +# We set a port that is not frequently used +server.port=${PORT:2019} +# +# Feature that determines what happens when no accessors are found for a type +# (and there are no annotations to indicate it is meant to be serialized). +spring.jackson.serialization.fail-on-empty-beans=false +# +# keeps a transaction inside of the same entity manager +# This property register an EntityManager to the current thread, +# so you will have the same EntityManager until the web request is finished. +spring.jpa.open-in-view=true +# +# What do with the schema +# drop n create table again, good for testing +spring.jpa.hibernate.ddl-auto=create +spring.datasource.initialization-mode=always +# +# Good for production! +# spring.jpa.hibernate.ddl-auto=update +# since we have our data in SeedData, do not also load it from data.sql +# spring.datasource.initialization-mode=never From d2886f50551c18b781affdcb50756c24b83dab9e Mon Sep 17 00:00:00 2001 From: Lauren Emick Date: Thu, 20 Aug 2020 08:20:23 -0700 Subject: [PATCH 3/5] userserviceimpl done --- .../services/UserServiceImplTest.java | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java b/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java index 31fd96a..5db4caf 100644 --- a/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java +++ b/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java @@ -1,12 +1,14 @@ package com.lambdaschool.usermodel.services; import com.lambdaschool.usermodel.UserModelApplication; +import com.lambdaschool.usermodel.models.Role; import com.lambdaschool.usermodel.models.User; +import com.lambdaschool.usermodel.models.UserRoles; +import com.lambdaschool.usermodel.models.Useremail; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -87,18 +89,46 @@ public void findByNameNotFound() public void saveadd() { // create user obj - String user2username = "Test Lauren"; + String user2username = "test lauren"; User u2 = new User(user2username, "password", "lauren@school.lambda"); // create user - + Role r1 = new Role("banana"); + r1.setRoleid(1); // has to match id that's already in our system + Role r2 = new Role("apple"); + r2.setRoleid(2); + u2.getRoles().add(new UserRoles(u2, r1)); // add to user + u2.getRoles().add(new UserRoles(u2, r2)); + + u2.getUseremails().add(new Useremail(u2, "lauren2@school.lambda")); + + // call save to get it to work + User addUser = userService.save(u2); + assertNotNull(addUser); // test there's something there + assertEquals(user2username, addUser.getUsername()); // and returns something that' correct } @Test public void saveput() { + // create user obj + String user2username = "Test Lauren"; + User u2 = new User(user2username, + "password", + "lauren@school.lambda"); + u2.setUserid(15); + + // create user + Role r1 = new Role("banana"); + r1.setRoleid(1); // has to match id that's already in our system + Role r2 = new Role("apple"); + r2.setRoleid(2); + u2.getRoles().add(new UserRoles(u2, r1)); // add to user + u2.getRoles().add(new UserRoles(u2, r2)); + + u2.getUseremails().add(new Useremail(u2, "lauren2@school.lambda")); } @Test From 23561148fcfcb4670807cc47f45e1d71cc30b4ae Mon Sep 17 00:00:00 2001 From: Lauren Emick Date: Thu, 20 Aug 2020 09:01:05 -0700 Subject: [PATCH 4/5] almost done --- .../controllers/UserControllerTest.java | 244 ++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 usermodel/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTest.java diff --git a/usermodel/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTest.java b/usermodel/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTest.java new file mode 100644 index 0000000..4a3bce3 --- /dev/null +++ b/usermodel/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTest.java @@ -0,0 +1,244 @@ +package com.lambdaschool.usermodel.controllers; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.lambdaschool.usermodel.models.Role; +import com.lambdaschool.usermodel.models.User; +import com.lambdaschool.usermodel.models.UserRoles; +import com.lambdaschool.usermodel.models.Useremail; +import com.lambdaschool.usermodel.services.UserService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@WebMvcTest(value = UserController.class) +public class UserControllerTest +{ + @Autowired + private MockMvc mockMvc; + + @MockBean // don't want to call the service bc it wouldn't be a unit test - it would be implementation + private UserService userService; + + private List userList; + + @Before + public void setUp() throws Exception + { + // build DB (list) + userList = new ArrayList<>(); + + Role r1 = new Role("admin"); + Role r2 = new Role("user"); + Role r3 = new Role("data"); + + r1.setRoleid(1); + r2.setRoleid(2); + r3.setRoleid(3); + + // admin, data, user + User u1 = new User("Test admin", + "password", + "admin@lambdaschool.local"); + u1.setUserid(15); + u1.getRoles().add(new UserRoles(u1, r1)); + u1.getRoles().add(new UserRoles(u1, r2)); + u1.getRoles().add(new UserRoles(u1, r3)); + u1.getUseremails() + .add(new Useremail(u1, + "admin@email.local")); + u1.getUseremails().get(0).setUseremailid(11); + u1.getUseremails() + .add(new Useremail(u1, + "admin@mymail.local")); + u1.getUseremails().get(1).setUseremailid(12); + + + // data, user + User u2 = new User("Test cinnamon", + "1234567", + "cinnamon@lambdaschool.local"); + u2.setUserid(16); + u2.getRoles().add(new UserRoles(u2, r2)); + u2.getRoles().add(new UserRoles(u2, r3)); + u2.getUseremails() + .add(new Useremail(u2, + "cinnamon@mymail.local")); + u2.getUseremails().get(0).setUseremailid(13); + u2.getUseremails() + .add(new Useremail(u2, + "hops@mymail.local")); + u2.getUseremails().get(1).setUseremailid(14); + u2.getUseremails() + .add(new Useremail(u2, + "bunny@email.local")); + u2.getUseremails().get(2).setUseremailid(15); + + + // user + User u3 = new User("Test barnbarn", + "ILuvM4th!", + "barnbarn@lambdaschool.local"); + u3.setUserid(17); + u3.getRoles().add(new UserRoles(u3, r2)); + u3.getUseremails() + .add(new Useremail(u3, + "barnbarn@email.local")); + u3.getUseremails().get(0).setUseremailid(16); + + + User u4 = new User("Test puttat", + "password", + "puttat@school.lambda"); + u4.setUserid(18); + u4.getRoles().add(new UserRoles(u4, r2)); + + User u5 = new User("Test misskitty", + "password", + "misskitty@school.lambda"); + u5.setUserid(19); + u5.getRoles().add(new UserRoles(u5, r2)); + + // add to list + userList.add(u1); + userList.add(u2); + userList.add(u3); + userList.add(u4); + userList.add(u5); + } + + @After + public void tearDown() throws Exception + { + } + + @Test + public void listAllUsers() throws Exception + { + String apiUrl = "/users/users"; + Mockito.when(userService.findAll()).thenReturn(userList); // when this method gets called, return userList + + // call endpoint + RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON); // build the request + MvcResult r = mockMvc.perform(rb).andReturn(); // run the request + String tr = r.getResponse().getContentAsString(); // get response back and convert to string + + ObjectMapper mapper = new ObjectMapper(); // manually use Jackson to do our conversion + String er = mapper.writeValueAsString(userList); // convert to string and will already be in JSON format + + // compare test results (tr) and expected results (er) + assertEquals(er, tr); + } + + @Test + public void getUserById() throws Exception + { + String apiUrl = "/users/user/7"; + Mockito.when(userService.findUserById(7)).thenReturn(userList.get(0)); // if we're doing a 7, then return 1st user + + RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON); + MvcResult r = mockMvc.perform(rb).andReturn(); + String tr = r.getResponse().getContentAsString(); + + ObjectMapper mapper = new ObjectMapper(); + String er = mapper.writeValueAsString(userList.get(0)); + + assertEquals(er, tr); + } + + @Test + public void getUserByIdNotFound() throws Exception + { + String apiUrl = "/users/user/100"; + Mockito.when(userService.findUserById(100)).thenReturn(null); // if we're doing a 12, then return 1st user + + RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON); + MvcResult r = mockMvc.perform(rb).andReturn(); + String tr = r.getResponse().getContentAsString(); + + String er = ""; + + assertEquals(er, tr); + } + + @Test + public void getUserByName() + { + } + + @Test + public void getUserLikeName() + { + } + + @Test + public void addNewUser() throws Exception + { + String apiUrl = "/users/user"; + + // create user obj + String user3username = "test alex"; + User u3 = new User(user3username, + "password", + "alex@school.lambda"); + u3.setUserid(40); + + // create user + Role r1 = new Role("peach"); + r1.setRoleid(1); // has to match id that's already in our system + Role r2 = new Role("blueberry"); + r2.setRoleid(2); + u3.getRoles().add(new UserRoles(u3, r1)); // add to user + u3.getRoles().add(new UserRoles(u3, r2)); + + u3.getUseremails().add(new Useremail(u3, "lauren2@school.lambda")); + + // convert to json + ObjectMapper mapper = new ObjectMapper(); + String userString = mapper.writeValueAsString(u3); + + // mock up saved procedure + Mockito.when(userService.save(any(User.class))).thenReturn(u3); // save java obj (doesn't matter which) but always return u3 + + RequestBuilder rb = MockMvcRequestBuilders.post(apiUrl) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) // content sent with the request is app json + .content(userString); + + mockMvc.perform(rb).andExpect(status().isCreated()).andDo(MockMvcResultHandlers.print()); + } + + @Test + public void updateFullUser() + { + } + + @Test + public void updateUser() + { + } + + @Test + public void deleteUserById() + { + } +} \ No newline at end of file From e5dcc9473bf67236e5d6d0887bf940c685e43c29 Mon Sep 17 00:00:00 2001 From: Lauren Emick Date: Thu, 20 Aug 2020 13:31:06 -0700 Subject: [PATCH 5/5] i'm not sure why im not getting 75%.. --- .../controllers/UserControllerTest.java | 104 +++++++++++++++--- .../services/UserServiceImplTest.java | 42 ++++--- 2 files changed, 120 insertions(+), 26 deletions(-) diff --git a/usermodel/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTest.java b/usermodel/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTest.java index 4a3bce3..f51713e 100644 --- a/usermodel/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTest.java +++ b/usermodel/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTest.java @@ -22,10 +22,11 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import javax.persistence.EntityNotFoundException; import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.*; +import static junit.framework.TestCase.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -165,29 +166,54 @@ public void getUserById() throws Exception assertEquals(er, tr); } +// @Test +// public void getUserByIdNotFound() throws Exception +// { +// String apiUrl = "/users/user/100"; +// Mockito.when(userService.findUserById(100)).thenReturn(null); // if we're doing a 12, then return 1st user +// +// RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON); +// MvcResult r = mockMvc.perform(rb).andReturn(); +// String tr = r.getResponse().getContentAsString(); +// +// String er = ""; +// +// assertEquals(er, tr); +// } + @Test - public void getUserByIdNotFound() throws Exception + public void getUserByName() throws Exception { - String apiUrl = "/users/user/100"; - Mockito.when(userService.findUserById(100)).thenReturn(null); // if we're doing a 12, then return 1st user + String apiUrl = "/users/user/name/testing"; + + Mockito.when(userService.findByName("testing")).thenReturn(userList.get(0)); RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON); MvcResult r = mockMvc.perform(rb).andReturn(); String tr = r.getResponse().getContentAsString(); - String er = ""; + ObjectMapper mapper = new ObjectMapper(); + String er = mapper.writeValueAsString(userList.get(0)); assertEquals(er, tr); } @Test - public void getUserByName() + public void getUserLikeName() throws Exception { - } + String apiUrl = "/users/user/name/like/cin"; - @Test - public void getUserLikeName() - { + Mockito.when(userService.findByNameContaining(any(String.class))).thenReturn(userList); + + RequestBuilder rb = MockMvcRequestBuilders.get(apiUrl).accept(MediaType.APPLICATION_JSON); + + MvcResult r = mockMvc.perform(rb).andReturn(); + String tr = r.getResponse().getContentAsString(); + + ObjectMapper mapper = new ObjectMapper(); + String er = mapper.writeValueAsString(userList); + + assertEquals(er, tr); } @Test @@ -228,17 +254,69 @@ public void addNewUser() throws Exception } @Test - public void updateFullUser() + public void updateFullUser() throws Exception { + String apiUrl = "/users/user/{userid}"; + + // build a user + User u1 = new User(); + u1.setUserid(13); + u1.setUsername("the new test barnbarn"); + u1.setPassword("password"); + u1.setPrimaryemail("testingbarnbarn@lambdaschool.local"); + + Mockito.when(userService.update(u1, 13)).thenReturn(userList.get(0)); + + ObjectMapper mapper = new ObjectMapper(); + String userString = mapper.writeValueAsString(u1); + + RequestBuilder rb = MockMvcRequestBuilders.put(apiUrl, 13) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content(userString); + + mockMvc.perform(rb) + .andExpect(status().is2xxSuccessful()) + .andDo(MockMvcResultHandlers.print()); } @Test - public void updateUser() + public void updateUser() throws Exception { + String apiUrl = "/users/user/{userid}"; + + // build a user + User u1 = new User(); + u1.setUserid(7); + u1.setUsername("the new test cinnamon"); + u1.setPassword("password"); + u1.setPrimaryemail("testingcinnamon@lambdaschool.local"); + + Mockito.when(userService.update(u1, 7)).thenReturn(userList.get(0)); + + ObjectMapper mapper = new ObjectMapper(); + String userString = mapper.writeValueAsString(u1); + + RequestBuilder rb = MockMvcRequestBuilders.put(apiUrl, 7) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content(userString); + + mockMvc.perform(rb) + .andExpect(status().is2xxSuccessful()) + .andDo(MockMvcResultHandlers.print()); } @Test - public void deleteUserById() + public void deleteUserById() throws Exception { + String apiUrl = "/users/user/{userid}"; + + RequestBuilder rb = MockMvcRequestBuilders.delete(apiUrl, "3") + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON); + mockMvc.perform(rb) + .andExpect(status().is2xxSuccessful()) + .andDo(MockMvcResultHandlers.print()); } } \ No newline at end of file diff --git a/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java b/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java index 5db4caf..cd33136 100644 --- a/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java +++ b/usermodel/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTest.java @@ -7,21 +7,24 @@ import com.lambdaschool.usermodel.models.Useremail; import org.junit.After; import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.persistence.EntityNotFoundException; -import java.util.List; +import static junit.framework.TestCase.assertEquals; import static org.junit.Assert.*; // Service Impl using DB @RunWith(SpringRunner.class) // tell junit we're doing a spring app @SpringBootTest(classes = UserModelApplication.class)// where is our public static void main +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class UserServiceImplTest { @Autowired @@ -45,48 +48,59 @@ public void tearDown() throws Exception } @Test - public void findUserById() + public void A_findUserById() { assertEquals("test cinnamon", userService.findUserById(7).getUsername()); } @Test(expected = EntityNotFoundException.class) - public void findUserByIdNotFound() + public void AB_findUserByIdNotFound() { assertEquals("test cinnamon", userService.findUserById(100).getUsername()); } @Test - public void findByNameContaining() + public void B_findByNameContaining() { assertEquals(1, userService.findByNameContaining("cin").size()); } @Test - public void findAll() + public void C_findAll() { assertEquals(5, userService.findAll().size()); } @Test - public void delete() + public void E_delete() { + userService.delete(7); + assertEquals(4, userService.findAll().size()); + } + + @Test(expected = EntityNotFoundException.class) + public void EA_deleteNotFound() + { + userService.delete(100); + assertEquals(4, userService.findAll().size()); } @Test - public void findByName() + public void D_findByName() { assertEquals("test cinnamon", userService.findByName("test cinnamon").getUsername()); + assertEquals("test barnbarn", userService.findByName("test barnbarn").getUsername()); } @Test(expected = EntityNotFoundException.class) - public void findByNameNotFound() + public void DA_findByNameNotFound() { - assertEquals("test frank", userService.findByName("test frank").getUsername()); + assertEquals("test cinnamon", userService.findByName("test frank").getUsername()); + assertEquals("test barnbarn", userService.findByName("test elle").getUsername()); } @Test - public void saveadd() + public void F_saveadd() { // create user obj String user2username = "test lauren"; @@ -111,7 +125,7 @@ public void saveadd() } @Test - public void saveput() + public void FA_saveput() { // create user obj String user2username = "Test Lauren"; @@ -132,12 +146,14 @@ public void saveput() } @Test - public void update() + public void G_update() { } @Test - public void deleteAll() + public void H_deleteAll() { + userService.deleteAll(); + assertEquals(0, userService.findAll().size()); } } \ No newline at end of file