Advertisement
Guest User

Untitled

a guest
May 23rd, 2020
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.14 KB | None | 0 0
  1. import com.webbank.configuration.AppConfig;
  2. import com.webbank.controller.AppController;
  3. import com.webbank.dao.*;
  4. import com.webbank.model.*;
  5. import com.webbank.service.*;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.junit.runner.RunWith;
  9. import org.mockito.InjectMocks;
  10. import org.mockito.Mock;
  11. import org.mockito.MockitoAnnotations;
  12. import org.mockito.Spy;
  13. import org.springframework.context.MessageSource;
  14. import org.springframework.http.MediaType;
  15. import org.springframework.mock.web.MockHttpSession;
  16. import org.springframework.security.authentication.AuthenticationTrustResolver;
  17. import org.springframework.security.core.Authentication;
  18. import org.springframework.security.core.context.SecurityContext;
  19. import org.springframework.security.core.context.SecurityContextHolder;
  20. import org.springframework.test.context.ContextConfiguration;
  21. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  22. import org.springframework.test.context.web.WebAppConfiguration;
  23. import org.springframework.test.util.ReflectionTestUtils;
  24. import org.springframework.test.web.servlet.MockMvc;
  25. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  26. import org.springframework.ui.ModelMap;
  27.  
  28. import javax.servlet.http.HttpServletRequest;
  29. import java.util.HashMap;
  30. import java.util.HashSet;
  31. import java.util.Map;
  32. import java.util.Set;
  33.  
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.when;
  36. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  37. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  38. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  39.  
  40. @RunWith(SpringJUnit4ClassRunner.class)
  41. @ContextConfiguration(classes = { AppConfig.class })
  42. @WebAppConfiguration
  43. public class SpringTest {
  44. private MockMvc mockMvc;
  45.  
  46. @Mock
  47. private UserService userService;
  48. @Mock
  49. MessageSource messageSource;
  50. @Mock
  51. UserProfileServiceImpl userProfileService;
  52. @Mock
  53. SecurityServiceImpl securityService;
  54. @Mock
  55. AuthenticationTrustResolver authenticationTrustResolver;
  56. @Mock
  57. HttpServletRequest request;
  58. @Mock
  59. ModelMap model;
  60. @Spy
  61. BusinessServiceImpl businessService;
  62.  
  63.  
  64. @Spy
  65. @InjectMocks
  66. private AppController userController;
  67.  
  68. @Before
  69. public void init(){
  70. MockitoAnnotations.initMocks(this);
  71. mockMvc = MockMvcBuilders
  72. .standaloneSetup(userController)
  73. .build();
  74.  
  75. ReflectionTestUtils.setField(businessService, "accountDao", mock(AccountDao.class));
  76. ReflectionTestUtils.setField(businessService, "cardDao", mock(CardDao.class));
  77. ReflectionTestUtils.setField(businessService, "paymentDao", mock(PaymentDao.class));
  78. ReflectionTestUtils.setField(businessService, "bankDao", mock(BankDao.class));
  79. }
  80. public void setSecurityContext(String role){
  81. UserProfile userProfile = new UserProfile();
  82. userProfile.setType(role);
  83. User user = new User();
  84. user.setUsername("xyz");
  85. Set<UserProfile> userProfileSet = new HashSet<>();
  86. userProfileSet.add(userProfile);
  87. user.setUserProfiles(userProfileSet);
  88. when(userService.findByUsername("xyz")).thenReturn(user);
  89. when(userProfileService.findByType("User")).thenReturn(userProfile);
  90. //Authentication auth = mock(Authentication.class);
  91. Authentication authentication = mock(Authentication.class);
  92. SecurityContext securityContext = mock(SecurityContext.class);
  93. when(authentication.getPrincipal()).thenReturn("xyz");
  94. when(securityContext.getAuthentication()).thenReturn(authentication);
  95. SecurityContextHolder.setContext(securityContext);
  96.  
  97. }
  98. @Test
  99. public void submitRegistration() throws Exception {
  100. //boolean b = userService.isUserNameUnique("xyz");
  101. //boolean a = userService.isUserNameUnique("x");
  102. when(userService.isUserNameUnique("xyz")).thenReturn(true);
  103. when(userProfileService.findByType("User")).thenReturn(new UserProfile());
  104. this.mockMvc
  105. .perform(
  106. post("/newuser")
  107. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  108. .param("name", "xyz")
  109. .param("username", "xyz")
  110. .param("password", "xyz")
  111. )
  112. .andExpect(status().isOk());
  113. //userService.deleteUserById(userService.findByUsername("xyz").getId());
  114. }
  115. @Test
  116. public void submitRegistrationWithWrongName() throws Exception {
  117. //boolean b = userService.isUserNameUnique("xyz");
  118. //boolean a = userService.isUserNameUnique("x");
  119. when(userService.isUserNameUnique("xyz")).thenReturn(false);
  120. this.mockMvc
  121. .perform(
  122. post("/newuser")
  123. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  124. .param("name", "xyz")
  125. .param("username", "xyz")
  126. .param("password", "xyz")
  127. )
  128. .andExpect(status().isOk());
  129. //userService.deleteUserById(userService.findByUsername("xyz").getId());
  130. }
  131. @Test
  132. public void loginUser() throws Exception {
  133. setSecurityContext("USER");
  134.  
  135. this.mockMvc
  136. .perform(
  137. get("/login")
  138. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  139. .param("username", "xyz")
  140. .param("password", "xyz")
  141. )
  142. .andExpect(redirectedUrl("/userPage"))
  143. .andExpect(status().is3xxRedirection());
  144. }
  145. @Test
  146. public void loginAdmin() throws Exception {
  147. setSecurityContext("ADMIN");
  148. this.mockMvc
  149. .perform(
  150. get("/login")
  151. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  152. .param("username", "xyz")
  153. .param("password", "xyz")
  154. )
  155. .andExpect(redirectedUrl("/adminPage"))
  156. .andExpect(status().is3xxRedirection());
  157. }
  158.  
  159. @Test
  160. public void PayNotEnoughMoney() throws Exception {
  161. setSecurityContext("USER");
  162. Map<Card, Account> cardAccountMap = new HashMap<>();
  163. Card card = new Card();
  164. card.setCardNumber(123);
  165. Account account = new Account();
  166. account.setMoneyAmount(1);
  167. cardAccountMap.put(card, account);
  168. MockHttpSession session = mock(MockHttpSession.class);
  169. when(session.getAttribute("cardAccountMap")).thenReturn(cardAccountMap);
  170. this.mockMvc
  171. .perform(
  172. post("/operation")
  173. .session(session)
  174. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  175. .param("money", "10000")
  176. .param("info", "info")
  177. .param("card", "123")
  178. .param("command", "Pay")
  179. )
  180. .andExpect(model().attribute("warning", "Warning: not enough money"))
  181. .andExpect(status().isOk());
  182. }
  183. @Test
  184. public void PayBlockedCard() throws Exception {
  185. setSecurityContext("USER");
  186. Map<Card, Account> cardAccountMap = new HashMap<>();
  187. Card card = new Card();
  188. card.setCardNumber(123);
  189. Account account = new Account();
  190. account.setMoneyAmount(10);
  191. cardAccountMap.put(card, account);
  192. MockHttpSession session = mock(MockHttpSession.class);
  193. when(session.getAttribute("cardAccountMap")).thenReturn(cardAccountMap);
  194. this.mockMvc
  195. .perform(
  196. post("/operation")
  197. .session(session)
  198. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  199. .param("card", "123")
  200. .param("command", "Block")
  201. )
  202. .andExpect(model().attribute("warning", "Warning: blocked"))
  203. .andExpect(status().isOk());
  204. this.mockMvc
  205. .perform(
  206. post("/operation")
  207. .session(session)
  208. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  209. .param("money", "1")
  210. .param("info", "info")
  211. .param("card", "123")
  212. .param("command", "Pay")
  213. )
  214. .andExpect(model().attribute("warning", "Warning: Account is blocked"))
  215. .andExpect(status().isOk());
  216. }
  217. @Test
  218. public void PaySuccess() throws Exception {
  219. setSecurityContext("USER");
  220. Map<Card, Account> cardAccountMap = new HashMap<>();
  221. Card card = new Card();
  222. card.setCardNumber(123);
  223. Account account = new Account();
  224. account.setMoneyAmount(10);
  225. cardAccountMap.put(card, account);
  226. MockHttpSession session = mock(MockHttpSession.class);
  227. when(session.getAttribute("cardAccountMap")).thenReturn(cardAccountMap);
  228. User user = new User();
  229. user.setUsername("xyz");
  230. user.setId(1);
  231. when(session.getAttribute("User")).thenReturn(user);
  232. when(session.getAttribute("cardAccountMap")).thenReturn(cardAccountMap);
  233. this.mockMvc
  234. .perform(
  235. post("/operation")
  236. .session(session)
  237. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  238. .param("money", "1")
  239. .param("info", "info")
  240. .param("card", "123")
  241. .param("command", "Pay")
  242. )
  243. .andExpect(model().attribute("testPayment", "info 1 9 123"))
  244. .andExpect(status().isOk());
  245. }
  246. @Test
  247. public void TopUpBlockedCard() throws Exception {
  248. setSecurityContext("USER");
  249. Map<Card, Account> cardAccountMap = new HashMap<>();
  250. Card card = new Card();
  251. card.setCardNumber(123);
  252. Account account = new Account();
  253. account.setMoneyAmount(10);
  254. cardAccountMap.put(card, account);
  255. MockHttpSession session = mock(MockHttpSession.class);
  256. when(session.getAttribute("cardAccountMap")).thenReturn(cardAccountMap);
  257. this.mockMvc
  258. .perform(
  259. post("/operation")
  260. .session(session)
  261. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  262. .param("card", "123")
  263. .param("command", "Block")
  264. )
  265. .andExpect(model().attribute("warning", "Warning: blocked"))
  266. .andExpect(status().isOk());
  267. this.mockMvc
  268. .perform(
  269. post("/operation")
  270. .session(session)
  271. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  272. .param("money", "1")
  273. .param("info", "info")
  274. .param("card", "123")
  275. .param("command", "TopUp")
  276. )
  277. .andExpect(model().attribute("warning", "Warning: Account is blocked"))
  278. .andExpect(status().isOk());
  279. }
  280. @Test
  281. public void TopUpSuccess() throws Exception {
  282. setSecurityContext("USER");
  283. Map<Card, Account> cardAccountMap = new HashMap<>();
  284. Card card = new Card();
  285. card.setCardNumber(123);
  286. Account account = new Account();
  287. account.setMoneyAmount(10);
  288. cardAccountMap.put(card, account);
  289. MockHttpSession session = mock(MockHttpSession.class);
  290. when(session.getAttribute("cardAccountMap")).thenReturn(cardAccountMap);
  291. User user = new User();
  292. user.setUsername("xyz");
  293. user.setId(1);
  294. when(session.getAttribute("User")).thenReturn(user);
  295. this.mockMvc
  296. .perform(
  297. post("/operation")
  298. .session(session)
  299. .contentType(MediaType.APPLICATION_FORM_URLENCODED)
  300. .param("money", "1")
  301. .param("info", "info")
  302. .param("card", "123")
  303. .param("command", "TopUp")
  304. )
  305. .andExpect(model().attribute("topup", "+1$ to account."))
  306. .andExpect(status().isOk());
  307.  
  308. }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement