Skip to content

Commit

Permalink
Merge pull request #106 from jhipster/issue-79-entity-related-user
Browse files Browse the repository at this point in the history
Fixes relationships between entities and User
  • Loading branch information
JasonTypesCodes authored Jun 18, 2020
2 parents 538245e + 4d424d2 commit 8273cfc
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 17 deletions.
24 changes: 9 additions & 15 deletions generators/server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,19 +681,13 @@ const serverFiles = {
useBluePrint: true,
},
{
file: 'package/web/rest/package-info.java',
renameTo: generator => `${generator.javaDir}web/rest/package-info.java`,
file: 'package/web/rest/UserResource.java',
renameTo: generator => `${generator.javaDir}web/rest/UserResource.java`,
useBluePrint: true,
},
],
},
{
condition: generator => generator.authenticationType !== 'oauth2',
path: SERVER_MAIN_SRC_DIR,
templates: [
{
file: 'package/web/rest/UserResource.java',
renameTo: generator => `${generator.javaDir}web/rest/UserResource.java`,
file: 'package/web/rest/package-info.java',
renameTo: generator => `${generator.javaDir}web/rest/package-info.java`,
useBluePrint: true,
},
],
Expand Down Expand Up @@ -817,6 +811,11 @@ const serverFiles = {
renameTo: generator => `${generator.javaDir}web/rest/ClientForwardControllerIT.java`,
useBluePrint: true,
},
{
file: 'package/web/rest/UserResourceIT.java',
renameTo: generator => `${generator.javaDir}web/rest/UserResourceIT.java`,
useBluePrint: true,
},
{
file: 'package/web/rest/TestUtil.java',
renameTo: generator => `${generator.javaDir}web/rest/TestUtil.java`,
Expand All @@ -833,11 +832,6 @@ const serverFiles = {
renameTo: generator => `${generator.javaDir}web/rest/UserJWTControllerIT.java`,
useBluePrint: true,
},
{
file: 'package/web/rest/UserResourceIT.java',
renameTo: generator => `${generator.javaDir}web/rest/UserResourceIT.java`,
useBluePrint: true,
},
],
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ package <%=packageName%>.web.rest;

import <%=packageName%>.config.Constants;
import <%=packageName%>.domain.User;
<%_ if (authenticationType !== 'oauth2') { _%>
import <%=packageName%>.repository.UserRepository;
<%_ } _%>
import <%=packageName%>.security.AuthoritiesConstants;
<%_ if (authenticationType !== 'oauth2') { _%>
import <%=packageName%>.service.MailService;
<%_ } _%>
import <%=packageName%>.service.UserService;
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.util.HeaderUtil;
import <%=packageName%>.util.PaginationUtil;
<%_ if (authenticationType !== 'oauth2') { _%>
import <%=packageName%>.web.rest.errors.BadRequestAlertException;
import <%=packageName%>.web.rest.errors.EmailAlreadyUsedException;
import <%=packageName%>.web.rest.errors.LoginAlreadyUsedException;
<%_ } _%>

import io.micronaut.context.annotation.Value;
import io.micronaut.data.model.Page;
Expand Down Expand Up @@ -63,17 +69,23 @@ public class UserResource {

private final UserService userService;

<%_ if (authenticationType !== 'oauth2') { _%>
private final UserRepository userRepository;

private final MailService mailService;

public UserResource(UserService userService, UserRepository userRepository, MailService mailService) {

this.userService = userService;
this.userRepository = userRepository;
this.mailService = mailService;
}
<%_ } else { _%>
public UserResource(UserService userService) {
this.userService = userService;
}
<%_ } _%>

<%_ if (authenticationType !== 'oauth2') { _%>
/**
* {@code POST /users} : Creates a new user.
* <p>
Expand Down Expand Up @@ -138,6 +150,7 @@ public class UserResource {
.headers(headers -> HeaderUtil.createAlert(headers, applicationName, "userManagement.updated", userDTO.getLogin()))
).orElse(HttpResponse.notFound());
}
<%_ } _%>

/**
* {@code GET /users} : get all users.
Expand Down Expand Up @@ -176,6 +189,7 @@ public class UserResource {
.map(UserDTO::new);
}

<%_ if (authenticationType !== 'oauth2') { _%>
/**
* {@code DELETE /users/:login} : delete the "login" User.
*
Expand All @@ -189,4 +203,5 @@ public class UserResource {
userService.deleteUser(login);
return HttpResponse.noContent().headers(headers -> HeaderUtil.createAlert(headers, applicationName, "userManagement.deleted", login));
}
<%_ } _%>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import <%=packageName%>.domain.User;
import <%=packageName%>.repository.AuthorityRepository;
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.security.AuthoritiesConstants;
<%_ if (authenticationType !== 'oauth2') { _%>
import <%=packageName%>.service.MailService;
<%_ } _%>
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.service.mapper.UserMapper;
import <%=packageName%>.web.rest.vm.ManagedUserVM;
Expand Down Expand Up @@ -41,7 +43,11 @@ public class UserResourceIT {
private static final String DEFAULT_LOGIN = "johndoe";
private static final String UPDATED_LOGIN = "jhipster";

<%_ if (authenticationType !== 'oauth2') { _%>
private static final Long DEFAULT_ID = 1L;
<%_ } else { _%>
private static final String DEFAULT_ID = "id1";
<%_ } _%>
private static final String DEFAULT_PASSWORD = "passjohndoe";
private static final String UPDATED_PASSWORD = "passjhipster";
Expand Down Expand Up @@ -81,7 +87,11 @@ public class UserResourceIT {
public static User createEntity() {
User user = new User();
user.setLogin(DEFAULT_LOGIN + RandomStringUtils.randomAlphabetic(5));
<%_ if (authenticationType !== 'oauth2') { _%>
user.setPassword(RandomStringUtils.random(60));
<%_ } else { _%>
user.setId(DEFAULT_ID);
<%_ } _%>
user.setActivated(true);
user.setEmail(RandomStringUtils.randomAlphabetic(5) + DEFAULT_EMAIL);
user.setFirstName(DEFAULT_FIRSTNAME);
Expand Down Expand Up @@ -128,6 +138,7 @@ public class UserResourceIT {
userRepository.flush();
user = userRepository.saveAndFlush(reInsert);
}
<%_ if (authenticationType !== 'oauth2') { _%>

@Test
public void createUser() throws Exception {
Expand Down Expand Up @@ -236,6 +247,7 @@ public class UserResourceIT {
List<User> userList = userRepository.findAll();
assertThat(userList.size()).isEqualTo(databaseSizeBeforeCreate);
}
<%_ } _%>

@Test
public void getAllUsers() throws Exception {
Expand Down Expand Up @@ -273,6 +285,7 @@ public class UserResourceIT {

assertThat(response.getStatus().getCode()).isEqualTo(HttpStatus.NOT_FOUND.getCode());
}
<%_ if (authenticationType !== 'oauth2') { _%>

@Test
public void updateUser() throws Exception {
Expand Down Expand Up @@ -445,6 +458,7 @@ public class UserResourceIT {

resetDefaultUser();
}
<%_ } _%>

@Test
public void getAllAuthorities() throws Exception {
Expand All @@ -459,11 +473,23 @@ public class UserResourceIT {
@Test
public void testUserEquals() throws Exception {
User user1 = new User();
<%_ if (authenticationType !== 'oauth2') { _%>
user1.setId(1L);
<%_ } else { _%>
user1.setId("id1");
<%_ } _%>
User user2 = new User();
user2.setId(user1.getId());
<%_ if (authenticationType !== 'oauth2') { _%>
user2.setId(1L);
<%_ } else { _%>
user2.setId("id1");
<%_ } _%>
assertThat(user1).isEqualTo(user2);
<%_ if (authenticationType !== 'oauth2') { _%>
user2.setId(2L);
<%_ } else { _%>
user2.setId("id2");
<%_ } _%>
assertThat(user1).isNotEqualTo(user2);
user1.setId(null);
assertThat(user1).isNotEqualTo(user2);
Expand Down Expand Up @@ -545,9 +571,11 @@ public class UserResourceIT {
assertThat(authorityA).isEqualTo(authorityB);
assertThat(authorityA.hashCode()).isEqualTo(authorityB.hashCode());
}
<%_ if (authenticationType !== 'oauth2') { _%>

@MockBean(MailService.class)
MailService mailService() {
return Mockito.mock(MailService.class);
}
<%_ } _%>
}
3 changes: 3 additions & 0 deletions test-integration/samples/gradle-jwt-react.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ entity Operation {
relationship OneToMany {
BankAccount{operation} to Operation{bankAccount(name)}
}
relationship ManyToOne {
BankAccount{user(login)} to User
}
relationship ManyToMany {
Operation{label(label)} to Label{operation}
}
Expand Down
3 changes: 3 additions & 0 deletions test-integration/samples/mvn-no-client.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ entity Operation {
relationship OneToMany {
BankAccount{operation} to Operation{bankAccount(name)}
}
relationship ManyToOne {
BankAccount{user(login)} to User
}
relationship ManyToMany {
Operation{label(label)} to Label{operation}
}
Expand Down
3 changes: 3 additions & 0 deletions test-integration/samples/mvn-oauth-angular.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ entity Operation {
relationship OneToMany {
BankAccount{operation} to Operation{bankAccount(name)}
}
relationship ManyToOne {
BankAccount{user(login)} to User
}
relationship ManyToMany {
Operation{label(label)} to Label{operation}
}
Expand Down

0 comments on commit 8273cfc

Please sign in to comment.