From a4d84e5c2b53591e72f0b6ced24cb1b20d4586e9 Mon Sep 17 00:00:00 2001 From: George Hatzigeorgio Date: Thu, 22 Apr 2021 11:36:25 -0400 Subject: [PATCH 1/5] commit --- .../com/lambdaschool/usermodel/SeedData.java | 2 +- .../services/UserServiceImplTestNoDB.java | 230 ++++++++++++++++++ 2 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java diff --git a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/SeedData.java b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/SeedData.java index 50d2caa..59c9986 100755 --- a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/SeedData.java +++ b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/SeedData.java @@ -23,7 +23,7 @@ * after the application context has been loaded. */ @Transactional -@Component +//@Component public class SeedData implements CommandLineRunner { diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java new file mode 100644 index 0000000..6bc8b0d --- /dev/null +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java @@ -0,0 +1,230 @@ +package com.lambdaschool.usermodel.services; + +import com.lambdaschool.usermodel.UserModelApplicationTesting; +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.repository.RoleRepository; +import com.lambdaschool.usermodel.repository.UserRepository; +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.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = UserModelApplicationTesting.class, + properties = { + "command.line.runner.enabled=false"}) +public class UserServiceImplTestNoDB { + @Autowired + private RoleService roleService; + @Autowired + private UserService userService; + + @MockBean + private UserRepository userRepository; + + @MockBean + private RoleRepository rolerepos; + + + @Autowired + private HelperFunctions helperFunctions; + + private List userList; + + @Before + public void setUp() throws Exception { + userList = new ArrayList<>(); + + Role role1 = new Role("admin"); + Role role2 = new Role("user"); + Role role3 = new Role("data"); + + role1.setRoleid(1);; + role2.setRoleid(2); + role3.setRoleid(3); + + User u1 = new User("admin","password", + "admin@lambdaschool.com"); + u1.setUserid(10); + u1.getRoles() + .add(new UserRoles(u1, + role1)); + u1.getRoles() + .add(new UserRoles(u1, + role2)); + u1.getRoles() + .add(new UserRoles(u1, + role3)); + u1.getUseremails() + .add(new Useremail(u1, + "admin@email.local")); + u1.getUseremails() + .add(new Useremail(u1, + "admin@mymail.local")); + + userList.add(u1); + + // data, user + User u2 = new User("cinnamon", + "1234567", + "cinnamon@lambdaschool.local"); + u2.setUserid(20); + u2.getRoles() + .add(new UserRoles(u2, + role2)); + u2.getRoles() + .add(new UserRoles(u2, + role3)); + 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")); + userList.add(u2); + + // user + User u3 = new User("barnbarn", + "ILuvM4th!", + "barnbarn@lambdaschool.local"); + u3.setUserid(30); + u3.getRoles() + .add(new UserRoles(u3, + role2)); + u3.getUseremails() + .add(new Useremail(u3, + "barnbarn@email.local")); + userList.add(u3); + + User u4 = new User("puttat", + "password", + "puttat@school.lambda"); + u4.setUserid(40); + u4.getRoles() + .add(new UserRoles(u4, + role2)); + userList.add(u4); + + + User u5 = new User("misskitty", + "password", + "misskitty@school.lambda"); + u5.setUserid(50); + u5.getRoles() + .add(new UserRoles(u5, + role2)); + userList.add(u5); + MockitoAnnotations.initMocks(this); + + } + + @After + public void tearDown() throws Exception { + } + + + @Test + public void findUserById() { + Mockito.when(userRepository.findById(20L)) + .thenReturn(Optional.of(userList.get(1))); + + assertEquals("cinnamon", + userService.findUserById(20) + .getUsername()); + + + } + + @Test + public void findByNameContaining () { + Mockito.when(userRepository.findByUsernameContainingIgnoreCase("barnbarn")) + .thenReturn(userList); + + assertEquals(5, userService.findByNameContaining("barnbarn").size()); + + } + @Test + public void findAll() { + Mockito.when(userRepository.findAll()) + .thenReturn(userList); + assertEquals(5,userService.findAll().size()); + + } + + @Test + public void delete() { + Mockito.when(userRepository.findById(30L)) + .thenReturn(Optional.of(userList.get(0))); + Mockito.doNothing() + .when(userRepository) + .deleteById(30L); + } + + @Test + public void findByName () { + Mockito.when(userRepository.findByUsername("admin")) + .thenReturn(userList.get(0)); + assertEquals("admin",userService.findByName("admin").getUsername()); + } + @Test + public void save() { + String userName3 = "marley"; + User user3 = new User(userName3, + "cingaderosa", + "marley@gmail.com"); + Role roleType1 = new Role("dog"); + roleType1.setRoleid(5L); + + user3.getRoles() + .add(new UserRoles(user3, roleType1)); + Mockito.when(userRepository.save(any(User.class))) + .thenReturn(user3); + Mockito.when(rolerepos.findById(5L)) + .thenReturn(Optional.of(roleType1)); + + assertEquals("marley", userService.save(user3) + .getUsername()); + + } + + @Test + public void update() { + } + + @Test + public void deleteAll() { + } +} + + + + + + + + + + + + From 1b60c5c2ea45c150fffd2b11533f0a716243ebaa Mon Sep 17 00:00:00 2001 From: George Hatzigeorgio Date: Thu, 22 Apr 2021 17:28:46 -0400 Subject: [PATCH 2/5] unit tests done --- usermodel-initial/pom.xml | 6 +- .../services/UserServiceImplTestNoDB.java | 99 +++++++++++++++---- 2 files changed, 85 insertions(+), 20 deletions(-) diff --git a/usermodel-initial/pom.xml b/usermodel-initial/pom.xml index 4a5df7a..a5f5a11 100644 --- a/usermodel-initial/pom.xml +++ b/usermodel-initial/pom.xml @@ -23,7 +23,11 @@ org.springframework.boot spring-boot-starter-data-jpa - + + io.rest-assured + spring-mock-mvc + 3.3.0 + org.springframework.boot spring-boot-starter-web diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java index 6bc8b0d..1a944b5 100644 --- a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java @@ -1,5 +1,7 @@ package com.lambdaschool.usermodel.services; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.lambdaschool.usermodel.UserModelApplicationTesting; import com.lambdaschool.usermodel.models.Role; import com.lambdaschool.usermodel.models.User; @@ -24,8 +26,10 @@ import java.util.List; import java.util.Optional; +import static junit.framework.TestCase.assertEquals; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; @RunWith(SpringRunner.class) @SpringBootTest(classes = UserModelApplicationTesting.class, @@ -44,7 +48,7 @@ public class UserServiceImplTestNoDB { private RoleRepository rolerepos; - @Autowired + @MockBean private HelperFunctions helperFunctions; private List userList; @@ -53,32 +57,30 @@ public class UserServiceImplTestNoDB { public void setUp() throws Exception { userList = new ArrayList<>(); - Role role1 = new Role("admin"); - Role role2 = new Role("user"); - Role role3 = new Role("data"); + Role r1 = new Role("admin"); + Role r2 = new Role("user"); + Role r3 = new Role("data"); - role1.setRoleid(1);; - role2.setRoleid(2); - role3.setRoleid(3); + r1.setRoleid(1);; + r2.setRoleid(2); + r3.setRoleid(3); User u1 = new User("admin","password", "admin@lambdaschool.com"); u1.setUserid(10); u1.getRoles() .add(new UserRoles(u1, - role1)); + r1)); u1.getRoles() .add(new UserRoles(u1, - role2)); + r2)); u1.getRoles() .add(new UserRoles(u1, - role3)); + r3)); u1.getUseremails() .add(new Useremail(u1, "admin@email.local")); - u1.getUseremails() - .add(new Useremail(u1, - "admin@mymail.local")); + userList.add(u1); @@ -89,10 +91,10 @@ public void setUp() throws Exception { u2.setUserid(20); u2.getRoles() .add(new UserRoles(u2, - role2)); + r2)); u2.getRoles() .add(new UserRoles(u2, - role3)); + r3)); u2.getUseremails() .add(new Useremail(u2, "cinnamon@mymail.local")); @@ -111,7 +113,7 @@ public void setUp() throws Exception { u3.setUserid(30); u3.getRoles() .add(new UserRoles(u3, - role2)); + r2)); u3.getUseremails() .add(new Useremail(u3, "barnbarn@email.local")); @@ -123,7 +125,7 @@ public void setUp() throws Exception { u4.setUserid(40); u4.getRoles() .add(new UserRoles(u4, - role2)); + r2)); userList.add(u4); @@ -133,7 +135,7 @@ public void setUp() throws Exception { u5.setUserid(50); u5.getRoles() .add(new UserRoles(u5, - role2)); + r2)); userList.add(u5); MockitoAnnotations.initMocks(this); @@ -210,10 +212,46 @@ public void save() { @Test public void update() { + + Role r2 = new Role ("user"); + r2.setRoleid(2); + User u2 = new User("cinnamon","password","cinnamon@school.lambda"); + u2.getRoles().add(new UserRoles(u2,r2)); + u2.getUseremails().add(new Useremail(u2,"cinnamon@mymail.thump")); + u2.getUseremails().add(new Useremail(u2,"hops@mymail.thump")); + u2.getUseremails().add(new Useremail(u2,"bunny@email.thump")); + + + + Mockito.when(rolerepos.findById(2L)) + .thenReturn(Optional.of(r2)); + Mockito.when(userRepository.findById(103L)) + .thenReturn(Optional.of(userList.get(2))); + + Mockito.when(userRepository.save(any(User.class))) + .thenReturn(u2); + Mockito.when(helperFunctions.isAuthorizedToMakeChange(anyString())) + .thenReturn(true); + assertEquals("bunny@email.thump", + userService.update(u2,103L) + .getUseremails() + .get(2) + .getUseremail()); } + @Test - public void deleteAll() { + public void deleteAll(){ + Mockito.doNothing() + .when(userRepository) + .deleteAll(); + userService.deleteAll(); + assertEquals(5,userList.size()); + + + + + } } @@ -228,3 +266,26 @@ public void deleteAll() { + + + + + +// @Test +// public void deleteAll() { +// Mockito.when(userRepository.deleteAll()) +// .thenReturn(); +// } +//} + + + + + + + + + + + + From 4faa14191e463dceb815207a7856ec87d4282545 Mon Sep 17 00:00:00 2001 From: George Hatzigeorgio Date: Thu, 22 Apr 2021 19:16:48 -0400 Subject: [PATCH 3/5] having some technical difficulties --- .../lambdaschool/usermodel/models/Role.java | 1 + .../usermodel/models/Useremail.java | 1 + .../controllers/UserControllerTestNoDB.java | 211 ++++++++++++++++++ .../resources/application.properties | 34 +++ .../usermodel/resources/info/curl.txt | 69 ++++++ .../services/UserServiceImplTestNoDB.java | 6 +- 6 files changed, 319 insertions(+), 3 deletions(-) create mode 100644 usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestNoDB.java create mode 100644 usermodel-initial/src/test/java/com/lambdaschool/usermodel/resources/application.properties create mode 100644 usermodel-initial/src/test/java/com/lambdaschool/usermodel/resources/info/curl.txt diff --git a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/models/Role.java b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/models/Role.java index f74d50b..93a5a2d 100644 --- a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/models/Role.java +++ b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/models/Role.java @@ -1,5 +1,6 @@ package com.lambdaschool.usermodel.models; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import javax.persistence.*; diff --git a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/models/Useremail.java b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/models/Useremail.java index 54a80a8..96c80e4 100644 --- a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/models/Useremail.java +++ b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/models/Useremail.java @@ -1,5 +1,6 @@ package com.lambdaschool.usermodel.models; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import javax.persistence.*; diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestNoDB.java b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestNoDB.java new file mode 100644 index 0000000..b22934d --- /dev/null +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestNoDB.java @@ -0,0 +1,211 @@ +package com.lambdaschool.usermodel.controllers; + +import com.lambdaschool.usermodel.UserModelApplicationTesting; +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.repository.UserRepository; +import com.lambdaschool.usermodel.services.UserService; +import io.restassured.module.mockmvc.RestAssuredMockMvc; +import org.codehaus.jackson.map.ObjectMapper; +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.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; +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.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@WithMockUser(username = "admin", + roles = {"USER", "ADMIN"}) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = UserModelApplicationTesting.class, + properties = { + "command.line.runner.enabled=false"}) +@AutoConfigureMockMvc +public class UserControllerTestNoDB { + @Autowired + private WebApplicationContext webApplicationContext; + + private MockMvc mockMvc; + + @Autowired + private UserRepository userRepository; + + @MockBean + private UserService userService; + + private List userList; + + + @Before + public void setUp() throws Exception { + 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); + + User u1 = new User("admin","password", + "admin@lambdaschool.com"); + u1.setUserid(10); + 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")); + + + userList.add(u1); + + // data, user + User u2 = new User("cinnamon", + "1234567", + "cinnamon@lambdaschool.local"); + u2.setUserid(20); + 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")); + + userList.add(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")); + + userList.add(u3); + + User u4 = new User("puttat", + "password", + "puttat@school.lambda"); + u4.setUserid(40); + u4.getRoles() + .add(new UserRoles(u4, + r2)); + userList.add(u4); + + + User u5 = new User("misskitty", + "password", + "misskitty@school.lambda"); + u5.setUserid(50); + u5.getRoles() + .add(new UserRoles(u5, + r2)); + userList.add(u5); + + RestAssuredMockMvc.webAppContextSetup(webApplicationContext); + + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .apply(SecurityMockMvcConfigurers.springSecurity()) + .build(); + + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void listAllUsers() throws Exception { + String ApiUrl = "/users/users"; + Mockito.when(userService.findAll()) + .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); + System.out.println(er); +// assertEquals("er",tr); + + } + + @Test + public void getUserById() { + } + + @Test + public void getUserByName() { + } + + @Test + public void getUserLikeName() { + } + + @Test + public void addNewUser() { + } + + @Test + public void updateFullUser() { + } + + @Test + public void updateUser() { + } + + @Test + public void deleteUserById() { + } + + @Test + public void getCurrentUserInfo() { + } +} \ No newline at end of file diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/resources/application.properties b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/resources/application.properties new file mode 100644 index 0000000..6467ccd --- /dev/null +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/resources/application.properties @@ -0,0 +1,34 @@ +# 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 +# +# Used to set the date format for JSON Output +spring.jackson.date-format=yyyy-MM-dd HH:mm:ss +spring.jackson.time-zone=America/Los_Angeles +# +# Turns off Spring Boot automatic exception handling +server.error.whitelabel.enabled=false +spring.mvc.throw-exception-if-no-handler-found=true +spring.resources.add-mappings=false diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/resources/info/curl.txt b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/resources/info/curl.txt new file mode 100644 index 0000000..8e6a532 --- /dev/null +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/resources/info/curl.txt @@ -0,0 +1,69 @@ +// admins access token = 56ab5075-de2e-43ba-a407-e52b8f267ac7 +// barnbarns access token = a87cdc74-7188-43d7-9c2e-f689841f083f + +// get admins access code +curl -X POST --user "lambda-client:lambda-secret" -d "grant_type=password&username=admin&password=password" http://localhost:2019/login + +// this works +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" http://localhost:2019/users/users + +// get users access code +curl -X POST --user "lambda-client:lambda-secret" -d 'grant_type=password&username=barnbarn&password=ILuvM4th!' http://localhost:2019/login + +// this has an authentication error +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer a87cdc74-7188-43d7-9c2e-f689841f083f" http://localhost:2019/users/users + +// switch back to admins access code and this works +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" http://localhost:2019/users/users + +// add snoopy as a user +curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" -d "{ \"username\":\"snoopy\", \"password\":\"password\", \"roleid\":"1" }" http://localhost:2019/users/user + +// look up users and find snoopy's id +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" http://localhost:2019/users/users + +// delete snoopy user +curl -X DELETE -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" http://localhost:2019/users/user/15 + +// verify that snoopy is deleted +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" http://localhost:2019/users/user/15 + +// add ginger a new user +curl -X POST -H "Content-Type: application/json" -d "{\"username\": \"Ginger\", \"password\": \"EATEATEAT\"}" http://localhost:2019/createnewuser + +// add snoopy again with full information +curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" -d "{ \"username\": \"Snoopy\", \"password\": \"password\", \"userRoles\": [ { \"role\": { \"roleid\": 1 } } ], \"userphones\": [ { \"userphone\": \"myemail@local.home\" } ]}" http://localhost:2019/users/user + +// look at all users and verify that snoopy and ginger are available +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" http://localhost:2019/users/users + +// revokes token for admin +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" http://localhost:2019/oauth/revoke-token + +// this should fail with invalid token +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer 56ab5075-de2e-43ba-a407-e52b8f267ac7" http://localhost:2019/users/users + +// get admins access code +curl -X POST --user "lambda-client:lambda-secret" -d "grant_type=password&username=admin&password=password" http://localhost:2019/login + +// look at all users and ginger's id +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer 3d28337c-b699-4ae6-86fe-4961f42b1142" http://localhost:2019/users/users + +// add email to ginger +curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer 3d28337c-b699-4ae6-86fe-4961f42b1142" -d "{ \"userid\": 16, \"userphones\": [ { \"userphone\": \"whisker@home.local\" } ] }" http://localhost:2019/users/user/16 + +// look at all users and ginger's emails +curl -X GET -H "Accept: application/json" -H "Authorization: Bearer 3d28337c-b699-4ae6-86fe-4961f42b1142" http://localhost:2019/users/users + +*** using a different host + +// sign on admin when on heroku +curl -X POST --user "lambda-client:lambda-secret" -d "grant_type=password&username=admin&password=password" http://jrmmba-starthere.herokuapp.com/oauth/token + + +{ + "userid": 7, + "userphones": [ + { "userphone": "whisker@home.local"} + ] +} \ No newline at end of file diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java index 1a944b5..d06d944 100644 --- a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestNoDB.java @@ -32,9 +32,9 @@ import static org.mockito.ArgumentMatchers.anyString; @RunWith(SpringRunner.class) -@SpringBootTest(classes = UserModelApplicationTesting.class, - properties = { - "command.line.runner.enabled=false"}) +//@SpringBootTest(classes = UserModelApplicationTesting.class, +// properties = { +// "command.line.runner.enabled=false"}) public class UserServiceImplTestNoDB { @Autowired private RoleService roleService; From 349363cf8b5663501a071831566f023866effe77 Mon Sep 17 00:00:00 2001 From: George Hatzigeorgio Date: Thu, 22 Apr 2021 20:11:25 -0400 Subject: [PATCH 4/5] commit --- .../controllers/UserControllerTestNoDB.java | 113 ++++++++++++++++-- 1 file changed, 102 insertions(+), 11 deletions(-) diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestNoDB.java b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestNoDB.java index b22934d..d59f0cb 100644 --- a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestNoDB.java +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestNoDB.java @@ -8,11 +8,12 @@ import com.lambdaschool.usermodel.repository.UserRepository; import com.lambdaschool.usermodel.services.UserService; import io.restassured.module.mockmvc.RestAssuredMockMvc; -import org.codehaus.jackson.map.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; @@ -27,13 +28,21 @@ 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 org.springframework.test.web.servlet.result.StatusResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import javax.print.attribute.standard.Media; +import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) @WithMockUser(username = "admin", @@ -173,16 +182,47 @@ public void listAllUsers() throws Exception { ObjectMapper mapper = new ObjectMapper(); String er = mapper.writeValueAsString(userList); System.out.println(er); -// assertEquals("er",tr); + assertEquals(er,tr); } @Test - public void getUserById() { + public void getUserById() throws Exception { + String apiUrl ="/users/user/12"; + Mockito.when(userService.findUserById(12)) + .thenReturn(userList.get(1)); + 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(1)); + + assertEquals(er,tr); + + + + } @Test - public void getUserByName() { + public void getUserByName() throws Exception { + 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(); + ObjectMapper mapper = new ObjectMapper(); + String er = mapper.writeValueAsString(userList.get(0)); + + assertEquals(er,tr); } @Test @@ -190,22 +230,73 @@ public void getUserLikeName() { } @Test - public void addNewUser() { - } + public void addNewUser() throws Exception { + String apiUrl ="/users/user"; + Mockito.when(userService.save(any(User.class))) + .thenReturn(userList.get(0)); + + RequestBuilder rb = MockMvcRequestBuilders.post(apiUrl) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content("{\"username\": \"tiger\", \"password\" : \"ILuvM4th\" ,\"primaryemail\" : \"tiger@home.local\"}"); + mockMvc.perform(rb) + .andExpect(status().isCreated()) + .andDo(MockMvcResultHandlers.print()); + + - @Test - public void updateFullUser() { } + + + @Test - public void updateUser() { + public void updateUser() throws Exception { + String apiUrl = "/users/user/{userid}"; + Mockito.when(userService.update(any(User.class), + any(Long.class))) + .thenReturn(userList.get(0)); + RequestBuilder rb = MockMvcRequestBuilders.put(apiUrl,100L) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content("{\"username\": \"tigerUpdated\", \"password\" : \"grubgrub\" ,\"primaryemail\" : \"marley@home.local\"}"); + + 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()); } @Test - public void getCurrentUserInfo() { + public void getCurrentUserInfo() throws Exception { + String ApiUrl = "/users/getuserinfo"; + Mockito.when(userService.findByName(anyString())) + .thenReturn(userList.get(0)); + 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)); + System.out.println(er); + assertEquals(er,tr); + } } \ No newline at end of file From 43c4299d146bf16beaef18946a912c3d91c5d159 Mon Sep 17 00:00:00 2001 From: George Hatzigeorgio Date: Thu, 22 Apr 2021 20:46:05 -0400 Subject: [PATCH 5/5] deploying to heroku --- usermodel-initial/pom.xml | 22 +++++++++- .../usermodel/config/DataSourceConfig.java | 41 +++++++++++++++++++ .../src/main/resources/application.properties | 7 +++- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 usermodel-initial/src/main/java/com/lambdaschool/usermodel/config/DataSourceConfig.java diff --git a/usermodel-initial/pom.xml b/usermodel-initial/pom.xml index a5f5a11..af9a3f4 100644 --- a/usermodel-initial/pom.xml +++ b/usermodel-initial/pom.xml @@ -32,7 +32,10 @@ org.springframework.boot spring-boot-starter-web - + + org.postgresql + postgresql + org.springframework.boot spring-boot-devtools @@ -102,7 +105,24 @@ + ghatzigeorgio-usermodel + + com.heroku.sdk + heroku-maven-plugin + 3.0.2 + + ${project.build.finalName} + false + + ${project.build.directory}/${project.build.finalName}.jar + + ${java.version} + + java $JAVA_OPTS -Dserver.port=$PORT -jar target/${project.build.finalName}.jar + + + org.springframework.boot spring-boot-maven-plugin diff --git a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/config/DataSourceConfig.java b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/config/DataSourceConfig.java new file mode 100644 index 0000000..7d0b355 --- /dev/null +++ b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/config/DataSourceConfig.java @@ -0,0 +1,41 @@ +package com.lambdaschool.usermodel.config; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import javax.sql.DataSource; +@Configuration +public class DataSourceConfig { + @Value("${local.run.db:H2}") + private String dbValue; + + @Value("${spring.datasource.url:}") + private String dbURL; + + @Bean + public DataSource dataSource() { + if (dbValue.equalsIgnoreCase("POSTGRESQL")) { + //Assume Heroku + HikariConfig config = new HikariConfig(); + config.setDriverClassName("org.postgresql.Driver"); + config.setJdbcUrl(dbURL); + return new HikariDataSource(config); + } else { + //Assume H2 + String URLString = "jbdc:h2:mem:testdb"; + String DriverClass = "org.h2.Driver"; + String DBUser = "sa"; + String DBpass = ""; + + return DataSourceBuilder.create() + .username(DBUser) + .password(DBpass) + .url(URLString) + .driverClassName(DriverClass) + .build(); + } + } +} diff --git a/usermodel-initial/src/main/resources/application.properties b/usermodel-initial/src/main/resources/application.properties index 6467ccd..555b561 100644 --- a/usermodel-initial/src/main/resources/application.properties +++ b/usermodel-initial/src/main/resources/application.properties @@ -1,4 +1,8 @@ # Configurations useful for working with H2 + +local.run.db=POSTGRESQL +spring.h2.console.settings.web-allow-others=true + spring.h2.console.enabled=true spring.h2.console.path=/h2-console # @@ -17,7 +21,8 @@ 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 +spring.datasource.initialization-mode=never +command.line.runner.enabled=true # # Good for production! # spring.jpa.hibernate.ddl-auto=update