Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
fdaugan committed May 7, 2024
1 parent 0bddbb8 commit 0fd818d
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.ligoj.api</groupId>
<artifactId>plugin-parent</artifactId>
<version>4.1.7</version>
<version>4.1.8-SNAPSHOT</version>
<relativePath />
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public int compare(final UserOrg o1, final UserOrg o2) {
* Return a safe string representation of the mail of a user.
*/
private String toSafeString(final UserOrg o1) {
return o1.getMails().isEmpty() ? "" : o1.getMails().get(0);
return o1.getMails().isEmpty() ? "" : o1.getMails().getFirst();
}

}
26 changes: 13 additions & 13 deletions src/main/java/org/ligoj/app/plugin/id/resource/UserOrgResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,19 @@ private void updateGroupUser(final String user, final String group, final BiPred
/**
* Update the given user.
*
* @param user The user definition, and associated groups. Group changes are checked.User definition changes are
* @param user The user definition, and associated groups. Group changes and user attributes changes are
* checked.
* @return the update attributes and related changes. Currently only `groups` attributes is supported and contains only the
*/
@PUT
public void update(final UserOrgEditionVo user) {
public UserUpdateResult update(final UserOrgEditionVo user) {
// Check the right on the company and the groups
final var hasAttributeChange = validateChanges(securityHelper.getLogin(), user);

// Check the user exists
getUser().findByIdExpected(user.getId());

saveOrUpdate(user, hasAttributeChange);
return saveOrUpdate(user, hasAttributeChange);
}

/**
Expand Down Expand Up @@ -387,12 +388,10 @@ private boolean validateChanges(final String principal, final UserOrgEditionVo i
// Replace with the normalized company
importEntry.setCompany(cleanCompany);

// Check the groups : one group not writable implies entry creation to
// fail
// Check the groups : one group not writable implies entry creation to fail
validateAndGroupsCN(userOrg, importEntry, delegates);

// Replace the user groups by the normalized groups including the ones
// the user does not see
// Replace the user groups by the normalized groups including the ones the user does not see
if (userOrg != null) {
// Check the company change
if (!userOrg.getCompany().equals(importEntry.getCompany())) {
Expand Down Expand Up @@ -481,8 +480,7 @@ private Collection<String> mergeGroups(final List<DelegateOrg> delegates, final
for (final var oldGroup : userOrg.getGroups()) {
final var oldGroupDn = getGroup().findById(oldGroup).getDn();
if (!groups.contains(oldGroup) && canWrite(delegates, oldGroupDn, DelegateType.GROUP)) {
// This group is writable, so it has been explicitly removed by
// the current user
// This group is writable, so it has been explicitly removed by the current user
newGroups.remove(oldGroup);
}
}
Expand Down Expand Up @@ -561,8 +559,9 @@ private boolean hasAttributeChange(final SimpleUser user1, final SimpleUser user
* @param importEntry The entry to save or to update.
* @param quiet Flag to turn off the possible notification such as mail.
* @param hasAttributeChange When <code>false</code>, underlying user database will not be updated, only membership as needed.
* @return the updated attributes and related changes. Currently only `groups` attributes is supported and contains only the
*/
private void saveOrUpdate(final UserOrgEditionVo importEntry, final boolean quiet, final boolean hasAttributeChange) {
private UserUpdateResult saveOrUpdate(final UserOrgEditionVo importEntry, final boolean quiet, final boolean hasAttributeChange) {

// Create as needed the user, groups will be proceeded after.
final var repository = getUser();
Expand All @@ -583,7 +582,7 @@ private void saveOrUpdate(final UserOrgEditionVo importEntry, final boolean quie
}

// Update membership
repository.updateMembership(importEntry.getGroups(), user);
return repository.updateMembership(importEntry.getGroups(), user);
}

/**
Expand All @@ -597,9 +596,10 @@ private void saveOrUpdate(final UserOrgEditionVo importEntry, final boolean quie
*
* @param importEntry The entry to save or to update.
* @param hasAttributeChange When <code>false</code>, underlying user database will not be updated, only membership as needed.
* @return the update attributes and related changes. Currently only `groups` attributes is supported and contains only the
*/
public void saveOrUpdate(final UserOrgEditionVo importEntry, final boolean hasAttributeChange) {
saveOrUpdate(importEntry, false, hasAttributeChange);
public UserUpdateResult saveOrUpdate(final UserOrgEditionVo importEntry, final boolean hasAttributeChange) {
return saveOrUpdate(importEntry, false, hasAttributeChange);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

/**
* Base batch processor.
*
*
* @param <B> The batch element type.
*/
@Slf4j
Expand All @@ -37,7 +37,7 @@ public abstract class AbstractBatchTask<B extends BatchElement> implements Runna

@Override
public void run() {
log.info("Executing " + task.toString());
log.info("Executing {}", task.toString());

// Expose the current user
securityHelper.setUserName(task.getPrincipal());
Expand All @@ -51,7 +51,7 @@ public void run() {

/**
* Process an entry.
*
*
* @param entry A batch entry.
* @param quiet Flag to turn off the possible notification such as mail.
* @throws Exception Any error cause the abortion for this entry.
Expand All @@ -60,7 +60,7 @@ public void run() {

/**
* Process an entry.
*
*
* @param entry A batch entry.
* @throws Exception Any error cause the abortion for this entry.
*/
Expand Down Expand Up @@ -96,7 +96,7 @@ private void doBatch() {

/**
* Configure the task.
*
*
* @param task The batch task.
*/
public void configure(final BatchTaskVo<B> task) {
Expand All @@ -107,7 +107,7 @@ public void configure(final BatchTaskVo<B> task) {
// Save the CXF factory for JSON serialization
this.jaxrsFactory = getMessage() == null ? ServerProviderFactory.getInstance()
: (ServerProviderFactory) getMessage().getExchange().getEndpoint()
.get("org.apache.cxf.jaxrs.provider.ServerProviderFactory");
.get("org.apache.cxf.jaxrs.provider.ServerProviderFactory");
}

protected Message getMessage() {
Expand All @@ -116,7 +116,7 @@ protected Message getMessage() {

/**
* Split and normalize a string to a collection, ignoring empty items.
*
*
* @param rawValue The raw string to split.
* @return A collection from the raw string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ define(function () {
* Reset the selected user password after popup confirmation, or directly from its identifier.
*/
resetUserPassword: function (id, name) {
current.confirmUserOperation($(this), id, name, 'reset', 'reseted', 'PUT');
current.confirmUserOperation($(this), id, name, 'reset', 'reset-ok', 'PUT');
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ define({
'isolated-confirm': '{{this}} a été isolé. Le mot de passe a été effacé.',
'reset': 'Réinitialisation mot de passe',
'reset-confirm': 'Êtes-vous certain de réinitialiser le mot de passe de {{{this}}}?<br>Un mail sera envoyé à l\'utilisateur et vous.',
'reseted-confirm': 'Le mot de passe de {{this}} a été réinitialisé.',
'reset-ok': 'Le mot de passe de {{this}} a été réinitialisé.',
'show-password' : 'Afficher le mot de passe',
'password' : 'Mot de passe',
'quiet': 'Silencieux',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ define({
'isolated-confirm': '{{this}} has been isolated. Password has been cleared',
'reset': 'Password Reset',
'reset-confirm': 'Are you sure to reset the password of {{this}}?<br>An email will be sent to this user and you.',
'reseted-confirm': '{{this}} password has been reset.',
'reset-ok': '{{this}} password has been reset.',
'show-password' : 'Show password',
'password' : 'Password',
'quiet': 'Silent',
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/org/ligoj/app/plugin/id/dao/IdCacheDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ void addGroupToGroup() {
.createQuery("FROM CacheMembership WHERE group.id = :id AND subGroup.id = :sid", CacheMembership.class)
.setParameter("id", "group").setParameter("sid", "name-sg-other").getResultList();
Assertions.assertEquals(1, memberships.size());
Assertions.assertEquals("group", memberships.get(0).getGroup().getId());
Assertions.assertEquals("name-sg-other", memberships.get(0).getSubGroup().getId());
Assertions.assertNull(memberships.get(0).getUser());
Assertions.assertEquals("group", memberships.getFirst().getGroup().getId());
Assertions.assertEquals("name-sg-other", memberships.getFirst().getSubGroup().getId());
Assertions.assertNull(memberships.getFirst().getUser());
}

@Test
Expand Down Expand Up @@ -514,9 +514,9 @@ void reset() {
final var memberships = em.createQuery("FROM CacheMembership", CacheMembership.class)
.getResultList();
Assertions.assertEquals(2, memberships.size());
Assertions.assertEquals("group", memberships.get(0).getGroup().getId());
Assertions.assertNull(memberships.get(0).getSubGroup());
Assertions.assertEquals("u", memberships.get(0).getUser().getId());
Assertions.assertEquals("group", memberships.getFirst().getGroup().getId());
Assertions.assertNull(memberships.getFirst().getSubGroup());
Assertions.assertEquals("u", memberships.getFirst().getUser().getId());

// Check the state of the previous delegates
Assertions.assertEquals(2, delegateOrgRepository.count());
Expand Down Expand Up @@ -568,8 +568,8 @@ void updateUser() {
.createQuery("FROM CacheMembership WHERE user.id = :id", CacheMembership.class).setParameter("id", "u0")
.getResultList();
Assertions.assertEquals(1, memberships.size());
Assertions.assertEquals("group", memberships.get(0).getGroup().getId());
Assertions.assertNull(memberships.get(0).getSubGroup());
Assertions.assertEquals("u0", memberships.get(0).getUser().getId());
Assertions.assertEquals("group", memberships.getFirst().getGroup().getId());
Assertions.assertNull(memberships.getFirst().getSubGroup());
Assertions.assertEquals("u0", memberships.getFirst().getUser().getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void findAll() {
Assertions.assertEquals(2, groups.getRecordsFiltered());
Assertions.assertEquals(2, groups.getData().size());

final var group0 = groups.getData().get(0);
final var group0 = groups.getData().getFirst();
Assertions.assertEquals("DIG", group0.getName());
Assertions.assertEquals(1, group0.getCount());
Assertions.assertEquals(0, group0.getCountVisible());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected <U extends BatchElement> U checkImportTask(final BatchTaskVo<U> import
Assertions.assertEquals(Boolean.TRUE, importTask.getStatus().getStatus());
Assertions.assertEquals(1, importTask.getStatus().getEntries());
Assertions.assertEquals(1, importTask.getStatus().getDone());
return importTask.getEntries().get(0);
return importTask.getEntries().getFirst();
}

protected <U extends BatchElement> BatchTaskVo<U> waitImport(final BatchTaskVo<U> importTask)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void full() throws IOException, InterruptedException {
if (data.getAllInvocations().size() != 1) {
throw new MockitoException("Expect one call");
}
final GroupEditionVo group = (GroupEditionVo) data.getAllInvocations().get(0).getArguments()[0];
final GroupEditionVo group = (GroupEditionVo) data.getAllInvocations().getFirst().getArguments()[0];
Assertions.assertNotNull(group);
Assertions.assertEquals("Ligoj France", group.getName());
Assertions.assertNotNull(group.getScope());
Expand Down Expand Up @@ -117,16 +117,16 @@ void fullFull() throws IOException, InterruptedException {
if (data.getAllInvocations().size() != 1) {
throw new MockitoException("Expect one call");
}
final GroupEditionVo group = (GroupEditionVo) data.getAllInvocations().get(0).getArguments()[0];
final GroupEditionVo group = (GroupEditionVo) data.getAllInvocations().getFirst().getArguments()[0];
Assertions.assertNotNull(group);
Assertions.assertEquals("Special", group.getName());
Assertions.assertNotNull(group.getScope());
Assertions.assertEquals(2, group.getOwners().size());
Assertions.assertEquals("fdaugan", group.getOwners().get(0));
Assertions.assertEquals("fdaugan", group.getOwners().getFirst());
Assertions.assertEquals(2, group.getAssistants().size());
Assertions.assertEquals("jdoe5", group.getAssistants().get(0));
Assertions.assertEquals("jdoe5", group.getAssistants().getFirst());
Assertions.assertEquals(2, group.getDepartments().size());
Assertions.assertEquals("700301", group.getDepartments().get(0));
Assertions.assertEquals("700301", group.getDepartments().getFirst());
Assertions.assertEquals("Operations", group.getParent());
})).create(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,15 @@ void setup() {
void doBatchInvalidOperation() {
final UserUpdateEntry entry = new UserUpdateEntry();
entry.setOperation("any");
MatcherUtil.assertThrows(Assertions.assertThrows(ValidationJsonException.class, () -> {
task.doBatch(entry);
}), "operation", "unsupported-operation");
MatcherUtil.assertThrows(Assertions.assertThrows(ValidationJsonException.class, () -> task.doBatch(entry)), "operation", "unsupported-operation");
}

@Test
void doBatchExtraValue() {
final UserUpdateEntry entry = new UserUpdateEntry();
entry.setOperation("lock");
entry.setValue("any");
MatcherUtil.assertThrows(Assertions.assertThrows(ValidationJsonException.class, () -> {
task.doBatch(entry);
}), "value", "null-value-expected");
MatcherUtil.assertThrows(Assertions.assertThrows(ValidationJsonException.class, () -> task.doBatch(entry)), "value", "null-value-expected");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void execute() throws IOException, InterruptedException {
if (data.getAllInvocations().size() != 1) {
throw new MockitoException("Expect one call");
}
final UserOrgEditionVo user = (UserOrgEditionVo) data.getAllInvocations().get(0).getArguments()[0];
final UserOrgEditionVo user = (UserOrgEditionVo) data.getAllInvocations().getFirst().getArguments()[0];
Assertions.assertNotNull(user);
Assertions.assertEquals("Sébastien", user.getFirstName());
Assertions.assertEquals("Loubli", user.getLastName());
Expand Down Expand Up @@ -97,15 +97,15 @@ void executeDefaultHeader() throws IOException, InterruptedException {
initSpringSecurityContext(DEFAULT_USER);
final BatchTaskVo<UserImportEntry> importTask = waitImport(
resource.getImportTask(resource.execute(input, new String[0], "cp1250", false)));
Assertions.assertEquals(Boolean.TRUE, importTask.getEntries().get(0).getStatus());
Assertions.assertNull(importTask.getEntries().get(0).getStatusText());
Assertions.assertEquals(Boolean.TRUE, importTask.getEntries().getFirst().getStatus());
Assertions.assertNull(importTask.getEntries().getFirst().getStatusText());

// Check user
Mockito.verify(mockResource, new DefaultVerificationMode(data -> {
if (data.getAllInvocations().size() != 1) {
throw new MockitoException("Expect one call");
}
final UserOrgEditionVo user = (UserOrgEditionVo) data.getAllInvocations().get(0).getArguments()[0];
final UserOrgEditionVo user = (UserOrgEditionVo) data.getAllInvocations().getFirst().getArguments()[0];
Assertions.assertNotNull(user);
Assertions.assertNotNull(user);
Assertions.assertEquals("Sébastien", user.getFirstName());
Expand All @@ -132,7 +132,7 @@ void executeEmptyGroups() throws IOException, InterruptedException {
if (data.getAllInvocations().size() != 1) {
throw new MockitoException("Expect one call");
}
final UserOrgEditionVo user = (UserOrgEditionVo) data.getAllInvocations().get(0).getArguments()[0];
final UserOrgEditionVo user = (UserOrgEditionVo) data.getAllInvocations().getFirst().getArguments()[0];
Assertions.assertNotNull(user);
Assertions.assertEquals("kloubli9", user.getId());
Assertions.assertEquals(1, user.getGroups().size());
Expand All @@ -149,29 +149,25 @@ void executeFailed() throws IOException, InterruptedException {
initSpringSecurityContext(DEFAULT_USER);
final BatchTaskVo<UserImportEntry> importTask = waitImport(
resource.getImportTask(resource.execute(input, new String[0], "cp1250", false)));
Assertions.assertEquals("message", importTask.getEntries().get(0).getStatusText());
Assertions.assertEquals(Boolean.FALSE, importTask.getEntries().get(0).getStatus());
Assertions.assertEquals("message", importTask.getEntries().getFirst().getStatusText());
Assertions.assertEquals(Boolean.FALSE, importTask.getEntries().getFirst().getStatus());
}

@Test
void executeInvalidHeaders() throws IOException {
final InputStream input = new ByteArrayInputStream(
"Loubli;Sébastien;kloubli4;my.address@sample.com;ligoj;jira".getBytes("cp1250"));
initSpringSecurityContext(DEFAULT_USER);
Assertions.assertEquals("csv-file:Invalid header __?__", Assertions.assertThrows(ValidationJsonException.class, () -> {
resource.execute(input, new String[] { "lastName", "firstName", "id", "__?__", "company", "groups" },
"cp1250", false);
}).getMessage());
Assertions.assertEquals("csv-file:Invalid header __?__", Assertions.assertThrows(ValidationJsonException.class, () -> resource.execute(input, new String[] { "lastName", "firstName", "id", "__?__", "company", "groups" },
"cp1250", false)).getMessage());
}

@Test
void executeMisingLogin() throws IOException {
final InputStream input = new ByteArrayInputStream(
"Loubli;Sébastien;;my.address@sample.com;ligoj;jira".getBytes("cp1250"));
initSpringSecurityContext(DEFAULT_USER);
MatcherUtil.assertThrows(Assertions.assertThrows(ConstraintViolationException.class, () -> {
resource.execute(input, new String[0], "cp1250", false);
}), "id", "NotBlank");
MatcherUtil.assertThrows(Assertions.assertThrows(ConstraintViolationException.class, () -> resource.execute(input, new String[0], "cp1250", false)), "id", "NotBlank");
}

@Test
Expand Down

0 comments on commit 0fd818d

Please sign in to comment.