From adbcd786f0cd20caca30f4621150678f769730c6 Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Tue, 2 Apr 2019 16:38:54 -0600 Subject: [PATCH] PAN-15 fix the request changes --- .../msudenver/tsp/services/UserService.java | 8 +- .../tsp/services/UserServiceTest.java | 221 +++++++++++++++++- .../website/controller/LogInController.java | 35 --- 3 files changed, 216 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/edu/msudenver/tsp/website/controller/LogInController.java diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index 593b0d9..d5333f0 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -27,7 +27,7 @@ public class UserService { } public Optional getListOfAccount(){ - final String auth = ""; + final String auth = null; final Instant start = Instant.now(); try { @@ -60,7 +60,7 @@ public class UserService { return Optional.empty(); } - final String auth = ""; + final String auth = null; final Instant start = Instant.now(); try { @@ -92,7 +92,7 @@ public class UserService { return Optional.empty(); } - final String auth = ""; + final String auth = null; final Instant start = Instant.now(); try { @@ -208,7 +208,7 @@ public class UserService { LOG.info("Returning {}", persistenceApiResponse); } else { - LOG.info("Unable to delete user {}", account.toString()); + LOG.info("Unable to delete user {}"); } return persistenceApiResponse; diff --git a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java index 4a478d9..6debc4a 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -13,10 +13,8 @@ import java.util.Date; import java.util.List; import java.util.Optional; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.when; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) public class UserServiceTest { @@ -39,10 +37,41 @@ public class UserServiceTest { assertEquals(accountList, response.get()); } + @Test + public void testGetListOfAccounts_persistenceApiResponseIsNotPresent(){ + final Account account1 = createAccount(); + final Account account2 = createAccount(); + final List accountList = new ArrayList<>(); + accountList.add(account1); + accountList.add(account2); + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.empty()); + final Optional response = userService.getListOfAccount(); + + assertFalse(response.isPresent()); + } + + @Test + public void testGetListOfAccounts_catchException(){ + final Account account1 = createAccount(); + final Account account2 = createAccount(); + final List accountList = new ArrayList<>(); + accountList.add(account1); + accountList.add(account2); + final Exception e = new Exception(); + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenThrow(Exception.class); + final Optional response = userService.getListOfAccount(); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + } + @Test public void testGetAccountById(){ final Account account = createAccount(); - account.setId(1); when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) .thenReturn(Optional.of(account)); @@ -50,20 +79,74 @@ public class UserServiceTest { assertTrue(response.isPresent()); assertEquals(account, response.get()); + verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); + } + + @Test + public void testGetAccountById_nullId() { + final Optional response = userService.getAccountById(0); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verifyZeroInteractions(restService); + } + + @Test + public void testGetAccountById_persistenceApiResponseIsNotPresent(){ + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.empty()); + final Optional response = userService.getAccountById(1); + + assertFalse(response.isPresent()); + verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); + } + + @Test + public void testGetAccountById_catchException(){ + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenThrow(Exception.class); + final Optional response = userService.getAccountById(1); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); } @Test public void testGetAccountByUsername(){ final Account account = createAccount(); - final String username = account.getUsername(); when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) .thenReturn(Optional.of(account)); - final Optional response = userService.getAccountByUsername(username); + final Optional response = userService.getAccountByUsername(account.getUsername()); assertTrue(response.isPresent()); assertEquals(account, response.get()); + verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); } + + @Test + public void testGetAccountByUsername_nullUsername(){ + final Optional response = userService.getAccountByUsername(null); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verifyZeroInteractions(restService); + } + + @Test + public void testGetAccountByUsername_persistenceApiResponseIsNotPresent(){ + final Account account = createAccount(); + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.empty()); + final Optional response = userService.getAccountByUsername(account.getUsername()); + + assertFalse(response.isPresent()); + verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); + } + @Test public void testCreateAccount(){ final Account account = createAccount(); @@ -74,6 +157,27 @@ public class UserServiceTest { assertTrue(response.isPresent()); assertEquals(account, response.get()); + verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testCreateAccount_NoAccountFound() { + final Optional response = userService.createAccount(null); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verify(restService, times(0)).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testCreateAccount_catchException(){ + final Account account = createAccount(); + + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class); + final Optional response = userService.createAccount(account); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); } @Test @@ -87,6 +191,54 @@ public class UserServiceTest { assertTrue(response.isPresent()); assertEquals(account, response.get()); + verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testUpdateAccount_nullAccount(){ + final Optional response = userService.updateAccount(null); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testUpdateAccount_nullId(){ + final Account account = createAccount(); + account.setId(0); + + final Optional response = userService.updateAccount(account); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testUpdateAccount_PersistenceApiResponseIsNotPresent(){ + final Account account = createAccount(); + account.setId(1); + + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + .thenReturn(Optional.empty()); + final Optional response = userService.updateAccount(account); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + } + + @Test + public void testUpdateAccount_catchException(){ + final Account account = createAccount(); + account.setId(1); + + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + .thenThrow(Exception.class); + final Optional response = userService.updateAccount(account); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); } @Test @@ -99,8 +251,59 @@ public class UserServiceTest { .thenReturn(response); final boolean persistenceApiResponse = userService.deleteAccount(account); - assertTrue(persistenceApiResponse ); - assertEquals(response, persistenceApiResponse ); + assertNotNull(persistenceApiResponse); + assertTrue(persistenceApiResponse); + assertEquals(response, persistenceApiResponse); + verify(restService).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteAccount_nullAccount(){ + final boolean persistenceApiResponse = userService.deleteAccount(null); + + assertNotNull(persistenceApiResponse); + assertFalse(persistenceApiResponse); + verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteAccount_nullId(){ + final Account account = createAccount(); + account.setId(0); + + final boolean persistenceApiResponse = userService.deleteAccount(account); + + assertNotNull(persistenceApiResponse); + assertFalse(persistenceApiResponse); + verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteAccount_persistenceApiResponseIsNotPresent(){ + final boolean response= false; + final Account account = createAccount(); + account.setId(1); + + when(restService.delete(anyString(), anyInt(), anyInt(), any())) + .thenReturn(response); + final boolean persistenceApiResponse = userService.deleteAccount(account); + + assertNotNull(persistenceApiResponse); + assertFalse(persistenceApiResponse); + assertEquals(response, persistenceApiResponse); + verify(restService).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteAccount_catchException(){ + final Account account = createAccount(); + account.setId(1); + + when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenThrow(Exception.class); + final boolean persistenceApiResponse = userService.deleteAccount(account); + + assertNotNull(persistenceApiResponse); + assertFalse(persistenceApiResponse); } diff --git a/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java b/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java deleted file mode 100644 index 70247ff..0000000 --- a/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java +++ /dev/null @@ -1,35 +0,0 @@ -package edu.msudenver.tsp.website.controller; - -import edu.msudenver.tsp.website.forms.TheoremForm; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.servlet.ModelAndView; - - - @Slf4j - @Controller - @AllArgsConstructor - @RequestMapping("/login") - public class LogInController { - @GetMapping({"/",""}) - public ModelAndView enterTheoremPage() { - LOG.info("Received request to display the theorem entry page: returning model with name 'Theorem'"); - return new ModelAndView("Theorem"); - } - - @PostMapping({"/",""}) - public String saveTheorem(@Validated final TheoremForm theoremForm, final Model model) { - model.addAttribute("theoremName", theoremForm.getTheoremName()); - model.addAttribute("theorem", theoremForm.getTheorem()); - LOG.info("Saving theorem {}...", theoremForm); - - return "success"; - } - } -}