Search in sources :

Example 6 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class UserResource method createUser.

/**
 * POST  /users  : Creates a new user.
 * <p>
 * Creates a new user if the login and email are not already used, and sends an
 * mail with an activation link.
 * The user needs to be activated on creation.
 * </p>
 *
 * @param managedUserVM the user to create
 * @return the ResponseEntity with status 201 (Created) and with body the new user, or with status 400 (Bad Request) if the login or email is already in use
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/users")
@Timed
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity createUser(@RequestBody ManagedUserVM managedUserVM) throws URISyntaxException {
    log.debug("REST request to save User : {}", managedUserVM);
    if (managedUserVM.getId() != null) {
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new user cannot already have an ID")).body(null);
    // Lowercase the user login before comparing with database
    } else if (userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).isPresent()) {
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "userexists", "Login already in use")).body(null);
    } else if (userRepository.findOneByEmail(managedUserVM.getEmail()).isPresent()) {
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "emailexists", "Email already in use")).body(null);
    } else {
        User newUser = userService.createUser(managedUserVM);
        mailService.sendCreationEmail(newUser);
        return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin())).headers(HeaderUtil.createAlert("userManagement.created", newUser.getLogin())).body(newUser);
    }
}
Also used : User(com.baeldung.domain.User) URI(java.net.URI) Secured(org.springframework.security.access.annotation.Secured) Timed(com.codahale.metrics.annotation.Timed)

Example 7 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class UserResourceIntegrationTest method equalsVerifier.

@Test
@Transactional
public void equalsVerifier() throws Exception {
    User userA = new User();
    userA.setLogin("AAA");
    User userB = new User();
    userB.setLogin("BBB");
    assertThat(userA).isNotEqualTo(userB);
}
Also used : User(com.baeldung.domain.User) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class UserResourceIntegrationTest method updateUser.

@Test
@Transactional
public void updateUser() throws Exception {
    // Initialize the database
    userRepository.saveAndFlush(user);
    int databaseSizeBeforeUpdate = userRepository.findAll().size();
    // Update the user
    User updatedUser = userRepository.findOne(user.getId());
    Set<String> autorities = new HashSet<>();
    autorities.add("ROLE_USER");
    ManagedUserVM managedUserVM = new ManagedUserVM(updatedUser.getId(), updatedUser.getLogin(), UPDATED_PASSWORD, UPDATED_FIRSTNAME, UPDATED_LASTNAME, UPDATED_EMAIL, updatedUser.getActivated(), UPDATED_IMAGEURL, UPDATED_LANGKEY, updatedUser.getCreatedBy(), updatedUser.getCreatedDate(), updatedUser.getLastModifiedBy(), updatedUser.getLastModifiedDate(), autorities);
    restUserMockMvc.perform(put("/api/users").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(managedUserVM))).andExpect(status().isOk());
    // Validate the User in the database
    List<User> userList = userRepository.findAll();
    assertThat(userList).hasSize(databaseSizeBeforeUpdate);
    User testUser = userList.get(userList.size() - 1);
    assertThat(testUser.getFirstName()).isEqualTo(UPDATED_FIRSTNAME);
    assertThat(testUser.getLastName()).isEqualTo(UPDATED_LASTNAME);
    assertThat(testUser.getEmail()).isEqualTo(UPDATED_EMAIL);
    assertThat(testUser.getImageUrl()).isEqualTo(UPDATED_IMAGEURL);
    assertThat(testUser.getLangKey()).isEqualTo(UPDATED_LANGKEY);
}
Also used : User(com.baeldung.domain.User) ManagedUserVM(com.baeldung.web.rest.vm.ManagedUserVM) HashSet(java.util.HashSet) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class UserResourceIntegrationTest method createUser.

@Test
@Transactional
public void createUser() throws Exception {
    int databaseSizeBeforeCreate = userRepository.findAll().size();
    // Create the User
    Set<String> autorities = new HashSet<>();
    autorities.add("ROLE_USER");
    ManagedUserVM managedUserVM = new ManagedUserVM(null, DEFAULT_LOGIN, DEFAULT_PASSWORD, DEFAULT_FIRSTNAME, DEFAULT_LASTNAME, DEFAULT_EMAIL, true, DEFAULT_IMAGEURL, DEFAULT_LANGKEY, null, null, null, null, autorities);
    restUserMockMvc.perform(post("/api/users").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(managedUserVM))).andExpect(status().isCreated());
    // Validate the User in the database
    List<User> userList = userRepository.findAll();
    assertThat(userList).hasSize(databaseSizeBeforeCreate + 1);
    User testUser = userList.get(userList.size() - 1);
    assertThat(testUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
    assertThat(testUser.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
    assertThat(testUser.getLastName()).isEqualTo(DEFAULT_LASTNAME);
    assertThat(testUser.getEmail()).isEqualTo(DEFAULT_EMAIL);
    assertThat(testUser.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
    assertThat(testUser.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
}
Also used : User(com.baeldung.domain.User) ManagedUserVM(com.baeldung.web.rest.vm.ManagedUserVM) HashSet(java.util.HashSet) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class UserMapper method userFromId.

default User userFromId(Long id) {
    if (id == null) {
        return null;
    }
    User user = new User();
    user.setId(id);
    return user;
}
Also used : User(com.baeldung.domain.User)

Aggregations

User (com.baeldung.domain.User)32 Test (org.junit.Test)23 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)23 Transactional (org.springframework.transaction.annotation.Transactional)18 ManagedUserVM (com.baeldung.web.rest.vm.ManagedUserVM)16 HashSet (java.util.HashSet)8 ZonedDateTime (java.time.ZonedDateTime)5 Authority (com.baeldung.domain.Authority)3 UserRepository (com.baeldung.repository.UserRepository)2 UserDTO (com.baeldung.service.dto.UserDTO)2 Timed (com.codahale.metrics.annotation.Timed)2 java.util (java.util)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Post (com.baeldung.domain.Post)1 SecurityUtils (com.baeldung.security.SecurityUtils)1 MailService (com.baeldung.service.MailService)1 UserService (com.baeldung.service.UserService)1 HeaderUtil (com.baeldung.web.rest.util.HeaderUtil)1 KeyAndPasswordVM (com.baeldung.web.rest.vm.KeyAndPasswordVM)1