urn_perun_user_attribute_def_virt_userCertificatesLimited extends UserVirtualAttributesModuleAbstract implements UserVirtualAttributesModuleImplApi { @Override public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) { Attribute resultAttribute = new Attribute(attributeDefinition); List<String> result = new ArrayList<>(); Attribute userCertsAttribute = getUserCertsAttribute(sess, user); Map<String,String> certs = userCertsAttribute.valueAsMap(); if (certs != null) { Map<String, String> certsExpiration = ModulesUtilsBlImpl.retrieveCertificatesExpiration(certs); DateFormat dateFormatInstance = DateFormat.getDateInstance(); Map<String, Long> certsExpirationInMilliSeconds = new HashMap<>(); certsExpiration.forEach((key, value) -> { try { certsExpirationInMilliSeconds.put(key, dateFormatInstance.parse(value).getTime()); } catch (ParseException ex) { throw new ConsistencyErrorException(ex); } }); certsExpirationInMilliSeconds.entrySet().stream() .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())) .limit(10) .forEachOrdered(entry -> result.add(certs.get(entry.getKey()))); Utils.copyAttributeToViAttributeWithoutValue(userCertsAttribute, resultAttribute); } resultAttribute.setValue(result); return resultAttribute; }  @Override Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition); @Override List<String> getStrongDependencies(); @Override AttributeDefinition getAttributeDefinition();  }
@Test public void testGetAttributeValueWithMoreThanTen() { System.out.println("testGetAttributeValueWithMoreThanTen()"); when(mockedCertificateAttribute.valueAsMap()).thenReturn((LinkedHashMap<String, String>) moreCertificates); when(mockedExpirationsAttribute.valueAsMap()).thenReturn((LinkedHashMap<String, String>) moreCertsExpiration); List<String> result = classInstance.getAttributeValue(session, user, virtAttributeCertificates).valueAsList(); assertFalse(result.contains(oldestCertificate)); }
@Test public void testGetAttributeValueWithLessThanTen() { System.out.println("testGetAttributeValueWithLessThanTen()"); when(mockedCertificateAttribute.valueAsMap()).thenReturn((LinkedHashMap<String, String>) lessCertificates); when(mockedExpirationsAttribute.valueAsMap()).thenReturn((LinkedHashMap<String, String>) lessCertsExpiration); assertEquals(3, classInstance.getAttributeValue(session, user, virtAttributeCertificates).valueAsList().size()); }