Skip to content

Commit 8808b77

Browse files
committed
lint & coverage
1 parent cdc0688 commit 8808b77

5 files changed

Lines changed: 58 additions & 38 deletions

File tree

src/main/java/org/ligoj/app/plugin/id/dao/IdCacheDaoImpl.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public void delete(final GroupOrg group) {
191191
}
192192

193193
private void removeAll(Query... queries) {
194+
//noinspection unchecked
194195
Stream.of(queries).map(Query::getResultList).forEach(l -> l.forEach(em::remove));
195196
em.flush();
196197
}
@@ -364,7 +365,7 @@ public void removeUserFromGroup(final UserOrg user, final GroupOrg group) {
364365
}
365366

366367
private void deleteBatch(Class<?> cls, List<String> ids, Consumer<List<String>> batchConsumer) {
367-
log.info("Deleting removed cache {} {} entries", cls.getSimpleName(), (Object) ids.size());
368+
log.info("Deleting removed cache {} {} entries", cls.getSimpleName(), ids.size());
368369
ListUtils.partition(ids, 1000).forEach(batchConsumer);
369370
}
370371

@@ -492,24 +493,21 @@ private long updateDelegateDn(final Map<String, ? extends CacheContainer> contai
492493
final var updated = new AtomicInteger();
493494
// Get all delegates of he related receiver type
494495
delegateOrgRepository.findAllBy(typePath, type).stream().filter(d -> {
495-
// Consider only the existing ones
496-
final var dn = Optional.ofNullable(containers.get(id.apply(d))).map(CacheContainer::getDescription)
497-
.orElse(null);
498-
499-
// Consider only the dirty one
500496
final var delegateDn = getDn.apply(d);
501-
if (delegateDn == null) {
502-
return false;
503-
}
497+
// Consider only the existing ones
498+
final var container = containers.get(id.apply(d));
499+
final var dn = Optional.ofNullable(container).map(CacheContainer::getDescription).orElse(null);
504500
if (dn == null) {
505-
// Not resolved DN for this entity's id
501+
// Not resolved DN for this entity's id, can be deleted
506502
return true;
507503
}
508504
if (!delegateDn.equalsIgnoreCase(dn)) {
509505
// The delegate DN needed this update
510506
setDn.accept(d, dn);
511507
updated.incrementAndGet();
512508
}
509+
510+
// Keep this delegate
513511
return false;
514512
}).forEach(delegateOrgRepository::delete);
515513
return updated.get();

src/main/java/org/ligoj/app/plugin/id/resource/UserOrgResource.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -356,19 +356,19 @@ public String create(final UserOrgEditionVo user) {
356356
/**
357357
* Validate the user changes regarding the current user's right, replace group names with the exact CN, and replace
358358
* the company with a normalized one.<br>
359-
* Rules, order is important :
359+
* Rules where order is important:
360360
* <ul>
361361
* <li>At least one valid delegate must exist (valid or not against the involved user). If not, act as if the
362362
* company does not exist.</li>
363363
* <li>Involved company must exist</li>
364-
* <li>Involved company must be visible by the principal user. If not at if it does not exist, one</li>
365-
* <li>Involved company must be writable by the principal user when there is one updated attribute. Otherwise
364+
* <li>Involved company must be visible by the principal user. If not, act if it does not exist, one</li>
365+
* <li>Involved company must be writable by the principal user when there is one updated attribute. Otherwise,
366366
* indicate the read-only state.</li>
367367
* <li>Involved groups must exist</li>
368368
* <li>Involved groups must be visible by the current user, if not, act as if it does not exist. So the user can
369369
* only involve visible groups he/she. These groups are completed with the other invisible groups the user may
370370
* already have.</li>
371-
* <li>Involved changed groups must writable by the principal user. Otherwise indicate the read-only state.</li>
371+
* <li>Involved changed groups must writable by the principal user. Otherwise, indicate the read-only state.</li>
372372
* </ul>
373373
*/
374374
private boolean validateChanges(final String principal, final UserOrgEditionVo importEntry) {
@@ -552,7 +552,11 @@ private boolean hasAttributeChange(final SimpleUser importEntry, boolean hasChan
552552
@SafeVarargs
553553
private boolean hasAttributeChange(final SimpleUser user1, final SimpleUser user2, final Function<SimpleUser, String>... equals) {
554554
final var predicateFalse = Arrays.stream(equals).filter(f -> !Strings.CI.equals(StringUtils.trimToNull(f.apply(user2)), StringUtils.trimToNull(f.apply(user1)))).findFirst().orElse(null);
555-
return predicateFalse != null && hasAttributeChange(user1, true, String.format("'%s' != '%s'", predicateFalse.apply(user1), predicateFalse.apply(user2)));
555+
if (predicateFalse == null) {
556+
return false;
557+
}
558+
hasAttributeChange(user1, true, String.format("'%s' != '%s'", predicateFalse.apply(user1), predicateFalse.apply(user2)));
559+
return true;
556560
}
557561

558562
/**
@@ -714,7 +718,7 @@ public void lock(@PathParam("user") final String user) {
714718
* tools are watching.<br>
715719
* All memberships are updated, the user's DN is changed, all groups must be updated. Rules, order is important :
716720
* <ul>
717-
* <li>Only users managing the company of this user can perform the disable, if not, act as if the user did not
721+
* <li>Only users managing the company of this user can perform the disablement, if not, act as if the user did not
718722
* exist</li>
719723
* <li>User must exist</li>
720724
* </ul>
@@ -734,7 +738,7 @@ public void isolate(@PathParam("user") final String user) {
734738
* Unlock a user.<br>
735739
* Rules, order is important :
736740
* <ul>
737-
* <li>Only users managing the company of this user can perform the enable, if not, act as if the user did not
741+
* <li>Only users managing the company of this user can perform the enablement, if not, act as if the user did not
738742
* exist</li>
739743
* <li>User must exist</li>
740744
* </ul>
@@ -753,7 +757,7 @@ public void unlock(@PathParam("user") final String user) {
753757
* Restore a user from the isolate zone to the old company.<br>
754758
* Rules, order is important :
755759
* <ul>
756-
* <li>Only users managing the company of this user can perform the enable, if not, act as if the user did not
760+
* <li>Only users managing the company of this user can perform the enablement, if not, act as if the user did not
757761
* exist</li>
758762
* <li>User must exist</li>
759763
* </ul>
@@ -988,20 +992,14 @@ public void decorate(final SessionSettings settings) {
988992

989993
@Override
990994
public Collection<GrantedAuthority> getGrantedAuthorities(final String username) {
991-
try {
992-
// Check if the user lock status without using cache
993-
final var rawUserOrg = getUserRepository().toUser(Normalizer.normalize(username));
994-
final var roles = new ArrayList<GrantedAuthority>();
995-
for (String group : CollectionUtils.emptyIfNull(rawUserOrg.getGroups())) {
996-
roles.add(new SimpleGrantedAuthority(group.toUpperCase()));
997-
roles.add(new SimpleGrantedAuthority(group.toLowerCase()));
998-
roles.add(new SimpleGrantedAuthority(group));
999-
}
1000-
return roles;
1001-
} catch (ValidationJsonException ve) {
1002-
// Ignore this error
1003-
log.debug("User being authenticated '{}' is not defined in primary identity provider ", username);
1004-
return Collections.emptyList();
995+
// Check if the user lock status without using cache
996+
final var rawUserOrg = getUserRepository().toUser(Normalizer.normalize(username));
997+
final var roles = new HashSet<GrantedAuthority>();
998+
for (String group : CollectionUtils.emptyIfNull(rawUserOrg.getGroups())) {
999+
roles.add(new SimpleGrantedAuthority(group.toUpperCase()));
1000+
roles.add(new SimpleGrantedAuthority(group.toLowerCase()));
1001+
roles.add(new SimpleGrantedAuthority(group));
10051002
}
1003+
return roles;
10061004
}
10071005
}

src/test/java/org/ligoj/app/plugin/id/dao/IdCacheDaoTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ void reset() {
511511
up2dateDelegate.setReceiverDn("dn=group1");
512512
up2dateDelegate.setReceiverType(ReceiverType.GROUP);
513513
delegateOrgRepository.saveAndFlush(up2dateDelegate);
514+
514515
em.flush();
515516

516517
// Pre state
@@ -604,4 +605,9 @@ void updateUser() {
604605
Assertions.assertNull(memberships.getFirst().getSubGroup());
605606
Assertions.assertEquals("u0", memberships.getFirst().getUser().getId());
606607
}
608+
609+
@Test
610+
void test() {
611+
Assertions.assertEquals(0, new IdCacheDaoImpl().getCacheRefreshTime());
612+
}
607613
}

src/test/java/org/ligoj/app/plugin/id/resource/ContainerScopeResourceTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
*/
44
package org.ligoj.app.plugin.id.resource;
55

6-
import java.io.IOException;
7-
import java.nio.charset.StandardCharsets;
8-
import java.util.List;
9-
6+
import jakarta.persistence.EntityNotFoundException;
107
import jakarta.transaction.Transactional;
118
import jakarta.ws.rs.core.UriInfo;
12-
139
import org.apache.cxf.jaxrs.impl.MetadataMap;
1410
import org.junit.jupiter.api.Assertions;
1511
import org.junit.jupiter.api.BeforeEach;
@@ -29,6 +25,10 @@
2925
import org.springframework.test.context.ContextConfiguration;
3026
import org.springframework.test.context.junit.jupiter.SpringExtension;
3127

28+
import java.io.IOException;
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.List;
31+
3232
/**
3333
* Test class of {@link ContainerScopeResource}
3434
*/
@@ -46,7 +46,7 @@ class ContainerScopeResourceTest extends AbstractJpaTest {
4646

4747
@BeforeEach
4848
void setUpEntities() throws IOException {
49-
persistEntities("csv", new Class<?>[] { ContainerScope.class }, StandardCharsets.UTF_8);
49+
persistEntities("csv", new Class<?>[]{ContainerScope.class}, StandardCharsets.UTF_8);
5050
}
5151

5252
@Test
@@ -134,6 +134,11 @@ void findByName() {
134134
checkType(resource.findByName("Project"));
135135
}
136136

137+
@Test
138+
void findByNameNotExist() {
139+
Assertions.assertThrows(EntityNotFoundException.class, () -> resource.findByName(ContainerType.GROUP, "Other"));
140+
}
141+
137142
private void checkType(final ContainerScope type) {
138143
Assertions.assertEquals("Project", type.getName());
139144
Assertions.assertTrue(type.isLocked());

src/test/java/org/ligoj/app/plugin/id/resource/UserOrgResourceTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.beans.factory.annotation.Autowired;
3636
import org.springframework.context.ApplicationContext;
3737
import org.springframework.data.domain.PageImpl;
38+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3839
import org.springframework.test.annotation.Rollback;
3940
import org.springframework.test.context.ContextConfiguration;
4041
import org.springframework.test.context.junit.jupiter.SpringExtension;
@@ -1235,4 +1236,16 @@ public UserOrg findById(@PathParam("user") final String user) {
12351236
});
12361237
}
12371238

1239+
1240+
@Test
1241+
void getGrantedAuthorities() {
1242+
final UserOrg userOrg = new UserOrg();
1243+
userOrg.setGroups(List.of("Group1", "Group2"));
1244+
Mockito.when(userRepository.toUser("junit")).thenReturn(userOrg);
1245+
var authorities = resource.getGrantedAuthorities("junit");
1246+
Assertions.assertTrue( authorities.contains(new SimpleGrantedAuthority("Group1")));
1247+
Assertions.assertTrue( authorities.contains(new SimpleGrantedAuthority("group1")));
1248+
Assertions.assertTrue( authorities.contains(new SimpleGrantedAuthority("GROUP1")));
1249+
}
1250+
12381251
}

0 commit comments

Comments
 (0)