diff --git a/usermodel-initial/pom.xml b/usermodel-initial/pom.xml index 4a5df7a..a5396ff 100644 --- a/usermodel-initial/pom.xml +++ b/usermodel-initial/pom.xml @@ -15,7 +15,7 @@ Demo project for Spring Boot - 11 + 14 @@ -77,6 +77,14 @@ + + + io.rest-assured + spring-mock-mvc + 3.3.0 + test + + org.springframework.boot @@ -95,14 +103,37 @@ 2.3.6.RELEASE + + + + + + + org.springframework.boot spring-boot-maven-plugin + + + + + + + + + + + + + + + + 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..acbdeb5 100755 --- a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/SeedData.java +++ b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/SeedData.java @@ -11,6 +11,7 @@ import com.lambdaschool.usermodel.services.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -23,6 +24,11 @@ * after the application context has been loaded. */ @Transactional +@ConditionalOnProperty( + prefix = "command.line.runner", + value = "enabled", + havingValue = "true", + matchIfMissing = true) @Component public class SeedData implements CommandLineRunner diff --git a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/services/HelperFunctionsImpl.java b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/services/HelperFunctionsImpl.java index 76211bf..fb2d250 100644 --- a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/services/HelperFunctionsImpl.java +++ b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/services/HelperFunctionsImpl.java @@ -27,7 +27,8 @@ public List getConstraintViolation(Throwable cause) // data validations get wrapped in other exceptions as we work through the Spring // exception chain. Hence we have to search the entire Spring Exception Stack // to see if we have any violation constraints. - while ((cause != null) && !(cause instanceof org.hibernate.exception.ConstraintViolationException || cause instanceof MethodArgumentNotValidException)) + while ((cause != null) && !(cause instanceof ConstraintViolationException || + cause instanceof MethodArgumentNotValidException)) { System.out.println(cause.getClass() .toString()); @@ -39,9 +40,9 @@ public List getConstraintViolation(Throwable cause) // we know that cause either null or an instance of ConstraintViolationException if (cause != null) { - if (cause instanceof org.hibernate.exception.ConstraintViolationException) + if (cause instanceof ConstraintViolationException) { - org.hibernate.exception.ConstraintViolationException ex = (ConstraintViolationException) cause; + ConstraintViolationException ex = (ConstraintViolationException) cause; ValidationError newVe = new ValidationError(); newVe.setCode(ex.getMessage()); newVe.setMessage(ex.getConstraintName()); diff --git a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/services/SecurityUserServiceImpl.java b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/services/SecurityUserServiceImpl.java index 45b9b56..daa6988 100644 --- a/usermodel-initial/src/main/java/com/lambdaschool/usermodel/services/SecurityUserServiceImpl.java +++ b/usermodel-initial/src/main/java/com/lambdaschool/usermodel/services/SecurityUserServiceImpl.java @@ -43,7 +43,8 @@ public UserDetails loadUserByUsername(String s) { throw new ResourceNotFoundException("Invalid username or password."); } - return new org.springframework.security.core.userdetails.User(user.getUsername(), + return new org.springframework.security.core.userdetails.User( + user.getUsername(), user.getPassword(), user.getAuthority()); } diff --git a/usermodel-initial/src/main/resources/application.properties b/usermodel-initial/src/main/resources/application.properties index 6467ccd..0d04048 100644 --- a/usermodel-initial/src/main/resources/application.properties +++ b/usermodel-initial/src/main/resources/application.properties @@ -1,13 +1,18 @@ +#local.run.db=H2 +#local.run.db=POSTGRESQL +# # Configurations useful for working with H2 spring.h2.console.enabled=true spring.h2.console.path=/h2-console +#Allow Heroku to connect +#spring.h2.console.settings.web-allow-others=true # # 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 +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, diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/SeedData.java b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/SeedData.java new file mode 100644 index 0000000..d991be1 --- /dev/null +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/SeedData.java @@ -0,0 +1,170 @@ +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.*; +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.boot.autoconfigure.condition.ConditionalOnProperty; +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 +@ConditionalOnProperty( + prefix = "command.line.runner", + value = "enabled", + havingValue = "true", + matchIfMissing = true) +@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("testadmin"); + Role r2 = new Role("testuser"); + Role r3 = new Role("testdata"); + + r1 = roleService.save(r1); + r2 = roleService.save(r2); + r3 = roleService.save(r3); + + // admin, data, user + User u1 = new User("testadmin", + "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("testcinnamon", + "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("testbarnbarn", + "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("testputtat", + "password", + "puttat@school.lambda"); + u4.getRoles() + .add(new UserRoles(u4, + r2)); + userService.save(u4); + + User u5 = new User("testmisskitty", + "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 diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestUnitTestNoDB.java b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestUnitTestNoDB.java new file mode 100644 index 0000000..f48d5ad --- /dev/null +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/controllers/UserControllerTestUnitTestNoDB.java @@ -0,0 +1,52 @@ +package com.lambdaschool.usermodel.controllers; + +import org.junit.*; + +import static org.junit.Assert.*; + +public class UserControllerTestUnitTestNoDB { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void listAllUsers() { + } + + @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/services/UserServiceImplTestUnitTestNoDB.java b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestUnitTestNoDB.java new file mode 100644 index 0000000..0e81f4f --- /dev/null +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestUnitTestNoDB.java @@ -0,0 +1,429 @@ +package com.lambdaschool.usermodel.services; + +import com.lambdaschool.usermodel.UserModelApplicationTesting; +import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; +import com.lambdaschool.usermodel.models.*; +import com.lambdaschool.usermodel.repository.RoleRepository; +import com.lambdaschool.usermodel.repository.UserRepository; +import org.junit.*; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.mockito.*; +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.*; + +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, + properties = { + "command.line.runner.enabled=false"}) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class UserServiceImplTestUnitTestNoDB { + + @Autowired + private UserService userService; + + // mocks -> fake data + // stubs -> fake methods + // Java -> mocks + + @MockBean + private HelperFunctions helperFunctions; + + @MockBean + private RoleService roleService; + + @MockBean + private UserRepository userrepos; + + @MockBean + private RoleRepository rolerepos; + + + private List userList; + + @Before + public void setUp() throws Exception { + userList = new ArrayList<>(); + + Role r1 = new Role("testadmin"); + Role r2 = new Role("testuser"); + Role r3 = new Role("testdata"); + + r1.setRoleid(1); + r2.setRoleid(2); + r3.setRoleid(3); + + // admin, data, user + User u1 = new User("testadmin", + "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")); + + userList.add(u1); + + // data, user + User u2 = new User("testcinnamon", + "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")); + userList.add(u2); + + // user + User u3 = new User("testbarnbarn", + "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("testputtat", + "password", + "puttat@school.lambda"); + u4.getRoles() + .add(new UserRoles(u4, + r2)); + userList.add(u4); + + User u5 = new User("testmisskitty", + "password", + "misskitty@school.lambda"); + u5.getRoles() + .add(new UserRoles(u5, + r2)); + userList.add(u5); + + MockitoAnnotations.initMocks(this); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test_findUserById() { + Mockito.when(userrepos.findById(1L)) + .thenReturn(Optional.of(userList.get(0))); + + assertEquals("testadmin", userService.findUserById(1).getUsername()); + } + @Test(expected = ResourceNotFoundException.class) + public void test_findUserByIdNotFound() + { + Mockito.when(userrepos.findById(10000L)) + .thenThrow(ResourceNotFoundException.class); + + assertEquals("testadmin", + userService.findUserById(10000) + .getUsername()); + } + + @Test + public void test_findByNameContaining() { + Mockito.when(userrepos.findByUsernameContainingIgnoreCase("testbarnbarn")) + .thenReturn(userList); + + assertEquals(5, userService.findByNameContaining("testbarnbarn") + .size()); + } + + @Test(expected = ResourceNotFoundException.class) + public void test_findUserByNameFailed() + { + Mockito.when(userrepos.findByUsername("Lambda")) + .thenThrow(ResourceNotFoundException.class); + + assertEquals("Lambda", + userService.findByName("testmisskitty") + .getUsername()); + } + + @Test + public void test_findAll() { + Mockito.when(userrepos.findAll()).thenReturn(userList); + + System.out.println(userService.findAll().toString()); + assertEquals(5, userService.findAll().size()); + } + + @Test + public void test_delete() { + Mockito.when(userrepos.findById(6L)).thenReturn( + Optional.of(userList.get(0))); + + Mockito.doNothing() + .when(userrepos) + .deleteById(6L); + + userService.delete(6); + assertEquals(5, userList.size()); + } + + @Test(expected = ResourceNotFoundException.class) + public void test_deletefailed() + { + Mockito.when(userrepos.findById(777L)) + .thenReturn(Optional.empty()); + + Mockito.doNothing() + .when(userrepos) + .deleteById(777L); + + userService.delete(777L); + assertEquals(5, + userList.size()); + } + + @Test + public void test_findByName() { + Mockito.when(userrepos.findByUsername("testcinnamon")) + .thenReturn(userList.get(1)); + + assertEquals("testcinnamon", userService.findByName( + "testCinnaMon").getUsername()); + } + + @Test + public void test_save() { + User newUser = new User( + "testtarah", + "password", + "tarah.agbokhana@gmail.com"); + + Role newRole = new Role("DATA-MGR"); + newRole.setRoleid(1); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole)); + newUser.getUseremails() + .add(new Useremail(newUser, "tagbokhana@gmail.com")); + + Mockito.when(userrepos.save(any(User.class))) + .thenReturn(newUser); + + Mockito.when(rolerepos.findById(1L)) + .thenReturn(Optional.of(newRole)); + + User addUser = userService.save(newUser); + assertNotNull(addUser); + assertEquals("testtarah",addUser.getUsername()); + } + @Test + public void test_save_put(){ + User newUser = new User( + "testtarah", + "password", + "tarah.agbokhana@gmail.com"); + + newUser.setUserid(7); + + Role newRole1 = new Role("Unknown1"); + newRole1.setRoleid(1); + + newUser.getRoles() + .clear(); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole1)); + + newUser.getUseremails() + .add(new Useremail(newUser, "tagbokhana@gmail.com")); + + Mockito.when(userrepos.findById(7L)) + .thenReturn(Optional.of(newUser)); + + Mockito.when(rolerepos.findById(1L)) + .thenReturn(Optional.of(newRole1)); + + Mockito.when(userrepos.save(any(User.class))) + .thenReturn(newUser); + + assertEquals(7L, + userService.save(newUser).getUserid()); + } + + @Test(expected = ResourceNotFoundException.class) + public void test_save_put_failed() { + User newUser = new User( + "testtarah", + "password", + "tarah.agbokhana@gmail.com"); + + newUser.setUserid(7); + + Role newRole1 = new Role("Unknown1"); + newRole1.setRoleid(1); + + Role newRole2 = new Role("Unknown2"); + newRole2.setRoleid(2); + + newUser.getRoles() + .clear(); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole1)); + newUser.getRoles() + .add(new UserRoles(newUser, newRole2)); + + newUser.getUseremails() + .add(new Useremail(newUser, "tagbokhana@gmail.com")); + + Mockito.when(userrepos.findById(77L)) + .thenThrow(ResourceNotFoundException.class); + + Mockito.when(userrepos.save(any(User.class))) + .thenReturn(newUser); + + Mockito.when(rolerepos.findById(1L)) + .thenReturn(Optional.of(newRole1)); + + Mockito.when(rolerepos.findById(2L)) + .thenReturn(Optional.of(newRole2)); + + assertEquals(77L, + userService.save(newUser).getUserid()); + } + + @Test + public void test_update() { + User newUser = new User( + "testtarah", + "password", + "tarah.agbokhana@gmail.com"); + + Role newRole1 = new Role("Unknown1"); + newRole1.setRoleid(1); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole1)); + + newUser.getUseremails() + .add(new Useremail(newUser, "tagbokhana@gmail.com")); + newUser.getUseremails() + .add(new Useremail(newUser, "tarah@gmail.com")); + System.out.println(newRole1.getRoleid()); + + Mockito.when(roleService.findRoleById(1)) + .thenReturn(newRole1); + System.out.println(roleService.findRoleById(1)); + + Mockito.when(userrepos.findById(7L)) + .thenReturn(Optional.of(userList.get(1))); + System.out.println(userrepos.findById(7L)); + + Mockito.when(userrepos.save(any(User.class))) + .thenReturn(newUser); + + Mockito.when(helperFunctions.isAuthorizedToMakeChange(anyString())) + .thenReturn(true); + +/// User addUser = userService.update(newUser, 22L); + // System.out.println("adduser: " + addUser.getUsername()); + + + assertEquals("tarah@gmail.com", + userService.update(newUser, 7L) + .getUseremails() + .get(1) + .getUseremail()); + } + + @Test(expected = ResourceNotFoundException.class) + public void test_update_failed() throws Exception { + User newUser = new User( + "testtarah", + "password", + "tarah.agbokhana@gmail.com"); + + newUser.setUserid(18); + + Role newRole1 = new Role("Unknown1"); + newRole1.setRoleid(1); + + Role newRole2 = new Role("Unknown2"); + newRole2.setRoleid(2); + + newUser.getRoles() + .clear(); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole1)); + newUser.getRoles() + .add(new UserRoles(newUser, newRole2)); + + newUser.getUseremails() + .add(new Useremail(newUser, "tagbokhana@gmail.com")); + + Mockito.when(userrepos.findById(18L)) + .thenThrow(ResourceNotFoundException.class); + + Mockito.when(rolerepos.findById(1L)) + .thenReturn(Optional.of(newRole1)); + + Mockito.when(rolerepos.findById(2L)) + .thenReturn(Optional.of(newRole2)); + + Mockito.when(userrepos.save(any(User.class))) + .thenReturn(newUser); + + User addUser = userService.update(newUser, 18); + System.out.println("#3 "+ addUser.getUsername()); + assertNotNull(addUser); + assertEquals("testtarah", + addUser.getUsername()); + } + + @Test + public void test_deleteAll() { + Mockito.when(userrepos.findAll()).thenReturn(userList); + + Mockito.doNothing() + .when(userrepos) + .deleteAll(); + + userService.deleteAll(); + + assertEquals(5, userList.size()); + } + +} \ No newline at end of file diff --git a/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestUnitTestWithDB.java b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestUnitTestWithDB.java new file mode 100644 index 0000000..2a234b7 --- /dev/null +++ b/usermodel-initial/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplTestUnitTestWithDB.java @@ -0,0 +1,308 @@ +package com.lambdaschool.usermodel.services; + +import com.lambdaschool.usermodel.UserModelApplicationTesting; +import com.lambdaschool.usermodel.exceptions.ResourceFoundException; +import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; +import com.lambdaschool.usermodel.models.*; +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 javax.annotation.Resource; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.anyString; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = UserModelApplicationTesting.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class UserServiceImplTestUnitTestWithDB { + + @Autowired + private UserService userService; + + @Autowired + private RoleService roleService; + + @MockBean + private HelperFunctions helperFunctions; + + @Before + public void setUp() throws Exception { + + MockitoAnnotations.initMocks(this); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDB_findUserById() { + System.out.println(userService.findUserById(4) + .getUsername()); + assertEquals("testadmin", + userService.findUserById(4) + .getUsername()); + } + + @Test(expected = ResourceNotFoundException.class) + public void testDB_findUserByIdNotFound() { + assertEquals("testadmin", + userService.findUserById(10) + .getUsername()); + } + + @Test + public void testDB_findByNameContaining() { + + } + + @Test + public void testDB_findAll() { + assertEquals(5, + userService.findAll().size()); + } + + @Test + public void ztestDB_delete() { + userService.delete(13); + assertEquals(5, + userService.findAll().size()); + } + + @Test + public void testDB_findByName() { + assertEquals("testadmin", + userService.findByName("testadmin").getUsername()); + } + + @Test + public void testDB_save() { + Role newRole = new Role("ADMIN"); + newRole.setRoleid(1); //sets a role that already exist + + User newUser = new User("tagbokhana", + "password", + "tagbokhana@gmail.com"); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole)); + newUser.getUseremails() + .add(new Useremail(newUser, "tarah.agbokhana@gmail.com")); + + User addUser = userService.save(newUser); + + assertEquals("tagbokhana", addUser.getUsername()); + } + + @Test(expected = ResourceNotFoundException.class) + public void testDB_saveNotFound() { + Role newRole = new Role("ADMIN"); + newRole.setRoleid(8); //sets a role that already exist + + User newUser = new User("tagbokhana", + "password", + "tagbokhana@gmail.com"); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole)); + newUser.getUseremails() + .add(new Useremail(newUser, "tarah.agbokhana@gmail.com")); + + User addUser = userService.save(newUser); + + assertEquals("tagbokhana", addUser.getUsername()); + } + + @Test + public void testDB_savePut(){ + Role newRole = new Role("testuser"); + newRole.setRoleid(3); + + User newUser = new User("bebop", + "password", + "beep@lol.com"); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole)); + newUser.getUseremails() + .add(new Useremail(newUser, "beepbeep@lol.com")); + newUser.setUserid(4); + + User addUser = userService.save(newUser); + + assertEquals("beepbeep@lol.com", + addUser.getUseremails() + .get(0) + .getUseremail()); + } + + @Test(expected = ResourceNotFoundException.class) + public void testDB_savePutNotFound(){ + Role newRole = new Role("testuser"); + newRole.setRoleid(3); + + User newUser = new User("bebop", + "password", + "beep@lol.com"); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole)); + newUser.getUseremails() + .add(new Useremail(newUser, "beepbeep@lol.com")); + newUser.setUserid(40); + + User addUser = userService.save(newUser); + + assertEquals("beepbeep@lol.com", + addUser.getUseremails() + .get(0) + .getUseremail()); + } + + @Test + public void testDB_update() { + Mockito.when(helperFunctions.isAuthorizedToMakeChange(anyString())) + .thenReturn(true); + + Role newRole = new Role("testnewuser"); + newRole.setRoleid(1); + + User newUser = new User("tata", + "password", + "tata@agency.com"); + newUser.getRoles() + .add(new UserRoles(newUser, newRole)); + newUser.getUseremails() + .add(new Useremail(newUser, "admin@agency.com")); + newUser.getUseremails() + .add(new Useremail(newUser, "manager@agency.com")); + + User updatedUser = userService.update(newUser, 4); + + int lastIndex = updatedUser.getUseremails() + .size() - 1; + assertEquals("manager@agency.com", + updatedUser.getUseremails() + .get(lastIndex) + .getUseremail()); + } + + + @Test(expected = ResourceNotFoundException.class) + public void testDB_updateNotFound() { + Mockito.when(helperFunctions.isAuthorizedToMakeChange(anyString())) + .thenReturn(true); + + Role newRole = new Role("testnewuser"); + newRole.setRoleid(1); + + User newUser = new User("tata", + "password", + "tata@agency.com"); + newUser.getRoles() + .add(new UserRoles(newUser, newRole)); + newUser.getUseremails() + .add(new Useremail(newUser, "admin@agency.com")); + newUser.getUseremails() + .add(new Useremail(newUser, "manager@agency.com")); + + User updatedUser = userService.update(newUser, 8); + + int lastIndex = updatedUser.getUseremails() + .size() - 1; + assertEquals("manager@agency.com", + updatedUser.getUseremails() + .get(lastIndex) + .getUseremail()); + } + + @Test + public void ztestDB_deleteAll() { + userService.deleteAll(); + assertEquals(0, + userService.findAll().size()); + } + + @Test + public void testDB_findAllRoles() { + assertEquals(3, + roleService.findAll().size()); + } + + @Test + public void testDB_findRoleByName() { + assertEquals("testadmin".toUpperCase(), + roleService.findByName("testadmin".toUpperCase()).getName()); + } + + @Test(expected = ResourceNotFoundException.class) + public void testDB_findRoleByNameNotFound() { + assertEquals("bunnyrabbit".toUpperCase(), + roleService.findByName("bunnyrabbit".toUpperCase()).getName()); + } + + @Test(expected = ResourceFoundException.class) + public void testDB_saveRoleNotFound(){ + Role newRole = new Role("ADMIN"); + newRole.setRoleid(99); + + User newUser = new User("tagbokhana", + "password", + "tagbokhana@gmail.com"); + + User newUser2 = new User("tagbokhana", + "password", + "tagbokhana@gmail.com"); + newRole.getUsers() + .add(new UserRoles(newUser, newRole)); + newRole.getUsers() + .add(new UserRoles(newUser2, newRole)); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole)); + newUser.getUseremails() + .add(new Useremail(newUser, "tarah.agbokhana@gmail.com")); + + Role addRole = roleService.save(newRole); + + assertEquals(2, addRole.getUsers()); + } + + @Test(expected = ResourceNotFoundException.class) + public void testDB_updateRoleNotFound(){ + Mockito.when(helperFunctions.isAuthorizedToMakeChange(anyString())) + .thenReturn(true); + + User newUser = new User("tata", + "password", + "tata@agency.com"); + newUser.setUserid(48); + + Role newRole = new Role("testnewuser"); +// newRole.setRoleid(50); + + newUser.getRoles() + .add(new UserRoles(newUser, newRole)); + newUser.getUseremails() + .add(new Useremail(newUser, "admin@agency.com")); + newUser.getUseremails() + .add(new Useremail(newUser, "manager@agency.com")); + + Role updatedRole = roleService.update(8L, newRole); + + assertEquals(50, + updatedRole.getRoleid()); + } +} \ No newline at end of file diff --git a/usermodel-initial/src/test/resources/application.properties b/usermodel-initial/src/test/resources/application.properties new file mode 100644 index 0000000..617331c --- /dev/null +++ b/usermodel-initial/src/test/resources/application.properties @@ -0,0 +1,35 @@ +# 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=never +# +# 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 +#spring.batch.job.enabled=false \ No newline at end of file