@Async
    @Transactional
    public SamlMetadataProcessResult startCollectMetadata(Connection con, Map<String, String> map) {
        SamlMetadataProcessResult result = new SamlMetadataProcessResult(con.getId());
        EntitiesDescriptor descriptor;

        try {
            String metadataXML = getMetadataFromConnection(con);
            descriptor = convertMetadataXMLtoEntitiesDescriptor(metadataXML);
            String hash = getSignatureValue(descriptor.getSignature());

            Optional<SamlMetadataProcessResult> process = samlMetadataProcessResultRepository.findByConnectionIdAndHash(con.getId(), hash);

            if (process.isPresent()) return result;

            updateMetadata(descriptor, con, map, result);
            result.setMetadata(metadataXML);

            if (result.allEntriesSuccessful()) {
                result.setHash(hash);
            }
        } catch (InitializationException | ComponentInitializationException | UnmarshallingException | IOException | MetadataParseException e) {
            map.put("status", "failed");
            LOGGER.error("Failed to collect/parse metadata: {}", e.getMessage());
            result.addProcessError(e.getMessage(), "");
        }

        samlMetadataProcessResultRepository.saveAndFlush(result);

        return result;
    }
@Test
    public void startCollectMetadataWithInvalidSignatureTest() throws IOException {
        Map<String, String> map = new HashMap<>();
        Connection connection = newConnection();

        when(httpClientMock.execute(any(HttpGet.class))).thenReturn(httpResponseMock);
        when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
        when(httpEntityMock.getContent()).thenReturn(getClass().getClassLoader().getResourceAsStream("metadata/invalid-signature-metadata.xml"));

        SamlMetadataProcessResult result = metadataProcessorServiceMock.startCollectMetadata(connection, map);

        assertEquals(0, result.getTotalUpdated());
        assertEquals(0, result.getTotalProcessed());
        assertEquals(1, result.getTotalErrors());
        assertEquals(1, result.getSamlMetadataProcessErrors().size());
        assertEquals("failed", map.get("status"));
        assertEquals("Metadata signature invalid", result.getSamlMetadataProcessErrors().get(0).getErrorReason());

        assertNull(result.getMetadata());
        assertEquals(0, connection.getCertificates().size());
    }