PageRenderTime 460ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/alex/zakupki/web/rest/UserResourceIntTest.java

https://gitlab.com/bourd0n/zakupki
Java | 660 lines | 538 code | 84 blank | 38 comment | 0 complexity | 93b132ca4032eecbd7f2f8737f95d332 MD5 | raw file
  1. package com.alex.zakupki.web.rest;
  2. import com.alex.zakupki.ZakupkiApp;
  3. import com.alex.zakupki.domain.Authority;
  4. import com.alex.zakupki.domain.User;
  5. import com.alex.zakupki.repository.UserRepository;
  6. import com.alex.zakupki.repository.search.UserSearchRepository;
  7. import com.alex.zakupki.security.AuthoritiesConstants;
  8. import com.alex.zakupki.service.MailService;
  9. import com.alex.zakupki.service.UserService;
  10. import com.alex.zakupki.service.dto.UserDTO;
  11. import com.alex.zakupki.service.mapper.UserMapper;
  12. import com.alex.zakupki.web.rest.errors.ExceptionTranslator;
  13. import com.alex.zakupki.web.rest.vm.ManagedUserVM;
  14. import org.apache.commons.lang3.RandomStringUtils;
  15. import org.junit.Before;
  16. import org.junit.Test;
  17. import org.junit.runner.RunWith;
  18. import org.mockito.MockitoAnnotations;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.boot.test.context.SpringBootTest;
  21. import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
  22. import org.springframework.http.MediaType;
  23. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  24. import org.springframework.test.context.junit4.SpringRunner;
  25. import org.springframework.test.web.servlet.MockMvc;
  26. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import javax.persistence.EntityManager;
  29. import java.time.Instant;
  30. import java.util.Arrays;
  31. import java.util.HashSet;
  32. import java.util.List;
  33. import java.util.Set;
  34. import java.util.stream.Collectors;
  35. import java.util.stream.Stream;
  36. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.hamcrest.Matchers.hasItem;
  39. import static org.hamcrest.Matchers.containsInAnyOrder;
  40. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  41. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  42. /**
  43. * Test class for the UserResource REST controller.
  44. *
  45. * @see UserResource
  46. */
  47. @RunWith(SpringRunner.class)
  48. @SpringBootTest(classes = ZakupkiApp.class)
  49. public class UserResourceIntTest {
  50. private static final Long DEFAULT_ID = 1L;
  51. private static final String DEFAULT_LOGIN = "johndoe";
  52. private static final String UPDATED_LOGIN = "jhipster";
  53. private static final String DEFAULT_PASSWORD = "passjohndoe";
  54. private static final String UPDATED_PASSWORD = "passjhipster";
  55. private static final String DEFAULT_EMAIL = "johndoe@localhost";
  56. private static final String UPDATED_EMAIL = "jhipster@localhost";
  57. private static final String DEFAULT_FIRSTNAME = "john";
  58. private static final String UPDATED_FIRSTNAME = "jhipsterFirstName";
  59. private static final String DEFAULT_LASTNAME = "doe";
  60. private static final String UPDATED_LASTNAME = "jhipsterLastName";
  61. private static final String DEFAULT_IMAGEURL = "http://placehold.it/50x50";
  62. private static final String UPDATED_IMAGEURL = "http://placehold.it/40x40";
  63. private static final String DEFAULT_LANGKEY = "en";
  64. private static final String UPDATED_LANGKEY = "fr";
  65. @Autowired
  66. private UserRepository userRepository;
  67. @Autowired
  68. private UserSearchRepository userSearchRepository;
  69. @Autowired
  70. private MailService mailService;
  71. @Autowired
  72. private UserService userService;
  73. @Autowired
  74. private UserMapper userMapper;
  75. @Autowired
  76. private MappingJackson2HttpMessageConverter jacksonMessageConverter;
  77. @Autowired
  78. private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
  79. @Autowired
  80. private ExceptionTranslator exceptionTranslator;
  81. @Autowired
  82. private EntityManager em;
  83. private MockMvc restUserMockMvc;
  84. private User user;
  85. @Before
  86. public void setup() {
  87. MockitoAnnotations.initMocks(this);
  88. UserResource userResource = new UserResource(userRepository, mailService, userService, userSearchRepository);
  89. this.restUserMockMvc = MockMvcBuilders.standaloneSetup(userResource)
  90. .setCustomArgumentResolvers(pageableArgumentResolver)
  91. .setControllerAdvice(exceptionTranslator)
  92. .setMessageConverters(jacksonMessageConverter)
  93. .build();
  94. }
  95. /**
  96. * Create a User.
  97. *
  98. * This is a static method, as tests for other entities might also need it,
  99. * if they test an entity which has a required relationship to the User entity.
  100. */
  101. public static User createEntity(EntityManager em) {
  102. User user = new User();
  103. user.setLogin(DEFAULT_LOGIN);
  104. user.setPassword(RandomStringUtils.random(60));
  105. user.setActivated(true);
  106. user.setEmail(DEFAULT_EMAIL);
  107. user.setFirstName(DEFAULT_FIRSTNAME);
  108. user.setLastName(DEFAULT_LASTNAME);
  109. user.setImageUrl(DEFAULT_IMAGEURL);
  110. user.setLangKey(DEFAULT_LANGKEY);
  111. return user;
  112. }
  113. @Before
  114. public void initTest() {
  115. user = createEntity(em);
  116. }
  117. @Test
  118. @Transactional
  119. public void createUser() throws Exception {
  120. int databaseSizeBeforeCreate = userRepository.findAll().size();
  121. // Create the User
  122. Set<String> authorities = new HashSet<>();
  123. authorities.add("ROLE_USER");
  124. ManagedUserVM managedUserVM = new ManagedUserVM(
  125. null,
  126. DEFAULT_LOGIN,
  127. DEFAULT_PASSWORD,
  128. DEFAULT_FIRSTNAME,
  129. DEFAULT_LASTNAME,
  130. DEFAULT_EMAIL,
  131. true,
  132. DEFAULT_IMAGEURL,
  133. DEFAULT_LANGKEY,
  134. null,
  135. null,
  136. null,
  137. null,
  138. authorities);
  139. restUserMockMvc.perform(post("/api/users")
  140. .contentType(TestUtil.APPLICATION_JSON_UTF8)
  141. .content(TestUtil.convertObjectToJsonBytes(managedUserVM)))
  142. .andExpect(status().isCreated());
  143. // Validate the User in the database
  144. List<User> userList = userRepository.findAll();
  145. assertThat(userList).hasSize(databaseSizeBeforeCreate + 1);
  146. User testUser = userList.get(userList.size() - 1);
  147. assertThat(testUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
  148. assertThat(testUser.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
  149. assertThat(testUser.getLastName()).isEqualTo(DEFAULT_LASTNAME);
  150. assertThat(testUser.getEmail()).isEqualTo(DEFAULT_EMAIL);
  151. assertThat(testUser.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
  152. assertThat(testUser.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
  153. }
  154. @Test
  155. @Transactional
  156. public void createUserWithExistingId() throws Exception {
  157. int databaseSizeBeforeCreate = userRepository.findAll().size();
  158. Set<String> authorities = new HashSet<>();
  159. authorities.add("ROLE_USER");
  160. ManagedUserVM managedUserVM = new ManagedUserVM(
  161. 1L,
  162. DEFAULT_LOGIN,
  163. DEFAULT_PASSWORD,
  164. DEFAULT_FIRSTNAME,
  165. DEFAULT_LASTNAME,
  166. DEFAULT_EMAIL,
  167. true,
  168. DEFAULT_IMAGEURL,
  169. DEFAULT_LANGKEY,
  170. null,
  171. null,
  172. null,
  173. null,
  174. authorities);
  175. // An entity with an existing ID cannot be created, so this API call must fail
  176. restUserMockMvc.perform(post("/api/users")
  177. .contentType(TestUtil.APPLICATION_JSON_UTF8)
  178. .content(TestUtil.convertObjectToJsonBytes(managedUserVM)))
  179. .andExpect(status().isBadRequest());
  180. // Validate the User in the database
  181. List<User> userList = userRepository.findAll();
  182. assertThat(userList).hasSize(databaseSizeBeforeCreate);
  183. }
  184. @Test
  185. @Transactional
  186. public void createUserWithExistingLogin() throws Exception {
  187. // Initialize the database
  188. userRepository.saveAndFlush(user);
  189. userSearchRepository.save(user);
  190. int databaseSizeBeforeCreate = userRepository.findAll().size();
  191. Set<String> authorities = new HashSet<>();
  192. authorities.add("ROLE_USER");
  193. ManagedUserVM managedUserVM = new ManagedUserVM(
  194. null,
  195. DEFAULT_LOGIN, // this login should already be used
  196. DEFAULT_PASSWORD,
  197. DEFAULT_FIRSTNAME,
  198. DEFAULT_LASTNAME,
  199. "anothermail@localhost",
  200. true,
  201. DEFAULT_IMAGEURL,
  202. DEFAULT_LANGKEY,
  203. null,
  204. null,
  205. null,
  206. null,
  207. authorities);
  208. // Create the User
  209. restUserMockMvc.perform(post("/api/users")
  210. .contentType(TestUtil.APPLICATION_JSON_UTF8)
  211. .content(TestUtil.convertObjectToJsonBytes(managedUserVM)))
  212. .andExpect(status().isBadRequest());
  213. // Validate the User in the database
  214. List<User> userList = userRepository.findAll();
  215. assertThat(userList).hasSize(databaseSizeBeforeCreate);
  216. }
  217. @Test
  218. @Transactional
  219. public void createUserWithExistingEmail() throws Exception {
  220. // Initialize the database
  221. userRepository.saveAndFlush(user);
  222. userSearchRepository.save(user);
  223. int databaseSizeBeforeCreate = userRepository.findAll().size();
  224. Set<String> authorities = new HashSet<>();
  225. authorities.add("ROLE_USER");
  226. ManagedUserVM managedUserVM = new ManagedUserVM(
  227. null,
  228. "anotherlogin",
  229. DEFAULT_PASSWORD,
  230. DEFAULT_FIRSTNAME,
  231. DEFAULT_LASTNAME,
  232. DEFAULT_EMAIL, // this email should already be used
  233. true,
  234. DEFAULT_IMAGEURL,
  235. DEFAULT_LANGKEY,
  236. null,
  237. null,
  238. null,
  239. null,
  240. authorities);
  241. // Create the User
  242. restUserMockMvc.perform(post("/api/users")
  243. .contentType(TestUtil.APPLICATION_JSON_UTF8)
  244. .content(TestUtil.convertObjectToJsonBytes(managedUserVM)))
  245. .andExpect(status().isBadRequest());
  246. // Validate the User in the database
  247. List<User> userList = userRepository.findAll();
  248. assertThat(userList).hasSize(databaseSizeBeforeCreate);
  249. }
  250. @Test
  251. @Transactional
  252. public void getAllUsers() throws Exception {
  253. // Initialize the database
  254. userRepository.saveAndFlush(user);
  255. userSearchRepository.save(user);
  256. // Get all the users
  257. restUserMockMvc.perform(get("/api/users?sort=id,desc")
  258. .accept(MediaType.APPLICATION_JSON))
  259. .andExpect(status().isOk())
  260. .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  261. .andExpect(jsonPath("$.[*].login").value(hasItem(DEFAULT_LOGIN)))
  262. .andExpect(jsonPath("$.[*].firstName").value(hasItem(DEFAULT_FIRSTNAME)))
  263. .andExpect(jsonPath("$.[*].lastName").value(hasItem(DEFAULT_LASTNAME)))
  264. .andExpect(jsonPath("$.[*].email").value(hasItem(DEFAULT_EMAIL)))
  265. .andExpect(jsonPath("$.[*].imageUrl").value(hasItem(DEFAULT_IMAGEURL)))
  266. .andExpect(jsonPath("$.[*].langKey").value(hasItem(DEFAULT_LANGKEY)));
  267. }
  268. @Test
  269. @Transactional
  270. public void getUser() throws Exception {
  271. // Initialize the database
  272. userRepository.saveAndFlush(user);
  273. userSearchRepository.save(user);
  274. // Get the user
  275. restUserMockMvc.perform(get("/api/users/{login}", user.getLogin()))
  276. .andExpect(status().isOk())
  277. .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  278. .andExpect(jsonPath("$.login").value(user.getLogin()))
  279. .andExpect(jsonPath("$.firstName").value(DEFAULT_FIRSTNAME))
  280. .andExpect(jsonPath("$.lastName").value(DEFAULT_LASTNAME))
  281. .andExpect(jsonPath("$.email").value(DEFAULT_EMAIL))
  282. .andExpect(jsonPath("$.imageUrl").value(DEFAULT_IMAGEURL))
  283. .andExpect(jsonPath("$.langKey").value(DEFAULT_LANGKEY));
  284. }
  285. @Test
  286. @Transactional
  287. public void getNonExistingUser() throws Exception {
  288. restUserMockMvc.perform(get("/api/users/unknown"))
  289. .andExpect(status().isNotFound());
  290. }
  291. @Test
  292. @Transactional
  293. public void updateUser() throws Exception {
  294. // Initialize the database
  295. userRepository.saveAndFlush(user);
  296. userSearchRepository.save(user);
  297. int databaseSizeBeforeUpdate = userRepository.findAll().size();
  298. // Update the user
  299. User updatedUser = userRepository.findOne(user.getId());
  300. Set<String> authorities = new HashSet<>();
  301. authorities.add("ROLE_USER");
  302. ManagedUserVM managedUserVM = new ManagedUserVM(
  303. updatedUser.getId(),
  304. updatedUser.getLogin(),
  305. UPDATED_PASSWORD,
  306. UPDATED_FIRSTNAME,
  307. UPDATED_LASTNAME,
  308. UPDATED_EMAIL,
  309. updatedUser.getActivated(),
  310. UPDATED_IMAGEURL,
  311. UPDATED_LANGKEY,
  312. updatedUser.getCreatedBy(),
  313. updatedUser.getCreatedDate(),
  314. updatedUser.getLastModifiedBy(),
  315. updatedUser.getLastModifiedDate(),
  316. authorities);
  317. restUserMockMvc.perform(put("/api/users")
  318. .contentType(TestUtil.APPLICATION_JSON_UTF8)
  319. .content(TestUtil.convertObjectToJsonBytes(managedUserVM)))
  320. .andExpect(status().isOk());
  321. // Validate the User in the database
  322. List<User> userList = userRepository.findAll();
  323. assertThat(userList).hasSize(databaseSizeBeforeUpdate);
  324. User testUser = userList.get(userList.size() - 1);
  325. assertThat(testUser.getFirstName()).isEqualTo(UPDATED_FIRSTNAME);
  326. assertThat(testUser.getLastName()).isEqualTo(UPDATED_LASTNAME);
  327. assertThat(testUser.getEmail()).isEqualTo(UPDATED_EMAIL);
  328. assertThat(testUser.getImageUrl()).isEqualTo(UPDATED_IMAGEURL);
  329. assertThat(testUser.getLangKey()).isEqualTo(UPDATED_LANGKEY);
  330. }
  331. @Test
  332. @Transactional
  333. public void updateUserLogin() throws Exception {
  334. // Initialize the database
  335. userRepository.saveAndFlush(user);
  336. userSearchRepository.save(user);
  337. int databaseSizeBeforeUpdate = userRepository.findAll().size();
  338. // Update the user
  339. User updatedUser = userRepository.findOne(user.getId());
  340. Set<String> authorities = new HashSet<>();
  341. authorities.add("ROLE_USER");
  342. ManagedUserVM managedUserVM = new ManagedUserVM(
  343. updatedUser.getId(),
  344. UPDATED_LOGIN,
  345. UPDATED_PASSWORD,
  346. UPDATED_FIRSTNAME,
  347. UPDATED_LASTNAME,
  348. UPDATED_EMAIL,
  349. updatedUser.getActivated(),
  350. UPDATED_IMAGEURL,
  351. UPDATED_LANGKEY,
  352. updatedUser.getCreatedBy(),
  353. updatedUser.getCreatedDate(),
  354. updatedUser.getLastModifiedBy(),
  355. updatedUser.getLastModifiedDate(),
  356. authorities);
  357. restUserMockMvc.perform(put("/api/users")
  358. .contentType(TestUtil.APPLICATION_JSON_UTF8)
  359. .content(TestUtil.convertObjectToJsonBytes(managedUserVM)))
  360. .andExpect(status().isOk());
  361. // Validate the User in the database
  362. List<User> userList = userRepository.findAll();
  363. assertThat(userList).hasSize(databaseSizeBeforeUpdate);
  364. User testUser = userList.get(userList.size() - 1);
  365. assertThat(testUser.getLogin()).isEqualTo(UPDATED_LOGIN);
  366. assertThat(testUser.getFirstName()).isEqualTo(UPDATED_FIRSTNAME);
  367. assertThat(testUser.getLastName()).isEqualTo(UPDATED_LASTNAME);
  368. assertThat(testUser.getEmail()).isEqualTo(UPDATED_EMAIL);
  369. assertThat(testUser.getImageUrl()).isEqualTo(UPDATED_IMAGEURL);
  370. assertThat(testUser.getLangKey()).isEqualTo(UPDATED_LANGKEY);
  371. }
  372. @Test
  373. @Transactional
  374. public void updateUserExistingEmail() throws Exception {
  375. // Initialize the database with 2 users
  376. userRepository.saveAndFlush(user);
  377. userSearchRepository.save(user);
  378. User anotherUser = new User();
  379. anotherUser.setLogin("jhipster");
  380. anotherUser.setPassword(RandomStringUtils.random(60));
  381. anotherUser.setActivated(true);
  382. anotherUser.setEmail("jhipster@localhost");
  383. anotherUser.setFirstName("java");
  384. anotherUser.setLastName("hipster");
  385. anotherUser.setImageUrl("");
  386. anotherUser.setLangKey("en");
  387. userRepository.saveAndFlush(anotherUser);
  388. userSearchRepository.save(anotherUser);
  389. // Update the user
  390. User updatedUser = userRepository.findOne(user.getId());
  391. Set<String> authorities = new HashSet<>();
  392. authorities.add("ROLE_USER");
  393. ManagedUserVM managedUserVM = new ManagedUserVM(
  394. updatedUser.getId(),
  395. updatedUser.getLogin(),
  396. updatedUser.getPassword(),
  397. updatedUser.getFirstName(),
  398. updatedUser.getLastName(),
  399. "jhipster@localhost", // this email should already be used by anotherUser
  400. updatedUser.getActivated(),
  401. updatedUser.getImageUrl(),
  402. updatedUser.getLangKey(),
  403. updatedUser.getCreatedBy(),
  404. updatedUser.getCreatedDate(),
  405. updatedUser.getLastModifiedBy(),
  406. updatedUser.getLastModifiedDate(),
  407. authorities);
  408. restUserMockMvc.perform(put("/api/users")
  409. .contentType(TestUtil.APPLICATION_JSON_UTF8)
  410. .content(TestUtil.convertObjectToJsonBytes(managedUserVM)))
  411. .andExpect(status().isBadRequest());
  412. }
  413. @Test
  414. @Transactional
  415. public void updateUserExistingLogin() throws Exception {
  416. // Initialize the database
  417. userRepository.saveAndFlush(user);
  418. userSearchRepository.save(user);
  419. User anotherUser = new User();
  420. anotherUser.setLogin("jhipster");
  421. anotherUser.setPassword(RandomStringUtils.random(60));
  422. anotherUser.setActivated(true);
  423. anotherUser.setEmail("jhipster@localhost");
  424. anotherUser.setFirstName("java");
  425. anotherUser.setLastName("hipster");
  426. anotherUser.setImageUrl("");
  427. anotherUser.setLangKey("en");
  428. userRepository.saveAndFlush(anotherUser);
  429. userSearchRepository.save(anotherUser);
  430. // Update the user
  431. User updatedUser = userRepository.findOne(user.getId());
  432. Set<String> authorities = new HashSet<>();
  433. authorities.add("ROLE_USER");
  434. ManagedUserVM managedUserVM = new ManagedUserVM(
  435. updatedUser.getId(),
  436. "jhipster", // this login should already be used by anotherUser
  437. updatedUser.getPassword(),
  438. updatedUser.getFirstName(),
  439. updatedUser.getLastName(),
  440. updatedUser.getEmail(),
  441. updatedUser.getActivated(),
  442. updatedUser.getImageUrl(),
  443. updatedUser.getLangKey(),
  444. updatedUser.getCreatedBy(),
  445. updatedUser.getCreatedDate(),
  446. updatedUser.getLastModifiedBy(),
  447. updatedUser.getLastModifiedDate(),
  448. authorities);
  449. restUserMockMvc.perform(put("/api/users")
  450. .contentType(TestUtil.APPLICATION_JSON_UTF8)
  451. .content(TestUtil.convertObjectToJsonBytes(managedUserVM)))
  452. .andExpect(status().isBadRequest());
  453. }
  454. @Test
  455. @Transactional
  456. public void deleteUser() throws Exception {
  457. // Initialize the database
  458. userRepository.saveAndFlush(user);
  459. userSearchRepository.save(user);
  460. int databaseSizeBeforeDelete = userRepository.findAll().size();
  461. // Delete the user
  462. restUserMockMvc.perform(delete("/api/users/{login}", user.getLogin())
  463. .accept(TestUtil.APPLICATION_JSON_UTF8))
  464. .andExpect(status().isOk());
  465. // Validate the database is empty
  466. List<User> userList = userRepository.findAll();
  467. assertThat(userList).hasSize(databaseSizeBeforeDelete - 1);
  468. }
  469. @Test
  470. @Transactional
  471. public void getAllAuthorities() throws Exception {
  472. restUserMockMvc.perform(get("/api/users/authorities")
  473. .accept(TestUtil.APPLICATION_JSON_UTF8)
  474. .contentType(TestUtil.APPLICATION_JSON_UTF8))
  475. .andExpect(status().isOk())
  476. .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  477. .andExpect(jsonPath("$").isArray())
  478. .andExpect(jsonPath("$").value(containsInAnyOrder("ROLE_USER", "ROLE_ADMIN")));
  479. }
  480. @Test
  481. @Transactional
  482. public void testUserEquals() throws Exception {
  483. TestUtil.equalsVerifier(User.class);
  484. User user1 = new User();
  485. user1.setId(1L);
  486. User user2 = new User();
  487. user2.setId(user1.getId());
  488. assertThat(user1).isEqualTo(user2);
  489. user2.setId(2L);
  490. assertThat(user1).isNotEqualTo(user2);
  491. user1.setId(null);
  492. assertThat(user1).isNotEqualTo(user2);
  493. }
  494. @Test
  495. public void testUserFromId() {
  496. assertThat(userMapper.userFromId(DEFAULT_ID).getId()).isEqualTo(DEFAULT_ID);
  497. assertThat(userMapper.userFromId(null)).isNull();
  498. }
  499. @Test
  500. public void testUserDTOtoUser() {
  501. UserDTO userDTO = new UserDTO(
  502. DEFAULT_ID,
  503. DEFAULT_LOGIN,
  504. DEFAULT_FIRSTNAME,
  505. DEFAULT_LASTNAME,
  506. DEFAULT_EMAIL,
  507. true,
  508. DEFAULT_IMAGEURL,
  509. DEFAULT_LANGKEY,
  510. DEFAULT_LOGIN,
  511. null,
  512. DEFAULT_LOGIN,
  513. null,
  514. Stream.of(AuthoritiesConstants.USER).collect(Collectors.toSet()));
  515. User user = userMapper.userDTOToUser(userDTO);
  516. assertThat(user.getId()).isEqualTo(DEFAULT_ID);
  517. assertThat(user.getLogin()).isEqualTo(DEFAULT_LOGIN);
  518. assertThat(user.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
  519. assertThat(user.getLastName()).isEqualTo(DEFAULT_LASTNAME);
  520. assertThat(user.getEmail()).isEqualTo(DEFAULT_EMAIL);
  521. assertThat(user.getActivated()).isEqualTo(true);
  522. assertThat(user.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
  523. assertThat(user.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
  524. assertThat(user.getCreatedBy()).isNull();
  525. assertThat(user.getCreatedDate()).isNotNull();
  526. assertThat(user.getLastModifiedBy()).isNull();
  527. assertThat(user.getLastModifiedDate()).isNotNull();
  528. assertThat(user.getAuthorities()).extracting("name").containsExactly(AuthoritiesConstants.USER);
  529. }
  530. @Test
  531. public void testUserToUserDTO() {
  532. user.setId(DEFAULT_ID);
  533. user.setCreatedBy(DEFAULT_LOGIN);
  534. user.setCreatedDate(Instant.now());
  535. user.setLastModifiedBy(DEFAULT_LOGIN);
  536. user.setLastModifiedDate(Instant.now());
  537. Set<Authority> authorities = new HashSet<>();
  538. Authority authority = new Authority();
  539. authority.setName(AuthoritiesConstants.USER);
  540. authorities.add(authority);
  541. user.setAuthorities(authorities);
  542. UserDTO userDTO = userMapper.userToUserDTO(user);
  543. assertThat(userDTO.getId()).isEqualTo(DEFAULT_ID);
  544. assertThat(userDTO.getLogin()).isEqualTo(DEFAULT_LOGIN);
  545. assertThat(userDTO.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
  546. assertThat(userDTO.getLastName()).isEqualTo(DEFAULT_LASTNAME);
  547. assertThat(userDTO.getEmail()).isEqualTo(DEFAULT_EMAIL);
  548. assertThat(userDTO.isActivated()).isEqualTo(true);
  549. assertThat(userDTO.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
  550. assertThat(userDTO.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
  551. assertThat(userDTO.getCreatedBy()).isEqualTo(DEFAULT_LOGIN);
  552. assertThat(userDTO.getCreatedDate()).isEqualTo(user.getCreatedDate());
  553. assertThat(userDTO.getLastModifiedBy()).isEqualTo(DEFAULT_LOGIN);
  554. assertThat(userDTO.getLastModifiedDate()).isEqualTo(user.getLastModifiedDate());
  555. assertThat(userDTO.getAuthorities()).containsExactly(AuthoritiesConstants.USER);
  556. assertThat(userDTO.toString()).isNotNull();
  557. }
  558. @Test
  559. public void testAuthorityEquals() throws Exception {
  560. Authority authorityA = new Authority();
  561. assertThat(authorityA).isEqualTo(authorityA);
  562. assertThat(authorityA).isNotEqualTo(null);
  563. assertThat(authorityA).isNotEqualTo(new Object());
  564. assertThat(authorityA.hashCode()).isEqualTo(0);
  565. assertThat(authorityA.toString()).isNotNull();
  566. Authority authorityB = new Authority();
  567. assertThat(authorityA).isEqualTo(authorityB);
  568. authorityB.setName(AuthoritiesConstants.ADMIN);
  569. assertThat(authorityA).isNotEqualTo(authorityB);
  570. authorityA.setName(AuthoritiesConstants.USER);
  571. assertThat(authorityA).isNotEqualTo(authorityB);
  572. authorityB.setName(AuthoritiesConstants.USER);
  573. assertThat(authorityA).isEqualTo(authorityB);
  574. assertThat(authorityA.hashCode()).isEqualTo(authorityB.hashCode());
  575. }
  576. }